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/binary"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/interop/iterator"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
lockTransferMsg = []byte("lock assets to withdraw")
|
|
|
|
unlockTransferMsg = []byte("asset lock expired")
|
|
|
|
|
|
|
|
ctx storage.Context
|
|
|
|
token Token
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
ctx = storage.GetContext()
|
|
|
|
token = CreateToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(addrNetmap, addrContainer []byte) {
|
|
|
|
if storage.Get(ctx, netmapContractKey) != nil {
|
|
|
|
panic("init: contract already deployed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrNetmap) != 20 || len(addrContainer) != 20 {
|
|
|
|
panic("init: incorrect length of contract script hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
storage.Put(ctx, netmapContractKey, addrNetmap)
|
|
|
|
storage.Put(ctx, containerContractKey, addrContainer)
|
|
|
|
|
|
|
|
runtime.Log("balance contract initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Symbol() string {
|
|
|
|
return token.Symbol
|
|
|
|
}
|
|
|
|
|
|
|
|
func Decimals() int {
|
|
|
|
return token.Decimals
|
|
|
|
}
|
|
|
|
|
|
|
|
func TotalSupply() int {
|
|
|
|
return token.getSupply(ctx)
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func BalanceOf(account interop.Hash160) int {
|
|
|
|
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 {
|
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 {
|
2020-10-27 12:14:06 +00:00
|
|
|
var (
|
|
|
|
n int // number of votes for inner ring invoke
|
|
|
|
hashTxID []byte // ballot key of the inner ring invocation
|
|
|
|
)
|
|
|
|
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("transferX: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
|
|
|
fromKnownContract := fromKnownContract(runtime.GetCallingScriptHash())
|
|
|
|
if fromKnownContract {
|
|
|
|
n = threshold
|
|
|
|
runtime.Log("transferX: processed indirect invoke")
|
|
|
|
} else {
|
2021-02-02 17:42:05 +00:00
|
|
|
hashTxID = common.InvokeID([]interface{}{from, to, amount}, []byte("transfer"))
|
2021-02-02 17:36:20 +00:00
|
|
|
n = common.Vote(ctx, hashTxID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashTxID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
result := token.transfer(ctx, from, to, amount, true, details)
|
|
|
|
if result {
|
|
|
|
runtime.Log("transferX: success")
|
|
|
|
} else {
|
|
|
|
// consider panic there
|
|
|
|
runtime.Log("transferX: fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func Lock(txID []byte, from, to interop.Hash160, amount, until int) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("lock: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
hashTxID := common.InvokeID([]interface{}{txID, from, to, amount, until}, []byte("lock"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, hashTxID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, hashTxID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
lockAccount := Account{
|
|
|
|
Balance: 0,
|
|
|
|
Until: until,
|
|
|
|
Parent: from,
|
|
|
|
}
|
2021-02-02 17:36:20 +00:00
|
|
|
common.SetSerialized(ctx, to, lockAccount)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
result := token.transfer(ctx, from, to, amount, true, lockTransferMsg)
|
|
|
|
if !result {
|
|
|
|
// consider using `return false` to remove votes
|
|
|
|
panic("lock: can't lock funds")
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("lock: created lock account")
|
|
|
|
runtime.Notify("Lock", txID, from, to, amount, until)
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("lock: processed invoke from inner ring")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEpoch(epochNum int) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("epochNum: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
epochID := common.InvokeID([]interface{}{epochNum}, []byte("epoch"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, epochID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, epochID)
|
2021-02-08 15:23:08 +00:00
|
|
|
it := storage.Find(ctx, []byte{}, storage.KeysOnly)
|
2020-10-27 12:14:06 +00:00
|
|
|
for iterator.Next(it) {
|
2021-02-08 15:23:08 +00:00
|
|
|
addr := iterator.Value(it).([]byte) // it MUST BE `storage.KeysOnly`
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(addr) != 20 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
acc := getAccount(ctx, addr)
|
|
|
|
if acc.Until == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if epochNum >= acc.Until {
|
|
|
|
// return assets back to the parent
|
|
|
|
token.transfer(ctx, addr, acc.Parent, acc.Balance, true, unlockTransferMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.Log("newEpoch: processed invoke from inner ring")
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func Mint(to interop.Hash160, amount int, details []byte) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("burn: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
mintID := common.InvokeID([]interface{}{to, amount, details}, []byte("mint"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, mintID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, mintID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
ok := token.transfer(ctx, nil, to, amount, true, details)
|
|
|
|
if !ok {
|
|
|
|
panic("mint: can't transfer assets")
|
|
|
|
}
|
|
|
|
|
|
|
|
supply := token.getSupply(ctx)
|
|
|
|
supply = supply + amount
|
|
|
|
storage.Put(ctx, token.CirculationKey, supply)
|
|
|
|
runtime.Log("mint: assets were minted")
|
|
|
|
runtime.Notify("Mint", to, amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func Burn(from interop.Hash160, amount int, details []byte) bool {
|
2021-02-02 19:20:31 +00:00
|
|
|
innerRing := irList()
|
2020-10-27 12:14:06 +00:00
|
|
|
threshold := len(innerRing)/3*2 + 1
|
|
|
|
|
2021-02-02 18:26:34 +00:00
|
|
|
irKey := common.InnerRingInvoker(innerRing)
|
2020-10-27 12:14:06 +00:00
|
|
|
if len(irKey) == 0 {
|
|
|
|
panic("burn: this method must be invoked from inner ring")
|
|
|
|
}
|
|
|
|
|
2021-02-02 17:42:05 +00:00
|
|
|
burnID := common.InvokeID([]interface{}{from, amount, details}, []byte("burn"))
|
2020-10-27 12:14:06 +00:00
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
n := common.Vote(ctx, burnID, irKey)
|
2020-10-27 12:14:06 +00:00
|
|
|
if n >= threshold {
|
2021-02-02 17:36:20 +00:00
|
|
|
common.RemoveVotes(ctx, burnID)
|
2020-10-27 12:14:06 +00:00
|
|
|
|
|
|
|
ok := token.transfer(ctx, from, nil, amount, true, details)
|
|
|
|
if !ok {
|
|
|
|
panic("burn: can't transfer assets")
|
|
|
|
}
|
|
|
|
|
|
|
|
supply := token.getSupply(ctx)
|
|
|
|
if supply < amount {
|
|
|
|
panic("panic, negative supply after burn")
|
|
|
|
}
|
|
|
|
|
|
|
|
supply = supply - amount
|
|
|
|
storage.Put(ctx, token.CirculationKey, supply)
|
|
|
|
runtime.Log("burn: assets were burned")
|
|
|
|
runtime.Notify("Burn", from, amount)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return binary.Deserialize(data.([]byte)).(Account)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Account{}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check if invocation made from known container or audit contracts.
|
|
|
|
This is necessary because calls from these contracts require to do transfer
|
|
|
|
without signature collection (1 invoke transfer).
|
|
|
|
|
|
|
|
IR1, IR2, IR3, IR4 -(4 invokes)-> [ Container Contract ] -(1 invoke)-> [ Balance Contract ]
|
|
|
|
|
|
|
|
We can do 1 invoke transfer if:
|
|
|
|
- invoke happened from inner ring node,
|
|
|
|
- it is indirect invocation from other smart-contract.
|
|
|
|
|
|
|
|
However there is a possible attack, when malicious inner ring node creates
|
|
|
|
malicious smart-contract in morph chain to do inderect call.
|
|
|
|
|
|
|
|
MaliciousIR -(1 invoke)-> [ Malicious Contract ] -(1 invoke) -> [ Balance Contract ]
|
|
|
|
|
|
|
|
To prevent that, we have to allow 1 invoke transfer from authorised well known
|
|
|
|
smart-contracts, that will be set up at `Init` method.
|
|
|
|
*/
|
|
|
|
|
2020-12-09 13:03:09 +00:00
|
|
|
func fromKnownContract(caller interop.Hash160) bool {
|
2020-10-27 12:14:06 +00:00
|
|
|
containerContractAddr := storage.Get(ctx, containerContractKey).([]byte)
|
|
|
|
|
2021-02-02 17:36:20 +00:00
|
|
|
return common.BytesEqual(caller, containerContractAddr)
|
2020-10-27 12:14:06 +00:00
|
|
|
}
|
2021-02-02 19:20:31 +00:00
|
|
|
|
|
|
|
func irList() []common.IRNode {
|
|
|
|
return common.InnerRingListViaStorage(ctx, netmapContractKey)
|
|
|
|
}
|