2020-10-27 12:14:06 +00:00
|
|
|
package balancecontract
|
|
|
|
|
|
|
|
import (
|
2020-12-09 13:03:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
2021-02-11 15:55:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/management"
|
2021-03-12 12:16:36 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/native/std"
|
2020-10-27 12:14:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/storage"
|
2021-02-02 16:39:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-contract/common"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Token holds all token info.
|
|
|
|
Token struct {
|
|
|
|
// Ticker symbol
|
|
|
|
Symbol string
|
|
|
|
// Amount of decimals
|
|
|
|
Decimals int
|
|
|
|
// Storage key for circulation value
|
|
|
|
CirculationKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
Account struct {
|
|
|
|
// Active balance
|
|
|
|
Balance int
|
|
|
|
// Until valid for lock accounts
|
|
|
|
Until int
|
|
|
|
// Parent field used in lock accounts, used to return assets back if
|
|
|
|
// account wasn't burnt.
|
|
|
|
Parent []byte
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
symbol = "NEOFS"
|
|
|
|
decimals = 12
|
|
|
|
circulation = "MainnetGAS"
|
|
|
|
version = 1
|
|
|
|
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
|
|
|
containerContractKey = "containerScriptHash"
|
2021-04-27 10:48:40 +00:00
|
|
|
notaryDisabledKey = "notary"
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
2021-05-19 08:46:33 +00:00
|
|
|
var token Token
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
// CreateToken initializes the Token Interface for the Smart Contract to operate with.
|
|
|
|
func CreateToken() Token {
|
|
|
|
return Token{
|
|
|
|
Symbol: symbol,
|
|
|
|
Decimals: decimals,
|
|
|
|
CirculationKey: circulation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
token = CreateToken()
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
|
|
|
args := data.([]interface{})
|
|
|
|
notaryDisabled := args[0].(bool)
|
|
|
|
owner := args[1].(interop.Hash160)
|
|
|
|
addrNetmap := args[2].(interop.Hash160)
|
|
|
|
addrContainer := args[3].(interop.Hash160)
|
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
panic("only owner can reinitialize contract")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrContainer) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
storage.Put(ctx, common.OwnerKey, owner)
|
2020-10-27 12:14:06 +00:00
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, containerContractKey, addrContainer)
|
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
|
|
|
storage.Put(ctx, notaryDisabledKey, notaryDisabled)
|
|
|
|
if notaryDisabled {
|
|
|
|
common.InitVote(ctx)
|
2021-05-04 14:02:05 +00:00
|
|
|
runtime.Log("balance contract notary disabled")
|
2021-04-27 10:48:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
runtime.Log("balance contract initialized")
|
|
|
|
}
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
func Migrate(script []byte, manifest []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
|
|
|
|
2021-02-11 15:55:32 +00:00
|
|
|
if !common.HasUpdateAccess(ctx) {
|
|
|
|
runtime.Log("only owner can update contract")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
management.Update(script, manifest)
|
|
|
|
runtime.Log("balance contract updated")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
func Symbol() string {
|
|
|
|
return token.Symbol
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decimals() int {
|
|
|
|
return token.Decimals
|
|
|
|
}
|
|
|
|
|
|
|
|
func TotalSupply() int {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return token.getSupply(ctx)
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func BalanceOf(account interop.Hash160) int {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetReadOnlyContext()
|
2020-12-09 13:03:09 +00:00
|
|
|
return token.balanceOf(ctx, account)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func Transfer(from, to interop.Hash160, amount int, data interface{}) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2020-10-27 12:14:06 +00:00
|
|
|
return token.transfer(ctx, from, to, amount, false, nil)
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func TransferX(from, to interop.Hash160, amount int, details []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:12:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
2021-05-05 13:30:30 +00:00
|
|
|
indirectCall bool
|
2021-04-29 13:12:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("transferX: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:30:30 +00:00
|
|
|
indirectCall = common.FromKnownContract(
|
2021-04-29 13:12:41 +00:00
|
|
|
ctx,
|
|
|
|
runtime.GetCallingScriptHash(),
|
|
|
|
containerContractKey,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("transferX: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:30:30 +00:00
|
|
|
if notaryDisabled && !indirectCall {
|
2021-04-29 13:12:41 +00:00
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{from, to, amount}, []byte("transfer"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
result := token.transfer(ctx, from, to, amount, true, details)
|
|
|
|
if result {
|
|
|
|
runtime.Log("transferX: success")
|
2020-10-27 12:14:06 +00:00
|
|
|
} else {
|
2021-02-19 15:27:25 +00:00
|
|
|
// consider panic there
|
|
|
|
runtime.Log("transferX: fail")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
return result
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:17:00 +00:00
|
|
|
func Lock(txDetails []byte, from, to interop.Hash160, amount, until int) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:12:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("lock: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("lock: this method must be invoked from inner ring")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
details := common.LockTransferDetails(txDetails)
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
lockAccount := Account{
|
|
|
|
Balance: 0,
|
|
|
|
Until: until,
|
|
|
|
Parent: from,
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{txDetails}, []byte("lock"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
common.SetSerialized(ctx, to, lockAccount)
|
2021-04-07 06:17:00 +00:00
|
|
|
|
|
|
|
result := token.transfer(ctx, from, to, amount, true, details)
|
2021-02-19 15:27:25 +00:00
|
|
|
if !result {
|
|
|
|
// consider using `return false` to remove votes
|
|
|
|
panic("lock: can't lock funds")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
runtime.Log("lock: created lock account")
|
2021-04-07 06:17:00 +00:00
|
|
|
runtime.Notify("Lock", txDetails, from, to, amount, until)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEpoch(epochNum int) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:12:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
indirectCall := common.FromKnownContract(
|
|
|
|
ctx,
|
|
|
|
runtime.GetCallingScriptHash(),
|
|
|
|
netmapContractKey,
|
|
|
|
)
|
|
|
|
if !indirectCall {
|
|
|
|
panic("newEpoch: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("newEpoch: this method must be invoked from inner ring")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
it := storage.Find(ctx, []byte{}, storage.KeysOnly)
|
|
|
|
for iterator.Next(it) {
|
2021-03-04 19:21:49 +00:00
|
|
|
addr := iterator.Value(it).(interop.Hash160) // it MUST BE `storage.KeysOnly`
|
2021-02-19 15:27:25 +00:00
|
|
|
if len(addr) != 20 {
|
|
|
|
continue
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
acc := getAccount(ctx, addr)
|
|
|
|
if acc.Until == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if epochNum >= acc.Until {
|
2021-04-07 06:17:00 +00:00
|
|
|
details := common.UnlockTransferDetails(epochNum)
|
2021-02-19 15:27:25 +00:00
|
|
|
// return assets back to the parent
|
2021-04-07 06:17:00 +00:00
|
|
|
token.transfer(ctx, addr, acc.Parent, acc.Balance, true, details)
|
2021-02-19 15:27:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:17:00 +00:00
|
|
|
func Mint(to interop.Hash160, amount int, txDetails []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:12:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("mint: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("mint: this method must be invoked from inner ring")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:17:00 +00:00
|
|
|
details := common.MintTransferDetails(txDetails)
|
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{txDetails}, []byte("mint"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
ok := token.transfer(ctx, nil, to, amount, true, details)
|
|
|
|
if !ok {
|
|
|
|
panic("mint: can't transfer assets")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
supply := token.getSupply(ctx)
|
|
|
|
supply = supply + amount
|
|
|
|
storage.Put(ctx, token.CirculationKey, supply)
|
|
|
|
runtime.Log("mint: assets were minted")
|
|
|
|
runtime.Notify("Mint", to, amount)
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-04-07 06:17:00 +00:00
|
|
|
func Burn(from interop.Hash160, amount int, txDetails []byte) bool {
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
2021-04-29 13:12:41 +00:00
|
|
|
notaryDisabled := storage.Get(ctx, notaryDisabledKey).(bool)
|
2021-03-09 19:15:58 +00:00
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
var ( // for invocation collection without notary
|
|
|
|
alphabet []common.IRNode
|
|
|
|
nodeKey []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
if notaryDisabled {
|
|
|
|
alphabet = common.AlphabetNodes()
|
|
|
|
nodeKey = common.InnerRingInvoker(alphabet)
|
|
|
|
if len(nodeKey) == 0 {
|
|
|
|
panic("burn: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
|
|
|
if !runtime.CheckWitness(multiaddr) {
|
|
|
|
panic("burn: this method must be invoked from inner ring")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:17:00 +00:00
|
|
|
details := common.BurnTransferDetails(txDetails)
|
|
|
|
|
2021-04-29 13:12:41 +00:00
|
|
|
if notaryDisabled {
|
|
|
|
threshold := len(alphabet)*2/3 + 1
|
|
|
|
id := common.InvokeID([]interface{}{txDetails}, []byte("burn"))
|
|
|
|
|
|
|
|
n := common.Vote(ctx, id, nodeKey)
|
|
|
|
if n < threshold {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
ok := token.transfer(ctx, from, nil, amount, true, details)
|
|
|
|
if !ok {
|
|
|
|
panic("burn: can't transfer assets")
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
supply := token.getSupply(ctx)
|
|
|
|
if supply < amount {
|
|
|
|
panic("panic, negative supply after burn")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
supply = supply - amount
|
|
|
|
storage.Put(ctx, token.CirculationKey, supply)
|
|
|
|
runtime.Log("burn: assets were burned")
|
|
|
|
runtime.Notify("Burn", from, amount)
|
|
|
|
|
2020-10-27 12:14:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func Version() int {
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSupply gets the token totalSupply value from VM storage.
|
|
|
|
func (t Token) getSupply(ctx storage.Context) int {
|
|
|
|
supply := storage.Get(ctx, t.CirculationKey)
|
|
|
|
if supply != nil {
|
|
|
|
return supply.(int)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// BalanceOf gets the token balance of a specific address.
|
2020-12-09 13:03:09 +00:00
|
|
|
func (t Token) balanceOf(ctx storage.Context, holder interop.Hash160) int {
|
2020-10-27 12:14:06 +00:00
|
|
|
acc := getAccount(ctx, holder)
|
|
|
|
|
|
|
|
return acc.Balance
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func (t Token) transfer(ctx storage.Context, from, to interop.Hash160, amount int, innerRing bool, details []byte) bool {
|
2020-10-27 12:14:06 +00:00
|
|
|
amountFrom, ok := t.canTransfer(ctx, from, to, amount, innerRing)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(from) == 20 {
|
|
|
|
if amountFrom.Balance == amount {
|
|
|
|
storage.Delete(ctx, from)
|
|
|
|
} else {
|
|
|
|
amountFrom.Balance = amountFrom.Balance - amount // neo-go#953
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, from, amountFrom)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(to) == 20 {
|
|
|
|
amountTo := getAccount(ctx, to)
|
|
|
|
amountTo.Balance = amountTo.Balance + amount // neo-go#953
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, to, amountTo)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
runtime.Notify("Transfer", from, to, amount)
|
|
|
|
runtime.Notify("TransferX", from, to, amount, details)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// canTransfer returns the amount it can transfer.
|
2020-12-09 13:03:09 +00:00
|
|
|
func (t Token) canTransfer(ctx storage.Context, from, to interop.Hash160, amount int, innerRing bool) (Account, bool) {
|
2020-10-27 12:14:06 +00:00
|
|
|
var (
|
|
|
|
emptyAcc = Account{}
|
|
|
|
)
|
|
|
|
|
|
|
|
if !innerRing {
|
|
|
|
if len(to) != 20 || !isUsableAddress(from) {
|
|
|
|
runtime.Log("transfer: bad script hashes")
|
|
|
|
return emptyAcc, false
|
|
|
|
}
|
|
|
|
} else if len(from) == 0 {
|
|
|
|
return emptyAcc, true
|
|
|
|
}
|
|
|
|
|
|
|
|
amountFrom := getAccount(ctx, from)
|
|
|
|
if amountFrom.Balance < amount {
|
|
|
|
runtime.Log("transfer: not enough assets")
|
|
|
|
return emptyAcc, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// return amountFrom value back to transfer, reduces extra Get
|
|
|
|
return amountFrom, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// isUsableAddress checks if the sender is either the correct NEO address or SC address.
|
2020-12-09 13:03:09 +00:00
|
|
|
func isUsableAddress(addr interop.Hash160) bool {
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(addr) == 20 {
|
|
|
|
if runtime.CheckWitness(addr) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if a smart contract is calling script hash
|
|
|
|
callingScriptHash := runtime.GetCallingScriptHash()
|
2021-02-02 17:36:20 +00:00
|
|
|
if common.BytesEqual(callingScriptHash, addr) {
|
2020-10-27 12:14:06 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAccount(ctx storage.Context, key interface{}) Account {
|
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
2021-03-12 12:16:36 +00:00
|
|
|
return std.Deserialize(data.([]byte)).(Account)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Account{}
|
|
|
|
}
|