rpc: add NEP11TokenInfo method

This commit is contained in:
Anna Shaleva 2021-04-22 17:44:09 +03:00
parent 5924123927
commit 2fb083bd37
4 changed files with 34 additions and 13 deletions

View file

@ -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
}

View file

@ -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.

View file

@ -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'

View file

@ -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)