/
contract.go
284 lines (247 loc) · 7.84 KB
/
contract.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
package cmd
import (
"encoding/json"
"fmt"
"io/ioutil"
"math"
"github.com/RichCG888/gotron-sdk/pkg/address"
"github.com/RichCG888/gotron-sdk/pkg/client/transaction"
"github.com/RichCG888/gotron-sdk/pkg/common"
"github.com/RichCG888/gotron-sdk/pkg/contract"
"github.com/RichCG888/gotron-sdk/pkg/keystore"
"github.com/RichCG888/gotron-sdk/pkg/store"
"github.com/spf13/cobra"
)
var (
abiSTR string
abiFile string
bcSTR string
bcFile string
feeLimit int64
curPercent int64
oeLimit int64
tAmount float64
tTokenID string
tTokenAmount float64
)
func contractSub() []*cobra.Command {
cmdDeploy := &cobra.Command{
Use: "deploy <CONTRACT_NAME>",
Short: "deploy smart contract",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if abiSTR == "" {
if abiFile != "" {
abiBytes, err := ioutil.ReadFile(abiFile)
if err != nil {
return fmt.Errorf("cannot read ABI file: %s %v", abiFile, err)
}
abiSTR = string(abiBytes)
} else {
return fmt.Errorf("no ABI string or ABI file specified")
}
}
ABI, err := contract.JSONtoABI(abiSTR)
if err != nil {
return fmt.Errorf("cannot parse ABI: %v", err)
}
if bcSTR == "" {
if bcFile != "" {
bcBytes, err := ioutil.ReadFile(bcFile)
if err != nil {
return fmt.Errorf("cannot read Bytecode file: %s %v", bcFile, err)
}
bcSTR = string(bcBytes)
} else {
return fmt.Errorf("no Bytecode string or Bytecode file specified")
}
}
if signerAddress.String() == "" {
return fmt.Errorf("no signer specified")
}
// TODO: add constructor arguments
tx, err := conn.DeployContract(signerAddress.String(), args[0],
ABI, bcSTR, feeLimit, curPercent, oeLimit)
if err != nil {
return err
}
var ctrlr *transaction.Controller
if useLedgerWallet {
account := keystore.Account{Address: signerAddress.GetAddress()}
ctrlr = transaction.NewController(conn, nil, &account, tx.Transaction, opts)
} else {
ks, acct, err := store.UnlockedKeystore(signerAddress.String(), passphrase)
if err != nil {
return err
}
ctrlr = transaction.NewController(conn, ks, acct, tx.Transaction, opts)
}
if err = ctrlr.ExecuteTransaction(); err != nil {
return err
}
if noPrettyOutput {
fmt.Println(tx)
return nil
}
addrResult := address.Address(ctrlr.Receipt.ContractAddress).String()
result := make(map[string]interface{})
result["txID"] = common.BytesToHexString(tx.GetTxid())
result["blockNumber"] = ctrlr.Receipt.BlockNumber
result["message"] = string(ctrlr.Result.Message)
result["contractAddress"] = addrResult
result["success"] = ctrlr.GetResultError() == nil
result["resMessage"] = string(ctrlr.Receipt.ResMessage)
result["receipt"] = map[string]interface{}{
"fee": ctrlr.Receipt.Fee,
"energyFee": ctrlr.Receipt.Receipt.EnergyFee,
"energyUsage": ctrlr.Receipt.Receipt.EnergyUsage,
"originEnergyUsage": ctrlr.Receipt.Receipt.OriginEnergyUsage,
"energyUsageTotal": ctrlr.Receipt.Receipt.EnergyUsageTotal,
"netFee": ctrlr.Receipt.Receipt.NetFee,
"netUsage": ctrlr.Receipt.Receipt.NetUsage,
}
asJSON, _ := json.Marshal(result)
fmt.Println(common.JSONPrettyFormat(string(asJSON)))
return nil
},
}
cmdDeploy.Flags().StringVar(&abiSTR, "abi", "", "abi JSON string")
cmdDeploy.Flags().StringVar(&abiFile, "abiFile", "", "abi file location")
cmdDeploy.Flags().StringVar(&bcSTR, "bc", "", "bytecode HEX string")
cmdDeploy.Flags().StringVar(&bcFile, "bcFile", "", "bytecode file location")
cmdDeploy.Flags().Int64Var(&feeLimit, "feeLimit", 1000000000, "fee limit")
cmdDeploy.Flags().Int64Var(&curPercent, "curPercent", 100, "consome user resource percentage")
cmdDeploy.Flags().Int64Var(&oeLimit, "oeLimit", 1000000, "origin energy limit")
cmdConstant := &cobra.Command{
Use: "constant <CONTRACT_ADDRESS> <METHOD> [PARAMETER]",
Short: "constantTrigger contract",
Args: cobra.RangeArgs(2, 3),
PreRunE: validateAddress,
RunE: func(cmd *cobra.Command, args []string) error {
if signerAddress.String() == "" {
return fmt.Errorf("no signer specified")
}
param := ""
if len(args) == 3 {
param = args[2]
}
tx, err := conn.TriggerConstantContract(
signerAddress.String(),
addr.String(),
args[1],
param,
)
if err != nil {
return err
}
cResult := tx.GetConstantResult()
if noPrettyOutput {
fmt.Println(cResult)
return nil
}
result := make(map[string]interface{})
//TODO: parse based on contract ABI
result["Result"] = common.ToHex(cResult[0])
asJSON, _ := json.Marshal(result)
fmt.Println(common.JSONPrettyFormat(string(asJSON)))
return nil
},
}
cmdTrigger := &cobra.Command{
Use: "trigger <CONTRACT_ADDRESS> <METHOD> [PARAMETER]",
Short: "trigger smartcontract",
Args: cobra.RangeArgs(2, 3),
PreRunE: validateAddress,
RunE: func(cmd *cobra.Command, args []string) error {
if signerAddress.String() == "" {
return fmt.Errorf("no signer specified")
}
// get amount
valueInt := int64(0)
if tAmount > 0 {
valueInt = int64(tAmount * math.Pow10(6))
}
tokenInt := int64(0)
if tTokenAmount > 0 {
// get token info
info, err := conn.GetAssetIssueByID(tTokenID)
if err != nil {
return err
}
tokenInt = int64(tAmount * math.Pow10(int(info.Precision)))
}
param := ""
if len(args) == 3 {
param = args[2]
}
tx, err := conn.TriggerContract(
signerAddress.String(),
addr.String(),
args[1],
param,
feeLimit,
valueInt,
tTokenID,
tokenInt,
)
if err != nil {
return err
}
var ctrlr *transaction.Controller
if useLedgerWallet {
account := keystore.Account{Address: signerAddress.GetAddress()}
ctrlr = transaction.NewController(conn, nil, &account, tx.Transaction, opts)
} else {
ks, acct, err := store.UnlockedKeystore(signerAddress.String(), passphrase)
if err != nil {
return err
}
ctrlr = transaction.NewController(conn, ks, acct, tx.Transaction, opts)
}
if err = ctrlr.ExecuteTransaction(); err != nil {
return err
}
if noPrettyOutput {
fmt.Println(tx)
return nil
}
addrResult := address.Address(ctrlr.Receipt.ContractAddress).String()
result := make(map[string]interface{})
result["txID"] = common.BytesToHexString(tx.GetTxid())
result["blockNumber"] = ctrlr.Receipt.BlockNumber
result["message"] = string(ctrlr.Result.Message)
result["contractAddress"] = addrResult
result["success"] = ctrlr.GetResultError() == nil
result["resMessage"] = string(ctrlr.Receipt.ResMessage)
result["receipt"] = map[string]interface{}{
"fee": ctrlr.Receipt.Fee,
"energyFee": ctrlr.Receipt.Receipt.EnergyFee,
"energyUsage": ctrlr.Receipt.Receipt.EnergyUsage,
"originEnergyUsage": ctrlr.Receipt.Receipt.OriginEnergyUsage,
"energyUsageTotal": ctrlr.Receipt.Receipt.EnergyUsageTotal,
"netFee": ctrlr.Receipt.Receipt.NetFee,
"netUsage": ctrlr.Receipt.Receipt.NetUsage,
}
asJSON, _ := json.Marshal(result)
fmt.Println(common.JSONPrettyFormat(string(asJSON)))
return nil
},
}
cmdTrigger.Flags().Int64Var(&feeLimit, "feeLimit", 10000000, "fee limit")
cmdTrigger.Flags().Float64Var(&tAmount, "value", 0, "trx amount")
cmdTrigger.Flags().StringVar(&tTokenID, "token", "", "token id")
cmdTrigger.Flags().Float64Var(&tTokenAmount, "tokenValue", 0, "token amount")
return []*cobra.Command{cmdDeploy, cmdConstant, cmdTrigger}
}
func init() {
cmdContract := &cobra.Command{
Use: "contract",
Short: "SmartContract actions",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
return nil
},
}
cmdContract.AddCommand(contractSub()...)
RootCmd.AddCommand(cmdContract)
}