Skip to content

Commit

Permalink
feat: support events with the same topic from different contracts (#38)
Browse files Browse the repository at this point in the history
Currently, 2 events with the same topic but from different contracts are not
handled correctly, the first register handler callback is called for both
events. This commit makes the lookup key contains the both topic and contract
address to correctly find the handler callback for the event.
  • Loading branch information
minh-bq committed Oct 24, 2023
1 parent 13047bb commit 0c3cbf6
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ func (c *Controller) processBehindBlock(listener Listener, height, latestBlockHe
return nil
}

type eventMapKey struct {
topic common.Hash
address common.Address
}

func (c *Controller) processBatchLogs(listener Listener, fromHeight, toHeight uint64) uint64 {
var (
contractAddresses []common.Address
Expand All @@ -398,14 +403,19 @@ func (c *Controller) processBatchLogs(listener Listener, fromHeight, toHeight ui
}
addedContract := make(map[common.Address]struct{})
filteredMethods := make(map[*abi.ABI]map[string]struct{})
eventIds := make(map[common.Hash]string)
eventIds := make(map[eventMapKey]string)
for subscriptionName, subscription := range listener.GetSubscriptions() {
name := subscription.Handler.Name
if filteredMethods[subscription.Handler.ABI] == nil {
filteredMethods[subscription.Handler.ABI] = make(map[string]struct{})
}
filteredMethods[subscription.Handler.ABI][name] = struct{}{}
eventIds[subscription.Handler.ABI.Events[name].ID] = subscriptionName

key := eventMapKey{
topic: subscription.Handler.ABI.Events[name].ID,
address: common.HexToAddress(subscription.To),
}
eventIds[key] = subscriptionName
contractAddress := common.HexToAddress(subscription.To)

if _, ok := addedContract[contractAddress]; !ok {
Expand Down Expand Up @@ -444,7 +454,12 @@ func (c *Controller) processBatchLogs(listener Listener, fromHeight, toHeight ui
for i, eventLog := range logs {
eventId := eventLog.Topics[0]
log.Trace("[Controller][processBatchLogs] processing log", "topic", eventLog.Topics[0].Hex(), "address", eventLog.Address.Hex(), "transaction", eventLog.TxHash.Hex(), "listener", listener.GetName())
if _, ok := eventIds[eventId]; !ok {
lookupKey := eventMapKey{
topic: eventId,
address: eventLog.Address,
}

if _, ok := eventIds[lookupKey]; !ok {
continue
}
data, err := json.Marshal(eventLog)
Expand All @@ -453,7 +468,7 @@ func (c *Controller) processBatchLogs(listener Listener, fromHeight, toHeight ui
continue
}
processedBlocks[eventLog.BlockNumber] = struct{}{}
name := eventIds[eventId]
name := eventIds[lookupKey]
tx := NewEmptyTransaction(chainId, eventLog.TxHash, eventLog.Data, nil, &eventLog.Address)
jobs = append(jobs, listener.GetListenHandleJob(name, tx, eventId.Hex(), data))
}
Expand Down

0 comments on commit 0c3cbf6

Please sign in to comment.