-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontract.go
132 lines (110 loc) · 3.76 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
package deployer
import (
"bytes"
"errors"
"fmt"
"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/InjectiveLabs/etherman/sol"
)
type TransactFunc func(opts *bind.TransactOpts, contract *common.Address, input []byte) (*types.Transaction, error)
type BoundContract struct {
*bind.BoundContract
transactFn TransactFunc
client *ethclient.Client
address common.Address
src *sol.Contract
abi abi.ABI
}
func BindContract(client *ethclient.Client, contract *sol.Contract) (*BoundContract, error) {
if contract == nil {
err := errors.New("contract must not be nil")
return nil, err
}
parsedABI, err := abi.JSON(bytes.NewReader(contract.ABI))
if err != nil {
err = fmt.Errorf("failed to parse contract ABI: %v", err)
return nil, err
}
bound := &BoundContract{
BoundContract: bind.NewBoundContract(contract.Address, parsedABI, client, client, client),
client: client,
address: contract.Address,
abi: parsedABI,
src: contract,
}
return bound, nil
}
func (contract *BoundContract) SetTransact(fn TransactFunc) {
contract.transactFn = fn
}
func (contract *BoundContract) SetClient(client *ethclient.Client) {
contract.client = client
contract.BoundContract = bind.NewBoundContract(
contract.address, contract.abi, client, client, client)
}
func (contract *BoundContract) Client() *ethclient.Client {
return contract.client
}
func (contract *BoundContract) Address() common.Address {
return contract.address
}
func (contract *BoundContract) SetAddress(address common.Address) {
contract.address = address
contract.BoundContract = bind.NewBoundContract(
address, contract.abi, contract.client, contract.client, contract.client)
}
func (contract *BoundContract) Source() *sol.Contract {
return contract.src
}
func (contract *BoundContract) ABI() abi.ABI {
return contract.abi
}
// DeployContract deploys a contract onto the Ethereum blockchain and binds the
// deployment address with a Go wrapper.
func (c *BoundContract) DeployContract(opts *bind.TransactOpts,
params ...interface{}) (common.Address, *types.Transaction, error) {
if c.transactFn == nil {
addr, tx, bound, err := bind.DeployContract(opts, c.abi, common.FromHex(c.src.Bin), c.client, params...)
if err != nil {
return addr, tx, err
}
c.BoundContract = bound
return addr, tx, nil
}
c.BoundContract = bind.NewBoundContract(common.Address{}, c.abi, c.client, c.client, c.client)
input, err := c.abi.Pack("", params...)
if err != nil {
return common.Address{}, nil, err
}
tx, err := c.transactFn(opts, nil, append(common.FromHex(c.src.Bin), input...))
if err != nil {
return common.Address{}, nil, err
}
c.address = crypto.CreateAddress(opts.From, tx.Nonce())
c.BoundContract = bind.NewBoundContract(c.address, c.abi, c.client, c.client, c.client)
return c.address, tx, nil
}
// Transact invokes the (paid) contract method with params as input values.
func (c *BoundContract) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) {
if c.transactFn == nil {
return c.BoundContract.Transact(opts, method, params...)
}
input, err := c.abi.Pack(method, params...)
if err != nil {
return nil, err
}
return c.transactFn(opts, &c.address, input)
}
// Transfer initiates a plain transaction to move funds to the contract, calling
// its default method if one is available.
func (c *BoundContract) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) {
if c.transactFn == nil {
return c.BoundContract.Transfer(opts)
}
return c.transactFn(opts, &c.address, nil)
}