From 2fb083bd376b7de6d3880ac5aef2627c2567cb7e Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Thu, 22 Apr 2021 17:44:09 +0300 Subject: [PATCH] rpc: add NEP11TokenInfo method --- pkg/rpc/client/nep.go | 18 ++++++++++++++++++ pkg/rpc/client/nep11.go | 5 +++++ pkg/rpc/client/nep17.go | 14 +------------- pkg/rpc/server/client_test.go | 10 ++++++++++ 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pkg/rpc/client/nep.go b/pkg/rpc/client/nep.go index 092669eb1..d202a628a 100644 --- a/pkg/rpc/client/nep.go +++ b/pkg/rpc/client/nep.go @@ -3,6 +3,7 @@ package client import ( "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/wallet" ) // nepDecimals invokes `decimals` NEP* method on a specified contract. @@ -70,3 +71,20 @@ func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID *string) (int return topIntFromStack(result.Stack) } + +// nepTokenInfo returns full NEP* token info. +func (c *Client) nepTokenInfo(tokenHash util.Uint160) (*wallet.Token, error) { + cs, err := c.GetContractStateByHash(tokenHash) + if err != nil { + return nil, err + } + symbol, err := c.nepSymbol(tokenHash) + if err != nil { + return nil, err + } + decimals, err := c.nepDecimals(tokenHash) + if err != nil { + return nil, err + } + return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals), nil +} diff --git a/pkg/rpc/client/nep11.go b/pkg/rpc/client/nep11.go index 664430cd5..1e424593f 100644 --- a/pkg/rpc/client/nep11.go +++ b/pkg/rpc/client/nep11.go @@ -35,6 +35,11 @@ func (c *Client) NEP11BalanceOf(tokenHash, owner util.Uint160) (int64, error) { return c.nepBalanceOf(tokenHash, owner, nil) } +// NEP11TokenInfo returns full NEP11 token info. +func (c *Client) NEP11TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) { + return c.nepTokenInfo(tokenHash) +} + // TransferNEP11 creates an invocation transaction that invokes 'transfer' method // on a given token to move the whole NEP11 token with the specified token ID to // given account and sends it to the network returning just a hash of it. diff --git a/pkg/rpc/client/nep17.go b/pkg/rpc/client/nep17.go index 470de9380..56455e09e 100644 --- a/pkg/rpc/client/nep17.go +++ b/pkg/rpc/client/nep17.go @@ -50,19 +50,7 @@ func (c *Client) NEP17BalanceOf(tokenHash, acc util.Uint160) (int64, error) { // NEP17TokenInfo returns full NEP17 token info. func (c *Client) NEP17TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) { - cs, err := c.GetContractStateByHash(tokenHash) - if err != nil { - return nil, err - } - symbol, err := c.NEP17Symbol(tokenHash) - if err != nil { - return nil, err - } - decimals, err := c.NEP17Decimals(tokenHash) - if err != nil { - return nil, err - } - return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals), nil + return c.nepTokenInfo(tokenHash) } // CreateNEP17TransferTx creates an invocation transaction for the 'transfer' diff --git a/pkg/rpc/server/client_test.go b/pkg/rpc/server/client_test.go index 826981bf5..b32deb4e9 100644 --- a/pkg/rpc/server/client_test.go +++ b/pkg/rpc/server/client_test.go @@ -800,6 +800,16 @@ func TestClient_NEP11(t *testing.T) { require.NoError(t, err) require.Equal(t, "NNS", sym) }) + t.Run("TokenInfo", func(t *testing.T) { + tok, err := c.NEP11TokenInfo(h) + require.NoError(t, err) + require.Equal(t, &wallet.Token{ + Name: nativenames.NameService, + Hash: h, + Decimals: 0, + Symbol: "NNS", + }, tok) + }) t.Run("BalanceOf", func(t *testing.T) { b, err := c.NEP11BalanceOf(h, acc) require.NoError(t, err)