examples: update examples

Closes #1234
This commit is contained in:
Anna Shaleva 2020-08-10 13:42:02 +03:00
parent d8db85ef55
commit ae3f15523c
7 changed files with 165 additions and 260 deletions

View file

@ -11,11 +11,16 @@ const (
multiplier = 100000000
)
var owner = util.FromAddress("NMipL5VsNoLUBUJKPKLhxaEbPQVCZnyJyB")
var (
owner = util.FromAddress("NULwe3UAHckN2fzNdcVg31tDiaYtMDwANt")
token nep5.Token
ctx storage.Context
)
// createToken initializes the Token Interface for the Smart Contract to operate with
func createToken() nep5.Token {
return nep5.Token{
// init initializes the Token Interface and storage context for the Smart
// Contract to operate with
func init() {
token = nep5.Token{
Name: "Awesome NEO Token",
Symbol: "ANT",
Decimals: decimals,
@ -23,95 +28,40 @@ func createToken() nep5.Token {
TotalSupply: 11000000 * multiplier,
CirculationKey: "TokenCirculation",
}
}
// Main function = contract entry
func Main(operation string, args []interface{}) interface{} {
if operation == "name" {
return Name()
}
if operation == "symbol" {
return Symbol()
}
if operation == "decimals" {
return Decimals()
}
if operation == "totalSupply" {
return TotalSupply()
}
if operation == "balanceOf" {
hodler := args[0].([]byte)
return BalanceOf(hodler)
}
if operation == "transfer" && checkArgs(args, 3) {
from := args[0].([]byte)
to := args[1].([]byte)
amount := args[2].(int)
return Transfer(from, to, amount)
}
if operation == "mint" && checkArgs(args, 1) {
addr := args[0].([]byte)
return Mint(addr)
}
return true
}
// checkArgs checks args array against a length indicator
func checkArgs(args []interface{}, length int) bool {
if len(args) == length {
return true
}
return false
ctx = storage.GetContext()
}
// Name returns the token name
func Name() string {
t := createToken()
return t.Name
return token.Name
}
// Symbol returns the token symbol
func Symbol() string {
t := createToken()
return t.Symbol
return token.Symbol
}
// Decimals returns the token decimals
func Decimals() int {
t := createToken()
return t.Decimals
return token.Decimals
}
// TotalSupply returns the token total supply value
func TotalSupply() interface{} {
t := createToken()
ctx := storage.GetContext()
return t.GetSupply(ctx)
func TotalSupply() int {
return token.GetSupply(ctx)
}
// BalanceOf returns the amount of token on the specified address
func BalanceOf(holder []byte) interface{} {
t := createToken()
ctx := storage.GetContext()
return t.BalanceOf(ctx, holder)
return token.BalanceOf(ctx, holder)
}
// Transfer token from one user to another
func Transfer(from []byte, to []byte, amount int) bool {
t := createToken()
ctx := storage.GetContext()
return t.Transfer(ctx, from, to, amount)
return token.Transfer(ctx, from, to, amount)
}
// Mint initial supply of tokens
func Mint(to []byte) bool {
t := createToken()
ctx := storage.GetContext()
return t.Mint(ctx, to)
return token.Mint(ctx, to)
}