returning empty pointer instead of error if topic not found

This commit is contained in:
JFO 2019-05-22 17:24:05 +02:00
parent 9eff6b80bb
commit d1a828ca31
2 changed files with 10 additions and 11 deletions

View file

@ -148,13 +148,12 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
}
// EventById looks up a event by the topic hash
// returns nil if none found
func (abi *ABI) EventById(topic common.Hash) (*Event, error) {
// EventById looks up for events by the topic hash
func (abi *ABI) EventById(topic common.Hash) *Event {
for _, event := range abi.Events {
if event.Id() == topic {
return &event, nil
return &event
}
}
return nil, fmt.Errorf("no event with topic: %#x", topic.Hex())
return nil
}

View file

@ -762,10 +762,9 @@ func TestABI_EventById(t *testing.T) {
topic := "received(address,uint256,bytes)"
topicID := crypto.Keccak256Hash([]byte(topic))
event, err := abi.EventById(topicID)
if err != nil {
t.Fatalf("Failed to look up ABI event: %v", err)
event := abi.EventById(topicID)
if event == nil {
t.Fatalf("Failed to look up ABI event for topic %s", topicID.Hex())
}
if event.Id() != topicID {
@ -773,7 +772,8 @@ func TestABI_EventById(t *testing.T) {
}
unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent"))
if _, err := abi.EventById(unknowntopicID); err == nil {
t.Errorf("Expected error, no matching event id")
unknownEvent := abi.EventById(unknowntopicID)
if unknownEvent != nil {
t.Fatalf("We should not find any event for the topic %s", unknowntopicID.Hex())
}
}