github.com/ethersphere/bee/v2@v2.2.0/pkg/transaction/event.go (about) 1 // Copyright 2021 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package transaction 6 7 import ( 8 "errors" 9 10 "github.com/ethereum/go-ethereum/accounts/abi" 11 "github.com/ethereum/go-ethereum/common" 12 "github.com/ethereum/go-ethereum/core/types" 13 ) 14 15 var ( 16 ErrEventNotFound = errors.New("event not found") 17 ErrNoTopic = errors.New("no topic") 18 ) 19 20 // ParseEvent will parse the specified abi event from the given log 21 func ParseEvent(a *abi.ABI, eventName string, c interface{}, e types.Log) error { 22 if len(e.Topics) == 0 { 23 return ErrNoTopic 24 } 25 if len(e.Data) > 0 { 26 if err := a.UnpackIntoInterface(c, eventName, e.Data); err != nil { 27 return err 28 } 29 } 30 var indexed abi.Arguments 31 for _, arg := range a.Events[eventName].Inputs { 32 if arg.Indexed { 33 indexed = append(indexed, arg) 34 } 35 } 36 return abi.ParseTopics(c, indexed, e.Topics[1:]) 37 } 38 39 // FindSingleEvent will find the first event of the given kind. 40 func FindSingleEvent(abi *abi.ABI, receipt *types.Receipt, contractAddress common.Address, event abi.Event, out interface{}) error { 41 if receipt.Status != 1 { 42 return ErrTransactionReverted 43 } 44 for _, log := range receipt.Logs { 45 if log.Address != contractAddress { 46 continue 47 } 48 if len(log.Topics) == 0 { 49 continue 50 } 51 if log.Topics[0] != event.ID { 52 continue 53 } 54 55 return ParseEvent(abi, event.Name, out, *log) 56 } 57 return ErrEventNotFound 58 }