2018-08-22 16:49:30 +00:00
|
|
|
package nep5
|
|
|
|
|
|
|
|
import (
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/util"
|
2018-08-22 16:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Token holds all token info
|
|
|
|
type Token struct {
|
|
|
|
// Token name
|
|
|
|
Name string
|
|
|
|
// Ticker symbol
|
|
|
|
Symbol string
|
|
|
|
// Amount of decimals
|
|
|
|
Decimals int
|
|
|
|
// Token owner address
|
|
|
|
Owner []byte
|
|
|
|
// Total tokens * multiplier
|
|
|
|
TotalSupply int
|
|
|
|
// Storage key for circulation value
|
|
|
|
CirculationKey string
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:06:42 +00:00
|
|
|
// getIntFromDB is a helper that checks for nil result of storage.Get and returns
|
|
|
|
// zero as the default value.
|
|
|
|
func getIntFromDB(ctx storage.Context, key []byte) int {
|
|
|
|
var res int
|
|
|
|
val := storage.Get(ctx, key)
|
|
|
|
if val != nil {
|
|
|
|
res = val.(int)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2018-08-22 16:49:30 +00:00
|
|
|
// GetSupply gets the token totalSupply value from VM storage
|
2020-08-10 10:42:02 +00:00
|
|
|
func (t Token) GetSupply(ctx storage.Context) int {
|
2020-06-23 21:06:42 +00:00
|
|
|
return getIntFromDB(ctx, []byte(t.CirculationKey))
|
2018-08-22 16:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:23:31 +00:00
|
|
|
// BalanceOf gets the token balance of a specific address
|
2020-08-10 10:42:02 +00:00
|
|
|
func (t Token) BalanceOf(ctx storage.Context, holder []byte) int {
|
2020-07-07 12:40:34 +00:00
|
|
|
return getIntFromDB(ctx, holder)
|
2018-08-22 16:49:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 14:23:31 +00:00
|
|
|
// Transfer token from one user to another
|
|
|
|
func (t Token) Transfer(ctx storage.Context, from []byte, to []byte, amount int) bool {
|
2018-08-22 16:49:30 +00:00
|
|
|
amountFrom := t.CanTransfer(ctx, from, to, amount)
|
|
|
|
if amountFrom == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if amountFrom == 0 {
|
|
|
|
storage.Delete(ctx, from)
|
|
|
|
}
|
|
|
|
|
|
|
|
if amountFrom > 0 {
|
|
|
|
diff := amountFrom - amount
|
|
|
|
storage.Put(ctx, from, diff)
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:06:42 +00:00
|
|
|
amountTo := getIntFromDB(ctx, to)
|
2018-08-22 16:49:30 +00:00
|
|
|
totalAmountTo := amountTo + amount
|
|
|
|
storage.Put(ctx, to, totalAmountTo)
|
|
|
|
runtime.Notify("transfer", from, to, amount)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanTransfer returns the amount it can transfer
|
|
|
|
func (t Token) CanTransfer(ctx storage.Context, from []byte, to []byte, amount int) int {
|
2020-05-12 13:30:31 +00:00
|
|
|
if len(to) != 20 || !IsUsableAddress(from) {
|
2018-08-22 16:49:30 +00:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:06:42 +00:00
|
|
|
amountFrom := getIntFromDB(ctx, from)
|
2018-08-22 16:49:30 +00:00
|
|
|
if amountFrom < amount {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell Transfer the result is equal - special case since it uses Delete
|
|
|
|
if amountFrom == amount {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// return amountFrom value back to Transfer, reduces extra Get
|
|
|
|
return amountFrom
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsUsableAddress checks if the sender is either the correct NEO address or SC address
|
|
|
|
func IsUsableAddress(addr []byte) bool {
|
|
|
|
if len(addr) == 20 {
|
|
|
|
|
|
|
|
if runtime.CheckWitness(addr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a smart contract is calling scripthash
|
2020-06-16 08:54:48 +00:00
|
|
|
callingScriptHash := runtime.GetCallingScriptHash()
|
2018-08-23 17:44:17 +00:00
|
|
|
if util.Equals(callingScriptHash, addr) {
|
2018-08-22 16:49:30 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2020-04-03 09:26:13 +00:00
|
|
|
|
2020-08-10 10:42:02 +00:00
|
|
|
// Mint initial supply of tokens
|
2020-07-29 14:23:31 +00:00
|
|
|
func (t Token) Mint(ctx storage.Context, to []byte) bool {
|
2020-04-03 09:26:13 +00:00
|
|
|
if !IsUsableAddress(t.Owner) {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-23 21:06:42 +00:00
|
|
|
minted := storage.Get(ctx, []byte("minted"))
|
|
|
|
if minted != nil && minted.(bool) == true {
|
2020-04-03 09:26:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, to, t.TotalSupply)
|
|
|
|
storage.Put(ctx, []byte("minted"), true)
|
2020-08-11 08:37:28 +00:00
|
|
|
storage.Put(ctx, []byte(t.CirculationKey), t.TotalSupply)
|
2020-04-03 09:26:13 +00:00
|
|
|
runtime.Notify("transfer", "", to, t.TotalSupply)
|
|
|
|
return true
|
|
|
|
}
|