2020-03-06 13:23:41 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2020-06-01 18:46:01 +00:00
|
|
|
"encoding/json"
|
2020-03-05 16:31:35 +00:00
|
|
|
"errors"
|
2020-03-06 13:23:41 +00:00
|
|
|
"fmt"
|
2020-06-01 18:46:01 +00:00
|
|
|
"io/ioutil"
|
2020-06-25 08:40:22 +00:00
|
|
|
"strings"
|
2020-03-06 13:23:41 +00:00
|
|
|
|
2020-03-06 14:22:14 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/flags"
|
2020-06-17 21:15:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2020-03-05 16:31:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-03-06 13:23:41 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
2020-06-01 18:46:01 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/context"
|
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-07 07:24:58 +00:00
|
|
|
// validUntilBlockIncrement is the number of extra blocks to add to an exported transaction
|
|
|
|
const validUntilBlockIncrement = 50
|
|
|
|
|
2020-06-25 08:40:22 +00:00
|
|
|
var (
|
|
|
|
neoToken = wallet.NewToken(client.NeoContractHash, "NEO", "neo", 0)
|
|
|
|
gasToken = wallet.NewToken(client.GasContractHash, "GAS", "gas", 8)
|
|
|
|
)
|
|
|
|
|
2020-07-24 13:34:46 +00:00
|
|
|
var (
|
|
|
|
tokenFlag = cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Usage: "Token to use",
|
|
|
|
}
|
|
|
|
gasFlag = flags.Fixed8Flag{
|
|
|
|
Name: "gas",
|
|
|
|
Usage: "Amount of GAS to attach to a tx",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-03-06 13:23:41 +00:00
|
|
|
func newNEP5Commands() []cli.Command {
|
2020-06-17 21:15:13 +00:00
|
|
|
balanceFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
tokenFlag,
|
2020-06-17 21:15:13 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "addr",
|
|
|
|
Usage: "Address to use",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
balanceFlags = append(balanceFlags, options.RPC...)
|
|
|
|
importFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Usage: "Token contract hash in LE",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
importFlags = append(importFlags, options.RPC...)
|
|
|
|
transferFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
outFlag,
|
|
|
|
fromAddrFlag,
|
|
|
|
toAddrFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
tokenFlag,
|
|
|
|
gasFlag,
|
2020-06-17 21:15:13 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "amount",
|
|
|
|
Usage: "Amount of asset to send",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
transferFlags = append(transferFlags, options.RPC...)
|
2020-07-24 13:34:46 +00:00
|
|
|
multiTransferFlags := []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
outFlag,
|
|
|
|
fromAddrFlag,
|
|
|
|
gasFlag,
|
|
|
|
}
|
|
|
|
multiTransferFlags = append(multiTransferFlags, 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-06-25 07:34:02 +00:00
|
|
|
UsageText: "balance --wallet <path> --rpc-endpoint <node> --timeout <time> --addr <addr> [--token <hash-or-name>]",
|
2020-03-05 16:31:35 +00:00
|
|
|
Action: getNEP5Balance,
|
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",
|
|
|
|
Usage: "import NEP5 token to a wallet",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "import --wallet <path> --rpc-endpoint <node> --timeout <time> --token <hash>",
|
2020-03-06 13:23:41 +00:00
|
|
|
Action: importNEP5Token,
|
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",
|
|
|
|
Usage: "print imported NEP5 token info",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "print --wallet <path> [--token <hash-or-name>]",
|
2020-03-06 14:22:28 +00:00
|
|
|
Action: printNEP5Info,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Usage: "Token name or hash",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-13 14:15:39 +00:00
|
|
|
{
|
|
|
|
Name: "remove",
|
|
|
|
Usage: "remove NEP5 token from the wallet",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "remove --wallet <path> <hash-or-name>",
|
2020-03-13 14:15:39 +00:00
|
|
|
Action: removeNEP5Token,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
walletPathFlag,
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "token",
|
|
|
|
Usage: "Token name or hash",
|
|
|
|
},
|
|
|
|
forceFlag,
|
|
|
|
},
|
|
|
|
},
|
2020-03-06 14:22:14 +00:00
|
|
|
{
|
|
|
|
Name: "transfer",
|
|
|
|
Usage: "transfer NEP5 tokens",
|
2020-06-25 07:34:02 +00:00
|
|
|
UsageText: "transfer --wallet <path> --rpc-endpoint <node> --timeout <time> --from <addr> --to <addr> --token <hash> --amount string",
|
2020-03-06 14:22:14 +00:00
|
|
|
Action: transferNEP5,
|
2020-06-17 21:15:13 +00:00
|
|
|
Flags: transferFlags,
|
2020-03-06 14:22:14 +00:00
|
|
|
},
|
2020-07-24 13:34:46 +00:00
|
|
|
{
|
|
|
|
Name: "multitransfer",
|
2020-08-14 09:16:24 +00:00
|
|
|
Usage: "transfer NEP5 tokens to multiple recipients",
|
2020-07-24 13:34:46 +00:00
|
|
|
UsageText: `multitransfer --wallet <path> --rpc-endpoint <node> --timeout <time> --from <addr>` +
|
2020-08-04 07:35:47 +00:00
|
|
|
` <token1>:<addr1>:<amount1> [<token2>:<addr2>:<amount2> [...]]`,
|
2020-07-24 13:34:46 +00:00
|
|
|
Action: multiTransferNEP5,
|
|
|
|
Flags: multiTransferFlags,
|
|
|
|
},
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-05 16:31:35 +00:00
|
|
|
func getNEP5Balance(ctx *cli.Context) error {
|
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 {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
addr := ctx.String("addr")
|
|
|
|
addrHash, err := address.StringToUint160(addr)
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid address: %w", err), 1)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
acc := wall.GetAccount(addrHash)
|
|
|
|
if acc == nil {
|
|
|
|
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", addr), 1)
|
|
|
|
}
|
|
|
|
|
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-06-17 21:15:13 +00:00
|
|
|
return err
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var token *wallet.Token
|
|
|
|
name := ctx.String("token")
|
|
|
|
if name != "" {
|
2020-08-28 09:11:19 +00:00
|
|
|
token, err = getMatchingToken(ctx, wall, name)
|
2020-03-05 16:31:35 +00:00
|
|
|
if err != nil {
|
2020-08-28 09:11:19 +00:00
|
|
|
token, err = getMatchingTokenRPC(ctx, c, addrHash, name)
|
2020-03-05 16:31:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
balances, err := c.GetNEP5Balances(addrHash)
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range balances.Balances {
|
|
|
|
asset := balances.Balances[i].Asset
|
|
|
|
if name != "" && !token.Hash.Equals(asset) {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "TokenHash: %s\n", asset.StringLE())
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", balances.Balances[i].Amount)
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tUpdated: %d\n", balances.Balances[i].LastUpdated)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func getMatchingToken(ctx *cli.Context, w *wallet.Wallet, name string) (*wallet.Token, error) {
|
2020-06-25 08:40:22 +00:00
|
|
|
switch strings.ToLower(name) {
|
|
|
|
case "neo":
|
|
|
|
return neoToken, nil
|
|
|
|
case "gas":
|
|
|
|
return gasToken, nil
|
|
|
|
}
|
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]
|
|
|
|
}, len(w.Extra.Tokens), name)
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func getMatchingTokenRPC(ctx *cli.Context, c *client.Client, addr util.Uint160, name string) (*wallet.Token, error) {
|
2020-03-05 16:31:35 +00:00
|
|
|
bs, err := c.GetNEP5Balances(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
get := func(i int) *wallet.Token {
|
|
|
|
t, _ := c.NEP5TokenInfo(bs.Balances[i].Asset)
|
|
|
|
return t
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
return getMatchingTokenAux(ctx, get, len(bs.Balances), name)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-28 09:11:19 +00:00
|
|
|
func getMatchingTokenAux(ctx *cli.Context, get func(i int) *wallet.Token, n int, name 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)
|
2020-03-23 09:46:42 +00:00
|
|
|
if t != nil && (t.Name == name || t.Symbol == name || t.Address() == name || t.Hash.StringLE() == name) {
|
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)
|
2020-03-05 16:31:35 +00:00
|
|
|
return nil, errors.New("multiple matching tokens found")
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
token = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count == 0 {
|
|
|
|
return nil, errors.New("token was not found")
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:23:41 +00:00
|
|
|
func importNEP5Token(ctx *cli.Context) 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()
|
|
|
|
|
|
|
|
tokenHash, err := util.Uint160DecodeStringLE(ctx.String("token"))
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid token contract hash: %w", err), 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range wall.Extra.Tokens {
|
|
|
|
if t.Hash.Equals(tokenHash) {
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, t)
|
2020-03-06 13:23:41 +00:00
|
|
|
return cli.NewExitError("token already exists", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-17 21:15:13 +00:00
|
|
|
return err
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := c.NEP5TokenInfo(tokenHash)
|
|
|
|
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())
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
2020-03-06 14:22:14 +00:00
|
|
|
|
2020-03-06 14:22:28 +00:00
|
|
|
func printNEP5Info(ctx *cli.Context) 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 != "" {
|
2020-08-28 09:11:19 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, name)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, t := range wall.Extra.Tokens {
|
|
|
|
if i > 0 {
|
2020-08-28 09:11:19 +00:00
|
|
|
fmt.Fprintln(ctx.App.Writer)
|
2020-03-06 14:22:28 +00:00
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, t)
|
2020-03-06 14:22:28 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:15:39 +00:00
|
|
|
func removeNEP5Token(ctx *cli.Context) 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()
|
|
|
|
|
|
|
|
name := ctx.Args().First()
|
|
|
|
if name == "" {
|
|
|
|
return cli.NewExitError("token must be specified", 1)
|
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, name)
|
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-07-24 13:34:46 +00:00
|
|
|
func multiTransferNEP5(ctx *cli.Context) error {
|
|
|
|
wall, err := openWallet(ctx.String("wallet"))
|
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
defer wall.Close()
|
|
|
|
|
|
|
|
fromFlag := ctx.Generic("from").(*flags.Address)
|
|
|
|
from := fromFlag.Uint160()
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-08-14 09:16:24 +00:00
|
|
|
var recipients []client.TransferTarget
|
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)
|
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 {
|
2020-08-28 09:11:19 +00:00
|
|
|
token, err = getMatchingToken(ctx, wall, ss[0])
|
2020-08-04 07:35:47 +00:00
|
|
|
if err != nil {
|
2020-08-28 09:11:19 +00:00
|
|
|
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"))
|
2020-08-04 07:35:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
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-08-04 07:35:47 +00:00
|
|
|
amount, err := util.FixedNFromString(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,
|
|
|
|
Amount: amount,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:16:24 +00:00
|
|
|
return signAndSendTransfer(ctx, c, acc, recipients)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-06 14:22:14 +00:00
|
|
|
func transferNEP5(ctx *cli.Context) 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)
|
|
|
|
from := fromFlag.Uint160()
|
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-06-17 21:15:13 +00:00
|
|
|
return err
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toFlag := ctx.Generic("to").(*flags.Address)
|
|
|
|
to := toFlag.Uint160()
|
2020-08-28 09:11:19 +00:00
|
|
|
token, err := getMatchingToken(ctx, wall, ctx.String("token"))
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
2020-08-28 09:11:19 +00:00
|
|
|
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"))
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
amount, err := util.FixedNFromString(ctx.String("amount"), int(token.Decimals))
|
|
|
|
if err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("invalid amount: %w", err), 1)
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 07:35:47 +00:00
|
|
|
return signAndSendTransfer(ctx, c, acc, []client.TransferTarget{{
|
|
|
|
Token: token.Hash,
|
2020-07-24 13:34:46 +00:00
|
|
|
Address: to,
|
|
|
|
Amount: amount,
|
|
|
|
}})
|
|
|
|
}
|
|
|
|
|
2020-08-14 09:16:24 +00:00
|
|
|
func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account, recipients []client.TransferTarget) error {
|
2020-03-12 16:41:54 +00:00
|
|
|
gas := flags.Fixed8FromContext(ctx, "gas")
|
2020-03-06 14:22:14 +00:00
|
|
|
|
2020-08-14 09:16:24 +00:00
|
|
|
tx, err := c.CreateNEP5MultiTransferTx(acc, int64(gas), recipients...)
|
2020-03-16 14:48:57 +00:00
|
|
|
if err != nil {
|
2020-03-06 14:22:14 +00:00
|
|
|
return cli.NewExitError(err, 1)
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:46:01 +00:00
|
|
|
if outFile := ctx.String("out"); outFile != "" {
|
2020-07-07 07:24:58 +00:00
|
|
|
// avoid fast transaction expiration
|
|
|
|
tx.ValidUntilBlock += validUntilBlockIncrement
|
2020-06-01 18:46:01 +00:00
|
|
|
priv := acc.PrivateKey()
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
sign := priv.Sign(tx.GetSignedPart())
|
|
|
|
scCtx := context.NewParameterContext("Neo.Core.ContractTransaction", tx)
|
|
|
|
if err := scCtx.AddSignature(acc.Contract, pub, sign); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't add signature: %w", err), 1)
|
2020-06-01 18:46:01 +00:00
|
|
|
} else if data, err := json.Marshal(scCtx); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't marshal tx to JSON: %w", err), 1)
|
2020-06-01 18:46:01 +00:00
|
|
|
} else if err := ioutil.WriteFile(outFile, data, 0644); err != nil {
|
2020-08-06 16:09:57 +00:00
|
|
|
return cli.NewExitError(fmt.Errorf("can't write tx to file: %w", err), 1)
|
2020-06-01 18:46:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_ = acc.SignTx(tx)
|
2020-07-21 07:31:45 +00:00
|
|
|
res, err := c.SendRawTransaction(tx)
|
|
|
|
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, res.StringLE())
|
2020-07-21 07:31:45 +00:00
|
|
|
return nil
|
2020-06-01 18:46:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|