native: make *totalSupply accept DAO

There is no need to provide full interop context.
This commit is contained in:
Evgenii Stratonikov 2020-08-03 14:31:42 +03:00
parent 3e39f0f211
commit f0b62cdaa6
3 changed files with 10 additions and 9 deletions

View file

@ -70,7 +70,7 @@ func (g *GAS) Initialize(ic *interop.Context) error {
if err := g.nep5TokenNative.Initialize(ic); err != nil {
return err
}
if g.nep5TokenNative.getTotalSupply(ic).Sign() != 0 {
if g.nep5TokenNative.getTotalSupply(ic.DAO).Sign() != 0 {
return errors.New("already initialized")
}
h, _, err := getStandbyValidatorsHash(ic)

View file

@ -121,7 +121,7 @@ func (n *NEO) Initialize(ic *interop.Context) error {
return err
}
if n.nep5TokenNative.getTotalSupply(ic).Sign() != 0 {
if n.nep5TokenNative.getTotalSupply(ic.DAO).Sign() != 0 {
return errors.New("already initialized")
}

View file

@ -4,6 +4,7 @@ import (
"errors"
"math/big"
"github.com/nspcc-dev/neo-go/pkg/core/dao"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
"github.com/nspcc-dev/neo-go/pkg/core/interop/runtime"
"github.com/nspcc-dev/neo-go/pkg/core/state"
@ -104,20 +105,20 @@ func (c *nep5TokenNative) Decimals(_ *interop.Context, _ []stackitem.Item) stack
}
func (c *nep5TokenNative) TotalSupply(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
return stackitem.NewBigInteger(c.getTotalSupply(ic))
return stackitem.NewBigInteger(c.getTotalSupply(ic.DAO))
}
func (c *nep5TokenNative) getTotalSupply(ic *interop.Context) *big.Int {
si := ic.DAO.GetStorageItem(c.ContractID, totalSupplyKey)
func (c *nep5TokenNative) getTotalSupply(d dao.DAO) *big.Int {
si := d.GetStorageItem(c.ContractID, totalSupplyKey)
if si == nil {
return big.NewInt(0)
}
return bigint.FromBytes(si.Value)
}
func (c *nep5TokenNative) saveTotalSupply(ic *interop.Context, supply *big.Int) error {
func (c *nep5TokenNative) saveTotalSupply(d dao.DAO, supply *big.Int) error {
si := &state.StorageItem{Value: bigint.ToBytes(supply)}
return ic.DAO.PutStorageItem(c.ContractID, totalSupplyKey, si)
return d.PutStorageItem(c.ContractID, totalSupplyKey, si)
}
func (c *nep5TokenNative) Transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item {
@ -248,9 +249,9 @@ func (c *nep5TokenNative) addTokens(ic *interop.Context, h util.Uint160, amount
panic(err)
}
supply := c.getTotalSupply(ic)
supply := c.getTotalSupply(ic.DAO)
supply.Add(supply, amount)
err := c.saveTotalSupply(ic, supply)
err := c.saveTotalSupply(ic.DAO, supply)
if err != nil {
panic(err)
}