-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlog_event.go
More file actions
220 lines (179 loc) · 5.99 KB
/
log_event.go
File metadata and controls
220 lines (179 loc) · 5.99 KB
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
216
217
218
219
220
// Copyright 2021 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eth
import (
"fmt"
"strings"
)
type Log struct {
Address []byte `json:"address,omitempty"`
Topics [][]byte `json:"topics,omitempty"`
Data []byte `json:"data,omitempty"`
IsAnonymous bool `json:"anonymous,omitempty"`
// supplement
Index uint32 `json:"index,omitempty"`
BlockIndex uint32 `json:"blockIndex,omitempty"`
}
type LogEventDef struct {
Name string
Parameters []*LogParameter
}
type LogParameter struct {
Name string
TypeName string
Type SolidityType
Indexed bool
// Components represents that struct fields of a particular tuple. Only
// filled up when `TypeName` is equal to `tuple` (or array of tuples).
//
// TODO: This is a blind copy from Method as I think the same concept applies
// to event, needs to validate using a test in `eth-go`.
Components []*StructComponent
}
// Signature returns the canonical type signature for this parameter,
// recursively expanding nested tuples.
func (p *LogParameter) Signature() string {
typeName := p.TypeName
if strings.HasPrefix(typeName, "tuple") {
componentSigs := make([]string, len(p.Components))
for i, component := range p.Components {
componentSigs[i] = component.Signature()
}
typeName = strings.Replace(typeName, "tuple", fmt.Sprintf("(%s)", strings.Join(componentSigs, ",")), 1)
}
return typeName
}
// returned instantiate a new event from the log definition and uses
// the received arguments as elements used to resolve the parameters
// values (indexed and non-indexed).
//
// A call is a particular instance of a Method where ultimately the
// parameters's value will be resolved. A method call in opposition
// to a method definition can be encoded according to Ethereum rules
// or decode data returned for this call against the definition.
func (f *LogEventDef) NewEvent(args ...interface{}) *LogEvent {
event := &LogEvent{Def: f}
if len(args) > 0 {
event.Data = make([]interface{}, len(args))
}
for i, arg := range args {
event.Data[i] = arg
}
return event
}
// NewCallFromString works exactly like `NewCall“ except that it
// actually assumes all arguments are string version of the actual
// Ethereum types defined by the method and append them to the Data
// slice by calling `AppendArgFromString` which converts the string
// representation to the correct type.
func (f *LogEventDef) NewEventFromString(args ...string) *LogEvent {
event := &LogEvent{Def: f}
for _, arg := range args {
event.AppendArgFromString(arg)
}
return event
}
type LogEvent struct {
Def *LogEventDef
Data []interface{}
err []error
}
func (f *LogEvent) AppendArgFromString(v string) {
i := len(f.Data)
if i >= len(f.Def.Parameters) {
f.err = append(f.err, fmt.Errorf("args exceeds method definition parameter count %d", len(f.Def.Parameters)))
return
}
param := f.Def.Parameters[i]
out, err := argToDataType(v, param.TypeName, param.Components)
if err != nil {
f.err = append(f.err, fmt.Errorf("invalid string argument %q for parameter %s: %w", param.Name, v, err))
return
}
f.Data = append(f.Data, out)
}
func (f *LogEvent) MustEncode() (topics []Topic, data []byte) {
topics, data, err := f.Encode()
if err != nil {
panic(fmt.Errorf("unable to encode log event: %w", err))
}
return topics, data
}
func (f *LogEvent) Encode() (topics []Topic, data []byte, err error) {
if len(f.err) > 0 {
return nil, nil, fmt.Errorf("%s", f.err)
}
if len(f.Data) != len(f.Def.Parameters) {
return nil, nil, fmt.Errorf("event has %d parameters but %d were provided", len(f.Def.Parameters), len(f.Data))
}
var indexedParameters []*LogParameter
var indexesFieldValues []interface{}
var unindexedParameters []*LogParameter
var unindexesDataValues []interface{}
for i, param := range f.Def.Parameters {
if param.Indexed {
indexedParameters = append(indexedParameters, param)
indexesFieldValues = append(indexesFieldValues, f.Data[i])
} else {
unindexedParameters = append(unindexedParameters, param)
unindexesDataValues = append(unindexesDataValues, f.Data[i])
}
}
topics = make([]Topic, len(indexedParameters)+1)
copy(topics[0][:], f.Def.LogID())
if len(indexedParameters) > 0 {
for i, param := range indexedParameters {
enc := NewEncoder()
if err := enc.WriteLogParameter(param, indexesFieldValues[i]); err != nil {
return nil, nil, fmt.Errorf("unable to encode %q (topic%d): %w", param.Name, i+1, err)
}
copy(topics[i+1][:], enc.Buffer())
}
}
enc := NewEncoder()
err = enc.WriteLogData(unindexedParameters, unindexesDataValues)
if err != nil {
return nil, nil, fmt.Errorf("unable to encode log data: %w", err)
}
return topics, enc.Buffer(), err
}
func (p *LogParameter) GetName(index int) string {
name := p.Name
if name == "" {
name = fmt.Sprintf("unamed%d", index+1)
}
return name
}
func (l *LogEventDef) LogID() []byte {
return Keccak256([]byte(l.Signature()))
}
func (l *LogEventDef) Signature() string {
var args []string
for _, parameter := range l.Parameters {
args = append(args, parameter.Signature())
}
// It's important that no spaces is introduced
return fmt.Sprintf("%s(%s)", l.Name, strings.Join(args, ","))
}
func (l *LogEventDef) String() string {
var args []string
for i, parameter := range l.Parameters {
indexed := ""
if parameter.Indexed {
indexed = " indexed"
}
args = append(args, fmt.Sprintf("%s%s %s", parameter.TypeName, indexed, parameter.GetName(i)))
}
return fmt.Sprintf("%s(%s)", l.Name, strings.Join(args, ", "))
}