Skip to content

encodeEventTopics() (and watchLogs()) do not handle anonymous events #4461

Closed

Description

@KartikSoneji

Check existing issues

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.

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

  1. Zelys-DFKH commented on Apr 5, 2026

    @Zelys-DFKH

    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.

  2. KartikSoneji commented on Apr 5, 2026

    @KartikSoneji
    Author

    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.

  3. Zelys-DFKH commented on Apr 5, 2026

    @Zelys-DFKH

    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.

  4. KartikSoneji commented on Apr 21, 2026

    @KartikSoneji
    Author

    Hi, gentle bump on this.

  5. github-actions commented on Sep 2, 2026

    @github-actions
    Contributor

    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.

  6. locked as resolved and limited conversation to collaborators on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions