Feature request: Expand event filters to support multiple events #287
Replies: 4 comments 1 reply
-
|
What about a separate function, like Design-wise, I'm not sure what the viem team prefers. |
Beta Was this translation helpful? Give feedback.
-
|
+1 on this |
Beta Was this translation helpful? Give feedback.
-
|
Do we have any updates on this? Is it possible to get more complex filtering using viem? For example, in ethers, we can do something like the code below using event filters. How can we achieve the same thing using viem? |
Beta Was this translation helpful? Give feedback.
-
|
+1, would being able to specify arbitrary log topics would allow multiple filters to be "collapsed" together. For example, a contract with: event AdminSet(address indexed user);
event AdminRevoked(address indexed user);could filter events by A workaround is to "trick" the filter by using an anonymous event with four indexed client.watchEvent({
event: {
name: 'RawEvent',
type: 'event',
anonymous: true,
inputs: [
{ name: 'topic0', type: 'bytes32', indexed: true },
{ name: 'topic1', type: 'bytes32', indexed: true },
{ name: 'topic2', type: 'bytes32', indexed: true },
{ name: 'topic3', type: 'bytes32', indexed: true },
],
},
args: {
topic1: [paddedUserAddress]
},
onLogs: console.log,
});But that doesn't work in the current version ( |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Today, the event filter methods (
watchEvent,createEventFilter,getFilterLogs,getLogs) support only one kind of topic set: one event signature, (optional) indexed argument values.There is another potentially useful kind of event filter - one that specifies many event signatures and no indexed argument values. For example, this could be used to filter for every ERC20
TransferandApprovalevent on the network, regardless of contract address. The topic set sent to the RPC provider in this case looks like[ [TransferSigHash, ApprovalSigHash] ].I can think of two ways Viem could add support for this:
topicsoption to every public client method that builds an event filter.topicswould be mutually exclusive with the currentevent/argsoptions.(pseudotype)
type EventLogFilterParameters< TAbiEvent extends AbiEvent | undefined = undefined, TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent> >= { address?: Address | Address[] } & ( | { event: TAbiEvent args?: MaybeExtractEventArgsFromAbi<[TAbiEvent], TEventName> + topics?: never } | { event?: never args?: never + topics?: never } + | { + event?: never + args?: never + topics: (Hex | Hex[] | null)[] + } )eventsoption to explicitly support the multiple events, no arguments pattern.type EventLogFilterParameters< TAbiEvent extends AbiEvent | undefined = undefined, TEventName extends string | undefined = MaybeAbiEventName<TAbiEvent> >= { address?: Address | Address[] } & ( | { event: TAbiEvent args?: MaybeExtractEventArgsFromAbi<[TAbiEvent], TEventName> + events?: never } | { event?: never args?: never + events?: never } + | { + event?: never + args?: never + events: TAbiEvent[] + } )I think it would be reasonable to also add these options to the Contract Instance event filter methods (
createContractEventFilter,watchContractEvent).cc @holic
Beta Was this translation helpful? Give feedback.
All reactions