-
Notifications
You must be signed in to change notification settings - Fork 16
/
ethcontract.go
60 lines (51 loc) · 1.79 KB
/
ethcontract.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
package ethcontract
import (
"fmt"
"github.com/0xsequence/ethkit"
"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
"github.com/0xsequence/ethkit/go-ethereum/common"
)
type Contract struct {
*bind.BoundContract
Address common.Address
ABI abi.ABI
}
func NewContractCaller(address common.Address, abi abi.ABI, caller bind.ContractCaller) *Contract {
return NewContract(address, abi, caller, nil, nil)
}
func NewContractTransactor(address common.Address, abi abi.ABI, caller bind.ContractCaller, transactor bind.ContractTransactor) *Contract {
return NewContract(address, abi, caller, transactor, nil)
}
func NewContractFilterer(address common.Address, abi abi.ABI, filterer bind.ContractFilterer) *Contract {
return NewContract(address, abi, nil, nil, filterer)
}
func NewContract(address common.Address, abi abi.ABI, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) *Contract {
contract := &Contract{
BoundContract: bind.NewBoundContract(address, abi, caller, transactor, filterer),
Address: address,
ABI: abi,
}
return contract
}
func (c *Contract) Encode(method string, args ...interface{}) ([]byte, error) {
m, ok := c.ABI.Methods[method]
if !ok {
return nil, fmt.Errorf("contract method %s not found", method)
}
input, err := m.Inputs.Pack(args...)
if err != nil {
return nil, err
}
input = append(m.ID, input...)
return input, nil
}
func (c *Contract) EventTopicHash(eventName string) (ethkit.Hash, error) {
ev, ok := c.ABI.Events[eventName]
if !ok {
return ethkit.Hash{}, fmt.Errorf("ethcontract: event '%s' not found in contract abi", eventName)
}
h := ethcoder.Keccak256Hash([]byte(ev.Sig))
return h, nil
}