-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathabi.go
More file actions
125 lines (97 loc) · 4.5 KB
/
abi.go
File metadata and controls
125 lines (97 loc) · 4.5 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
// 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 "go.uber.org/zap"
// ABI is our custom internal definition of a contract's ABI that bridges the information
// between the two ABI like formats for contract's, i.e. `.abi` file and AST file as output
// by `solc` compiler.
type ABI struct {
LogEventsMap map[string][]*LogEventDef
FunctionsMap map[string][]*MethodDef
ConstructorsMap map[string][]*ConstructorDef
LogEventsByNameMap map[string][]*LogEventDef
FunctionsByNameMap map[string][]*MethodDef
ConstructorsByNameMap map[string][]*ConstructorDef
}
// Deprecated Use FindLogByTopic
func (a *ABI) FindLog(topic []byte) *LogEventDef {
return a.FindLogByTopic(topic)
}
// FindLogByTopic finds the first log with the give `topic`. Multiple events could have the
// same topic, use `FindLogByTopic` to get them all.
func (a *ABI) FindLogByTopic(topic []byte) *LogEventDef {
zlog.Info("looking for first log by topic", zap.Stringer("topic", Hash(topic)))
return firstOr(a.LogEventsMap[string(topic)], nil)
}
// FindLogByTopic returns **all** logs with the give `topic`.
func (a *ABI) FindLogsByTopic(topic []byte) []*LogEventDef {
zlog.Info("looking for all logs by topic", zap.Stringer("topic", Hash(topic)))
return a.LogEventsMap[string(topic)]
}
// FindLogByName finds the first log with the give `topic`. Multiple events could have the
// same topic, use `FindLogsByName` to get them all.
func (a *ABI) FindLogByName(name string) *LogEventDef {
zlog.Info("looking for first log by name", zap.String("event_name", name))
return firstOr(a.LogEventsByNameMap[name], nil)
}
// FindLogsByName returns **all** logs with the give `topic`.
func (a *ABI) FindLogsByName(name string) []*LogEventDef {
zlog.Info("looking for all logs by name", zap.String("event_name", name))
return a.LogEventsByNameMap[name]
}
// Deprecated: Use FindFunctionByHash
func (a *ABI) FindFunction(functionHash []byte) *MethodDef {
return a.FindFunctionByHash(functionHash)
}
// FindFunctionByHash finds the function with the give `hash`.
func (a *ABI) FindFunctionByHash(functionHash []byte) *MethodDef {
zlog.Info("looking for first function by hash", zap.Stringer("method_hash", Hash(functionHash)))
return firstOr(a.FunctionsMap[string(functionHash)], nil)
}
// FindFunctionByName finds the first function with the give `name`. Multiple functions could have the
// same name, use `FindFunctionsByName` to get them all.
func (a *ABI) FindFunctionByName(name string) *MethodDef {
zlog.Info("looking for function by name", zap.Stringer("method_name", Hash(name)))
return firstOr(a.FunctionsByNameMap[name], nil)
}
// FindFunctionsByName returns **all** functions with the give `name`.
func (a *ABI) FindFunctionsByName(name string) []*MethodDef {
zlog.Info("looking for function by name", zap.Stringer("method_name", Hash(name)))
return a.FunctionsByNameMap[name]
}
// FindConstructor returns the first constructor defined in this ABI.
// Most contracts have a single constructor, so this is a convenience method.
func (a *ABI) FindConstructor() *ConstructorDef {
zlog.Info("looking for constructor")
return firstOr(a.ConstructorsByNameMap[""], nil)
}
// FindConstructors returns **all** constructors defined in this ABI.
// Note: While Solidity contracts can only have one constructor, ABIs parsed
// from multiple sources might contain multiple constructors.
func (a *ABI) FindConstructors() []*ConstructorDef {
zlog.Info("looking for all constructors")
return a.ConstructorsByNameMap[""]
}
// FindConstructorBySignature finds a constructor by its parameter signature.
// The signature should be in the format "(type1,type2,...)" e.g., "(address,uint256)".
func (a *ABI) FindConstructorBySignature(signature string) *ConstructorDef {
zlog.Info("looking for constructor by signature", zap.String("signature", signature))
return firstOr(a.ConstructorsMap[signature], nil)
}
func firstOr[T any](elements []T, defaultValue T) T {
if len(elements) == 0 {
return defaultValue
}
return elements[0]
}