-
Notifications
You must be signed in to change notification settings - Fork 146
/
contractcmd.go
287 lines (239 loc) · 6.45 KB
/
contractcmd.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
package core
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
"github.com/PlatONnetwork/PlatON-Go/common/hexutil"
"github.com/PlatONnetwork/PlatON-Go/rlp"
"gopkg.in/urfave/cli.v1"
)
var (
DeployCmd = cli.Command{
Name: "deploy",
Usage: "deploy a contract",
Action: deploy,
Flags: deployCmdFlags,
}
InvokeCmd = cli.Command{
Name: "invoke",
Aliases: []string{"i"},
Usage: "invoke contract function",
Action: invoke,
Flags: invokeCmdFlags,
}
)
func deploy(c *cli.Context) error {
abiPath := c.String("abi")
codePath := c.String("code")
parseConfigJson(c.String(ConfigPathFlag.Name))
err := DeployContract(abiPath, codePath)
if err != nil {
panic(fmt.Errorf("deploy contract error,%s", err.Error()))
}
return nil
}
func DeployContract(abiFilePath string, codeFilePath string) error {
var err error
abiBytes := parseFileToBytes(abiFilePath)
codeBytes := parseFileToBytes(codeFilePath)
param := [3][]byte{
Int64ToBytes(deployContract),
codeBytes,
abiBytes,
}
paramBytes, err := rlp.EncodeToBytes(param)
if err != nil {
return fmt.Errorf("rlp encode error,%s", err.Error())
}
deployParams := DeployParams{
From: config.From,
GasPrice: config.GasPrice,
Gas: config.Gas,
Data: hexutil.Encode(paramBytes),
}
params := make([]interface{}, 1)
params[0] = deployParams
//paramJson, _ := json.Marshal(paramList)
//fmt.Printf("\n request json data:%s\n", string(paramJson))
r, err := Send(params, "platon_sendTransaction")
//fmt.Printf("\nresponse json:%s\n", r)
resp := parseResponse(r)
fmt.Printf("\ntrasaction hash: %s\n", resp.Result)
// Get transaction receipt according to result
ch := make(chan string, 1)
exit := make(chan string, 1)
go GetTransactionReceipt(resp.Result, ch, exit)
// Getting receipt
select {
case address := <-ch:
fmt.Printf("contract address: %s\n", address)
case <-time.After(time.Second * 200):
exit <- "exit"
fmt.Printf("get contract receipt timeout...more than 200 second.\n")
}
return err
}
func parseFileToBytes(file string) []byte {
bytes, err := ioutil.ReadFile(file)
if err != nil {
panic(fmt.Sprintf("parse file %s error,%s", file, err.Error()))
}
return bytes
}
func invoke(c *cli.Context) error {
addr := c.String("addr")
abiPath := c.String("abi")
funcParams := c.String("func")
txType := c.Int("type")
//param check
if abiPath == "" {
fmt.Printf("abi can't be empty!")
return nil
}
if addr == "" {
fmt.Printf("addr can't be empty!")
return nil
}
if funcParams == "" {
fmt.Printf("func can't be empty!")
return nil
}
parseConfigJson(c.String(ConfigPathFlag.Name))
err := InvokeContract(addr, abiPath, funcParams, txType)
if err != nil {
panic(fmt.Errorf("invokeContract contract error,%s", err.Error()))
}
return nil
}
/**
*/
func InvokeContract(contractAddr string, abiPath string, funcParams string, txType int) error {
//Judging whether this contract exists or not
if !getContractByAddress(contractAddr) {
return fmt.Errorf("the contract address is not exist ...")
}
//parse the function and param
funcName, inputParams := GetFuncNameAndParams(funcParams)
//Judging whether this method exists or not
abiFunc, err := parseFuncFromAbi(abiPath, funcName)
if err != nil {
return err
}
if len(abiFunc.Inputs) != len(inputParams) {
return fmt.Errorf("incorrect number of parameters ,request=%d,get=%d\n", len(abiFunc.Inputs), len(inputParams))
}
if txType == 0 {
txType = invokeContract
}
paramArr := [][]byte{
Int64ToBytes(int64(txType)),
[]byte(funcName),
}
for i, v := range inputParams {
input := abiFunc.Inputs[i]
p, e := StringConverter(v, input.Type)
if e != nil {
return fmt.Errorf("incorrect param type: %s,index:%d", v, i)
}
paramArr = append(paramArr, p)
}
paramBytes, e := rlp.EncodeToBytes(paramArr)
if e != nil {
return fmt.Errorf("rpl encode error,%s", e.Error())
}
txParams := TxParams{
From: config.From,
To: contractAddr,
GasPrice: config.GasPrice,
Gas: config.Gas,
Data: hexutil.Encode(paramBytes),
}
var r string
if abiFunc.Constant == "true" {
params := make([]interface{}, 2)
params[0] = txParams
params[1] = "latest"
paramJson, _ := json.Marshal(params)
fmt.Printf("\n request json data:%s \n", string(paramJson))
r, err = Send(params, "platon_call")
} else {
params := make([]interface{}, 1)
params[0] = txParams
paramJson, _ := json.Marshal(params)
fmt.Printf("\n request json data:%s \n", string(paramJson))
r, err = Send(params, "platon_sendTransaction")
}
fmt.Printf("\n response json:%s \n", r)
if err != nil {
return fmt.Errorf("send http post to invokeContract contract error,%s", e.Error())
}
resp := parseResponse(r)
//parse the return type through adi
if abiFunc.Constant == "true" {
if len(abiFunc.Outputs) != 0 && abiFunc.Outputs[0].Type != "void" {
bytes, _ := hexutil.Decode(resp.Result)
result := BytesConverter(bytes, abiFunc.Outputs[0].Type)
fmt.Printf("\nresult: %v\n", result)
return nil
}
fmt.Printf("\n result: []\n")
} else {
fmt.Printf("\n trasaction hash: %s\n", resp.Result)
}
return nil
}
/**
Judging whether a contract exists through platon_getCode
*/
func getContractByAddress(addr string) bool {
params := []string{addr, "latest"}
r, err := Send(params, "platon_getCode")
if err != nil {
fmt.Printf("send http post to get contract address error ")
return false
}
var resp = Response{}
err = json.Unmarshal([]byte(r), &resp)
if err != nil {
fmt.Printf("parse platon_getCode result error ! \n %s", err.Error())
return false
}
if resp.Error.Code != 0 {
fmt.Printf("platon_getCode error ,error:%v", resp.Error.Message)
return false
}
//fmt.Printf("trasaction hash: %s\n", resp.Result)
if resp.Result != "" && len(resp.Result) > 2 {
return true
} else {
return false
}
}
/*
Loop call to get transactionReceipt... until 200s timeout
*/
func GetTransactionReceipt(txHash string, ch chan string, exit chan string) {
var receipt = Receipt{}
var contractAddr string
for {
res, e := Send([]string{txHash}, "platon_getTransactionReceipt")
if e != nil {
panic(fmt.Sprintf("send http post to get transaction receipt error!\n %s", e.Error()))
}
e = json.Unmarshal([]byte(res), &receipt)
if e != nil {
panic(fmt.Sprintf("parse get receipt result error ! \n %s", e.Error()))
}
contractAddr = receipt.Result.ContractAddress
if contractAddr != "" {
ch <- contractAddr
break
}
select {
case <-exit:
break
default:
}
}
}