/
call.go
289 lines (251 loc) · 7.28 KB
/
call.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package cli
import (
"context"
"fmt"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
)
func (cli *CLI) buildCallCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "call <functionName> [arg1Type arg1Value] [arg2Type arg2Value]... [--view] [--out outType]",
Short: "Call functions with args type and value",
Args: cobra.MinimumNArgs(1),
DisableFlagsInUseLine: true,
Example: fmt.Sprintf(`%s call transfer address 0x4Ba80F138543E75AbF788eB3fE2726425586b0ff uint256 1
%s call totalSupply --view --out uint256123
%s call name --view --out string
%s call balanceOf address 0x4Ba80F138543E75AbF788eB3fE2726425586b0fD --view --out uint256`,
cli.Name, cli.Name, cli.Name, cli.Name),
Run: func(cmd *cobra.Command, args []string) {
view, _ := cmd.Flags().GetBool("view")
unit, err := cmd.Flags().GetString("unit")
if err != nil {
fmt.Println("Error: ", err)
return
}
if !stringInSlice(unit, UnitList) {
fmt.Println("Error: ", errIllegalUnit)
return
}
amountStr, err := cmd.Flags().GetString("value")
if err != nil {
fmt.Println("Error:", err)
return
}
amountWei, err := getAmountWei(amountStr, unit)
if err != nil {
fmt.Println("Error: ", errIllegalAmount)
return
}
var valueArgs []string
var inputTypeArgs abi.Arguments
if len(args) > 1 {
argsLen := len(args)
if argsLen%2 == 0 {
fmt.Println("Error: len error ", argsLen, args)
return
}
for i := 1; i < argsLen-1; i += 2 {
arg := args[i]
if arg == "uint" {
arg = "uint256"
} else if arg == "int" {
arg = "int256"
}
argType, err := abi.NewType(arg, "", nil)
if err != nil {
fmt.Println(err)
return
}
argArg := abi.Argument{Type: argType}
inputTypeArgs = append(inputTypeArgs, argArg)
valueArgs = append(valueArgs, args[i+1])
}
}
var outTypeArgs abi.Arguments
if cmd.Flags().Changed("out") {
if !view {
fmt.Println("Error: --view not use")
return
}
outTypes, err := cmd.Flags().GetString("out")
if err != nil {
fmt.Println("Error: ", err)
return
}
outTypeList := strings.Split(outTypes, ",")
for _, outTypeStr := range outTypeList {
if outTypeStr[0] == '[' {
fmt.Println("Error: unsupported arg type:", outTypeStr)
return
}
if outTypeStr == "uint" {
outTypeStr = "uint256"
} else if outTypeStr == "int" {
outTypeStr = "int256"
}
outType, err := abi.NewType(outTypeStr, "", nil)
if err != nil {
fmt.Println(err)
return
}
outTypeArgs = append(outTypeArgs, abi.Argument{
Type: outType,
})
}
}
name := args[0]
method := abi.NewMethod(name, name, abi.Function, "", false, false, inputTypeArgs, outTypeArgs)
if err := cli.BuildClient(); err != nil {
fmt.Println(err)
return
}
client := cli.client
// view: abi will error when no out type
// abi
var bContract *bind.BoundContract
if !view {
a := abi.ABI{}
a.Methods = make(map[string]abi.Method)
a.Methods[method.Name] = method
bContract = bind.NewBoundContract(cli.contractAddress, a, client, client, client)
}
// input args
inputArgs, err := getConstructorArgs(method.Inputs, valueArgs)
if err != nil {
if len(method.Inputs) > 0 {
var argName []string
for _, input := range method.Inputs {
argName = append(argName, input.Name+" "+input.Type.String())
}
fmt.Println(fmt.Errorf("%v(%v)", err.Error(), strings.Join(argName, ", ")))
return // fmt.Errorf("%v(%v)", err.Error(), strings.Join(argName, ", "))
}
fmt.Println(err)
return // err
}
if view {
outByte, err := cli.view(method, inputArgs...)
if err != nil {
fmt.Printf("Error1: view function error(%v)\n", err)
return
}
if len(outByte) == 0 {
fmt.Println("Function always returns null")
return
}
cli.showOut(method, outByte)
return
}
var gasLimit uint64
if cmd.Flags().Changed("gasLimit") {
gasLimit, err = cmd.Flags().GetUint64("gasLimit")
if err != nil {
fmt.Println(err)
return
}
}
opts, err := cli.getTransactOpts("", gasLimit)
if err != nil {
fmt.Println("Error: ", err)
return
}
ctx := context.Background()
opts.Context = ctx
opts.Value = amountWei
// if amountWei.Cmp(big.NewInt(0)) > 0 {
// method.Payable = true
// }
if cmd.Flags().Changed("gasPrice") {
gasPriceStr, err := cmd.Flags().GetString("gasPrice")
if err != nil {
fmt.Println("Flags GetString error: ", err)
return
}
gasPrice, err := getAmountWei(gasPriceStr, UnitETH)
if err != nil {
fmt.Println("getAmountWei error: ", err)
return
}
opts.GasPrice = gasPrice
}
tx, err := bContract.Transact(opts, method.Name, inputArgs...)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(tx.Hash().String())
nowait, _ := cmd.Flags().GetBool("nowait")
if !nowait {
fmt.Printf("Transaction waiting to be mined: 0x%x\n", tx.Hash())
if _, err := bind.WaitMined(ctx, client, tx); err != nil {
fmt.Printf("Error: wait tx mined error(%v)\n", err)
return
}
showTransactionReceipt(cli.rpcURL, tx.Hash().String())
fmt.Println("Call function success")
}
return
},
}
cmd.Flags().BoolP("view", "v", false, "only view function and get output")
cmd.Flags().StringP("out", "o", "", "the out type list of the method, spilt by ',', only use with --view")
// cmd.Flags().Bool("force", false, "force execute function")
cmd.Flags().String("value", "", "the amount of unit send to the contract address")
cmd.Flags().StringP("unit", "u", UnitETH, fmt.Sprintf("unit for send value. %s.", UnitString))
cmd.Flags().StringP("gasPrice", "p", "", "the gas price in ETH")
cmd.Flags().Uint64P("gasLimit", "g", 21000, "the gas limit")
cmd.Flags().Bool("nowait", false, "not to wait tx to be mint")
return cmd
}
func (cli *CLI) view(method abi.Method, params ...interface{}) ([]byte, error) {
inputTypeArgsByte, err := method.Inputs.Pack(params...)
if err != nil {
return nil, err
}
input := append(method.ID, inputTypeArgsByte...)
// fmt.Printf("input: 0x%x\n", input)
msg := ethereum.CallMsg{From: cli.address, To: &cli.contractAddress, Data: input}
ctx := context.TODO() // context.Background()
if err := cli.BuildClient(); err != nil {
return nil, err
}
if err := cli.BuildClient(); err != nil {
return nil, err
}
return cli.client.CallContract(ctx, msg, nil)
}
func (cli *CLI) showOut(method abi.Method, outByte []byte) {
if len(outByte) == 0 {
fmt.Println("function return nil")
return
}
if len(method.Outputs) == 0 {
fmt.Printf("0x%x\n", outByte)
return
}
out, err := method.Outputs.UnpackValues(outByte)
if err != nil {
fmt.Println(err)
return
}
for _, v := range out {
showValue(v)
}
return
}
func showValue(v interface{}) {
if address, ok := v.(common.Address); ok {
fmt.Println(address.String())
} else if addressSlice, ok := v.([]common.Address); ok {
for _, address := range addressSlice {
fmt.Println(address.String())
}
} else {
fmt.Println(v)
}
}