From a3f32bf3061bd7c25397ebdac10ccaeaa8371aa2 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 26 Aug 2022 21:52:19 +0300 Subject: [PATCH] neptoken: move BalanceOf implementation to Base from nep11/nep17 It's the same, even though standards define parameter name in a bit different way. --- pkg/rpcclient/nep11/base.go | 6 ------ pkg/rpcclient/nep17/nep17.go | 18 +++++------------ pkg/rpcclient/neptoken/base.go | 9 +++++++++ pkg/rpcclient/neptoken/base_test.go | 30 +++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/pkg/rpcclient/nep11/base.go b/pkg/rpcclient/nep11/base.go index e9932579a..8aaf06a89 100644 --- a/pkg/rpcclient/nep11/base.go +++ b/pkg/rpcclient/nep11/base.go @@ -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 diff --git a/pkg/rpcclient/nep17/nep17.go b/pkg/rpcclient/nep17/nep17.go index e19f4ec7f..d6c263747 100644 --- a/pkg/rpcclient/nep17/nep17.go +++ b/pkg/rpcclient/nep17/nep17.go @@ -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 diff --git a/pkg/rpcclient/neptoken/base.go b/pkg/rpcclient/neptoken/base.go index 63e258d6b..610e2be8e 100644 --- a/pkg/rpcclient/neptoken/base.go +++ b/pkg/rpcclient/neptoken/base.go @@ -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)) +} diff --git a/pkg/rpcclient/neptoken/base_test.go b/pkg/rpcclient/neptoken/base_test.go index b251de5f0..7f7320d7c 100644 --- a/pkg/rpcclient/neptoken/base_test.go +++ b/pkg/rpcclient/neptoken/base_test.go @@ -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) +}