2020-03-06 13:23:41 +00:00
|
|
|
package wallet
|
|
|
|
|
|
|
|
import (
|
2022-02-04 16:18:35 +00:00
|
|
|
"encoding/hex"
|
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"
|
2020-06-17 21:15:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/options"
|
2022-10-06 19:59:47 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/cli/txctx"
|
2022-08-29 15:43:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2022-08-19 17:53:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
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"
|
2022-07-22 16:09:29 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/neorpc/result"
|
2022-07-21 19:39:53 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient"
|
2022-08-19 17:53:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
|
2022-08-15 13:42:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
|
2022-08-17 18:55:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
|
2022-08-19 18:35:00 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep11"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
|
2022-08-26 19:56:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpcclient/neptoken"
|
2022-08-19 17:53:16 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
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"
|
2024-07-09 18:24:39 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-03-06 13:23:41 +00:00
|
|
|
)
|
|
|
|
|
2023-10-09 19:34:33 +00:00
|
|
|
// transferTarget represents target address, token amount and data for transfer.
|
|
|
|
type transferTarget struct {
|
|
|
|
Token util.Uint160
|
|
|
|
Address util.Uint160
|
|
|
|
Amount int64
|
|
|
|
Data any
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:34:46 +00:00
|
|
|
var (
|
2024-07-09 18:24:39 +00:00
|
|
|
tokenFlag = &cli.StringFlag{
|
2020-07-24 13:34:46 +00:00
|
|
|
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
|
|
|
}
|
2021-04-23 15:42:59 +00:00
|
|
|
baseBalanceFlags = []cli.Flag{
|
2020-06-17 21:15:13 +00:00
|
|
|
walletPathFlag,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
tokenFlag,
|
2024-07-09 18:24:39 +00:00
|
|
|
&flags.AddressFlag{
|
|
|
|
Name: "address",
|
|
|
|
Aliases: []string{"a"},
|
|
|
|
Usage: "Address to use",
|
2020-06-17 21:15:13 +00:00
|
|
|
},
|
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,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2024-07-09 18:24:39 +00:00
|
|
|
&flags.AddressFlag{
|
2024-07-04 22:34:40 +00:00
|
|
|
Name: "token",
|
|
|
|
Usage: "Token contract address or hash in LE",
|
|
|
|
Required: true,
|
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,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2022-10-06 19:59:47 +00:00
|
|
|
txctx.OutFlag,
|
2020-06-17 21:15:13 +00:00
|
|
|
fromAddrFlag,
|
|
|
|
toAddrFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
tokenFlag,
|
2022-10-06 19:59:47 +00:00
|
|
|
txctx.GasFlag,
|
|
|
|
txctx.SysGasFlag,
|
|
|
|
txctx.ForceFlag,
|
2023-12-28 11:58:38 +00:00
|
|
|
txctx.AwaitFlag,
|
2024-07-09 18:24:39 +00:00
|
|
|
&cli.StringFlag{
|
2020-06-17 21:15:13 +00:00
|
|
|
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,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2022-10-06 19:59:47 +00:00
|
|
|
txctx.OutFlag,
|
2020-07-24 13:34:46 +00:00
|
|
|
fromAddrFlag,
|
2022-10-06 19:59:47 +00:00
|
|
|
txctx.GasFlag,
|
|
|
|
txctx.SysGasFlag,
|
|
|
|
txctx.ForceFlag,
|
2023-12-28 11:58:38 +00:00
|
|
|
txctx.AwaitFlag,
|
2021-04-27 14:28:28 +00:00
|
|
|
}, options.RPC...)
|
|
|
|
)
|
|
|
|
|
2024-07-09 18:24:39 +00:00
|
|
|
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...)
|
2024-07-09 18:24:39 +00:00
|
|
|
return []*cli.Command{
|
2020-03-05 16:31:35 +00:00
|
|
|
{
|
|
|
|
Name: "balance",
|
2024-07-03 12:35:18 +00:00
|
|
|
Usage: "Get address balance",
|
2022-08-05 15:27:24 +00:00
|
|
|
UsageText: "balance -w wallet [--wallet-config path] --rpc-endpoint <node> [--timeout <time>] [--address <address>] [--token <hash-or-name>]",
|
2022-08-29 15:43:25 +00:00
|
|
|
Description: `Prints NEP-17 balances for address and tokens specified. By default (no
|
|
|
|
address or token parameter) all tokens for all accounts in the specified wallet
|
|
|
|
are listed. A single account can be chosen with the address option and/or a
|
|
|
|
single token can be selected with the token option. Tokens can be specified
|
|
|
|
by hash, address, name or symbol. Hashes and addresses always work (as long
|
|
|
|
as they belong to a correct NEP-17 contract), while names or symbols (if
|
|
|
|
they're not NEO or GAS names/symbols) are matched against the token data
|
|
|
|
stored in the wallet (see import command) or balance data returned from the
|
|
|
|
server. If the token is not specified directly (with hash/address) and is
|
|
|
|
not found in the wallet then depending on the balances data from the server
|
|
|
|
this command can print no data at all or print multiple tokens for one
|
|
|
|
account (if they use the same names/symbols).
|
|
|
|
`,
|
|
|
|
Action: getNEP17Balance,
|
|
|
|
Flags: balanceFlags,
|
2020-03-05 16:31:35 +00:00
|
|
|
},
|
2020-03-06 13:23:41 +00:00
|
|
|
{
|
|
|
|
Name: "import",
|
2024-07-03 12:35:18 +00:00
|
|
|
Usage: "Import NEP-17 token to a wallet",
|
2024-07-04 22:34:40 +00:00
|
|
|
UsageText: "import -w wallet [--wallet-config 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",
|
2024-07-03 12:35:18 +00:00
|
|
|
Usage: "Print imported NEP-17 token info",
|
2022-08-05 15:27:24 +00:00
|
|
|
UsageText: "print -w wallet [--wallet-config 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,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
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",
|
2024-07-03 12:35:18 +00:00
|
|
|
Usage: "Remove NEP-17 token from the wallet",
|
2022-08-05 15:27:24 +00:00
|
|
|
UsageText: "remove -w wallet [--wallet-config 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,
|
2022-06-23 13:50:21 +00:00
|
|
|
walletConfigFlag,
|
2021-04-22 15:44:48 +00:00
|
|
|
tokenFlag,
|
2022-10-06 19:59:47 +00:00
|
|
|
txctx.ForceFlag,
|
2020-03-13 14:15:39 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-06 14:22:14 +00:00
|
|
|
{
|
|
|
|
Name: "transfer",
|
2024-07-03 12:35:18 +00:00
|
|
|
Usage: "Transfer NEP-17 tokens",
|
2024-07-04 22:34:40 +00:00
|
|
|
UsageText: "transfer -w wallet [--wallet-config path] [--await] --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-11-18 13:37:42 +00:00
|
|
|
Description: `Transfers specified NEP-17 token amount with optional 'data' parameter and cosigners
|
2021-04-21 09:50:04 +00:00
|
|
|
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
|
2023-12-28 11:58:38 +00:00
|
|
|
sender with CalledByEntry scope will be used as the only signer. When --await
|
|
|
|
flag is used, the command waits for the transaction to be included in a block
|
|
|
|
before exiting.
|
2021-04-21 09:50:04 +00:00
|
|
|
`,
|
2020-03-06 14:22:14 +00:00
|
|
|
},
|
2020-07-24 13:34:46 +00:00
|
|
|
{
|
|
|
|
Name: "multitransfer",
|
2024-07-03 12:35:18 +00:00
|
|
|
Usage: "Transfer NEP-17 tokens to multiple recipients",
|
2023-12-28 11:58:38 +00:00
|
|
|
UsageText: `multitransfer -w wallet [--wallet-config path] [--await] --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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 19:39:13 +00:00
|
|
|
func tokenMatch(curToken *wallet.Token, expToken *wallet.Token, name string) bool {
|
|
|
|
return name == "" || // No specification at all, everything matches.
|
|
|
|
(expToken != nil && expToken.Hash == curToken.Hash) || // Exact token specification, matches perfectly.
|
|
|
|
(expToken == nil && name != "" && (curToken.Name == name || curToken.Symbol == name)) // Loose (named non-native) token specification, best-effort.
|
|
|
|
}
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func getNEP17Balance(ctx *cli.Context) error {
|
2022-08-29 19:39:13 +00:00
|
|
|
return getNEPBalance(ctx, manifest.NEP17StandardName, func(ctx *cli.Context, c *rpcclient.Client, addrHash util.Uint160, name string, token *wallet.Token, _ string) error {
|
|
|
|
balances, err := c.GetNEP17Balances(addrHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tokenFound bool
|
|
|
|
for i := range balances.Balances {
|
|
|
|
curToken := tokenFromNEP17Balance(&balances.Balances[i])
|
|
|
|
if tokenMatch(curToken, token, name) {
|
|
|
|
printAssetBalance(ctx, balances.Balances[i])
|
|
|
|
tokenFound = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if name == "" || tokenFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if token != nil {
|
|
|
|
// We have an exact token, but there is no balance data for it -> print 0.
|
|
|
|
printAssetBalance(ctx, result.NEP17Balance{
|
|
|
|
Asset: token.Hash,
|
|
|
|
Amount: "0",
|
|
|
|
Decimals: int(token.Decimals),
|
|
|
|
LastUpdated: 0,
|
|
|
|
Name: token.Name,
|
|
|
|
Symbol: token.Symbol,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// We have no data for this token at all, maybe it's not even correct -> complain.
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "Can't find data for %q token\n", name)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNEPBalance(ctx *cli.Context, standard string, accHandler func(*cli.Context, *rpcclient.Client, util.Uint160, string, *wallet.Token, string) error) error {
|
2020-09-28 14:24:21 +00:00
|
|
|
var accounts []*wallet.Account
|
|
|
|
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-23 13:50:21 +00:00
|
|
|
wall, _, err := readWallet(ctx)
|
2020-03-05 16:31:35 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("bad wallet: %w", err), 1)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-03-05 16:31:35 +00:00
|
|
|
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(errors.New("no accounts in the wallet"), 1)
|
2020-09-28 14:24:21 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name := ctx.String("token")
|
2022-08-29 15:43:25 +00:00
|
|
|
var token *wallet.Token
|
|
|
|
|
|
|
|
if name != "" {
|
|
|
|
// Token was explicitly specified, let's try finding it, search in the wallet first.
|
2022-08-29 19:39:13 +00:00
|
|
|
token, err = getMatchingToken(ctx, wall, name, standard)
|
2022-08-29 15:43:25 +00:00
|
|
|
if err != nil {
|
|
|
|
var h util.Uint160
|
|
|
|
|
|
|
|
// Well-known hardcoded names/symbols.
|
2022-08-29 19:39:13 +00:00
|
|
|
if standard == manifest.NEP17StandardName && (name == nativenames.Neo || name == "NEO") {
|
2022-08-29 15:43:25 +00:00
|
|
|
h = neo.Hash
|
2022-08-29 19:39:13 +00:00
|
|
|
} else if standard == manifest.NEP17StandardName && (name == nativenames.Gas || name == "GAS") {
|
2022-08-29 15:43:25 +00:00
|
|
|
h = gas.Hash
|
|
|
|
} else {
|
|
|
|
// The last resort, maybe it's a direct hash or address.
|
|
|
|
h, _ = flags.ParseAddress(name)
|
|
|
|
}
|
|
|
|
// If the hash is not found then it's some kind of named token, there is
|
|
|
|
// no way for us to find it, but it's not an error, maybe we'll find it
|
|
|
|
// in balances.
|
|
|
|
if !h.Equals(util.Uint160{}) {
|
|
|
|
// But if we have an exact hash, it must be correct.
|
2022-08-29 19:39:13 +00:00
|
|
|
token, err = getTokenWithStandard(c, h, standard)
|
2022-08-29 15:43:25 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("%q is not a valid %s token: %w", name, standard, err), 1)
|
2022-08-29 15:43:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-29 19:39:13 +00:00
|
|
|
tokenID := ctx.String("id")
|
|
|
|
if standard == manifest.NEP11StandardName {
|
|
|
|
if len(tokenID) > 0 {
|
|
|
|
_, err = hex.DecodeString(tokenID)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("invalid token ID: %w", err), 1)
|
2022-08-29 19:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-28 14:24:21 +00:00
|
|
|
for k, acc := range accounts {
|
|
|
|
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
|
|
|
|
2022-09-01 16:04:47 +00:00
|
|
|
err = accHandler(ctx, c, acc.ScriptHash(), name, token, tokenID)
|
2022-08-29 19:39:13 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-09-28 13:47:47 +00:00
|
|
|
}
|
2020-03-05 16:31:35 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 19:39:13 +00:00
|
|
|
func decimalAmount(amount string, decimals int) string {
|
|
|
|
if decimals != 0 {
|
2021-12-27 13:29:35 +00:00
|
|
|
b, ok := new(big.Int).SetString(amount, 10)
|
|
|
|
if ok {
|
2022-08-29 19:39:13 +00:00
|
|
|
amount = fixedn.ToString(b, decimals)
|
2021-12-27 13:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-29 19:39:13 +00:00
|
|
|
return amount
|
|
|
|
}
|
|
|
|
|
|
|
|
func printAssetBalance(ctx *cli.Context, balance result.NEP17Balance) {
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "%s: %s (%s)\n", balance.Symbol, balance.Name, balance.Asset.StringLE())
|
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tAmount : %s\n", decimalAmount(balance.Amount, balance.Decimals))
|
2021-12-27 13:29:35 +00:00
|
|
|
fmt.Fprintf(ctx.App.Writer, "\tUpdated: %d\n", balance.LastUpdated)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-29 12:48:48 +00:00
|
|
|
func tokenFromNEP17Balance(bal *result.NEP17Balance) *wallet.Token {
|
|
|
|
return wallet.NewToken(bal.Asset, bal.Name, bal.Symbol, int64(bal.Decimals), manifest.NEP17StandardName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func tokenFromNEP11Balance(bal *result.NEP11AssetBalance) *wallet.Token {
|
|
|
|
return wallet.NewToken(bal.Asset, bal.Name, bal.Symbol, int64(bal.Decimals), manifest.NEP11StandardName)
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:39:53 +00:00
|
|
|
func getMatchingTokenRPC(ctx *cli.Context, c *rpcclient.Client, addr util.Uint160, name string, standard string) (*wallet.Token, error) {
|
2021-04-23 16:00:45 +00:00
|
|
|
switch standard {
|
|
|
|
case manifest.NEP17StandardName:
|
|
|
|
bs, err := c.GetNEP17Balances(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
get := func(i int) *wallet.Token {
|
2022-08-29 12:48:48 +00:00
|
|
|
return tokenFromNEP17Balance(&bs.Balances[i])
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
|
|
|
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
|
|
|
case manifest.NEP11StandardName:
|
2022-08-26 20:12:22 +00:00
|
|
|
bs, err := c.GetNEP11Balances(addr)
|
2021-04-23 16:00:45 +00:00
|
|
|
if err != nil {
|
2022-08-26 20:12:22 +00:00
|
|
|
return nil, err
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
|
|
|
get := func(i int) *wallet.Token {
|
2022-08-29 12:48:48 +00:00
|
|
|
return tokenFromNEP11Balance(&bs.Balances[i])
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
2022-08-26 20:12:22 +00:00
|
|
|
return getMatchingTokenAux(ctx, get, len(bs.Balances), name, standard)
|
2021-04-23 16:00:45 +00:00
|
|
|
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 {
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-04 10:27:45 +00:00
|
|
|
wall, _, err := openWallet(ctx, true)
|
2020-03-06 13:23:41 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-03-06 13:23:41 +00:00
|
|
|
|
2021-04-16 09:29:46 +00:00
|
|
|
tokenHashFlag := ctx.Generic("token").(*flags.Address)
|
|
|
|
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)
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 19:56:18 +00:00
|
|
|
tok, err := getTokenWithStandard(c, tokenHash, standard)
|
2020-03-06 13:23:41 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-06 13:23:41 +00:00
|
|
|
}
|
2020-08-28 09:11:19 +00:00
|
|
|
printTokenInfo(ctx, tok)
|
2020-03-06 13:23:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-26 19:56:18 +00:00
|
|
|
func getTokenWithStandard(c *rpcclient.Client, hash util.Uint160, std string) (*wallet.Token, error) {
|
|
|
|
token, err := neptoken.Info(c, hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if token.Standard != std {
|
|
|
|
return nil, fmt.Errorf("%s is not a %s token", hash.StringLE(), std)
|
|
|
|
}
|
|
|
|
return token, err
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-23 13:50:21 +00:00
|
|
|
wall, _, err := readWallet(ctx)
|
2020-03-06 14:22:28 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-06 14:22:28 +00:00
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-03-06 14:22:28 +00:00
|
|
|
|
|
|
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-06 14:22:28 +00:00
|
|
|
}
|
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 {
|
2022-08-05 10:32:37 +00:00
|
|
|
if err := cmdargs.EnsureNone(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-04 10:27:45 +00:00
|
|
|
wall, _, err := openWallet(ctx, true)
|
2020-03-13 14:15:39 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-13 14:15:39 +00:00
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-03-13 14:15:39 +00:00
|
|
|
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-13 14:15:39 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("can't remove token: %w", err), 1)
|
2020-03-13 14:15:39 +00:00
|
|
|
} else if err := wall.Save(); err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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 {
|
2022-06-23 13:50:21 +00:00
|
|
|
wall, pass, err := readWallet(ctx)
|
2020-07-24 13:34:46 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-07-24 13:34:46 +00:00
|
|
|
|
|
|
|
fromFlag := ctx.Generic("from").(*flags.Address)
|
2021-01-11 12:58:37 +00:00
|
|
|
from, err := getDefaultAddress(fromFlag, wall)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-01-11 12:58:37 +00:00
|
|
|
}
|
2023-12-04 14:02:44 +00:00
|
|
|
acc, err := options.GetUnlockedAccount(wall, from, pass)
|
2020-08-07 09:34:54 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gctx, cancel := options.GetTimeoutContext(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if ctx.NArg() == 0 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit("empty recipients list", 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2021-04-21 12:01:42 +00:00
|
|
|
var (
|
2023-10-09 19:34:33 +00:00
|
|
|
recipients []transferTarget
|
2023-12-07 07:37:01 +00:00
|
|
|
cosignersSepPos = ctx.NArg() // `--` position.
|
2021-04-21 12:01:42 +00:00
|
|
|
)
|
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 {
|
2023-12-07 07:37:01 +00:00
|
|
|
cosignersSepPos = i
|
2021-04-21 12:01:42 +00:00
|
|
|
break
|
|
|
|
}
|
2023-12-07 07:35:37 +00:00
|
|
|
}
|
2023-12-07 07:37:01 +00:00
|
|
|
cosigners, extErr := cmdargs.GetSignersFromContext(ctx, cosignersSepPos+1)
|
2023-12-07 07:35:37 +00:00
|
|
|
if extErr != nil {
|
|
|
|
return extErr
|
|
|
|
}
|
|
|
|
signersAccounts, err := cmdargs.GetSignersAccounts(acc, wall, cosigners, transaction.CalledByEntry)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("invalid signers: %w", err), 1)
|
2023-12-07 07:35:37 +00:00
|
|
|
}
|
|
|
|
c, act, exitErr := options.GetRPCWithActor(gctx, ctx, signersAccounts)
|
|
|
|
if exitErr != nil {
|
|
|
|
return exitErr
|
|
|
|
}
|
|
|
|
|
|
|
|
cache := make(map[string]*wallet.Token)
|
2023-12-07 07:37:01 +00:00
|
|
|
for i := 0; i < cosignersSepPos; i++ {
|
2023-12-07 07:35:37 +00:00
|
|
|
arg := ctx.Args().Get(i)
|
2020-08-04 07:35:47 +00:00
|
|
|
ss := strings.SplitN(arg, ":", 3)
|
|
|
|
if len(ss) != 3 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit("send format must be '<token>:<addr>:<amount>", 1)
|
2020-08-04 07:35:47 +00:00
|
|
|
}
|
|
|
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("invalid amount: %w", err), 1)
|
2020-07-24 13:34:46 +00:00
|
|
|
}
|
2023-10-09 19:34:33 +00:00
|
|
|
recipients = append(recipients, 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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-19 18:35:00 +00:00
|
|
|
tx, err := makeMultiTransferNEP17(act, recipients)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("can't make transaction: %w", err), 1)
|
2022-08-19 18:35:00 +00:00
|
|
|
}
|
2022-10-06 19:59:47 +00:00
|
|
|
return txctx.SignAndSend(ctx, act, acc, tx)
|
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 {
|
2022-08-19 18:35:00 +00:00
|
|
|
var tx *transaction.Transaction
|
|
|
|
|
2022-06-23 13:50:21 +00:00
|
|
|
wall, pass, err := readWallet(ctx)
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
2022-09-01 18:44:49 +00:00
|
|
|
defer wall.Close()
|
2020-03-06 14:22:14 +00:00
|
|
|
|
|
|
|
fromFlag := ctx.Generic("from").(*flags.Address)
|
2021-01-11 12:58:37 +00:00
|
|
|
from, err := getDefaultAddress(fromFlag, wall)
|
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(err, 1)
|
2021-01-11 12:58:37 +00:00
|
|
|
}
|
2023-12-04 14:02:44 +00:00
|
|
|
acc, err := options.GetUnlockedAccount(wall, from, pass)
|
2020-08-07 09:34:54 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(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
|
|
|
|
2023-12-07 07:35:37 +00:00
|
|
|
cosignersOffset, data, extErr := cmdargs.GetDataFromContext(ctx)
|
|
|
|
if extErr != nil {
|
|
|
|
return extErr
|
|
|
|
}
|
|
|
|
cosigners, extErr := cmdargs.GetSignersFromContext(ctx, cosignersOffset)
|
|
|
|
if extErr != nil {
|
|
|
|
return extErr
|
|
|
|
}
|
|
|
|
signersAccounts, err := cmdargs.GetSignersAccounts(acc, wall, cosigners, transaction.CalledByEntry)
|
2020-03-06 14:22:14 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("invalid signers: %w", err), 1)
|
2023-12-07 07:35:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, act, exitErr := options.GetRPCWithActor(gctx, ctx, signersAccounts)
|
|
|
|
if exitErr != nil {
|
|
|
|
return exitErr
|
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 {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("can't fetch matching token from RPC-node: %w", err), 1)
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 16:00:45 +00:00
|
|
|
amountArg := ctx.String("amount")
|
2022-08-19 18:35:00 +00:00
|
|
|
amount, err := fixedn.FromString(amountArg, int(token.Decimals))
|
|
|
|
// It's OK for NEP-11 transfer to not have amount set.
|
|
|
|
if err != nil && (standard == manifest.NEP17StandardName || amountArg != "") {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("invalid amount: %w", err), 1)
|
2022-08-19 18:35:00 +00:00
|
|
|
}
|
2021-04-23 16:00:45 +00:00
|
|
|
switch standard {
|
|
|
|
case manifest.NEP17StandardName:
|
2022-08-19 18:35:00 +00:00
|
|
|
n17 := nep17.New(act, token.Hash)
|
|
|
|
tx, err = n17.TransferUnsigned(act.Sender(), to, amount, data)
|
2021-04-23 16:00:45 +00:00
|
|
|
case manifest.NEP11StandardName:
|
|
|
|
tokenID := ctx.String("id")
|
|
|
|
if tokenID == "" {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(errors.New("token ID should be specified"), 1)
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
2022-08-19 18:35:00 +00:00
|
|
|
tokenIDBytes, terr := hex.DecodeString(tokenID)
|
|
|
|
if terr != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("invalid token ID: %w", terr), 1)
|
2022-02-04 16:18:35 +00:00
|
|
|
}
|
2021-04-23 16:00:45 +00:00
|
|
|
if amountArg == "" {
|
2022-08-19 18:35:00 +00:00
|
|
|
n11 := nep11.NewNonDivisible(act, token.Hash)
|
|
|
|
tx, err = n11.TransferUnsigned(to, tokenIDBytes, data)
|
|
|
|
} else {
|
|
|
|
n11 := nep11.NewDivisible(act, token.Hash)
|
|
|
|
tx, err = n11.TransferDUnsigned(act.Sender(), to, amount, tokenIDBytes, data)
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
|
|
|
default:
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("unsupported token standard %s", standard), 1)
|
2021-04-23 16:00:45 +00:00
|
|
|
}
|
2022-08-19 18:35:00 +00:00
|
|
|
if err != nil {
|
2024-07-09 18:24:39 +00:00
|
|
|
return cli.Exit(fmt.Errorf("can't make transaction: %w", err), 1)
|
2022-08-19 18:35:00 +00:00
|
|
|
}
|
2020-07-24 13:34:46 +00:00
|
|
|
|
2022-10-06 19:59:47 +00:00
|
|
|
return txctx.SignAndSend(ctx, act, acc, tx)
|
2022-08-19 18:35:00 +00:00
|
|
|
}
|
2020-03-06 14:22:14 +00:00
|
|
|
|
2023-10-09 19:34:33 +00:00
|
|
|
func makeMultiTransferNEP17(act *actor.Actor, recipients []transferTarget) (*transaction.Transaction, error) {
|
2022-08-19 17:53:16 +00:00
|
|
|
scr := smartcontract.NewBuilder()
|
|
|
|
for i := range recipients {
|
|
|
|
scr.InvokeWithAssert(recipients[i].Token, "transfer", act.Sender(),
|
|
|
|
recipients[i].Address, recipients[i].Amount, recipients[i].Data)
|
|
|
|
}
|
|
|
|
script, err := scr.Script()
|
|
|
|
if err != nil {
|
2022-08-19 18:35:00 +00:00
|
|
|
return nil, err
|
2020-03-06 14:22:14 +00:00
|
|
|
}
|
2022-08-19 18:35:00 +00:00
|
|
|
return act.MakeUnsignedRun(script, 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
|
|
|
|
}
|