2020-03-06 13:23:41 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2020-03-05 16:31:35 +00:00
|
|
|
"errors"
|
2020-03-06 13:23:41 +00:00
|
|
|
"fmt"
|
2021-01-11 12:02:17 +00:00
|
|
|
"math/big"
|
2020-06-25 08:40:22 +00:00
|
|
|
"strings"
|
2020-03-06 13:23:41 +00:00
|
|
|
|
2021-04-21 10:14:55 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/cmdargs"
|
2020-03-06 14:22:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
2021-09-15 09:40:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
2020-06-17 21:15:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-10-02 13:13:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/paramcontext"
|
2020-03-05 16:31:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-12-01 08:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-03-06 13:23:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
2021-04-23 14:32:48 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
2020-03-06 13:23:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
2020-07-24 13:34:46 +00:00
|
|
|
var (
|
|
|
|
tokenFlag = cli.StringFlag{
|
|
|
|
Name: "token",
|
2020-09-28 08:28:00 +00:00
|
|
|
Usage: "Token to use (hash or name (for NEO/GAS or imported tokens))",
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
gasFlag = flags.Fixed8Flag{
|
2021-07-02 08:57:54 +00:00
|
|
|
Name: "gas, g",
|
|
|
|
Usage: "network fee to add to the transaction (prioritizing it)",
|
|
|
|
}
|
|
|
|
sysGasFlag = flags.Fixed8Flag{
|
|
|
|
Name: "sysgas, e",
|
|
|
|
Usage: "system fee to add to transaction (compensating for execution)",
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2021-04-23 15:42:59 +00:00
|
|
|
baseBalanceFlags = []cli.Flag{
|
2020-06-17 21:15:13 +00:00
|
|
|
walletPathFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
tokenFlag,
|
2021-04-16 08:58:04 +00:00
|
|
|
flags.AddressFlag{
|
2020-09-28 15:43:53 +00:00
|
|
|
Name: "address, a",
|
2020-06-17 21:15:13 +00:00
|
|
|
Usage: "Address to use",
|
|
|
|
},
|
2021-04-23 15:42:59 +00:00
|
|
|
}
|
2021-04-27 14:28:28 +00:00
|
|
|
importFlags = append([]cli.Flag{
|
2020-06-17 21:15:13 +00:00
|
|
|
walletPathFlag,
|
2021-04-16 09:29:46 +00:00
|
|
|
flags.AddressFlag{
|
2020-06-17 21:15:13 +00:00
|
|
|
Name: "token",
|
2021-04-16 09:29:46 +00:00
|
|
|
Usage: "Token contract address or hash in LE",
|
2020-06-17 21:15:13 +00:00
|
|
|
},
|
2021-04-27 14:28:28 +00:00
|
|
|
}, options.RPC...)
|
2021-04-23 16:00:45 +00:00
|
|
|
baseTransferFlags = []cli.Flag{
|
2020-06-17 21:15:13 +00:00
|
|
|
walletPathFlag,
|
|
|
|
outFlag,
|
|
|
|
fromAddrFlag,
|
|
|
|
toAddrFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
tokenFlag,
|
|
|
|
gasFlag,
|
2021-07-02 08:57:54 +00:00
|
|
|
sysGasFlag,
|
2021-09-15 09:40:30 +00:00
|
|
|
forceFlag,
|
2020-06-17 21:15:13 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "amount",
|
|
|
|
Usage: "Amount of asset to send",
|
|
|
|
},
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
2021-04-27 14:28:28 +00:00
|
|
|
multiTransferFlags = append([]cli.Flag{
|
2020-07-24 13:34:46 +00:00
|
|
|
walletPathFlag,
|
|
|
|
outFlag,
|
|
|
|
fromAddrFlag,
|
|
|
|
gasFlag,
|
2021-07-02 08:57:54 +00:00
|
|
|
sysGasFlag,
|
2021-09-15 09:40:30 +00:00
|
|
|
forceFlag,
|
2021-04-27 14:28:28 +00:00
|
|
|
}, options.RPC...)
|
|
|
|
)
|
|
|
|
|
|
|
|
func newNEP17Commands() []cli.Command {
|
2021-04-23 15:42:59 +00:00
|
|
|
balanceFlags := make([]cli.Flag, len(baseBalanceFlags))
|
|
|
|
copy(balanceFlags, baseBalanceFlags)
|
|
|
|
balanceFlags = append(balanceFlags, options.RPC...)
|
2021-04-23 16:00:45 +00:00
|
|
|
transferFlags := make([]cli.Flag, len(baseTransferFlags))
|
|
|
|
copy(transferFlags, baseTransferFlags)
|
|
|
|
transferFlags = append(transferFlags, options.RPC...)
|
2020-03-06 13:23:41 +00:00
|
|
|
return []cli.Command{
|
2020-03-05 16:31:35 +00:00
|
|
|
{
|
|
|
|
Name: "balance",
|
|
|
|
Usage: "get address balance",
|
2020-09-28 15:43:53 +00:00
|
|
|
UsageText: "balance --wallet <path> --rpc-endpoint <node> [--timeout <time>] [--address <address>] [--token <hash-or-name>]",
|
2020-11-24 08:14:25 +00:00
|
|
|
Action: getNEP17Balance,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: balanceFlags,
|
2020-03-05 16:31:35 +00:00
|
|
|
},
|
2020-03-06 13:23:41 +00:00
|
|
|
{
|
|
|
|
Name: "import",
|
2020-11-24 08:14:25 +00:00
|
|
|
Usage: "import NEP17 token to a wallet",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "import --wallet <path> --rpc-endpoint <node> --timeout <time> --token <hash>",
|
2020-11-24 08:14:25 +00:00
|
|
|
Action: importNEP17Token,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: importFlags,
|
2020-03-06 13:23:41 +00:00
|
|
|
},
|
2020-03-06 14:22:28 +00:00
|
|
|
{
|
|
|
|
Name: "info",
|
2020-11-24 08:14:25 +00:00
|
|
|
Usage: "print imported NEP17 token info",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "print --wallet <path> [--token <hash-or-name>]",
|
2020-11-24 08:14:25 +00:00
|
|
|
Action: printNEP17Info,
|
2020-03-06 14:22:28 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
2021-04-22 15:44:48 +00:00
|
|
|
tokenFlag,
|
2020-03-06 14:22:28 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-13 14:15:39 +00:00
|
|
|
{
|
|
|
|
Name: "remove",
|
2020-11-24 08:14:25 +00:00
|
|
|
Usage: "remove NEP17 token from the wallet",
|
2020-09-01 12:40:32 +00:00
|
|
|
UsageText: "remove --wallet <path> --token <hash-or-name>",
|
2020-11-24 08:14:25 +00:00
|
|
|
Action: removeNEP17Token,
|
2020-03-13 14:15:39 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
2021-04-22 15:44:48 +00:00
|
|
|
tokenFlag,
|
2020-03-13 14:15:39 +00:00
|
|
|
forceFlag,
|
|
|
|
},
|
|
|
|
},
|
2020-03-06 14:22:14 +00:00
|
|
|
{
|
|
|
|
Name: "transfer",
|
2020-11-24 08:14:25 +00:00
|
|
|
Usage: "transfer NEP17 tokens",
|
2021-04-26 13:15:56 +00:00
|
|
|
UsageText: "transfer --wallet <path> --rpc-endpoint <node> --timeout <time> --from <addr> --to <addr> --token <hash-or-name> --amount string [data] [-- <cosigner1:Scope> [<cosigner2> [...]]]",
|
2020-11-24 08:14:25 +00:00
|
|
|
Action: transferNEP17,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: transferFlags,
|
2021-04-21 09:50:04 +00:00
|
|
|
Description: `Transfers specified NEP17 token amount with optional 'data' parameter and cosigners
|
|
|
|
list attached to the transfer. See 'contract testinvokefunction' documentation
|
|
|
|
for the details about 'data' parameter and cosigners syntax. If no 'data' is
|
|
|
|
given then default nil value will be used. If no cosigners are given then the
|
|
|
|
sender with CalledByEntry scope will be used as the only signer.
|
|
|
|
`,
|
2020-03-06 14:22:14 +00:00
|
|
|
},
|
2020-07-24 13:34:46 +00:00
|
|
|
{
|
|
|
|
Name: "multitransfer",
|
2020-11-24 08:14:25 +00:00
|
|
|
Usage: "transfer NEP17 tokens to multiple recipients",
|
2020-07-24 13:34:46 +00:00
|
|
|
UsageText: `multitransfer --wallet <path> --rpc-endpoint <node> --timeout <time> --from <addr>` +
|
2021-04-21 12:01:42 +00:00
|
|
|
` <token1>:<addr1>:<amount1> [<token2>:<addr2>:<amount2> [...]] [-- <cosigner1:Scope> [<cosigner2> [...]]]`,
|
2020-11-24 08:14:25 +00:00
|
|
|
Action: multiTransferNEP17,
|
2020-07-24 13:34:46 +00:00
|
|
|
Flags: multiTransferFlags,
|
|
|
|
},
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func getNEP17Balance(ctx *cli.Context) error {
|
2020-09-28 14:24:21 +00:00
|
|
|
var accounts []*wallet.Account
|
|
|
|
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-05 16:31:35 +00:00
|
|
|
if err != nil {
|
2020-09-28 14:24:21 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("bad wallet: %w", err), 1)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
2021-04-16 08:58:04 +00:00
|
|
|
addrFlag := ctx.Generic("address").(*flags.Address)
|
|
|
|
if addrFlag.IsSet {
|
|
|
|
addrHash := addrFlag.Uint160()
|
2020-09-28 14:24:21 +00:00
|
|
|
acc := wall.GetAccount(addrHash)
|
|
|
|
if acc == nil {
|
2021-04-16 08:58:04 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", address.Uint160ToString(addrHash)), 1)
|
2020-09-28 14:24:21 +00:00
|
|
|
}
|
|
|
|
accounts = append(accounts, acc)
|
|
|
|
} else {
|
|
|
|
if len(wall.Accounts) == 0 {
|
|
|
|
return cli.NewExitError(errors.New("no accounts in the wallet"), 1)
|
|
|
|
}
|
|
|
|
accounts = wall.Accounts
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
2020-03-05 16:31:35 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2020-03-05 16:31:35 +00:00
|
|
|
if err != nil {
|
2020-11-03 11:08:59 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := ctx.String("token")
|
2020-09-28 14:24:21 +00:00
|
|
|
|
|
|
|
for k, acc := range accounts {
|
|
|
|
addrHash, err := address.StringToUint160(acc.Address)
|
2020-03-05 16:31:35 +00:00
|
|
|
if err != nil {
|
2020-09-28 14:24:21 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid account address: %w", err), 1)
|
|
|
|
}
|
2020-11-24 08:14:25 +00:00
|
|
|
balances, err := c.GetNEP17Balances(addrHash)
|
2020-09-28 14:24:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-28 14:24:21 +00:00
|
|
|
if k != 0 {
|
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "Account %s\n", acc.Address)
|
2020-03-05 16:31:35 +00:00
|
|
|
|
2020-09-28 14:24:21 +00:00
|
|
|
for i := range balances.Balances {
|
|
|
|
var tokenName, tokenSymbol string
|
2021-01-11 12:02:17 +00:00
|
|
|
tokenDecimals := 0
|
2020-09-28 14:24:21 +00:00
|
|
|
asset := balances.Balances[i].Asset
|
2021-04-23 14:32:48 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, asset.StringLE(), manifest.NEP17StandardName)
|
2020-09-28 14:24:21 +00:00
|
|
|
if err != nil {
|
2020-11-24 08:14:25 +00:00
|
|
|
token, err = c.NEP17TokenInfo(asset)
|
2020-09-28 14:24:21 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if name != "" && !(token.Name == name || token.Symbol == name || token.Address() == name || token.Hash.StringLE() == name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tokenName = token.Name
|
|
|
|
tokenSymbol = token.Symbol
|
2021-01-11 12:02:17 +00:00
|
|
|
tokenDecimals = int(token.Decimals)
|
2020-09-28 14:24:21 +00:00
|
|
|
} else {
|
|
|
|
if name != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
tokenSymbol = "UNKNOWN"
|
|
|
|
}
|
2020-12-13 18:03:25 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "%s: %s (%s)\n", tokenSymbol, tokenName, asset.StringLE())
|
2021-01-11 12:02:17 +00:00
|
|
|
amount := balances.Balances[i].Amount
|
|
|
|
if tokenDecimals != 0 {
|
|
|
|
b, ok := new(big.Int).SetString(amount, 10)
|
|
|
|
if ok {
|
|
|
|
amount = fixedn.ToString(b, tokenDecimals)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", amount)
|
2020-09-28 14:24:21 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tUpdated: %d\n", balances.Balances[i].LastUpdated)
|
2020-09-28 13:47:47 +00:00
|
|
|
}
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:32:48 +00:00
|
|
|
func getMatchingToken(ctx *cli.Context, w *wallet.Wallet, name string, standard string) (*wallet.Token, error) {
|
2020-08-28 09:11:19 +00:00
|
|
|
return getMatchingTokenAux(ctx, func(i int) *wallet.Token {
|
2020-03-05 16:31:35 +00:00
|
|
|
return w.Extra.Tokens[i]
|
2021-04-23 14:32:48 +00:00
|
|
|
}, len(w.Extra.Tokens), name, standard)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 16:00:45 +00:00
|
|
|
func getMatchingTokenRPC(ctx *cli.Context, c *client.Client, addr util.Uint160, name string, standard string) (*wallet.Token, error) {
|
|
|
|
switch standard {
|
|
|
|
case manifest.NEP17StandardName:
|
|
|
|
bs, err := c.GetNEP17Balances(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
get := func(i int) *wallet.Token {
|
|
|
|
t, _ := c.NEP17TokenInfo(bs.Balances[i].Asset)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
|
|
|
case manifest.NEP11StandardName:
|
|
|
|
tokenHash, err := flags.ParseAddress(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("valid token adress or hash in LE should be specified for %s RPC-node request: %s", standard, err.Error())
|
|
|
|
}
|
|
|
|
get := func(i int) *wallet.Token {
|
|
|
|
t, _ := c.NEP11TokenInfo(tokenHash)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return getMatchingTokenAux(ctx, get, 1, name, standard)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported %s token", standard)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 14:32:48 +00:00
|
|
|
func getMatchingTokenAux(ctx *cli.Context, get func(i int) *wallet.Token, n int, name string, standard string) (*wallet.Token, error) {
|
2020-03-05 16:31:35 +00:00
|
|
|
var token *wallet.Token
|
|
|
|
var count int
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
t := get(i)
|
2021-04-23 14:32:48 +00:00
|
|
|
if t != nil && (t.Hash.StringLE() == name || t.Address() == name || t.Symbol == name || t.Name == name) && t.Standard == standard {
|
2020-03-05 16:31:35 +00:00
|
|
|
if count == 1 {
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, token)
|
|
|
|
printTokenInfo(ctx, t)
|
2021-04-22 15:21:18 +00:00
|
|
|
return nil, fmt.Errorf("multiple matching %s tokens found", standard)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
count++
|
|
|
|
token = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count == 0 {
|
2021-04-22 15:21:18 +00:00
|
|
|
return nil, fmt.Errorf("%s token was not found", standard)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func importNEP17Token(ctx *cli.Context) error {
|
2021-04-22 15:21:18 +00:00
|
|
|
return importNEPToken(ctx, manifest.NEP17StandardName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func importNEPToken(ctx *cli.Context, standard string) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-06 13:23:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
2021-04-16 09:29:46 +00:00
|
|
|
tokenHashFlag := ctx.Generic("token").(*flags.Address)
|
|
|
|
if !tokenHashFlag.IsSet {
|
|
|
|
return cli.NewExitError("token contract hash was not set", 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
2021-04-16 09:29:46 +00:00
|
|
|
tokenHash := tokenHashFlag.Uint160()
|
2020-03-06 13:23:41 +00:00
|
|
|
|
|
|
|
for _, t := range wall.Extra.Tokens {
|
2021-04-22 15:21:18 +00:00
|
|
|
if t.Hash.Equals(tokenHash) && t.Standard == standard {
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, t)
|
2021-04-22 15:21:18 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("%s token already exists", standard), 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
2020-03-06 13:23:41 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2020-03-06 13:23:41 +00:00
|
|
|
if err != nil {
|
2020-11-03 11:08:59 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 15:21:18 +00:00
|
|
|
var tok *wallet.Token
|
|
|
|
switch standard {
|
|
|
|
case manifest.NEP17StandardName:
|
|
|
|
tok, err = c.NEP17TokenInfo(tokenHash)
|
|
|
|
case manifest.NEP11StandardName:
|
|
|
|
tok, err = c.NEP11TokenInfo(tokenHash)
|
|
|
|
default:
|
|
|
|
return cli.NewExitError(fmt.Sprintf("unsupported token standard: %s", standard), 1)
|
|
|
|
}
|
2020-03-06 13:23:41 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't receive token info: %w", err), 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wall.AddToken(tok)
|
|
|
|
if err := wall.Save(); err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, tok)
|
2020-03-06 13:23:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func printTokenInfo(ctx *cli.Context, tok *wallet.Token) {
|
|
|
|
w := ctx.App.Writer
|
|
|
|
fmt.Fprintf(w, "Name:\t%s\n", tok.Name)
|
|
|
|
fmt.Fprintf(w, "Symbol:\t%s\n", tok.Symbol)
|
|
|
|
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())
|
2021-04-23 14:32:48 +00:00
|
|
|
fmt.Fprintf(w, "Standard:\t%s\n", tok.Standard)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
2020-03-06 14:22:14 +00:00
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func printNEP17Info(ctx *cli.Context) error {
|
2021-04-23 10:37:59 +00:00
|
|
|
return printNEPInfo(ctx, manifest.NEP17StandardName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func printNEPInfo(ctx *cli.Context, standard string) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-06 14:22:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
if name := ctx.String("token"); name != "" {
|
2021-04-23 10:37:59 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, name, standard)
|
2020-03-06 14:22:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, token)
|
2020-03-06 14:22:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-23 10:37:59 +00:00
|
|
|
var count int
|
|
|
|
for _, t := range wall.Extra.Tokens {
|
|
|
|
if count > 0 {
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
2020-03-06 14:22:28 +00:00
|
|
|
}
|
2021-04-23 10:37:59 +00:00
|
|
|
if t.Standard == standard {
|
|
|
|
printTokenInfo(ctx, t)
|
|
|
|
count++
|
|
|
|
}
|
2020-03-06 14:22:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func removeNEP17Token(ctx *cli.Context) error {
|
2021-04-23 15:08:47 +00:00
|
|
|
return removeNEPToken(ctx, manifest.NEP17StandardName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeNEPToken(ctx *cli.Context, standard string) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-13 14:15:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
2021-04-23 15:08:47 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, ctx.String("token"), standard)
|
2020-03-13 14:15:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
if !ctx.Bool("force") {
|
2020-08-28 09:11:19 +00:00
|
|
|
if ok := askForConsent(ctx.App.Writer); !ok {
|
2020-03-13 14:15:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := wall.RemoveToken(token.Hash); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't remove token: %w", err), 1)
|
2020-03-13 14:15:39 +00:00
|
|
|
} else if err := wall.Save(); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("error while saving wallet: %w", err), 1)
|
2020-03-13 14:15:39 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func multiTransferNEP17(ctx *cli.Context) error {
|
2020-07-24 13:34:46 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
fromFlag := ctx.Generic("from").(*flags.Address)
|
2021-01-11 12:58:37 +00:00
|
|
|
from, err := getDefaultAddress(fromFlag, wall)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
acc, err := getDecryptedAccount(ctx, wall, from)
|
2020-08-07 09:34:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
|
|
|
if err != nil {
|
2020-11-03 11:08:59 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.NArg() == 0 {
|
2020-08-14 09:16:24 +00:00
|
|
|
return cli.NewExitError("empty recipients list", 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2021-04-21 12:01:42 +00:00
|
|
|
var (
|
|
|
|
recipients []client.TransferTarget
|
|
|
|
cosignersOffset = ctx.NArg()
|
|
|
|
)
|
2020-08-04 07:35:47 +00:00
|
|
|
cache := make(map[string]*wallet.Token)
|
2020-07-24 13:34:46 +00:00
|
|
|
for i := 0; i < ctx.NArg(); i++ {
|
|
|
|
arg := ctx.Args().Get(i)
|
2021-04-21 12:01:42 +00:00
|
|
|
if arg == cmdargs.CosignersSeparator {
|
|
|
|
cosignersOffset = i + 1
|
|
|
|
break
|
|
|
|
}
|
2020-08-04 07:35:47 +00:00
|
|
|
ss := strings.SplitN(arg, ":", 3)
|
|
|
|
if len(ss) != 3 {
|
|
|
|
return cli.NewExitError("send format must be '<token>:<addr>:<amount>", 1)
|
|
|
|
}
|
|
|
|
token, ok := cache[ss[0]]
|
|
|
|
if !ok {
|
2021-04-23 14:32:48 +00:00
|
|
|
token, err = getMatchingToken(ctx, wall, ss[0], manifest.NEP17StandardName)
|
2020-08-04 07:35:47 +00:00
|
|
|
if err != nil {
|
2021-04-23 16:00:45 +00:00
|
|
|
token, err = getMatchingTokenRPC(ctx, c, from, ss[0], manifest.NEP17StandardName)
|
2020-08-04 07:35:47 +00:00
|
|
|
if err != nil {
|
2021-06-17 08:29:16 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't fetch matching token from RPC-node: %w", err), 1)
|
2020-08-04 07:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2020-08-04 07:35:47 +00:00
|
|
|
cache[ss[0]] = token
|
|
|
|
addr, err := address.StringToUint160(ss[1])
|
2020-07-24 13:34:46 +00:00
|
|
|
if err != nil {
|
2020-08-04 07:35:47 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid address: '%s'", ss[1]), 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2020-11-30 09:06:36 +00:00
|
|
|
amount, err := fixedn.FromString(ss[2], int(token.Decimals))
|
2020-07-24 13:34:46 +00:00
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2020-08-14 09:16:24 +00:00
|
|
|
recipients = append(recipients, client.TransferTarget{
|
2020-08-04 07:35:47 +00:00
|
|
|
Token: token.Hash,
|
2020-07-24 13:34:46 +00:00
|
|
|
Address: addr,
|
2020-11-30 09:06:36 +00:00
|
|
|
Amount: amount.Int64(),
|
2021-04-16 10:54:23 +00:00
|
|
|
Data: nil,
|
2020-07-24 13:34:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-21 12:01:42 +00:00
|
|
|
cosigners, extErr := cmdargs.GetSignersFromContext(ctx, cosignersOffset)
|
|
|
|
if extErr != nil {
|
|
|
|
return extErr
|
|
|
|
}
|
|
|
|
cosignersAccounts, err := cmdargs.GetSignersAccounts(wall, cosigners)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to create NEP17 multitransfer transaction: %w", err), 1)
|
|
|
|
}
|
|
|
|
|
2021-04-23 16:00:45 +00:00
|
|
|
return signAndSendNEP17Transfer(ctx, c, acc, recipients, cosignersAccounts)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func transferNEP17(ctx *cli.Context) error {
|
2021-04-23 16:00:45 +00:00
|
|
|
return transferNEP(ctx, manifest.NEP17StandardName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func transferNEP(ctx *cli.Context, standard string) error {
|
2020-06-25 15:46:24 +00:00
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
fromFlag := ctx.Generic("from").(*flags.Address)
|
2021-01-11 12:58:37 +00:00
|
|
|
from, err := getDefaultAddress(fromFlag, wall)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
acc, err := getDecryptedAccount(ctx, wall, from)
|
2020-08-07 09:34:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 21:15:13 +00:00
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
2020-03-06 14:22:14 +00:00
|
|
|
defer cancel()
|
2020-06-17 21:15:13 +00:00
|
|
|
|
|
|
|
c, err := options.GetRPCClient(gctx, ctx)
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
2020-11-03 11:08:59 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toFlag := ctx.Generic("to").(*flags.Address)
|
|
|
|
to := toFlag.Uint160()
|
2021-04-23 16:00:45 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, ctx.String("token"), standard)
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
2021-04-23 16:00:45 +00:00
|
|
|
token, err = getMatchingTokenRPC(ctx, c, from, ctx.String("token"), standard)
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
2021-06-17 08:29:16 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't fetch matching token from RPC-node: %w", err), 1)
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-21 09:50:04 +00:00
|
|
|
cosignersOffset, data, extErr := cmdargs.GetDataFromContext(ctx)
|
2021-04-16 10:45:54 +00:00
|
|
|
if extErr != nil {
|
|
|
|
return extErr
|
|
|
|
}
|
|
|
|
|
2021-04-21 09:50:04 +00:00
|
|
|
cosigners, extErr := cmdargs.GetSignersFromContext(ctx, cosignersOffset)
|
|
|
|
if extErr != nil {
|
|
|
|
return extErr
|
|
|
|
}
|
|
|
|
cosignersAccounts, err := cmdargs.GetSignersAccounts(wall, cosigners)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("failed to create NEP17 transfer transaction: %w", err), 1)
|
|
|
|
}
|
|
|
|
|
2021-04-23 16:00:45 +00:00
|
|
|
amountArg := ctx.String("amount")
|
|
|
|
switch standard {
|
|
|
|
case manifest.NEP17StandardName:
|
|
|
|
amount, err := fixedn.FromString(amountArg, int(token.Decimals))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1)
|
|
|
|
}
|
|
|
|
return signAndSendNEP17Transfer(ctx, c, acc, []client.TransferTarget{{
|
|
|
|
Token: token.Hash,
|
|
|
|
Address: to,
|
|
|
|
Amount: amount.Int64(),
|
|
|
|
Data: data,
|
|
|
|
}}, cosignersAccounts)
|
|
|
|
case manifest.NEP11StandardName:
|
|
|
|
tokenID := ctx.String("id")
|
|
|
|
if tokenID == "" {
|
|
|
|
return cli.NewExitError(errors.New("token ID should be specified"), 1)
|
|
|
|
}
|
|
|
|
if amountArg == "" {
|
2021-05-05 10:22:26 +00:00
|
|
|
return signAndSendNEP11Transfer(ctx, c, acc, token.Hash, to, tokenID, nil, data, cosignersAccounts)
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
|
|
|
amount, err := fixedn.FromString(amountArg, int(token.Decimals))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1)
|
|
|
|
}
|
2021-05-05 10:22:26 +00:00
|
|
|
return signAndSendNEP11Transfer(ctx, c, acc, token.Hash, to, tokenID, amount, data, cosignersAccounts)
|
2021-04-23 16:00:45 +00:00
|
|
|
default:
|
|
|
|
return cli.NewExitError(fmt.Errorf("unsupported token standard %s", standard), 1)
|
|
|
|
}
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-23 16:00:45 +00:00
|
|
|
func signAndSendNEP17Transfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recipients []client.TransferTarget, cosigners []client.SignerAccount) error {
|
2020-03-12 16:41:54 +00:00
|
|
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
2021-07-02 08:57:54 +00:00
|
|
|
sysgas := flags.Fixed8FromContext(ctx, "sysgas")
|
2020-03-06 14:22:14 +00:00
|
|
|
|
2021-04-21 09:50:04 +00:00
|
|
|
tx, err := c.CreateNEP17MultiTransferTx(acc, int64(gas), recipients, cosigners)
|
2020-03-16 14:48:57 +00:00
|
|
|
if err != nil {
|
2020-03-06 14:22:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
2021-07-02 08:57:54 +00:00
|
|
|
tx.SystemFee += int64(sysgas)
|
2020-03-06 14:22:14 +00:00
|
|
|
|
2020-06-01 18:46:01 +00:00
|
|
|
if outFile := ctx.String("out"); outFile != "" {
|
2021-03-25 16:18:01 +00:00
|
|
|
if err := paramcontext.InitAndSave(c.GetNetwork(), tx, acc, outFile); err != nil {
|
2020-10-02 13:13:17 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
2020-06-01 18:46:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-15 09:40:30 +00:00
|
|
|
if !ctx.Bool("force") {
|
|
|
|
err := input.ConfirmTx(ctx.App.Writer, tx)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 09:50:04 +00:00
|
|
|
_, err := c.SignAndPushTx(tx, acc, cosigners)
|
2020-07-21 07:31:45 +00:00
|
|
|
if err != nil {
|
2020-06-01 18:46:01 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer, tx.Hash().StringLE())
|
2020-03-06 14:22:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-11 12:58:37 +00:00
|
|
|
|
|
|
|
func getDefaultAddress(fromFlag *flags.Address, w *wallet.Wallet) (util.Uint160, error) {
|
|
|
|
if fromFlag.IsSet {
|
|
|
|
return fromFlag.Uint160(), nil
|
|
|
|
}
|
|
|
|
addr := w.GetChangeAddress()
|
|
|
|
if addr.Equals(util.Uint160{}) {
|
|
|
|
return util.Uint160{}, errors.New("can't get default address")
|
|
|
|
}
|
|
|
|
return addr, nil
|
|
|
|
}
|