mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 03:41:34 +00:00
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:
parent
53ff02f1ad
commit
db8cb752e3
4 changed files with 144 additions and 64 deletions
|
@ -14,10 +14,7 @@ func Main(operation string, args []interface{}) bool {
|
|||
|
||||
// Log owner upon Verification trigger
|
||||
if trigger == runtime.Verification() {
|
||||
if runtime.CheckWitness(owner) {
|
||||
runtime.Log("Verified Owner")
|
||||
}
|
||||
return true
|
||||
return CheckWitness()
|
||||
}
|
||||
|
||||
// Discerns between log and notify for this test
|
||||
|
@ -30,15 +27,33 @@ func Main(operation string, args []interface{}) bool {
|
|||
|
||||
func handleOperation(operation string, args []interface{}) bool {
|
||||
if operation == "log" {
|
||||
message := args[0].(string)
|
||||
runtime.Log(message)
|
||||
return true
|
||||
return Log(args)
|
||||
}
|
||||
|
||||
if operation == "notify" {
|
||||
runtime.Notify(args[0])
|
||||
return true
|
||||
return Notify(args)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// CheckWitness checks owner's witness
|
||||
func CheckWitness() bool {
|
||||
if runtime.CheckWitness(owner) {
|
||||
runtime.Log("Verified Owner")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Log logs given message
|
||||
func Log(args []interface{}) bool {
|
||||
message := args[0].(string)
|
||||
runtime.Log(message)
|
||||
return true
|
||||
}
|
||||
|
||||
// Notify notifies about given message
|
||||
func Notify(args []interface{}) bool {
|
||||
runtime.Notify(args[0])
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -7,51 +7,72 @@ import (
|
|||
|
||||
// Main is a very useful function.
|
||||
func Main(operation string, args []interface{}) interface{} {
|
||||
ctx := storage.GetContext()
|
||||
|
||||
// Puts value at key
|
||||
if operation == "put" {
|
||||
if checkArgs(args, 2) {
|
||||
key := args[0].([]byte)
|
||||
value := args[1].([]byte)
|
||||
storage.Put(ctx, key, value)
|
||||
return key
|
||||
}
|
||||
return Put(args)
|
||||
}
|
||||
|
||||
// Returns the value at passed key
|
||||
if operation == "get" {
|
||||
if checkArgs(args, 1) {
|
||||
key := args[0].([]byte)
|
||||
return storage.Get(ctx, key)
|
||||
}
|
||||
return Get(args)
|
||||
}
|
||||
|
||||
// Deletes the value at passed key
|
||||
if operation == "delete" {
|
||||
key := args[0].([]byte)
|
||||
storage.Delete(ctx, key)
|
||||
return true
|
||||
return Delete(args)
|
||||
}
|
||||
|
||||
// Returns an array of key-value pairs with key that matched the passed value
|
||||
if operation == "find" {
|
||||
if checkArgs(args, 1) {
|
||||
value := args[0].([]byte)
|
||||
iter := storage.Find(ctx, value)
|
||||
result := []string{}
|
||||
for iterator.Next(iter) {
|
||||
val := iterator.Value(iter)
|
||||
key := iterator.Key(iter)
|
||||
result = append(result, key.(string)+":"+val.(string))
|
||||
}
|
||||
return result
|
||||
}
|
||||
return Find(args)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Put puts value at key.
|
||||
func Put(args []interface{}) interface{} {
|
||||
ctx := storage.GetContext()
|
||||
if checkArgs(args, 2) {
|
||||
key := args[0].([]byte)
|
||||
value := args[1].([]byte)
|
||||
storage.Put(ctx, key, value)
|
||||
return key
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Get returns the value at passed key.
|
||||
func Get(args []interface{}) interface{} {
|
||||
ctx := storage.GetContext()
|
||||
if checkArgs(args, 1) {
|
||||
key := args[0].([]byte)
|
||||
return storage.Get(ctx, key)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Delete deletes the value at passed key.
|
||||
func Delete(args []interface{}) interface{} {
|
||||
ctx := storage.GetContext()
|
||||
key := args[0].([]byte)
|
||||
storage.Delete(ctx, key)
|
||||
return true
|
||||
}
|
||||
|
||||
// Find returns an array of key-value pairs with key that matched the passed value.
|
||||
func Find(args []interface{}) interface{} {
|
||||
ctx := storage.GetContext()
|
||||
if checkArgs(args, 1) {
|
||||
value := args[0].([]byte)
|
||||
iter := storage.Find(ctx, value)
|
||||
result := []string{}
|
||||
for iterator.Next(iter) {
|
||||
val := iterator.Value(iter)
|
||||
key := iterator.Key(iter)
|
||||
result = append(result, key.(string)+":"+val.(string))
|
||||
}
|
||||
return result
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkArgs(args []interface{}, length int) bool {
|
||||
if len(args) == length {
|
||||
return true
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue