2020-03-25 10:00:11 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-08-17 12:34:13 +00:00
|
|
|
"fmt"
|
2020-03-25 10:00:11 +00:00
|
|
|
"math/big"
|
|
|
|
|
2020-08-03 11:31:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/dao"
|
2020-03-25 10:00:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
2020-11-19 15:01:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/contract"
|
2020-05-19 07:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
|
2020-03-25 10:00:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-03-25 10:00:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-06-17 10:57:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
|
2020-03-25 10:00:11 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-11-19 15:01:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2020-06-03 12:55:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-03-25 10:00:11 +00:00
|
|
|
)
|
|
|
|
|
2020-04-23 18:28:37 +00:00
|
|
|
// prefixAccount is the standard prefix used to store account data.
|
|
|
|
const prefixAccount = 20
|
|
|
|
|
|
|
|
// makeAccountKey creates a key from account script hash.
|
|
|
|
func makeAccountKey(h util.Uint160) []byte {
|
|
|
|
k := make([]byte, util.Uint160Size+1)
|
|
|
|
k[0] = prefixAccount
|
|
|
|
copy(k[1:], h.BytesBE())
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
// nep17TokenNative represents NEP-17 token contract.
|
|
|
|
type nep17TokenNative struct {
|
2020-04-22 20:00:18 +00:00
|
|
|
interop.ContractMD
|
2020-09-23 08:48:31 +00:00
|
|
|
symbol string
|
|
|
|
decimals int64
|
|
|
|
factor int64
|
|
|
|
onPersist func(*interop.Context) error
|
|
|
|
postPersist func(*interop.Context) error
|
|
|
|
incBalance func(*interop.Context, util.Uint160, *state.StorageItem, *big.Int) error
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
// totalSupplyKey is the key used to store totalSupply value.
|
|
|
|
var totalSupplyKey = []byte{11}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) Metadata() *interop.ContractMD {
|
2020-03-25 10:00:11 +00:00
|
|
|
return &c.ContractMD
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
var _ interop.Contract = (*nep17TokenNative)(nil)
|
2020-03-25 10:00:11 +00:00
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func newNEP17Native(name string) *nep17TokenNative {
|
|
|
|
n := &nep17TokenNative{ContractMD: *interop.NewContractMD(name)}
|
|
|
|
n.Manifest.SupportedStandards = []string{manifest.NEP17StandardName}
|
2020-03-25 10:00:11 +00:00
|
|
|
|
|
|
|
desc := newDescriptor("name", smartcontract.StringType)
|
2020-10-09 12:06:28 +00:00
|
|
|
md := newMethodAndPrice(nameMethod(name), 0, smartcontract.NoneFlag)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = newDescriptor("symbol", smartcontract.StringType)
|
2020-06-19 09:45:51 +00:00
|
|
|
md = newMethodAndPrice(n.Symbol, 0, smartcontract.NoneFlag)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = newDescriptor("decimals", smartcontract.IntegerType)
|
2020-06-19 09:45:51 +00:00
|
|
|
md = newMethodAndPrice(n.Decimals, 0, smartcontract.NoneFlag)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
desc = newDescriptor("totalSupply", smartcontract.IntegerType)
|
2020-06-19 09:45:51 +00:00
|
|
|
md = newMethodAndPrice(n.TotalSupply, 1000000, smartcontract.AllowStates)
|
2020-04-22 20:00:18 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
desc = newDescriptor("balanceOf", smartcontract.IntegerType,
|
|
|
|
manifest.NewParameter("account", smartcontract.Hash160Type))
|
2020-06-19 09:45:51 +00:00
|
|
|
md = newMethodAndPrice(n.balanceOf, 1000000, smartcontract.AllowStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, true)
|
|
|
|
|
|
|
|
desc = newDescriptor("transfer", smartcontract.BoolType,
|
|
|
|
manifest.NewParameter("from", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("to", smartcontract.Hash160Type),
|
|
|
|
manifest.NewParameter("amount", smartcontract.IntegerType),
|
2020-11-19 15:01:42 +00:00
|
|
|
manifest.NewParameter("data", smartcontract.AnyType),
|
2020-03-25 10:00:11 +00:00
|
|
|
)
|
2020-06-19 09:45:51 +00:00
|
|
|
md = newMethodAndPrice(n.Transfer, 8000000, smartcontract.AllowModifyStates)
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-07-23 13:55:40 +00:00
|
|
|
desc = newDescriptor("onPersist", smartcontract.VoidType)
|
2020-06-17 10:57:54 +00:00
|
|
|
md = newMethodAndPrice(getOnPersistWrapper(n.OnPersist), 0, smartcontract.AllowModifyStates)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-09-23 08:48:31 +00:00
|
|
|
desc = newDescriptor("postPersist", smartcontract.VoidType)
|
|
|
|
md = newMethodAndPrice(getOnPersistWrapper(postPersistBase), 0, smartcontract.AllowModifyStates)
|
|
|
|
n.AddMethod(md, desc, false)
|
|
|
|
|
2020-03-25 10:00:11 +00:00
|
|
|
n.AddEvent("Transfer", desc.Parameters...)
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) Initialize(_ *interop.Context) error {
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) Symbol(_ *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewByteArray([]byte(c.symbol))
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) Decimals(_ *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBigInteger(big.NewInt(c.decimals))
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) TotalSupply(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-08-03 11:31:42 +00:00
|
|
|
return stackitem.NewBigInteger(c.getTotalSupply(ic.DAO))
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) getTotalSupply(d dao.DAO) *big.Int {
|
2020-08-03 11:31:42 +00:00
|
|
|
si := d.GetStorageItem(c.ContractID, totalSupplyKey)
|
2020-04-22 20:00:18 +00:00
|
|
|
if si == nil {
|
|
|
|
return big.NewInt(0)
|
|
|
|
}
|
2020-06-04 18:11:27 +00:00
|
|
|
return bigint.FromBytes(si.Value)
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) saveTotalSupply(d dao.DAO, supply *big.Int) error {
|
2020-06-04 18:11:27 +00:00
|
|
|
si := &state.StorageItem{Value: bigint.ToBytes(supply)}
|
2020-08-03 11:31:42 +00:00
|
|
|
return d.PutStorageItem(c.ContractID, totalSupplyKey, si)
|
2020-04-22 20:00:18 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) Transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-03-25 10:00:11 +00:00
|
|
|
from := toUint160(args[0])
|
|
|
|
to := toUint160(args[1])
|
|
|
|
amount := toBigInt(args[2])
|
2020-11-19 15:01:42 +00:00
|
|
|
err := c.TransferInternal(ic, from, to, amount, args[3])
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewBool(err == nil)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func addrToStackItem(u *util.Uint160) stackitem.Item {
|
2020-03-25 10:00:11 +00:00
|
|
|
if u == nil {
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.Null{}
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-06-03 12:55:06 +00:00
|
|
|
return stackitem.NewByteArray(u.BytesBE())
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) postTransfer(ic *interop.Context, from, to *util.Uint160, amount *big.Int, data stackitem.Item) {
|
|
|
|
c.emitTransfer(ic, from, to, amount)
|
|
|
|
if to == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cs, err := ic.DAO.GetContractState(*to)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fromArg := stackitem.Item(stackitem.Null{})
|
|
|
|
if from != nil {
|
|
|
|
fromArg = stackitem.NewByteArray((*from).BytesBE())
|
|
|
|
}
|
|
|
|
args := []stackitem.Item{
|
|
|
|
fromArg,
|
|
|
|
stackitem.NewBigInteger(amount),
|
|
|
|
data,
|
|
|
|
}
|
|
|
|
if err := contract.CallExInternal(ic, cs, manifest.MethodOnPayment, args, smartcontract.All, vm.EnsureIsEmpty); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *nep17TokenNative) emitTransfer(ic *interop.Context, from, to *util.Uint160, amount *big.Int) {
|
2020-03-25 10:00:11 +00:00
|
|
|
ne := state.NotificationEvent{
|
|
|
|
ScriptHash: c.Hash,
|
2020-06-29 08:25:32 +00:00
|
|
|
Name: "Transfer",
|
2020-06-03 12:55:06 +00:00
|
|
|
Item: stackitem.NewArray([]stackitem.Item{
|
2020-03-25 10:00:11 +00:00
|
|
|
addrToStackItem(from),
|
|
|
|
addrToStackItem(to),
|
2020-06-03 12:55:06 +00:00
|
|
|
stackitem.NewBigInteger(amount),
|
2020-03-25 10:00:11 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
ic.Notifications = append(ic.Notifications, ne)
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) updateAccBalance(ic *interop.Context, acc util.Uint160, amount *big.Int) error {
|
2020-06-23 19:20:17 +00:00
|
|
|
key := makeAccountKey(acc)
|
|
|
|
si := ic.DAO.GetStorageItem(c.ContractID, key)
|
|
|
|
if si == nil {
|
|
|
|
if amount.Sign() <= 0 {
|
|
|
|
return errors.New("insufficient funds")
|
|
|
|
}
|
|
|
|
si = new(state.StorageItem)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.incBalance(ic, acc, si, amount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if si.Value == nil {
|
|
|
|
err = ic.DAO.DeleteStorageItem(c.ContractID, key)
|
|
|
|
} else {
|
|
|
|
err = ic.DAO.PutStorageItem(c.ContractID, key, si)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:24:22 +00:00
|
|
|
// TransferInternal transfers NEO between accounts.
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) TransferInternal(ic *interop.Context, from, to util.Uint160, amount *big.Int, data stackitem.Item) error {
|
2020-03-25 10:00:11 +00:00
|
|
|
if amount.Sign() == -1 {
|
|
|
|
return errors.New("negative amount")
|
|
|
|
}
|
|
|
|
|
2020-08-07 13:58:09 +00:00
|
|
|
caller := ic.VM.GetCallingScriptHash()
|
2020-07-15 19:52:35 +00:00
|
|
|
if caller.Equals(util.Uint160{}) || !from.Equals(caller) {
|
|
|
|
ok, err := runtime.CheckHashedWitness(ic, from)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !ok {
|
|
|
|
return errors.New("invalid signature")
|
|
|
|
}
|
2020-05-19 07:19:05 +00:00
|
|
|
}
|
2020-03-25 10:00:11 +00:00
|
|
|
isEmpty := from.Equals(to) || amount.Sign() == 0
|
|
|
|
inc := amount
|
|
|
|
if isEmpty {
|
|
|
|
inc = big.NewInt(0)
|
2020-05-27 19:51:44 +00:00
|
|
|
} else {
|
|
|
|
inc = new(big.Int).Neg(inc)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-06-23 19:20:17 +00:00
|
|
|
if err := c.updateAccBalance(ic, from, inc); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isEmpty {
|
2020-06-23 19:20:17 +00:00
|
|
|
if err := c.updateAccBalance(ic, to, amount); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
c.postTransfer(ic, &from, &to, amount, data)
|
2020-03-25 10:00:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item {
|
2020-03-25 10:00:11 +00:00
|
|
|
h := toUint160(args[0])
|
2020-11-24 08:14:25 +00:00
|
|
|
bs, err := ic.DAO.GetNEP17Balances(h)
|
2020-03-25 10:00:11 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-07-28 09:23:58 +00:00
|
|
|
balance := bs.Trackers[c.ContractID].Balance
|
2020-07-09 09:57:24 +00:00
|
|
|
return stackitem.NewBigInteger(&balance)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) mint(ic *interop.Context, h util.Uint160, amount *big.Int) {
|
2020-06-04 12:41:54 +00:00
|
|
|
if amount.Sign() == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-03-25 10:00:11 +00:00
|
|
|
c.addTokens(ic, h, amount)
|
2020-11-19 15:01:42 +00:00
|
|
|
c.postTransfer(ic, nil, &h, amount, stackitem.Null{})
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) burn(ic *interop.Context, h util.Uint160, amount *big.Int) {
|
2020-06-04 12:41:54 +00:00
|
|
|
if amount.Sign() == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-06-18 13:19:07 +00:00
|
|
|
c.addTokens(ic, h, new(big.Int).Neg(amount))
|
2020-11-19 15:01:42 +00:00
|
|
|
c.postTransfer(ic, &h, nil, amount, stackitem.Null{})
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount *big.Int) {
|
2020-05-19 15:49:03 +00:00
|
|
|
if amount.Sign() == 0 {
|
2020-03-25 10:00:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:28:37 +00:00
|
|
|
key := makeAccountKey(h)
|
2020-06-18 10:50:30 +00:00
|
|
|
si := ic.DAO.GetStorageItem(c.ContractID, key)
|
2020-04-23 18:28:37 +00:00
|
|
|
if si == nil {
|
|
|
|
si = new(state.StorageItem)
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
2020-04-23 18:28:37 +00:00
|
|
|
if err := c.incBalance(ic, h, si, amount); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-06-18 10:50:30 +00:00
|
|
|
if err := ic.DAO.PutStorageItem(c.ContractID, key, si); err != nil {
|
2020-03-25 10:00:11 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:31:42 +00:00
|
|
|
supply := c.getTotalSupply(ic.DAO)
|
2020-04-22 20:00:18 +00:00
|
|
|
supply.Add(supply, amount)
|
2020-08-03 11:31:42 +00:00
|
|
|
err := c.saveTotalSupply(ic.DAO, supply)
|
2020-04-22 20:00:18 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:01:42 +00:00
|
|
|
func (c *nep17TokenNative) OnPersist(ic *interop.Context) error {
|
2020-10-29 16:14:49 +00:00
|
|
|
if ic.Trigger != trigger.OnPersist {
|
|
|
|
return errors.New("onPersist must be triggerred by system")
|
2020-06-17 10:57:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-03-25 10:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newDescriptor(name string, ret smartcontract.ParamType, ps ...manifest.Parameter) *manifest.Method {
|
|
|
|
return &manifest.Method{
|
|
|
|
Name: name,
|
|
|
|
Parameters: ps,
|
|
|
|
ReturnType: ret,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:00:18 +00:00
|
|
|
func newMethodAndPrice(f interop.Method, price int64, flags smartcontract.CallFlag) *interop.MethodAndPrice {
|
|
|
|
return &interop.MethodAndPrice{
|
2020-03-25 10:00:11 +00:00
|
|
|
Func: f,
|
|
|
|
Price: price,
|
|
|
|
RequiredFlags: flags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func toBigInt(s stackitem.Item) *big.Int {
|
2020-03-25 10:00:11 +00:00
|
|
|
bi, err := s.TryInteger()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return bi
|
|
|
|
}
|
|
|
|
|
2020-06-03 12:55:06 +00:00
|
|
|
func toUint160(s stackitem.Item) util.Uint160 {
|
2020-03-25 10:00:11 +00:00
|
|
|
buf, err := s.TryBytes()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
u, err := util.Uint160DecodeBytesBE(buf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
2020-05-08 17:54:24 +00:00
|
|
|
|
2020-06-17 10:57:54 +00:00
|
|
|
func getOnPersistWrapper(f func(ic *interop.Context) error) interop.Method {
|
|
|
|
return func(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2020-08-17 12:34:13 +00:00
|
|
|
err := f(ic)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("OnPersist for native contract: %w", err))
|
|
|
|
}
|
|
|
|
return stackitem.Null{}
|
2020-06-17 10:57:54 +00:00
|
|
|
}
|
|
|
|
}
|