-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
46 lines (40 loc) · 1.05 KB
/
event.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
package common
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/types"
"reflect"
)
type EVMEvent interface {
Name() string
GetLog() types.Log
}
type BaseEVMEvent struct {
log types.Log
}
func (e *BaseEVMEvent) GetLog() types.Log {
return e.log
}
// EVMEventHandler specifies method for an event handler.
type EVMEventHandler interface {
Handle(event EVMEvent) error
}
// UnpackLog unpacks a retrieved log into the provided output structure.
func UnpackLog(contractAbi abi.ABI, out interface{}, event string, log types.Log) error {
if log.Topics[0] != contractAbi.Events[event].ID {
return fmt.Errorf("event signature mismatch")
}
fmt.Println("UnpackLog", reflect.ValueOf(out).Kind())
if len(log.Data) > 0 {
if err := contractAbi.UnpackIntoInterface(out, event, log.Data); err != nil {
return err
}
}
var indexed abi.Arguments
for _, arg := range contractAbi.Events[event].Inputs {
if arg.Indexed {
indexed = append(indexed, arg)
}
}
return abi.ParseTopics(out, indexed, log.Topics[1:])
}