Skip to content

fix(parseEventLogs): normalize RpcLog inputs before decoding#4346

Merged
jxom merged 2 commits intowevm:mainfrom
Kropiunig:fix/parseEventLogs-format-rpc-log-inputs
Feb 23, 2026
Merged

fix(parseEventLogs): normalize RpcLog inputs before decoding#4346
jxom merged 2 commits intowevm:mainfrom
Kropiunig:fix/parseEventLogs-format-rpc-log-inputs

Conversation

@Kropiunig
Copy link
Copy Markdown
Contributor

Problem

parseEventLogs accepts (Log | RpcLog)[] in its type signature, but never normalizes RpcLog entries before processing them. When logs come directly from a JSON-RPC response (e.g. eth_getLogs), QUANTITY fields like blockNumber, logIndex, transactionIndex, and blockTimestamp are hex-encoded strings (0x...) per the Ethereum JSON-RPC spec.

These raw hex strings propagate into the returned Log objects, causing a runtime type violation: the TypeScript signature promises Log<bigint, number> but consumers receive hex strings at runtime. This can silently break downstream comparisons, arithmetic, and serialization that assumes native types.

Reproduction

const rpcLogs = await fetch(rpcUrl, {
  method: 'POST',
  body: JSON.stringify({
    jsonrpc: '2.0',
    method: 'eth_getLogs',
    params: [{ fromBlock: '0x0', toBlock: 'latest', address: contractAddress }],
    id: 1,
  }),
}).then(r => r.json()).then(r => r.result)

const parsed = parseEventLogs({ abi: myAbi, logs: rpcLogs })
console.log(typeof parsed[0].blockNumber) // "string" — should be "bigint"

Solution

Detect RpcLog inputs via typeof log.blockNumber === 'string' and normalize them through the existing formatLog utility before decoding. This converts hex-encoded QUANTITY fields to their native types (bigint for block numbers/timestamps, number for indices). Already-formatted Log inputs pass through unchanged.

This is the same normalization pattern used throughout viem's action layer (e.g. getBlock, getTransactionReceipt) but was missing from parseEventLogs.

Changes

  • src/utils/abi/parseEventLogs.ts: Added formatLog import and RpcLog detection/normalization at the start of the .map() callback. All subsequent references use the normalized log.
  • src/utils/abi/parseEventLogs.test.ts: Added test suite covering:
    • Hex-encoded RpcLog fields are correctly formatted to native types
    • Already-formatted Log inputs pass through unchanged (no double-formatting)
    • Mixed RpcLog and Log inputs in the same array

Fixes #4340

parseEventLogs accepts (Log | RpcLog)[] but never called formatLog on
RpcLog entries, causing hex-encoded QUANTITY fields (blockNumber,
logIndex, transactionIndex, blockTimestamp) to propagate into the
returned Log objects as raw strings instead of bigint/number.

This is a runtime type violation: the TypeScript signature promises
Log<bigint, number> but the actual values are hex strings when logs
come directly from an RPC response (e.g. eth_getLogs).

Detect RpcLog inputs via typeof blockNumber === 'string' and normalize
them through formatLog before processing. Already-formatted Log inputs
pass through unchanged.

Fixes wevm#4340
@vercel
Copy link
Copy Markdown

vercel bot commented Feb 17, 2026

@Kropiunig is attempting to deploy a commit to the Wevm Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Feb 17, 2026

🦋 Changeset detected

Latest commit: 53579db

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
viem Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Kropiunig
Copy link
Copy Markdown
Contributor Author

Hi @jxom — pinging for visibility. This PR normalizes RpcLog inputs before decoding in parseEventLogs, fixing a crash when logs contain extra fields. CI is passing. Would appreciate a review when you have a moment!

Amp-Thread-ID: https://ampcode.com/threads/T-019c886f-c262-7006-8da6-f79582ed6449
Co-authored-by: Amp <amp@ampcode.com>
@jxom jxom merged commit 45ff3ff into wevm:main Feb 23, 2026
2 of 3 checks passed
@github-actions github-actions bot mentioned this pull request Feb 23, 2026
ajhodges pushed a commit to ajhodges/viem that referenced this pull request Mar 4, 2026
* fix(parseEventLogs): normalize RpcLog inputs before decoding

parseEventLogs accepts (Log | RpcLog)[] but never called formatLog on
RpcLog entries, causing hex-encoded QUANTITY fields (blockNumber,
logIndex, transactionIndex, blockTimestamp) to propagate into the
returned Log objects as raw strings instead of bigint/number.

This is a runtime type violation: the TypeScript signature promises
Log<bigint, number> but the actual values are hex strings when logs
come directly from an RPC response (e.g. eth_getLogs).

Detect RpcLog inputs via typeof blockNumber === 'string' and normalize
them through formatLog before processing. Already-formatted Log inputs
pass through unchanged.

Fixes wevm#4340

* add changeset

Amp-Thread-ID: https://ampcode.com/threads/T-019c886f-c262-7006-8da6-f79582ed6449
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: jxom <7336481+jxom@users.noreply.github.com>
Co-authored-by: Amp <amp@ampcode.com>
@tmm tmm mentioned this pull request Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Typing on parseEventLogs

2 participants