Skip to content

Commit 5b792e0

Browse files
authored
accounts/abi: add ErrorById (#27277)
Adds `ErrorById` lookup
1 parent b46d37e commit 5b792e0

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

accounts/abi/abi.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,17 @@ func (abi *ABI) EventByID(topic common.Hash) (*Event, error) {
222222
return nil, fmt.Errorf("no event with id: %#x", topic.Hex())
223223
}
224224

225+
// ErrorByID looks up an error by the 4-byte id,
226+
// returns nil if none found.
227+
func (abi *ABI) ErrorByID(sigdata [4]byte) (*Error, error) {
228+
for _, errABI := range abi.Errors {
229+
if bytes.Equal(errABI.ID[:4], sigdata[:]) {
230+
return &errABI, nil
231+
}
232+
}
233+
return nil, fmt.Errorf("no error with id: %#x", sigdata[:])
234+
}
235+
225236
// HasFallback returns an indicator whether a fallback function is included.
226237
func (abi *ABI) HasFallback() bool {
227238
return abi.Fallback.Type == Fallback

accounts/abi/abi_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,34 @@ func TestABI_EventById(t *testing.T) {
10571057
}
10581058
}
10591059

1060+
func TestABI_ErrorByID(t *testing.T) {
1061+
abi, err := JSON(strings.NewReader(`[
1062+
{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"MyError1","type":"error"},
1063+
{"inputs":[{"components":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"string","name":"b","type":"string"},{"internalType":"address","name":"c","type":"address"}],"internalType":"struct MyError.MyStruct","name":"x","type":"tuple"},{"internalType":"address","name":"y","type":"address"},{"components":[{"internalType":"uint256","name":"a","type":"uint256"},{"internalType":"string","name":"b","type":"string"},{"internalType":"address","name":"c","type":"address"}],"internalType":"struct MyError.MyStruct","name":"z","type":"tuple"}],"name":"MyError2","type":"error"},
1064+
{"inputs":[{"internalType":"uint256[]","name":"x","type":"uint256[]"}],"name":"MyError3","type":"error"}
1065+
]`))
1066+
if err != nil {
1067+
t.Fatal(err)
1068+
}
1069+
for name, m := range abi.Errors {
1070+
a := fmt.Sprintf("%v", &m)
1071+
var id [4]byte
1072+
copy(id[:], m.ID[:4])
1073+
m2, err := abi.ErrorByID(id)
1074+
if err != nil {
1075+
t.Fatalf("Failed to look up ABI error: %v", err)
1076+
}
1077+
b := fmt.Sprintf("%v", m2)
1078+
if a != b {
1079+
t.Errorf("Error %v (id %x) not 'findable' by id in ABI", name, id)
1080+
}
1081+
}
1082+
// test unsuccessful lookups
1083+
if _, err = abi.ErrorByID([4]byte{}); err == nil {
1084+
t.Error("Expected error: no error with this id")
1085+
}
1086+
}
1087+
10601088
// TestDoubleDuplicateMethodNames checks that if transfer0 already exists, there won't be a name
10611089
// conflict and that the second transfer method will be renamed transfer1.
10621090
func TestDoubleDuplicateMethodNames(t *testing.T) {

accounts/abi/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func NewError(name string, inputs Arguments) Error {
7878
}
7979
}
8080

81-
func (e *Error) String() string {
81+
func (e Error) String() string {
8282
return e.str
8383
}
8484

0 commit comments

Comments
 (0)