2019-09-03 15:00:10 +00:00
|
|
|
package tokencontract
|
2018-08-22 16:23:19 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-03 14:21:42 +00:00
|
|
|
"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"
|
2018-08-22 16:23:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
decimals = 8
|
|
|
|
multiplier = 100000000
|
|
|
|
)
|
|
|
|
|
2020-07-03 11:56:59 +00:00
|
|
|
var owner = util.FromAddress("NMipL5VsNoLUBUJKPKLhxaEbPQVCZnyJyB")
|
2018-08-22 16:23:19 +00:00
|
|
|
|
2020-07-03 11:53:29 +00:00
|
|
|
// createToken initializes the Token Interface for the Smart Contract to operate with
|
|
|
|
func createToken() nep5.Token {
|
2018-08-22 16:49:30 +00:00
|
|
|
return nep5.Token{
|
2018-08-22 16:23:19 +00:00
|
|
|
Name: "Awesome NEO Token",
|
|
|
|
Symbol: "ANT",
|
|
|
|
Decimals: decimals,
|
|
|
|
Owner: owner,
|
|
|
|
TotalSupply: 11000000 * multiplier,
|
|
|
|
CirculationKey: "TokenCirculation",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main function = contract entry
|
|
|
|
func Main(operation string, args []interface{}) interface{} {
|
|
|
|
if operation == "name" {
|
2020-07-03 11:53:29 +00:00
|
|
|
return Name()
|
2018-08-22 16:23:19 +00:00
|
|
|
}
|
|
|
|
if operation == "symbol" {
|
2020-07-03 11:53:29 +00:00
|
|
|
return Symbol()
|
2018-08-22 16:23:19 +00:00
|
|
|
}
|
|
|
|
if operation == "decimals" {
|
2020-07-03 11:53:29 +00:00
|
|
|
return Decimals()
|
2018-08-22 16:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if operation == "totalSupply" {
|
2020-07-03 11:53:29 +00:00
|
|
|
return TotalSupply()
|
2018-08-22 16:23:19 +00:00
|
|
|
}
|
2020-07-03 11:53:29 +00:00
|
|
|
|
2018-08-22 16:23:19 +00:00
|
|
|
if operation == "balanceOf" {
|
|
|
|
hodler := args[0].([]byte)
|
2020-07-03 11:53:29 +00:00
|
|
|
return BalanceOf(hodler)
|
2018-08-22 16:23:19 +00:00
|
|
|
}
|
2020-07-03 11:53:29 +00:00
|
|
|
|
|
|
|
if operation == "transfer" && checkArgs(args, 3) {
|
2018-08-22 16:23:19 +00:00
|
|
|
from := args[0].([]byte)
|
|
|
|
to := args[1].([]byte)
|
|
|
|
amount := args[2].(int)
|
2020-07-03 11:53:29 +00:00
|
|
|
return Transfer(from, to, amount)
|
2018-08-22 16:23:19 +00:00
|
|
|
}
|
2020-07-03 11:53:29 +00:00
|
|
|
|
|
|
|
if operation == "mint" && checkArgs(args, 1) {
|
2020-04-03 09:26:13 +00:00
|
|
|
addr := args[0].([]byte)
|
2020-07-03 11:53:29 +00:00
|
|
|
return Mint(addr)
|
2020-04-03 09:26:13 +00:00
|
|
|
}
|
2018-08-22 16:23:19 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-07-03 11:53:29 +00:00
|
|
|
// checkArgs checks args array against a length indicator
|
|
|
|
func checkArgs(args []interface{}, length int) bool {
|
2018-08-22 16:23:19 +00:00
|
|
|
if len(args) == length {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2020-07-03 11:53:29 +00:00
|
|
|
|
|
|
|
// 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()
|
2020-07-29 14:23:31 +00:00
|
|
|
return t.BalanceOf(ctx, holder)
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer token from one user to another
|
|
|
|
func Transfer(from []byte, to []byte, amount int) bool {
|
|
|
|
t := createToken()
|
|
|
|
ctx := storage.GetContext()
|
2020-07-29 14:23:31 +00:00
|
|
|
return t.Transfer(ctx, from, to, amount)
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Mint initial supply of tokens
|
|
|
|
func Mint(to []byte) bool {
|
|
|
|
t := createToken()
|
|
|
|
ctx := storage.GetContext()
|
2020-07-29 14:23:31 +00:00
|
|
|
return t.Mint(ctx, to)
|
2020-07-03 11:53:29 +00:00
|
|
|
}
|