-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontract.go
More file actions
36 lines (30 loc) · 782 Bytes
/
contract.go
File metadata and controls
36 lines (30 loc) · 782 Bytes
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
package listener
import (
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)
type Contract struct {
Name string
ABI abi.ABI
Address common.Address
// event ID <-> event Name mapping
events map[common.Hash]string
}
func NewContract(name string, abiJSON string, address common.Address) (*Contract, error) {
c := &Contract{
Name: name,
Address: address,
events: make(map[common.Hash]string),
}
abi, err := abi.JSON(strings.NewReader(abiJSON))
if err != nil {
logger.Error("Failed to parse ABI interface", "name", name, "abi", abiJSON, "address", address.Hex(), "err", err)
return nil, err
}
c.ABI = abi
for _, event := range abi.Events {
c.events[event.Id()] = event.Name
}
return c, nil
}