-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathevent.go
215 lines (193 loc) · 5.89 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package abi
import (
"fmt"
"strings"
"github.com/defiweb/go-eth/crypto"
"github.com/defiweb/go-eth/types"
)
// Event represents an event in an ABI. The event can be used to decode events
// emitted by a contract.
type Event struct {
name string
inputs *EventTupleType
anonymous bool
abi *ABI
topic0 types.Hash
signature string
}
// NewEvent creates a new Event instance.
//
// This method is rarely used, see ParseEvent for a more convenient way to
// create a new Event.
func NewEvent(name string, inputs *EventTupleType, anonymous bool) *Event {
return Default.NewEvent(name, inputs, anonymous)
}
// ParseEvent parses an event signature and returns a new Event.
//
// An event signature is similar to a method signature, but returns no values.
// It can be optionally prefixed with the "event" keyword.
//
// The following examples are valid signatures:
//
// foo(int indexed,(uint256,bytes32)[])
// foo(int indexed a, (uint256 b, bytes32 c)[] d)
// event foo(int indexed a tuple(uint256 b, bytes32 c)[] d)
//
// This function is equivalent to calling Parser.ParseEvent with the default
// configuration.
func ParseEvent(signature string) (*Event, error) {
return Default.ParseEvent(signature)
}
// MustParseEvent is like ParseEvent but panics on error.
func MustParseEvent(signature string) *Event {
return Default.MustParseEvent(signature)
}
// NewEvent creates a new Event instance.
func (a *ABI) NewEvent(name string, inputs *EventTupleType, anonymous bool) *Event {
if inputs == nil {
inputs = NewEventTupleType()
}
e := &Event{
name: name,
inputs: inputs,
anonymous: anonymous,
abi: a,
}
e.generateSignature()
e.calculateTopic0()
return e
}
// ParseEvent parses an event signature and returns a new Event.
//
// See ParseEvent for more information.
func (a *ABI) ParseEvent(signature string) (*Event, error) {
return parseEvent(a, nil, signature)
}
// MustParseEvent is like ParseEvent but panics on error.
func (a *ABI) MustParseEvent(signature string) *Event {
e, err := a.ParseEvent(signature)
if err != nil {
panic(err)
}
return e
}
// Name returns the name of the event.
func (e *Event) Name() string {
return e.name
}
// Inputs returns the input arguments of the event as a tuple type.
func (e *Event) Inputs() *EventTupleType {
return e.inputs
}
// Topic0 returns the first topic of the event, that is, the Keccak256 hash of
// the event signature.
func (e *Event) Topic0() types.Hash {
return e.topic0
}
// Signature returns the event signature, that is, the event name and the
// canonical type of the input arguments.
func (e *Event) Signature() string {
return e.signature
}
// DecodeValue decodes the event into a map or structure. If a structure is
// given, it must have fields with the same names as the event arguments.
func (e *Event) DecodeValue(topics []types.Hash, data []byte, val any) error {
if e.anonymous {
return e.abi.DecodeValue(e.inputs, data, val)
}
if len(topics) != e.inputs.IndexedSize()+1 {
return fmt.Errorf("abi: wrong number of topics for event %s", e.name)
}
if topics[0] != e.topic0 {
return fmt.Errorf("abi: topic0 mismatch for event %s", e.name)
}
// The anymapper package does not zero out values before decoding into
// it, therefore we can decode topics and data into the same value.
if len(topics) > 1 {
if err := e.abi.DecodeValue(e.inputs.TopicsTuple(), hashSliceToBytes(topics[1:]), val); err != nil {
return err
}
}
if len(data) > 0 {
if err := e.abi.DecodeValue(e.inputs.DataTuple(), data, val); err != nil {
return err
}
}
return nil
}
// MustDecodeValue is like DecodeValue but panics on error.
func (e *Event) MustDecodeValue(topics []types.Hash, data []byte, val any) {
err := e.DecodeValue(topics, data, val)
if err != nil {
panic(err)
}
}
// DecodeValues decodes the event into a map or structure. If a structure is
// given, it must have fields with the same names as the event arguments.
func (e *Event) DecodeValues(topics []types.Hash, data []byte, vals ...any) error {
if e.anonymous {
return e.abi.DecodeValues(e.inputs, data, vals...)
}
if len(topics) != e.inputs.IndexedSize()+1 {
return fmt.Errorf("abi: wrong number of topics for event %s", e.name)
}
if topics[0] != e.topic0 {
return fmt.Errorf("abi: topic0 mismatch for event %s", e.name)
}
indexedVals := make([]any, 0, e.inputs.IndexedSize())
dataVals := make([]any, 0, e.inputs.DataSize())
for i := range e.inputs.Elements() {
if i >= len(vals) {
break
}
if e.inputs.Elements()[i].Indexed {
indexedVals = append(indexedVals, vals[i])
} else {
dataVals = append(dataVals, vals[i])
}
}
// The anymapper package does not zero out values before decoding into
// it, therefore we can decode topics and data into the same value.
if len(topics) > 1 {
if err := e.abi.DecodeValues(e.inputs.TopicsTuple(), hashSliceToBytes(topics[1:]), indexedVals...); err != nil {
return err
}
}
if len(data) > 0 {
if err := e.abi.DecodeValues(e.inputs.DataTuple(), data, dataVals...); err != nil {
return err
}
}
return nil
}
// MustDecodeValues is like DecodeValues but panics on error.
func (e *Event) MustDecodeValues(topics []types.Hash, data []byte, vals ...any) {
err := e.DecodeValues(topics, data, vals...)
if err != nil {
panic(err)
}
}
// String returns the human-readable signature of the event.
func (e *Event) String() string {
var buf strings.Builder
buf.WriteString("event ")
buf.WriteString(e.name)
buf.WriteString(e.inputs.String())
if e.anonymous {
buf.WriteString(" anonymous")
}
return buf.String()
}
func (e *Event) calculateTopic0() {
e.topic0 = types.Hash(crypto.Keccak256([]byte(e.signature)))
}
func (e *Event) generateSignature() {
e.signature = fmt.Sprintf("%s%s", e.name, e.inputs.CanonicalType())
}
func hashSliceToBytes(hashes []types.Hash) []byte {
buf := make([]byte, len(hashes)*types.HashLength)
for i, hash := range hashes {
copy(buf[i*types.HashLength:], hash[:])
}
return buf
}