Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support events with the same topic from different contracts #38

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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