mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-23 13:41:37 +00:00
wallet: add Standard
field to Token
We need to distinguish NEP11 and NEP17 tokens preesnted in the wallet.
This commit is contained in:
parent
28b78d1a6c
commit
40ae78cb88
9 changed files with 30 additions and 17 deletions
|
@ -12,6 +12,7 @@ import (
|
|||
"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/fixedn"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
||||
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@ -279,6 +280,7 @@ func TestNEP17ImportToken(t *testing.T) {
|
|||
e.checkNextLine(t, "^Hash:\\s*"+gasContractHash.StringLE())
|
||||
e.checkNextLine(t, "^Decimals:\\s*8")
|
||||
e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(gasContractHash))
|
||||
e.checkNextLine(t, "^Standard:\\s*"+manifest.NEP17StandardName)
|
||||
}
|
||||
t.Run("WithToken", func(t *testing.T) {
|
||||
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, "^Decimals:\\s*0")
|
||||
e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(neoContractHash))
|
||||
e.checkNextLine(t, "^Standard:\\s*"+manifest.NEP17StandardName)
|
||||
})
|
||||
t.Run("Remove", func(t *testing.T) {
|
||||
e.In.WriteString("y\r")
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"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/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/wallet"
|
||||
"github.com/urfave/cli"
|
||||
|
@ -186,7 +187,7 @@ func getNEP17Balance(ctx *cli.Context) error {
|
|||
var tokenName, tokenSymbol string
|
||||
tokenDecimals := 0
|
||||
asset := balances.Balances[i].Asset
|
||||
token, err := getMatchingToken(ctx, wall, asset.StringLE())
|
||||
token, err := getMatchingToken(ctx, wall, asset.StringLE(), manifest.NEP17StandardName)
|
||||
if err != nil {
|
||||
token, err = c.NEP17TokenInfo(asset)
|
||||
}
|
||||
|
@ -218,10 +219,10 @@ func getNEP17Balance(ctx *cli.Context) error {
|
|||
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 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) {
|
||||
|
@ -233,15 +234,15 @@ func getMatchingTokenRPC(ctx *cli.Context, c *client.Client, addr util.Uint160,
|
|||
t, _ := c.NEP17TokenInfo(bs.Balances[i].Asset)
|
||||
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 count int
|
||||
for i := 0; i < n; 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 {
|
||||
printTokenInfo(ctx, token)
|
||||
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, "Decimals: %d\n", tok.Decimals)
|
||||
fmt.Fprintf(w, "Address: %s\n", tok.Address())
|
||||
fmt.Fprintf(w, "Standard:\t%s\n", tok.Standard)
|
||||
}
|
||||
|
||||
func printNEP17Info(ctx *cli.Context) error {
|
||||
|
@ -315,7 +317,7 @@ func printNEP17Info(ctx *cli.Context) error {
|
|||
defer wall.Close()
|
||||
|
||||
if name := ctx.String("token"); name != "" {
|
||||
token, err := getMatchingToken(ctx, wall, name)
|
||||
token, err := getMatchingToken(ctx, wall, name, manifest.NEP17StandardName)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -339,7 +341,7 @@ func removeNEP17Token(ctx *cli.Context) error {
|
|||
}
|
||||
defer wall.Close()
|
||||
|
||||
token, err := getMatchingToken(ctx, wall, ctx.String("token"))
|
||||
token, err := getMatchingToken(ctx, wall, ctx.String("token"), manifest.NEP17StandardName)
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
@ -401,7 +403,7 @@ func multiTransferNEP17(ctx *cli.Context) error {
|
|||
}
|
||||
token, ok := cache[ss[0]]
|
||||
if !ok {
|
||||
token, err = getMatchingToken(ctx, wall, ss[0])
|
||||
token, err = getMatchingToken(ctx, wall, ss[0], manifest.NEP17StandardName)
|
||||
if err != nil {
|
||||
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])
|
||||
|
@ -466,7 +468,7 @@ func transferNEP17(ctx *cli.Context) error {
|
|||
|
||||
toFlag := ctx.Generic("to").(*flags.Address)
|
||||
to := toFlag.Uint160()
|
||||
token, err := getMatchingToken(ctx, wall, ctx.String("token"))
|
||||
token, err := getMatchingToken(ctx, wall, ctx.String("token"), manifest.NEP17StandardName)
|
||||
if err != nil {
|
||||
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"))
|
||||
|
|
|
@ -73,7 +73,7 @@ func (c *Client) nepBalanceOf(tokenHash, acc util.Uint160, tokenID *string) (int
|
|||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -86,5 +86,5 @@ func (c *Client) nepTokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals), nil
|
||||
return wallet.NewToken(tokenHash, cs.Manifest.Name, symbol, decimals, standard), nil
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"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/manifest"
|
||||
"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/opcode"
|
||||
|
@ -37,7 +38,7 @@ func (c *Client) NEP11BalanceOf(tokenHash, owner util.Uint160) (int64, error) {
|
|||
|
||||
// NEP11TokenInfo returns full NEP11 token info.
|
||||
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
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
||||
"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/manifest"
|
||||
"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/opcode"
|
||||
|
@ -50,7 +51,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) {
|
||||
return c.nepTokenInfo(tokenHash)
|
||||
return c.nepTokenInfo(tokenHash, manifest.NEP17StandardName)
|
||||
}
|
||||
|
||||
// CreateNEP17TransferTx creates an invocation transaction for the 'transfer'
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
"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/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/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
|
@ -808,6 +809,7 @@ func TestClient_NEP11(t *testing.T) {
|
|||
Hash: h,
|
||||
Decimals: 0,
|
||||
Symbol: "NNS",
|
||||
Standard: manifest.NEP11StandardName,
|
||||
}, tok)
|
||||
})
|
||||
t.Run("BalanceOf", func(t *testing.T) {
|
||||
|
|
|
@ -11,15 +11,17 @@ type Token struct {
|
|||
Hash util.Uint160 `json:"script_hash"`
|
||||
Decimals int64 `json:"decimals"`
|
||||
Symbol string `json:"symbol"`
|
||||
Standard string `json:"standard"`
|
||||
}
|
||||
|
||||
// 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{
|
||||
Name: name,
|
||||
Hash: tokenHash,
|
||||
Decimals: decimals,
|
||||
Symbol: symbol,
|
||||
Standard: standardName,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ func TestToken_MarshalJSON(t *testing.T) {
|
|||
h, err := util.Uint160DecodeStringLE("f8d448b227991cf07cb96a6f9c0322437f1599b9")
|
||||
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", tok.Symbol)
|
||||
require.EqualValues(t, 8, tok.Decimals)
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"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/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
@ -146,7 +147,7 @@ func removeWallet(t *testing.T, walletPath string) {
|
|||
|
||||
func TestWallet_AddToken(t *testing.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))
|
||||
w.AddToken(tok)
|
||||
require.Equal(t, 1, len(w.Extra.Tokens))
|
||||
|
|
Loading…
Reference in a new issue