Merge pull request #1876 from nspcc-dev/nep17-fix

core: get native nep17 balance from contract storage
This commit is contained in:
Roman Khimov 2021-04-01 19:39:58 +03:00 committed by GitHub
commit 87db4c9268
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 9 deletions

View file

@ -34,6 +34,7 @@ func newGAS() *GAS {
nep17.decimals = 8 nep17.decimals = 8
nep17.factor = GASFactor nep17.factor = GASFactor
nep17.incBalance = g.increaseBalance nep17.incBalance = g.increaseBalance
nep17.balFromBytes = g.balanceFromBytes
g.nep17TokenNative = *nep17 g.nep17TokenNative = *nep17
@ -59,6 +60,14 @@ func (g *GAS) increaseBalance(_ *interop.Context, _ util.Uint160, si *state.Stor
return nil return nil
} }
func (g *GAS) balanceFromBytes(si *state.StorageItem) (*big.Int, error) {
acc, err := state.NEP17BalanceStateFromBytes(*si)
if err != nil {
return nil, err
}
return &acc.Balance, err
}
// Initialize initializes GAS contract. // Initialize initializes GAS contract.
func (g *GAS) Initialize(ic *interop.Context) error { func (g *GAS) Initialize(ic *interop.Context) error {
if err := g.nep17TokenNative.Initialize(ic); err != nil { if err := g.nep17TokenNative.Initialize(ic); err != nil {

View file

@ -107,6 +107,7 @@ func newNEO() *NEO {
nep17.decimals = 0 nep17.decimals = 0
nep17.factor = 1 nep17.factor = 1
nep17.incBalance = n.increaseBalance nep17.incBalance = n.increaseBalance
nep17.balFromBytes = n.balanceFromBytes
n.nep17TokenNative = *nep17 n.nep17TokenNative = *nep17
n.votesChanged.Store(true) n.votesChanged.Store(true)
@ -404,6 +405,14 @@ func (n *NEO) increaseBalance(ic *interop.Context, h util.Uint160, si *state.Sto
return nil return nil
} }
func (n *NEO) balanceFromBytes(si *state.StorageItem) (*big.Int, error) {
acc, err := state.NEOBalanceStateFromBytes(*si)
if err != nil {
return nil, err
}
return &acc.Balance, err
}
func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOBalanceState) error { func (n *NEO) distributeGas(ic *interop.Context, h util.Uint160, acc *state.NEOBalanceState) error {
if ic.Block == nil || ic.Block.Index == 0 { if ic.Block == nil || ic.Block.Index == 0 {
return nil return nil

View file

@ -2,6 +2,7 @@ package native
import ( import (
"errors" "errors"
"fmt"
"math" "math"
"math/big" "math/big"
@ -29,10 +30,11 @@ func makeAccountKey(h util.Uint160) []byte {
// nep17TokenNative represents NEP-17 token contract. // nep17TokenNative represents NEP-17 token contract.
type nep17TokenNative struct { type nep17TokenNative struct {
interop.ContractMD interop.ContractMD
symbol string symbol string
decimals int64 decimals int64
factor int64 factor int64
incBalance func(*interop.Context, util.Uint160, *state.StorageItem, *big.Int) error incBalance func(*interop.Context, util.Uint160, *state.StorageItem, *big.Int) error
balFromBytes func(item *state.StorageItem) (*big.Int, error)
} }
// totalSupplyKey is the key used to store totalSupply value. // totalSupplyKey is the key used to store totalSupply value.
@ -221,12 +223,16 @@ func (c *nep17TokenNative) TransferInternal(ic *interop.Context, from, to util.U
func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item { func (c *nep17TokenNative) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item {
h := toUint160(args[0]) h := toUint160(args[0])
bs, err := ic.DAO.GetNEP17Balances(h) key := makeAccountKey(h)
if err != nil { si := ic.DAO.GetStorageItem(c.ID, key)
panic(err) if si == nil {
return stackitem.NewBigInteger(big.NewInt(0))
} }
balance := bs.Trackers[c.ID].Balance balance, err := c.balFromBytes(&si)
return stackitem.NewBigInteger(&balance) if err != nil {
panic(fmt.Errorf("can not deserialize balance state: %w", err))
}
return stackitem.NewBigInteger(balance)
} }
func (c *nep17TokenNative) mint(ic *interop.Context, h util.Uint160, amount *big.Int, callOnPayment bool) { func (c *nep17TokenNative) mint(ic *interop.Context, h util.Uint160, amount *big.Int, callOnPayment bool) {