forked from neo-ngd/neo-go
/
native_token.go
161 lines (149 loc) · 4.05 KB
/
native_token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package wallet
import (
"errors"
"fmt"
"math/big"
"github.com/DigitalLabs-web3/neo-go-evm/cli/flags"
"github.com/DigitalLabs-web3/neo-go-evm/cli/input"
"github.com/DigitalLabs-web3/neo-go-evm/cli/options"
"github.com/DigitalLabs-web3/neo-go-evm/pkg/wallet"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli"
)
var (
balanceFlags = []cli.Flag{
WalletPathFlag,
flags.AddressFlag{
Name: "address, a",
Usage: "address",
},
}
transferFlags = []cli.Flag{
WalletPathFlag,
FromAddrFlag,
toAddrFlag,
cli.StringFlag{
Name: "amount",
Usage: "Amount of asset to send",
},
}
)
func newNativeTokenCommands() []cli.Command {
balanceFlags = append(balanceFlags, options.RPC...)
transferFlags = append(transferFlags, options.RPC...)
return []cli.Command{
{
Name: "balance",
Usage: "get address GAS balance",
UsageText: "balance --wallet <path> --rpc-endpoint <node> [--timeout <time>] [--address <address>]",
Action: balance,
Flags: balanceFlags,
},
{
Name: "transfer",
Usage: "transfer GAS to address",
UsageText: "transfer --wallet <path> --rpc-endpoint <node> [--from <fromAddress>] --to <toAddress> --amount <amount>",
Action: transferNativeToken,
Flags: transferFlags,
},
}
}
func balance(ctx *cli.Context) error {
var accounts []*wallet.Account
wall, err := ReadWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(fmt.Errorf("bad wallet: %w", err), 1)
}
defer wall.Close()
addr := ctx.Generic("address").(*flags.Address)
if addr.IsSet {
acc := wall.GetAccount(addr.Address())
if acc == nil {
return cli.NewExitError(fmt.Errorf("can't find account for the address: %s", addr), 1)
}
accounts = append(accounts, acc)
} else {
if len(wall.Accounts) == 0 {
return cli.NewExitError(errors.New("no accounts in the wallet"), 1)
}
accounts = wall.Accounts
}
gctx, cancel := options.GetTimeoutContext(ctx)
defer cancel()
c, err := options.GetRPCClient(gctx, ctx)
if err != nil {
return cli.NewExitError(err, 1)
}
for k, acc := range accounts {
addr := acc.Address
if err != nil {
return cli.NewExitError(fmt.Errorf("invalid account address: %w", err), 1)
}
balance, err := c.Eth_GetBalance(addr)
if err != nil {
return cli.NewExitError(err, 1)
}
if k != 0 {
fmt.Fprintln(ctx.App.Writer)
}
fmt.Fprintf(ctx.App.Writer, "Account %s, Balance: %s\n", addr, balance)
}
return nil
}
func transferNativeToken(ctx *cli.Context) error {
wall, err := ReadWallet(ctx.String("wallet"))
if err != nil {
return cli.NewExitError(err, 1)
}
toFlag := ctx.Generic("to").(*flags.Address)
if !toFlag.IsSet {
return cli.NewExitError(fmt.Errorf("missing to address"), 1)
}
to := toFlag.Address()
if to == (common.Address{}) {
return cli.NewExitError(fmt.Errorf("invalid to address %s", to), 1)
}
samount := ctx.String("amount")
amount, ok := big.NewInt(0).SetString(samount, 10)
if !ok {
return cli.NewExitError(fmt.Errorf("could not parse amount: %s", samount), 1)
}
var facc *wallet.Account
fromFlag := ctx.Generic("from").(*flags.Address)
var from common.Address
if fromFlag.IsSet {
from = fromFlag.Address()
if from == (common.Address{}) {
return cli.NewExitError(fmt.Errorf("invalid from address"), 1)
}
for _, acc := range wall.Accounts {
if acc.Address == from {
facc = acc
break
}
}
} else {
if len(wall.Accounts) == 0 {
return cli.NewExitError(fmt.Errorf("could not find any account in wallet"), 1)
}
facc = wall.Accounts[0]
for _, acc := range wall.Accounts {
if acc.Default {
facc = acc
break
}
}
}
if facc == nil || facc.IsMultiSig() {
return MakeNeoTx(ctx, wall, from, to, amount, nil)
}
pass, err := input.ReadPassword(fmt.Sprintf("Enter %s password > ", facc.Address))
if err != nil {
return cli.NewExitError(fmt.Errorf("error reading password: %w", err), 1)
}
err = facc.Decrypt(pass, wall.Scrypt)
if err != nil {
return cli.NewExitError(fmt.Errorf("unable to decrypt account: %s", facc.Address), 1)
}
return MakeEthTx(ctx, facc, &to, amount, nil)
}