Skip to content
Closed
Show file tree
Hide file tree
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
84 changes: 84 additions & 0 deletions src/utils/abi/encodeEventTopics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,90 @@ test("errors: event doesn't exist", () => {
`)
})

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 },
{ name: 'topic2', type: 'bytes32', indexed: true },
{ name: 'topic3', 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 },
{ name: 'topic2', type: 'bytes32', indexed: true },
{ name: 'topic3', type: 'bytes32', indexed: true },
],
},
] as const,
args: {
topic0:
'0x000000000000000000000000000000000000000000000000000000000000abcd',
},
}),
).toEqual([
'0x000000000000000000000000000000000000000000000000000000000000abcd',
null,
null,
null,
])
})

test('anonymous event: all topics', () => {
expect(
encodeEventTopics({
abi: [
{
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,
args: {
topic0:
'0x0000000000000000000000000000000000000000000000000000000000000000',
topic1:
'0x0000000000000000000000000000000000000000000000000000000000000001',
topic2:
'0x0000000000000000000000000000000000000000000000000000000000000002',
topic3:
'0x0000000000000000000000000000000000000000000000000000000000000003',
},
}),
).toEqual([
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000001',
'0x0000000000000000000000000000000000000000000000000000000000000002',
'0x0000000000000000000000000000000000000000000000000000000000000003',
])
})

test('https://github.com/wevm/viem/issues/3278', () => {
const bugAbi = [
{
Expand Down
12 changes: 8 additions & 4 deletions src/utils/abi/encodeEventTopics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export type EncodeEventTopicsParameters<
> &
(hasEvents extends true ? unknown : never)

export type EncodeEventTopicsReturnType = [Hex, ...(Hex | Hex[] | null)[]]
export type EncodeEventTopicsReturnType =
| [Hex, ...(Hex | Hex[] | null)[]]
| (Hex | Hex[] | null)[]

export type EncodeEventTopicsErrorType =
| AbiEventNotFoundErrorType
Expand Down Expand Up @@ -94,9 +96,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(
Expand All @@ -121,6 +120,11 @@ export function encodeEventTopics<
}) ?? []
}
}

if ('anonymous' in abiItem && abiItem.anonymous) return topics

const definition = formatAbiItem(abiItem)
const signature = toEventSelector(definition as EventDefinition)
return [signature, ...topics]
}

Expand Down