wallet: add Standard field to Token

We need to distinguish NEP11 and NEP17 tokens preesnted in the wallet.
This commit is contained in:
Anna Shaleva 2021-04-23 17:32:48 +03:00
parent 28b78d1a6c
commit 40ae78cb88
9 changed files with 30 additions and 17 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -279,6 +280,7 @@ func TestNEP17ImportToken(t *testing.T) {
e.checkNextLine(t, "^Hash:\\s*"+gasContractHash.StringLE()) e.checkNextLine(t, "^Hash:\\s*"+gasContractHash.StringLE())
e.checkNextLine(t, "^Decimals:\\s*8") e.checkNextLine(t, "^Decimals:\\s*8")
e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(gasContractHash)) e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(gasContractHash))
e.checkNextLine(t, "^Standard:\\s*"+manifest.NEP17StandardName)
} }
t.Run("WithToken", func(t *testing.T) { t.Run("WithToken", func(t *testing.T) {
e.Run(t, "neo-go", "wallet", "nep17", "info", e.Run(t, "neo-go", "wallet", "nep17", "info",
@ -296,6 +298,7 @@ func TestNEP17ImportToken(t *testing.T) {
e.checkNextLine(t, "^Hash:\\s*"+neoContractHash.StringLE()) e.checkNextLine(t, "^Hash:\\s*"+neoContractHash.StringLE())
e.checkNextLine(t, "^Decimals:\\s*0") e.checkNextLine(t, "^Decimals:\\s*0")
e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(neoContractHash)) e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(neoContractHash))
e.checkNextLine(t, "^Standard:\\s*"+manifest.NEP17StandardName)
}) })
t.Run("Remove", func(t *testing.T) { t.Run("Remove", func(t *testing.T) {
e.In.WriteString("y\r") e.In.WriteString("y\r")

View file

@ -13,6 +13,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/wallet" "github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -186,7 +187,7 @@ func getNEP17Balance(ctx *cli.Context) error {
var tokenName, tokenSymbol string var tokenName, tokenSymbol string
tokenDecimals := 0 tokenDecimals := 0
asset := balances.Balances[i].Asset asset := balances.Balances[i].Asset
token, err := getMatchingToken(ctx, wall, asset.StringLE()) token, err := getMatchingToken(ctx, wall, asset.StringLE(), manifest.NEP17StandardName)
if err != nil { if err != nil {
token, err = c.NEP17TokenInfo(asset) token, err = c.NEP17TokenInfo(asset)
} }
@ -218,10 +219,10 @@ func getNEP17Balance(ctx *cli.Context) error {
return nil return nil
} }
func getMatchingToken(ctx *cli.Context, w *wallet.Wallet, name string) (*wallet.Token, error) { func getMatchingToken(ctx *cli.Context, w *wallet.Wallet, name string, standard string) (*wallet.Token, error) {
return getMatchingTokenAux(ctx, func(i int) *wallet.Token { return getMatchingTokenAux(ctx, func(i int) *wallet.Token {
return w.Extra.Tokens[i] return w.Extra.Tokens[i]
}, len(w.Extra.Tokens), name) }, len(w.Extra.Tokens), name, standard)
} }
func getMatchingTokenRPC(ctx *cli.Context, c *client.Client, addr util.Uint160, name string) (*wallet.Token, error) { func getMatchingTokenRPC(ctx *cli.Context, c *client.Client, addr util.Uint160, name string) (*wallet.Token, error) {
@ -233,15 +234,15 @@ func getMatchingTokenRPC(ctx *cli.Context, c *client.Client, addr util.Uint160,
t, _ := c.NEP17TokenInfo(bs.Balances[i].Asset) t, _ := c.NEP17TokenInfo(bs.Balances[i].Asset)
return t return t
} }
return getMatchingTokenAux(ctx, get, len(bs.Balances), name) return getMatchingTokenAux(ctx, get, len(bs.Balances), name, manifest.NEP17StandardName)
} }
func getMatchingTokenAux(ctx *cli.Context, get func(i int) *wallet.Token, n int, name string) (*wallet.Token, error) { func getMatchingTokenAux(ctx *cli.Context, get func(i int) *wallet.Token, n int, name string, standard string) (*wallet.Token, error) {
var token *wallet.Token var token *wallet.Token
var count int var count int
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
t := get(i) t := get(i)
if t != nil && (t.Hash.StringLE() == name || t.Address() == name || t.Symbol == name || t.Name == name) { if t != nil && (t.Hash.StringLE() == name || t.Address() == name || t.Symbol == name || t.Name == name) && t.Standard == standard {
if count == 1 { if count == 1 {
printTokenInfo(ctx, token) printTokenInfo(ctx, token)
printTokenInfo(ctx, t) printTokenInfo(ctx, t)
@ -305,6 +306,7 @@ func printTokenInfo(ctx *cli.Context, tok *wallet.Token) {
fmt.Fprintf(w, "Hash:\t%s\n", tok.Hash.StringLE()) fmt.Fprintf(w, "Hash:\t%s\n", tok.Hash.StringLE())
fmt.Fprintf(w, "Decimals: %d\n", tok.Decimals) fmt.Fprintf(w, "Decimals: %d\n", tok.Decimals)
fmt.Fprintf(w, "Address: %s\n", tok.Address()) fmt.Fprintf(w, "Address: %s\n", tok.Address())
fmt.Fprintf(w, "Standard:\t%s\n", tok.Standard)
} }
func printNEP17Info(ctx *cli.Context) error { func printNEP17Info(ctx *cli.Context) error {
@ -315,7 +317,7 @@ func printNEP17Info(ctx *cli.Context) error {
defer wall.Close() defer wall.Close()
if name := ctx.String("token"); name != "" { if name := ctx.String("token"); name != "" {
token, err := getMatchingToken(ctx, wall, name) token, err := getMatchingToken(ctx, wall, name, manifest.NEP17StandardName)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -339,7 +341,7 @@ func removeNEP17Token(ctx *cli.Context) error {
} }
defer wall.Close() defer wall.Close()
token, err := getMatchingToken(ctx, wall, ctx.String("token")) token, err := getMatchingToken(ctx, wall, ctx.String("token"), manifest.NEP17StandardName)
if err != nil { if err != nil {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
@ -401,7 +403,7 @@ func multiTransferNEP17(ctx *cli.Context) error {
} }
token, ok := cache[ss[0]] token, ok := cache[ss[0]]
if !ok { if !ok {
token, err = getMatchingToken(ctx, wall, ss[0]) token, err = getMatchingToken(ctx, wall, ss[0], manifest.NEP17StandardName)
if err != nil { if err != nil {
fmt.Fprintln(ctx.App.ErrWriter, "Can't find matching token in the wallet. Querying RPC-node for balances.") fmt.Fprintln(ctx.App.ErrWriter, "Can't find matching token in the wallet. Querying RPC-node for balances.")
token, err = getMatchingTokenRPC(ctx, c, from, ss[0]) token, err = getMatchingTokenRPC(ctx, c, from, ss[0])
@ -466,7 +468,7 @@ func transferNEP17(ctx *cli.Context) error {
toFlag := ctx.Generic("to").(*flags.Address) toFlag := ctx.Generic("to").(*flags.Address)
to := toFlag.Uint160() to := toFlag.Uint160()
token, err := getMatchingToken(ctx, wall, ctx.String("token")) token, err := getMatchingToken(ctx, wall, ctx.String("token"), manifest.NEP17StandardName)
if err != nil { if err != nil {
fmt.Fprintln(ctx.App.ErrWriter, "Can't find matching token in the wallet. Querying RPC-node for balances.") fmt.Fprintln(ctx.App.ErrWriter, "Can't find matching token in the wallet. Querying RPC-node for balances.")
token, err = getMatchingTokenRPC(ctx, c, from, ctx.String("token")) token, err = getMatchingTokenRPC(ctx, c, from, ctx.String("token"))

View file

@ -73,7 +73,7 @@ func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID *string) (int
} }
// nepTokenInfo returns full NEP* token info. // nepTokenInfo returns full NEP* token info.
func (c *Client) nepTokenInfo(tokenHash util.Uint160) (*wallet.Token, error) { func (c *Client) nepTokenInfo(tokenHash util.Uint160, standard string) (*wallet.Token, error) {
cs, err := c.GetContractStateByHash(tokenHash) cs, err := c.GetContractStateByHash(tokenHash)
if err != nil { if err != nil {
return nil, err return nil, err
@ -86,5 +86,5 @@ func (c *Client) nepTokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals), nil return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals, standard), nil
} }

View file

@ -8,6 +8,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
@ -37,7 +38,7 @@ func (c *Client) NEP11BalanceOf(tokenHash, owner util.Uint160) (int64, error) {
// NEP11TokenInfo returns full NEP11 token info. // NEP11TokenInfo returns full NEP11 token info.
func (c *Client) NEP11TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) { func (c *Client) NEP11TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
return c.nepTokenInfo(tokenHash) return c.nepTokenInfo(tokenHash, manifest.NEP11StandardName)
} }
// TransferNEP11 creates an invocation transaction that invokes 'transfer' method // TransferNEP11 creates an invocation transaction that invokes 'transfer' method

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io" "github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit" "github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode" "github.com/nspcc-dev/neo-go/pkg/vm/opcode"
@ -50,7 +51,7 @@ func (c *Client) NEP17BalanceOf(tokenHash, acc util.Uint160) (int64, error) {
// NEP17TokenInfo returns full NEP17 token info. // NEP17TokenInfo returns full NEP17 token info.
func (c *Client) NEP17TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) { func (c *Client) NEP17TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
return c.nepTokenInfo(tokenHash) return c.nepTokenInfo(tokenHash, manifest.NEP17StandardName)
} }
// CreateNEP17TransferTx creates an invocation transaction for the 'transfer' // CreateNEP17TransferTx creates an invocation transaction for the 'transfer'

View file

@ -17,6 +17,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpc/client" "github.com/nspcc-dev/neo-go/pkg/rpc/client"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm"
@ -808,6 +809,7 @@ func TestClient_NEP11(t *testing.T) {
Hash: h, Hash: h,
Decimals: 0, Decimals: 0,
Symbol: "NNS", Symbol: "NNS",
Standard: manifest.NEP11StandardName,
}, tok) }, tok)
}) })
t.Run("BalanceOf", func(t *testing.T) { t.Run("BalanceOf", func(t *testing.T) {

View file

@ -11,15 +11,17 @@ type Token struct {
Hash util.Uint160 `json:"script_hash"` Hash util.Uint160 `json:"script_hash"`
Decimals int64 `json:"decimals"` Decimals int64 `json:"decimals"`
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
Standard string `json:"standard"`
} }
// NewToken returns new token contract info. // NewToken returns new token contract info.
func NewToken(tokenHash util.Uint160, name, symbol string, decimals int64) *Token { func NewToken(tokenHash util.Uint160, name, symbol string, decimals int64, standardName string) *Token {
return &Token{ return &Token{
Name: name, Name: name,
Hash: tokenHash, Hash: tokenHash,
Decimals: decimals, Decimals: decimals,
Symbol: symbol, Symbol: symbol,
Standard: standardName,
} }
} }

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -13,7 +14,7 @@ func TestToken_MarshalJSON(t *testing.T) {
h, err := util.Uint160DecodeStringLE("f8d448b227991cf07cb96a6f9c0322437f1599b9") h, err := util.Uint160DecodeStringLE("f8d448b227991cf07cb96a6f9c0322437f1599b9")
require.NoError(t, err) require.NoError(t, err)
tok := NewToken(h, "NEP17 Standard", "NEP17", 8) tok := NewToken(h, "NEP17 Standard", "NEP17", 8, manifest.NEP17StandardName)
require.Equal(t, "NEP17 Standard", tok.Name) require.Equal(t, "NEP17 Standard", tok.Name)
require.Equal(t, "NEP17", tok.Symbol) require.Equal(t, "NEP17", tok.Symbol)
require.EqualValues(t, 8, tok.Decimals) require.EqualValues(t, 8, tok.Decimals)

View file

@ -8,6 +8,7 @@ import (
"testing" "testing"
"github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
"github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/util"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -146,7 +147,7 @@ func removeWallet(t *testing.T, walletPath string) {
func TestWallet_AddToken(t *testing.T) { func TestWallet_AddToken(t *testing.T) {
w := checkWalletConstructor(t) w := checkWalletConstructor(t)
tok := NewToken(util.Uint160{1, 2, 3}, "Rubl", "RUB", 2) tok := NewToken(util.Uint160{1, 2, 3}, "Rubl", "RUB", 2, manifest.NEP17StandardName)
require.Equal(t, 0, len(w.Extra.Tokens)) require.Equal(t, 0, len(w.Extra.Tokens))
w.AddToken(tok) w.AddToken(tok)
require.Equal(t, 1, len(w.Extra.Tokens)) require.Equal(t, 1, len(w.Extra.Tokens))