examples: update methods signatures

Update methods signatures in order to generate correct manifest files
with full description of available methods.
This commit is contained in:
Anna Shaleva 2020-07-03 14:53:29 +03:00
parent 53ff02f1ad
commit db8cb752e3
4 changed files with 144 additions and 64 deletions

View file

@ -38,13 +38,14 @@ func (t Token) GetSupply(ctx storage.Context) interface{} {
return getIntFromDB(ctx, []byte(t.CirculationKey))
}
// BalanceOf gets the token balance of a specific address
func (t Token) BalanceOf(ctx storage.Context, holder []byte) interface{} {
// TBalanceOf gets the token balance of a specific address
// TODO: https://github.com/nspcc-dev/neo-go/issues/1150
func (t Token) TBalanceOf(ctx storage.Context, holder []byte) interface{} {
return getIntFromDB(ctx, holder)
}
// Transfer token from one user to another
func (t Token) Transfer(ctx storage.Context, from []byte, to []byte, amount int) bool {
// TTransfer token from one user to another
func (t Token) TTransfer(ctx storage.Context, from []byte, to []byte, amount int) bool {
amountFrom := t.CanTransfer(ctx, from, to, amount)
if amountFrom == -1 {
return false
@ -104,8 +105,8 @@ func IsUsableAddress(addr []byte) bool {
return false
}
// Mint initial supply of tokens.
func (t Token) Mint(ctx storage.Context, to []byte) bool {
// TMint initial supply of tokens.
func (t Token) TMint(ctx storage.Context, to []byte) bool {
if !IsUsableAddress(t.Owner) {
return false
}

View file

@ -2,7 +2,6 @@ package tokencontract
import (
"github.com/nspcc-dev/neo-go/examples/token/nep5"
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
"github.com/nspcc-dev/neo-go/pkg/interop/util"
)
@ -14,8 +13,8 @@ const (
var owner = util.FromAddress("NPAsqZkx9WhNd4P72uhZxBhLinSuNkxfB8")
// CreateToken initializes the Token Interface for the Smart Contract to operate with
func CreateToken() nep5.Token {
// createToken initializes the Token Interface for the Smart Contract to operate with
func createToken() nep5.Token {
return nep5.Token{
Name: "Awesome NEO Token",
Symbol: "ANT",
@ -28,47 +27,91 @@ func CreateToken() nep5.Token {
// Main function = contract entry
func Main(operation string, args []interface{}) interface{} {
token := CreateToken()
if operation == "name" {
return token.Name
return Name()
}
if operation == "symbol" {
return token.Symbol
return Symbol()
}
if operation == "decimals" {
return token.Decimals
return Decimals()
}
// The following operations need ctx
ctx := storage.GetContext()
if operation == "totalSupply" {
return token.GetSupply(ctx)
return TotalSupply()
}
if operation == "balanceOf" {
hodler := args[0].([]byte)
return token.BalanceOf(ctx, hodler)
return BalanceOf(hodler)
}
if operation == "transfer" && CheckArgs(args, 3) {
if operation == "transfer" && checkArgs(args, 3) {
from := args[0].([]byte)
to := args[1].([]byte)
amount := args[2].(int)
return token.Transfer(ctx, from, to, amount)
return Transfer(from, to, amount)
}
if operation == "mint" && CheckArgs(args, 1) {
if operation == "mint" && checkArgs(args, 1) {
addr := args[0].([]byte)
return token.Mint(ctx, addr)
return Mint(addr)
}
return true
}
// CheckArgs checks args array against a length indicator
func CheckArgs(args []interface{}, length int) bool {
// checkArgs checks args array against a length indicator
func checkArgs(args []interface{}, length int) bool {
if len(args) == length {
return true
}
return false
}
// Name returns the token name
func Name() string {
t := createToken()
return t.Name
}
// Symbol returns the token symbol
func Symbol() string {
t := createToken()
return t.Symbol
}
// Decimals returns the token decimals
func Decimals() int {
t := createToken()
return t.Decimals
}
// TotalSupply returns the token total supply value
func TotalSupply() interface{} {
t := createToken()
ctx := storage.GetContext()
return t.GetSupply(ctx)
}
// BalanceOf returns the amount of token on the specified address
func BalanceOf(holder []byte) interface{} {
t := createToken()
ctx := storage.GetContext()
return t.TBalanceOf(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.TTransfer(ctx, from, to, amount)
}
// Mint initial supply of tokens
func Mint(to []byte) bool {
t := createToken()
ctx := storage.GetContext()
return t.TMint(ctx, to)
}