forked from lmittmann/w3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
96 lines (83 loc) · 2.62 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
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
package w3
import (
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
_abi "github.com/lmittmann/w3/internal/abi"
)
// Event represents a Smart Contract event decoder.
type Event struct {
Signature string // Event signature
Topic0 common.Hash // Hash of event signature (Topic 0)
Args abi.Arguments // Arguments
indexedArgs abi.Arguments // Subset of Args that are indexed
}
// NewEvent returns a new Smart Contract event log decoder from the given
// Solidity event signature.
//
// An error is returned if the signature parsing fails.
func NewEvent(signature string) (*Event, error) {
name, args, err := _abi.ParseWithName(signature)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidABI, err)
}
if name == "" {
return nil, fmt.Errorf("%w: missing event name", ErrInvalidABI)
}
indexedArgs := make(abi.Arguments, 0)
for _, arg := range args {
if arg.Indexed {
arg.Indexed = false
indexedArgs = append(indexedArgs, arg)
}
}
sig := args.SignatureWithName(name)
return &Event{
Signature: sig,
Topic0: crypto.Keccak256Hash([]byte(sig)),
Args: abi.Arguments(args),
indexedArgs: indexedArgs,
}, nil
}
// MustNewEvent is like [NewEvent] but panics if the signature parsing fails.
func MustNewEvent(signature string) *Event {
event, err := NewEvent(signature)
if err != nil {
panic(err)
}
return event
}
// DecodeArgs decodes the topics and data of the given log to the given args.
func (e *Event) DecodeArgs(log *types.Log, args ...any) error {
if len(log.Topics) <= 0 || e.Topic0 != log.Topics[0] {
return fmt.Errorf("w3: topic0 mismatch")
}
if len(e.Args) != len(args) {
return fmt.Errorf("%w: expected %d arguments, got %d", ErrArgumentMismatch, len(e.Args), len(args))
}
if len(e.indexedArgs) != len(log.Topics)-1 {
return fmt.Errorf("%w: expected %d indexed arguments, got %d", ErrArgumentMismatch, len(e.indexedArgs), len(log.Topics)-1)
}
indexedArgs := make([]any, 0, len(e.indexedArgs))
nonIndexedArgs := make([]any, 0, len(e.Args)-len(e.indexedArgs))
for i, arg := range e.Args {
if arg.Indexed {
indexedArgs = append(indexedArgs, args[i])
} else {
nonIndexedArgs = append(nonIndexedArgs, args[i])
}
}
// decode indexed args
for i, arg := range indexedArgs {
if err := (_abi.Arguments{e.indexedArgs[i]}).Decode(log.Topics[i+1][:], arg); err != nil {
return err
}
}
// decode non-indexed args
if err := _abi.Arguments(e.Args).Decode(log.Data, nonIndexedArgs...); err != nil {
return err
}
return nil
}