2021-07-04 11:00:56 +00:00
|
|
|
package balance
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-07 11:06:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-contract/common"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Token holds all token info.
|
|
|
|
Token struct {
|
|
|
|
// Ticker symbol
|
|
|
|
Symbol string
|
|
|
|
// Amount of decimals
|
|
|
|
Decimals int
|
|
|
|
// Storage key for circulation value
|
|
|
|
CirculationKey string
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:48:50 +00:00
|
|
|
// Account structure stores metadata of each FrostFS balance account.
|
2020-10-27 12:14:06 +00:00
|
|
|
Account struct {
|
2022-04-14 11:56:51 +00:00
|
|
|
// Active balance
|
2020-10-27 12:14:06 +00:00
|
|
|
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
|
|
|
|
}
|
2023-10-24 10:41:54 +00:00
|
|
|
|
|
|
|
// account is a stored view of Account with fixed int size
|
|
|
|
account struct {
|
|
|
|
Balance []byte
|
|
|
|
Until []byte
|
|
|
|
Parent []byte
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-01-11 07:48:50 +00:00
|
|
|
symbol = "FROSTFS"
|
2020-10-27 12:14:06 +00:00
|
|
|
decimals = 12
|
|
|
|
circulation = "MainnetGAS"
|
|
|
|
|
|
|
|
netmapContractKey = "netmapScriptHash"
|
|
|
|
containerContractKey = "containerScriptHash"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-07 12:18:48 +00:00
|
|
|
func _deploy(data any, isUpdate bool) {
|
2021-12-27 07:15:36 +00:00
|
|
|
ctx := storage.GetContext()
|
2023-03-07 08:21:11 +00:00
|
|
|
|
2021-06-03 07:49:07 +00:00
|
|
|
if isUpdate {
|
2023-11-07 12:18:48 +00:00
|
|
|
args := data.([]any)
|
2021-12-27 08:49:30 +00:00
|
|
|
common.CheckVersion(args[len(args)-1].(int))
|
2021-06-03 07:49:07 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:34:11 +00:00
|
|
|
args := data.(struct {
|
2023-11-07 11:55:02 +00:00
|
|
|
addrNetmap interop.Hash160
|
|
|
|
addrContainer interop.Hash160
|
2021-11-29 16:34:11 +00:00
|
|
|
})
|
2021-05-12 08:31:07 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
runtime.Log("balance contract initialized")
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Update method updates contract source code and manifest. It can be invoked
|
2021-09-20 15:41:46 +00:00
|
|
|
// only by committee.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Update(script []byte, manifest []byte, data any) {
|
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
|
|
|
}
|
|
|
|
|
2023-06-19 08:17:51 +00:00
|
|
|
management.UpdateWithData(script, manifest, common.AppendVersion(data))
|
2021-02-11 15:55:32 +00:00
|
|
|
runtime.Log("balance contract updated")
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:48:50 +00:00
|
|
|
// Symbol is a NEP-17 standard method that returns FROSTFS token symbol.
|
2020-10-27 12:14:06 +00:00
|
|
|
func Symbol() string {
|
|
|
|
return token.Symbol
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:48:50 +00:00
|
|
|
// Decimals is a NEP-17 standard method that returns precision of FrostFS
|
2021-07-06 15:55:04 +00:00
|
|
|
// 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
|
2023-01-11 07:48:50 +00:00
|
|
|
// chain GAS in FrostFS 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)
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:48:50 +00:00
|
|
|
// BalanceOf is a NEP-17 standard method that returns FrostFS balance of the specified
|
2021-07-06 15:55:04 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2023-01-11 07:48:50 +00:00
|
|
|
// Transfer is a NEP-17 standard method that transfers FrostFS balance from one
|
2022-04-14 11:56:51 +00:00
|
|
|
// account to another. It can be invoked only by the account owner.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It produces Transfer and TransferX notifications. TransferX notification
|
2021-07-06 15:55:04 +00:00
|
|
|
// will have empty details field.
|
2023-11-07 12:18:48 +00:00
|
|
|
func Transfer(from, to interop.Hash160, amount int, data any) 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)
|
|
|
|
}
|
|
|
|
|
2023-01-11 07:48:50 +00:00
|
|
|
// TransferX is a method for FrostFS balance to be transferred from one account to
|
2022-04-14 11:56:51 +00:00
|
|
|
// another. It can be invoked by the account owner or by Alphabet nodes.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It produces Transfer and TransferX notifications.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
|
|
|
// TransferX method expands Transfer method by having extra details argument.
|
2022-04-14 11:56:51 +00:00
|
|
|
// TransferX method also allows to transfer assets by Alphabet nodes of the
|
|
|
|
// Inner Ring with multisignature.
|
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
|
|
|
|
2023-02-10 15:07:44 +00:00
|
|
|
common.CheckAlphabetWitness()
|
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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Lock is a method that transfers assets from a user account to the lock account
|
|
|
|
// related to the user. It can be invoked only by Alphabet nodes of the Inner Ring.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It produces Lock, Transfer and TransferX notifications.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Lock method is invoked by Alphabet nodes of the Inner Ring when they process
|
2023-01-11 07:48:50 +00:00
|
|
|
// Withdraw notification from FrostFS contract. This should transfer assets
|
2022-04-14 11:56:51 +00:00
|
|
|
// to a new lock account that won't be used for anything beside 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()
|
2023-03-07 08:21:11 +00:00
|
|
|
|
2023-02-10 15:07:44 +00:00
|
|
|
common.CheckAlphabetWitness()
|
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
|
|
|
|
2023-10-24 10:41:54 +00:00
|
|
|
setAccount(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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// NewEpoch is a method that checks timeout on lock accounts and returns assets
|
|
|
|
// if lock is not available anymore. It can be invoked only by NewEpoch method
|
2021-07-06 15:55:04 +00:00
|
|
|
// of Netmap contract.
|
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It 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()
|
2023-03-07 08:21:11 +00:00
|
|
|
|
2023-02-10 15:07:44 +00:00
|
|
|
common.CheckAlphabetWitness()
|
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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Mint is a method that transfers assets to a user account from an empty account.
|
|
|
|
// It can be invoked only by Alphabet nodes of the Inner Ring.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It produces Mint, Transfer and TransferX notifications.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Mint method is invoked by Alphabet nodes of the Inner Ring when they process
|
2023-01-11 07:48:50 +00:00
|
|
|
// Deposit notification from FrostFS contract. Before that, Alphabet nodes should
|
2022-04-14 11:56:51 +00:00
|
|
|
// synchronize precision of mainchain GAS contract and Balance contract.
|
2023-01-11 07:48:50 +00:00
|
|
|
// Mint increases total supply of NEP-17 compatible FrostFS 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()
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2023-02-10 15:07:44 +00:00
|
|
|
common.CheckAlphabetWitness()
|
2021-04-29 13:12:41 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
details := common.MintTransferDetails(txDetails)
|
2021-04-29 13:12:41 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Burn is a method that transfers assets from a user account to an empty account.
|
|
|
|
// It can be invoked only by Alphabet nodes of the Inner Ring.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// It produces Burn, Transfer and TransferX notifications.
|
2021-07-06 15:55:04 +00:00
|
|
|
//
|
2022-04-14 11:56:51 +00:00
|
|
|
// Burn method is invoked by Alphabet nodes of the Inner Ring when they process
|
2023-01-11 07:48:50 +00:00
|
|
|
// Cheque notification from FrostFS contract. It means that locked assets have been
|
2022-04-14 11:56:51 +00:00
|
|
|
// transferred to the user in the mainchain, therefore the lock account should be destroyed.
|
|
|
|
// Before that, Alphabet nodes should synchronize precision of mainchain GAS
|
2021-07-06 15:55:04 +00:00
|
|
|
// contract and Balance contract. Burn decreases total supply of NEP-17
|
2023-01-11 07:48:50 +00:00
|
|
|
// compatible FrostFS 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-07 06:17:00 +00:00
|
|
|
|
2023-02-10 15:07:44 +00:00
|
|
|
common.CheckAlphabetWitness()
|
2021-04-29 13:12:41 +00:00
|
|
|
|
2023-03-07 08:21:11 +00:00
|
|
|
details := common.BurnTransferDetails(txDetails)
|
2021-04-29 13:12:41 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// Version returns the 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
|
2023-10-24 10:41:54 +00:00
|
|
|
setAccount(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
|
2023-10-24 10:41:54 +00:00
|
|
|
setAccount(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) {
|
2023-11-07 12:00:02 +00:00
|
|
|
emptyAcc := Account{}
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-14 11:56:51 +00:00
|
|
|
// isUsableAddress checks if the sender is either a 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
|
|
|
|
}
|
|
|
|
|
2023-11-07 12:18:48 +00:00
|
|
|
func getAccount(ctx storage.Context, key any) Account {
|
2020-10-27 12:14:06 +00:00
|
|
|
data := storage.Get(ctx, key)
|
|
|
|
if data != nil {
|
2023-10-24 10:41:54 +00:00
|
|
|
acc := std.Deserialize(data.([]byte)).(account)
|
|
|
|
return Account{
|
|
|
|
Balance: common.FromFixedWidth64(acc.Balance),
|
|
|
|
Until: common.FromFixedWidth64(acc.Until),
|
|
|
|
Parent: acc.Parent,
|
|
|
|
}
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Account{}
|
|
|
|
}
|
2023-10-24 10:41:54 +00:00
|
|
|
|
2023-11-07 12:18:48 +00:00
|
|
|
func setAccount(ctx storage.Context, key any, acc Account) {
|
2023-10-24 10:41:54 +00:00
|
|
|
common.SetSerialized(ctx, key, account{
|
|
|
|
Balance: common.ToFixedWidth64(acc.Balance),
|
|
|
|
Until: common.ToFixedWidth64(acc.Until),
|
|
|
|
Parent: acc.Parent,
|
|
|
|
})
|
|
|
|
}
|