2021-07-04 11:00:56 +00:00
|
|
|
package balance
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-09 13:03:09 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop"
|
2021-06-30 10:59:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/contract"
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Account structure stores metadata of each NeoFS balance account.
|
2020-10-27 12:14:06 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
func createToken() Token {
|
2020-10-27 12:14:06 +00:00
|
|
|
return Token{
|
|
|
|
Symbol: symbol,
|
|
|
|
Decimals: decimals,
|
|
|
|
CirculationKey: circulation,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2021-07-06 15:55:04 +00:00
|
|
|
token = createToken()
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 08:31:07 +00:00
|
|
|
func _deploy(data interface{}, isUpdate bool) {
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
|
|
|
notaryDisabled bool
|
|
|
|
addrNetmap interop.Hash160
|
|
|
|
addrContainer interop.Hash160
|
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
2021-03-09 19:15:58 +00:00
|
|
|
ctx := storage.GetContext()
|
|
|
|
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(args.addrNetmap) != interop.Hash160Len || len(args.addrContainer) != interop.Hash160Len {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("incorrect length of contract script hash")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, netmapContractKey, args.addrNetmap)
|
|
|
|
storage.Put(ctx, containerContractKey, args.addrContainer)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-04-27 10:48:40 +00:00
|
|
|
// initialize the way to collect signatures
|
2021-11-29 16:34:11 +00:00
|
|
|
storage.Put(ctx, notaryDisabledKey, args.notaryDisabled)
|
|
|
|
if args.notaryDisabled {
|
2021-04-27 10:48:40 +00:00
|
|
|
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-09-21 08:02:24 +00:00
|
|
|
// Update method updates contract source code and manifest. Can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2021-09-21 12:58:37 +00:00
|
|
|
func Update(script []byte, manifest []byte, data interface{}) {
|
2021-09-20 15:41:46 +00:00
|
|
|
if !common.HasUpdateAccess() {
|
|
|
|
panic("only committee can update contract")
|
2021-02-11 15:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 10:55:21 +00:00
|
|
|
contract.Call(interop.Hash160(management.Hash), "update",
|
|
|
|
contract.All, script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("balance contract updated")
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Symbol is a NEP-17 standard method that returns NEOFS token symbol.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Symbol() string {
|
|
|
|
return token.Symbol
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Decimals is a NEP-17 standard method that returns precision of NeoFS
|
|
|
|
// balances.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Decimals() int {
|
|
|
|
return token.Decimals
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// TotalSupply is a NEP-17 standard method that returns total amount of main
|
|
|
|
// chain GAS in the NeoFS network.
|
2020-10-27 12:14:06 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// BalanceOf is a NEP-17 standard method that returns NeoFS balance of specified
|
|
|
|
// account.
|
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
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Transfer is a NEP-17 standard method that transfers NeoFS balance from one
|
|
|
|
// account to other. Can be invoked only by account owner.
|
|
|
|
//
|
|
|
|
// Produces Transfer and TransferX notifications. TransferX notification
|
|
|
|
// will have empty details field.
|
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)
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// TransferX is a method for NeoFS balance transfers from one account to
|
|
|
|
// another. Can be invoked by account owner or by Alphabet nodes.
|
|
|
|
//
|
|
|
|
// Produces Transfer and TransferX notifications.
|
|
|
|
//
|
|
|
|
// TransferX method expands Transfer method by having extra details argument.
|
|
|
|
// Also TransferX method allows to transfer assets by Alphabet nodes of the
|
|
|
|
// Inner Ring with multi signature.
|
2021-05-21 11:37:31 +00:00
|
|
|
func TransferX(from, to interop.Hash160, amount int, details []byte) {
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked from inner ring")
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
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()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-05-21 11:37:31 +00:00
|
|
|
if !result {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("can't transfer assets")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-29 10:51:57 +00:00
|
|
|
runtime.Log("successfully transferred assets")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Lock is a method that transfers assets from user account to lock account
|
|
|
|
// related to the user. Can be invoked only by Alphabet nodes of the Inner Ring.
|
|
|
|
//
|
|
|
|
// Produces Lock, Transfer and TransferX notifications.
|
|
|
|
//
|
|
|
|
// Lock method invoked by Alphabet nodes of the Inner Ring when they process
|
|
|
|
// Withdraw notification from NeoFS contract. This should transfer assets
|
|
|
|
// to new lock account that won't be used for anything besides Unlock and Burn.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Lock(txDetails []byte, from, to interop.Hash160, amount, until int) {
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked from inner ring")
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("can't lock funds")
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("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
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// NewEpoch is a method that checks timeout on lock accounts and return assets
|
|
|
|
// if lock is not available anymore. Can be invoked only by NewEpoch method
|
|
|
|
// of Netmap contract.
|
|
|
|
//
|
|
|
|
// Produces Transfer and TransferX notifications.
|
2021-05-21 11:37:31 +00:00
|
|
|
func NewEpoch(epochNum int) {
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked from inner ring")
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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-11-29 16:43:01 +00:00
|
|
|
if len(addr) != interop.Hash160Len {
|
2021-02-19 15:27:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Mint is a method that transfers assets to user account from empty account.
|
|
|
|
// Can be invoked only by Alphabet nodes of the Inner Ring.
|
|
|
|
//
|
|
|
|
// Produces Mint, Transfer and TransferX notifications.
|
|
|
|
//
|
|
|
|
// Mint method invoked by Alphabet nodes of the Inner Ring when they process
|
|
|
|
// Deposit notification from NeoFS contract. Before that Alphabet nodes should
|
|
|
|
// synchronize precision of main chain GAS contract and Balance contract.
|
|
|
|
// Mint increases total supply of NEP-17 compatible NeoFS token.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Mint(to interop.Hash160, amount int, txDetails []byte) {
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked from inner ring")
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
ok := token.transfer(ctx, nil, to, amount, true, details)
|
|
|
|
if !ok {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("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)
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("assets were minted")
|
2021-02-19 15:27:25 +00:00
|
|
|
runtime.Notify("Mint", to, amount)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Burn is a method that transfers assets from user account to empty account.
|
|
|
|
// Can be invoked only by Alphabet nodes of the Inner Ring.
|
|
|
|
//
|
|
|
|
// Produces Burn, Transfer and TransferX notifications.
|
|
|
|
//
|
|
|
|
// Burn method invoked by Alphabet nodes of the Inner Ring when they process
|
|
|
|
// Cheque notification from NeoFS contract. It means that locked assets were
|
|
|
|
// transferred to user in main chain, therefore lock account should be destroyed.
|
|
|
|
// Before that Alphabet nodes should synchronize precision of main chain GAS
|
|
|
|
// contract and Balance contract. Burn decreases total supply of NEP-17
|
|
|
|
// compatible NeoFS token.
|
2021-05-21 11:37:31 +00:00
|
|
|
func Burn(from interop.Hash160, amount int, txDetails []byte) {
|
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 {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("this method must be invoked from inner ring")
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
multiaddr := common.AlphabetAddress()
|
2021-11-29 12:21:40 +00:00
|
|
|
common.CheckAlphabetWitness(multiaddr)
|
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 {
|
2021-05-21 11:37:31 +00:00
|
|
|
return
|
2021-04-29 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
common.RemoveVotes(ctx, id)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
ok := token.transfer(ctx, from, nil, amount, true, details)
|
|
|
|
if !ok {
|
2021-11-29 10:51:57 +00:00
|
|
|
panic("can't transfer assets")
|
2021-02-19 15:27:25 +00:00
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-19 15:27:25 +00:00
|
|
|
supply := token.getSupply(ctx)
|
|
|
|
if supply < amount {
|
2021-11-29 10:51:57 +00:00
|
|
|
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)
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("assets were burned")
|
2021-02-19 15:27:25 +00:00
|
|
|
runtime.Notify("Burn", from, amount)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:55:04 +00:00
|
|
|
// Version returns version of the contract.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Version() int {
|
2021-07-29 11:44:53 +00:00
|
|
|
return common.Version
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2021-11-29 16:43:01 +00:00
|
|
|
if len(to) != interop.Hash160Len || !isUsableAddress(from) {
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("bad script hashes")
|
2020-10-27 12:14:06 +00:00
|
|
|
return emptyAcc, false
|
|
|
|
}
|
|
|
|
} else if len(from) == 0 {
|
|
|
|
return emptyAcc, true
|
|
|
|
}
|
|
|
|
|
|
|
|
amountFrom := getAccount(ctx, from)
|
|
|
|
if amountFrom.Balance < amount {
|
2021-11-30 09:02:59 +00:00
|
|
|
runtime.Log("not enough assets")
|
2020-10-27 12:14:06 +00:00
|
|
|
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{}
|
|
|
|
}
|