diff --git a/src/utils/abi/encodeEventTopics.test.ts b/src/utils/abi/encodeEventTopics.test.ts index 78093eb10b..acbbbc0083 100644 --- a/src/utils/abi/encodeEventTopics.test.ts +++ b/src/utils/abi/encodeEventTopics.test.ts @@ -532,3 +532,46 @@ test('https://github.com/wevm/viem/issues/3278', () => { ] `) }) + +test('anonymous event: no args', () => { + expect( + encodeEventTopics({ + abi: [ + { + name: 'RawEvent', + type: 'event', + anonymous: true, + inputs: [ + { name: 'topic0', type: 'bytes32', indexed: true }, + { name: 'topic1', type: 'bytes32', indexed: true }, + ], + }, + ] as const, + }), + ).toEqual([]) +}) + +test('anonymous event: with args', () => { + expect( + encodeEventTopics({ + abi: [ + { + name: 'RawEvent', + type: 'event', + anonymous: true, + inputs: [ + { name: 'topic0', type: 'bytes32', indexed: true }, + { name: 'topic1', type: 'bytes32', indexed: true }, + ], + }, + ] as const, + args: { + topic0: + '0x000000000000000000000000000000000000000000000000000000000000abcd', + }, + }), + ).toEqual([ + '0x000000000000000000000000000000000000000000000000000000000000abcd', + null, + ]) +}) diff --git a/src/utils/abi/encodeEventTopics.ts b/src/utils/abi/encodeEventTopics.ts index 21415b5805..1741cad260 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,6 +118,12 @@ export function encodeEventTopics< }) ?? [] } } + + if ('anonymous' in abiItem && abiItem.anonymous) + return topics as (Hex | Hex[] | null)[] as EncodeEventTopicsReturnType + + const definition = formatAbiItem(abiItem) + const signature = toEventSelector(definition as EventDefinition) return [signature, ...topics] }