Repository navigation
Description
Check existing issues
- I checked there isn't already an issue for the bug I encountered.
Viem Version
2.47.10
Current Behavior
encodeEventTopics() does not handle anonymous events properly.
It incorrectly uses the signature as the first log topic.
Expected Behavior
To correctly encode the log topics.
Steps To Reproduce
const rawEventAbiItem = [
{
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 },
],
},
] as const
test('anonymous event: no args', () => {
expect(encodeEventTopics({ abi: rawEventAbiItem })).toEqual([])
})
test('anonymous event: topic0', () => {
expect(
encodeEventTopics({
abi: rawEventAbiItem,
args: {
topic0:
'0x000000000000000000000000000000000000000000000000000000000000abcd',
},
}),
).toEqual([
'0x000000000000000000000000000000000000000000000000000000000000abcd',
null,
null,
null,
])
})
test('anonymous event: all topics', () => {
expect(
encodeEventTopics({
abi: rawEventAbiItem,
args: {
topic0:
'0x0000000000000000000000000000000000000000000000000000000000000000',
topic1:
'0x0000000000000000000000000000000000000000000000000000000000000001',
topic2:
'0x0000000000000000000000000000000000000000000000000000000000000002',
topic3:
'0x0000000000000000000000000000000000000000000000000000000000000003',
},
}),
).toEqual([
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000002',
'0x0000000000000000000000000000000000000000000000000000000000000003',
])
})Link to Minimal Reproducible Example
No response
Anything else?
I came across this issue while trying to find a workaround to specify the log topics directly.
encodeEventTopics() does not check abiItem.anonymous before setting the sigature as topic0.
viem/src/utils/abi/encodeEventTopics.ts
Line 124 in 14f77b7
| return [signature, ...topics] |
The logic itself is trivial to fix:
diff --git a/src/utils/abi/encodeEventTopics.ts b/src/utils/abi/encodeEventTopics.ts
index 21415b5..c1c8a29 100644
--- a/src/utils/abi/encodeEventTopics.ts
+++ b/src/utils/abi/encodeEventTopics.ts
@@ -94,9 +94,6 @@ export function encodeEventTopics<
if (abiItem.type !== 'event')
throw new AbiEventNotFoundError(undefined, { docsPath })
- const definition = formatAbiItem(abiItem)
- const signature = toEventSelector(definition as EventDefinition)
-
let topics: (Hex | Hex[] | null)[] = []
if (args && 'inputs' in abiItem) {
const indexedInputs = abiItem.inputs?.filter(
@@ -121,7 +118,15 @@ export function encodeEventTopics<
}) ?? []
}
}
- return [signature, ...topics]
+
+ // only prepend signature for non-anonymous events
+ if (!abiItem.anonymous) {
+ const definition = formatAbiItem(abiItem)
+ const signature = toEventSelector(definition as EventDefinition)
+ topics.unshift(signature)
+ }
+
+ return topics
}
export type EncodeArgErrorType =But a bigger issue is the return type being [Hex, ...(Hex | Hex[] | null)[]], making an empty array fail the typecheck.
Not sure how that should be handled since changing the return type to [] can break existing code.
Maybe it can be inferred based on the type of event?
Activity
Zelys-DFKH commentedon Apr 5, 2026
Confirmed. encodeEventTopics computes and prepends the signature unconditionally — abiItem.anonymous is never checked. The runtime fix is a few lines. The return type ([Hex, ...(Hex | Hex[] | null)[]]) also needs to loosen since anonymous events can return an empty array. Happy to open a PR if that sounds right.
KartikSoneji commentedon Apr 5, 2026
I think the return type will have to be inferred based on the event definition.
Otherwise, existing code that assumes the return type of
encodeEventTopics()will always have at least one value will break.Zelys-DFKH commentedon Apr 5, 2026
Good catch on the return type. Updated the PR to infer it from the event definition — non-anonymous events keep
[Hex, ...(Hex | Hex[] | null)[]]and anonymous events get(Hex | Hex[] | null)[]. Also added type-level tests to cover both cases.KartikSoneji commentedon Apr 21, 2026
Hi, gentle bump on this.
github-actions commentedon Sep 2, 2026
This issue has been locked since it has been closed for more than 14 days.
If you found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Viem version. If you have any questions or comments you can create a new discussion thread.