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]) return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
} }
// EventById looks up a event by the topic hash // EventById looks up for events by the topic hash
// returns nil if none found func (abi *ABI) EventById(topic common.Hash) *Event {
func (abi *ABI) EventById(topic common.Hash) (*Event, error) {
for _, event := range abi.Events { for _, event := range abi.Events {
if event.Id() == topic { 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)" topic := "received(address,uint256,bytes)"
topicID := crypto.Keccak256Hash([]byte(topic)) topicID := crypto.Keccak256Hash([]byte(topic))
event := abi.EventById(topicID)
event, err := abi.EventById(topicID) if event == nil {
if err != nil { t.Fatalf("Failed to look up ABI event for topic %s", topicID.Hex())
t.Fatalf("Failed to look up ABI event: %v", err)
} }
if event.Id() != topicID { if event.Id() != topicID {
@ -773,7 +772,8 @@ func TestABI_EventById(t *testing.T) {
} }
unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent")) unknowntopicID := crypto.Keccak256Hash([]byte("unknownEvent"))
if _, err := abi.EventById(unknowntopicID); err == nil { unknownEvent := abi.EventById(unknowntopicID)
t.Errorf("Expected error, no matching event id") if unknownEvent != nil {
t.Fatalf("We should not find any event for the topic %s", unknowntopicID.Hex())
} }
} }