Repository navigation
Expand file tree
/
Copy pathcontract.go
More file actions
94 lines (81 loc) · 2.25 KB
/
Copy pathcontract.go
File metadata and controls
94 lines (81 loc) · 2.25 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
package main
import (
"bytes"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"io/ioutil"
"math/big"
)
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 {
panic(err)
}
c.byteCode = common.Hex2Bytes(string(bin))
abiByte, err := ioutil.ReadFile(abiFile)
if err != nil {
panic(err)
}
c.abi, err = abi.JSON(bytes.NewReader(abiByte))
if err != nil {
panic(err)
}
if len(address) > 0 {
c.addr = common.HexToAddress(address)
fmt.Printf("new contract: %s\n", address)
}
return c
}
func createDeploy(index int) (tx []byte, nonce uint64, err error) {
chainID := big.NewInt(ChainId)
if hexKeys[index] == "8ff3ca2d9985c3a52b459e2f6e7822b23e1af845961e22128d5f372fb9aa5f17" {
nonce++
}
//1. simulate unsignedTx as you want, fill out the parameters into a unsignedTx
unsignedTx, err := deployContractTx(index, nonce, counterContract)
if err != nil {
return
}
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(chainID), privateKeys[index])
if err != nil {
return
}
tx, err = signedTx.MarshalBinary()
return
}
func deployContractTx(index int, nonce uint64, contract *Contract) (*types.Transaction, error) {
value := big.NewInt(0)
// Constructor
input, err := contract.abi.Pack("")
if err != nil {
return nil, err
}
data := append(contract.byteCode, input...)
return types.NewContractCreation(nonce, value, GasLimit+uint64(index), big.NewInt(GasPrice+int64(index)), data), err
}
func createCall(index int, nonce uint64, data []byte) []byte {
amount := big.NewInt(0)
gasPrice := big.NewInt(GasPrice + int64(index))
unsignedTx := types.NewTransaction(nonce, counterContract.addr, amount, GasLimit+uint64(index), gasPrice, data)
// 2. sign unsignedTx -> rawTx
signedTx, err := types.SignTx(unsignedTx, types.NewEIP155Signer(big.NewInt(ChainId)), privateKeys[index])
if err != nil {
return nil
}
tx, _ := signedTx.MarshalBinary()
return tx
}