cli: implement NEP5 token import
To work with NEP5 tokens, one need to save some info e.g. token Name. This way we will not make additional RPC just to get Decimals.
This commit is contained in:
parent
22e99a5b3e
commit
519b27fe0e
2 changed files with 82 additions and 0 deletions
77
cli/wallet/nep5.go
Normal file
77
cli/wallet/nep5.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package wallet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
func newNEP5Commands() []cli.Command {
|
||||
return []cli.Command{
|
||||
{
|
||||
Name: "import",
|
||||
Usage: "import NEP5 token to a wallet",
|
||||
UsageText: "import --path <path> --rpc <node> --token <hash>",
|
||||
Action: importNEP5Token,
|
||||
Flags: []cli.Flag{
|
||||
walletPathFlag,
|
||||
rpcFlag,
|
||||
cli.StringFlag{
|
||||
Name: "token",
|
||||
Usage: "Token contract hash in LE",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func importNEP5Token(ctx *cli.Context) error {
|
||||
wall, err := openWallet(ctx.String("path"))
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
defer wall.Close()
|
||||
|
||||
tokenHash, err := util.Uint160DecodeStringLE(ctx.String("token"))
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Errorf("invalid token contract hash: %v", err), 1)
|
||||
}
|
||||
|
||||
for _, t := range wall.Extra.Tokens {
|
||||
if t.Hash.Equals(tokenHash) {
|
||||
printTokenInfo(t)
|
||||
return cli.NewExitError("token already exists", 1)
|
||||
}
|
||||
}
|
||||
|
||||
gctx, cancel := getGoContext(ctx)
|
||||
defer cancel()
|
||||
|
||||
c, err := client.New(gctx, ctx.String("rpc"), client.Options{})
|
||||
if err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
|
||||
tok, err := c.NEP5TokenInfo(tokenHash)
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Errorf("can't receive token info: %v", err), 1)
|
||||
}
|
||||
|
||||
wall.AddToken(tok)
|
||||
if err := wall.Save(); err != nil {
|
||||
return cli.NewExitError(err, 1)
|
||||
}
|
||||
printTokenInfo(tok)
|
||||
return nil
|
||||
}
|
||||
|
||||
func printTokenInfo(tok *wallet.Token) {
|
||||
fmt.Printf("Name:\t%s\n", tok.Name)
|
||||
fmt.Printf("Symbol:\t%s\n", tok.Symbol)
|
||||
fmt.Printf("Hash:\t%s\n", tok.Hash.StringLE())
|
||||
fmt.Printf("Decimals: %d\n", tok.Decimals)
|
||||
fmt.Printf("Address: %s\n", tok.Address)
|
||||
}
|
|
@ -186,6 +186,11 @@ func NewCommands() []cli.Command {
|
|||
Usage: "work with multisig address",
|
||||
Subcommands: newMultisigCommands(),
|
||||
},
|
||||
{
|
||||
Name: "nep5",
|
||||
Usage: "work with NEP5 contracts",
|
||||
Subcommands: newNEP5Commands(),
|
||||
},
|
||||
},
|
||||
}}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue