Repository navigation
Expand file tree
/
Copy pathcommon.go
More file actions
383 lines (326 loc) · 9.89 KB
/
Copy pathcommon.go
File metadata and controls
383 lines (326 loc) · 9.89 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package main
import (
"bytes"
"context"
"crypto/ecdsa"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"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/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/okex/exchain-ethereum-compatible/utils"
"io/ioutil"
"log"
"math/big"
"time"
)
const (
//RpcUrl = "https://exchaintestrpc.okex.org"
RpcUrl = "http://127.0.0.1:8545"
ChainId int64 = 67 // okc
//RpcUrl = "https://exchainrpc.okex.org"
//ChainId int64 = 66 // okc
GasPrice int64 = 100000000 // 0.1 gwei
GasLimit uint64 = 3000000
)
type Contract struct {
name string
address string
addr common.Address
abi abi.ABI
byteCode []byte
}
func newContract(name, address, abiFile string, byteCodeFile string) *Contract {
c := &Contract{
name: name,
address: address,
}
bin, err := ioutil.ReadFile(byteCodeFile)
if err != nil {
log.Fatal(err)
}
c.byteCode = common.Hex2Bytes(string(bin))
abiByte, err := ioutil.ReadFile(abiFile)
if err != nil {
log.Fatal(err)
}
c.abi, err = abi.JSON(bytes.NewReader(abiByte))
if err != nil {
log.Fatal(err)
}
if len(address) > 0 {
c.addr = common.HexToAddress(address)
fmt.Printf("new contract: %s\n", address)
}
return c
}
func str2bigInt(input string) *big.Int {
return sdk.MustNewDecFromStr(input).Int
}
func uint256Output(client *ethclient.Client, c *Contract, name string, args ...interface{}) *big.Int {
value := readContract(client, c, name, args...)
if len(value) == 0 {
return str2bigInt("0")
}
ret := value[0].(*big.Int)
arg0 := ""
if len(args) > 0 {
if value, ok := args[0].(common.Address); ok {
arg0 = value.String()
}
}
decRet := sdk.NewDecFromBigIntWithPrec(ret, sdk.Precision)
fmt.Printf(" <%s[%s(%s)]>: %s\n", c.address, name, arg0, decRet)
return ret
}
func writeContract(client *ethclient.Client,
contract *Contract,
fromAddress common.Address,
privateKey *ecdsa.PrivateKey,
amount *big.Int,
sleep time.Duration,
name string,
args ...interface{}) error {
// 0. get the value of nonce, based on address
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Printf("failed to fetch the value of nonce from network: %+v", err)
return err
}
// 0.5 get the gasPrice
gasPrice := big.NewInt(GasPrice)
fmt.Printf(
"==================================================\n"+
"%s: \n"+
" sender: <%s>, nonce<%d>\n"+
" contract: <%s>, abi: <%s %s>\n"+
"==================================================\n",
contract.name,
fromAddress.Hex(),
nonce,
contract.address,
name, args)
data, err := contract.abi.Pack(name, args...)
if err != nil {
log.Printf("%s", err)
return err
}
if amount == nil {
amount = big.NewInt(0)
}
unsignedTx := types.NewTransaction(nonce, contract.addr, amount, GasLimit, gasPrice, data)
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(big.NewInt(ChainId)), privateKey)
if err != nil {
log.Printf("failed to sign the unsignedTx offline: %+v", err)
return err
}
// 3. send rawTx
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Printf("%s", err)
return err
}
time.Sleep(sleep)
return nil
}
func transferOKT(client *ethclient.Client,
fromAddress common.Address,
toAddress common.Address,
amount *big.Int,
privateKey *ecdsa.PrivateKey,
sleep time.Duration) {
// 0. get the value of nonce, based on address
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatalf("failed to fetch the value of nonce from network: %+v", err)
}
// 0.5 get the gasPrice
gasPrice := big.NewInt(GasPrice)
fmt.Printf(
"==================================================\n"+
"Transfer OKT: \n"+
" from : <%s>\n"+
" to : <%s>\n"+
" amount: <%s>\n"+
"==================================================\n",
fromAddress,
toAddress,
sdk.NewDecFromBigIntWithPrec(amount, sdk.Precision),
)
unsignedTx := types.NewTransaction(nonce, toAddress, amount, GasLimit, gasPrice, nil)
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(big.NewInt(ChainId)), privateKey)
if err != nil {
log.Fatalf("failed to sign the unsignedTx offline: %+v", err)
}
// 3. send rawTx
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
if sleep > 0 {
time.Sleep(time.Second * sleep)
}
}
func sleep(second time.Duration) {
time.Sleep(second * time.Second)
}
func readContract(client *ethclient.Client, contract *Contract, name string, args ...interface{}) []interface{} {
data, err := contract.abi.Pack(name, args...)
if err != nil {
return nil
}
msg := ethereum.CallMsg{
To: &contract.addr,
Data: data,
}
output, err := client.CallContract(context.Background(), msg, nil)
if err != nil {
return nil
}
ret, err := contract.abi.Unpack(name, output)
if err != nil {
return nil
}
return ret
}
func initKey(key string) (*ecdsa.PrivateKey, common.Address) {
privateKey, err := crypto.HexToECDSA(key)
if err != nil {
log.Fatalf("failed to switch unencrypted private key -> secp256k1 private key: %+v", err)
}
pubkey := privateKey.Public()
pubkeyECDSA, ok := pubkey.(*ecdsa.PublicKey)
if !ok {
log.Fatalln("failed to switch secp256k1 private key -> pubkey")
}
senderAddress := crypto.PubkeyToAddress(*pubkeyECDSA)
return privateKey, senderAddress
}
func deployContract(client *ethclient.Client, fromAddress common.Address,
privateKey *ecdsa.PrivateKey, contract *Contract, blockTime time.Duration) error {
fmt.Printf("%s deploying contract\n", fromAddress.String())
chainID := big.NewInt(ChainId)
// 0. get the value of nonce, based on address
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Printf("failed to fetch the value of nonce from network: %+v", err)
return err
}
//1. simulate unsignedTx as you want, fill out the parameters into a unsignedTx
unsignedTx, err := deployContractTx(nonce, contract)
if err != nil {
return err
}
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Printf("failed to sign the unsignedTx offline: %+v", err)
return err
}
// 3. send rawTx
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Printf("SendTransaction err: %s", err)
return err
}
// 4. get the contract address based on tx hash
hash, err := utils.Hash(signedTx)
if err != nil {
log.Printf("Hash tx err: %s", err)
return err
}
var receipt *types.Receipt
var retry int
for err == nil {
sleep(blockTime)
receipt, err = client.TransactionReceipt(context.Background(), hash)
fmt.Printf("TransactionReceipt retry: %d, err: %s, tx hash<%s>\n", retry, err, hash.String())
if err != nil {
retry++
if retry > 10 {
return err
}
err = nil
} else {
break
}
}
contract.address = receipt.ContractAddress.String()
contract.addr = receipt.ContractAddress
fmt.Printf("new contract address: %s\n", contract.address)
return nil
}
func deployContractTx(nonce uint64, contract *Contract) (*types.Transaction, error) {
value := big.NewInt(0)
// Constructor
input, err := contract.abi.Pack("")
if err != nil {
log.Printf("contract.abi.Pack err: %s", err)
return nil, err
}
data := append(contract.byteCode, input...)
return types.NewContractCreation(nonce, value, GasLimit, big.NewInt(GasPrice), data), err
}
func deployStandardOIP20Contract(client *ethclient.Client, auth *bind.TransactOpts, symbol string,
name string, decimals uint8, totalSupply *big.Int,
ownerAddress common.Address, blockTime time.Duration) (contractAddress common.Address,
oip20 *Oip20, err error) {
fmt.Printf("%s deploying OIP20 contract\n", ownerAddress)
contractAddress, _, oip20, err = DeployOip20(auth, client, symbol, name, decimals, totalSupply, ownerAddress, ownerAddress)
fmt.Printf("Deploy standard OIP20 contract: <%s>\n", contractAddress)
time.Sleep(blockTime)
return contractAddress, oip20, err
}
func send(client *ethclient.Client, to, privKey string) {
privateKey, senderAddress := initKey(privKey)
toAddress := common.HexToAddress(to)
// send 0.001okt
transferOKT(client, senderAddress, toAddress, str2bigInt("0.001"), privateKey, 0)
}
func transferOip(client *ethclient.Client, oip20 *Oip20,
sender common.Address, auth *bind.TransactOpts, toAddress common.Address) (nonce uint64, err error) {
transferAmount := str2bigInt("100000")
nonce, err = client.PendingNonceAt(context.Background(), sender)
if err != nil {
log.Printf("failed to fetch nonce: %+v", err)
}
auth.Nonce = big.NewInt(int64(nonce))
_, err = oip20.Transfer(auth, toAddress, transferAmount)
if err != nil {
log.Printf("failed to transfer: %+v", err)
}
return
}
func deployOip(client *ethclient.Client, sender common.Address,
privateKey *ecdsa.PrivateKey) (oip20 *Oip20, auth *bind.TransactOpts, err error) {
var nonce uint64
var gasPrice *big.Int
nonce, err = client.PendingNonceAt(context.Background(), sender)
if err != nil {
log.Printf("failed to fetch nonce: %+v", err)
}
auth, err = bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(ChainId))
if err != nil {
log.Printf("failed to gen TransactOpts: %+v", err)
}
gasPrice, err = client.SuggestGasPrice(auth.Context)
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) // in wei
auth.GasLimit = GasLimit // in units
auth.GasPrice = gasPrice
auth.Context = context.Background()
symbol := "OIP20"
contractName := "OIP20 STD"
decimals := 18
if err == nil {
_, oip20, err = deployStandardOIP20Contract(client, auth, symbol,
contractName, uint8(decimals), str2bigInt("100000000000000000000000"), sender, 3*time.Second)
}
return
}