neptoken: move BalanceOf implementation to Base from nep11/nep17

It's the same, even though standards define parameter name in a bit different
way.
This commit is contained in:
Roman Khimov 2022-08-26 21:52:19 +03:00
parent bf06b32278
commit a3f32bf306
4 changed files with 44 additions and 19 deletions

View file

@ -86,12 +86,6 @@ func NewBase(actor Actor, hash util.Uint160) *Base {
return &Base{*NewBaseReader(actor, hash), actor}
}
// BalanceOf returns the number of NFTs owned by the given account. For divisible
// NFTs that's the sum of all parts of tokens.
func (t *BaseReader) BalanceOf(account util.Uint160) (*big.Int, error) {
return unwrap.BigInt(t.invoker.Call(t.hash, "balanceOf", account))
}
// Properties returns a set of token's properties such as name or URL. The map
// is returned as is from this method (stack item) for maximum flexibility,
// contracts can return a lot of specific data there. Most of the time though

View file

@ -12,7 +12,6 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neptoken"
"github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
)
@ -35,15 +34,13 @@ type Actor interface {
// used to query various data.
type TokenReader struct {
neptoken.Base
invoker Invoker
hash util.Uint160
}
// Token provides full NEP-17 interface, both safe and state-changing methods.
type Token struct {
TokenReader
hash util.Uint160
actor Actor
}
@ -62,21 +59,16 @@ type TransferParameters struct {
Data interface{}
}
// NewReader creates an instance of TokenReader for contract with the given hash
// using the given Invoker.
// NewReader creates an instance of TokenReader for contract with the given
// hash using the given Invoker.
func NewReader(invoker Invoker, hash util.Uint160) *TokenReader {
return &TokenReader{*neptoken.New(invoker, hash), invoker, hash}
return &TokenReader{*neptoken.New(invoker, hash)}
}
// New creates an instance of Token for contract with the given hash
// using the given Actor.
func New(actor Actor, hash util.Uint160) *Token {
return &Token{*NewReader(actor, hash), actor}
}
// BalanceOf returns the token balance of the given account.
func (t *TokenReader) BalanceOf(account util.Uint160) (*big.Int, error) {
return unwrap.BigInt(t.invoker.Call(t.hash, "balanceOf", account))
return &Token{*NewReader(actor, hash), hash, actor}
}
// Transfer creates and sends a transaction that performs a `transfer` method

View file

@ -61,3 +61,12 @@ func (b *Base) Symbol() (string, error) {
func (b *Base) TotalSupply() (*big.Int, error) {
return unwrap.BigInt(b.invoker.Call(b.hash, "totalSupply"))
}
// BalanceOf returns the token balance of the given account. For NEP-17 that's
// the token balance with decimals (1 TOK with 2 decimals will lead to 100
// returned from this method). For non-divisible NEP-11 that's the number of
// NFTs owned by the account, for divisible NEP-11 that's the sum of the parts
// of all NFTs owned by the account.
func (b *Base) BalanceOf(account util.Uint160) (*big.Int, error) {
return unwrap.BigInt(b.invoker.Call(b.hash, "balanceOf", account))
}

View file

@ -31,6 +31,8 @@ func TestBaseErrors(t *testing.T) {
require.Error(t, err)
_, err = base.TotalSupply()
require.Error(t, err)
_, err = base.BalanceOf(util.Uint160{1, 2, 3})
require.Error(t, err)
ti.err = nil
ti.res = &result.Invoke{
@ -43,6 +45,8 @@ func TestBaseErrors(t *testing.T) {
require.Error(t, err)
_, err = base.TotalSupply()
require.Error(t, err)
_, err = base.BalanceOf(util.Uint160{1, 2, 3})
require.Error(t, err)
ti.res = &result.Invoke{
State: "HALT",
@ -53,6 +57,8 @@ func TestBaseErrors(t *testing.T) {
require.Error(t, err)
_, err = base.TotalSupply()
require.Error(t, err)
_, err = base.BalanceOf(util.Uint160{1, 2, 3})
require.Error(t, err)
}
func TestBaseDecimals(t *testing.T) {
@ -135,3 +141,27 @@ func TestBaseTotalSupply(t *testing.T) {
_, err = base.TotalSupply()
require.Error(t, err)
}
func TestBaseBalanceOf(t *testing.T) {
ti := new(testInv)
base := New(ti, util.Uint160{1, 2, 3})
ti.res = &result.Invoke{
State: "HALT",
Stack: []stackitem.Item{
stackitem.Make(100500),
},
}
bal, err := base.BalanceOf(util.Uint160{1, 2, 3})
require.NoError(t, err)
require.Equal(t, big.NewInt(100500), bal)
ti.res = &result.Invoke{
State: "HALT",
Stack: []stackitem.Item{
stackitem.Make([]stackitem.Item{}),
},
}
_, err = base.BalanceOf(util.Uint160{1, 2, 3})
require.Error(t, err)
}