Skip to content

fix(viem-adapter): getChainId returns 0 without a static chain config - #994

Open
gomesalexandre wants to merge 1 commit into
cowprotocol:mainfrom
gomesalexandre:fix_viem_getchainid_static_chain
Open

fix(viem-adapter): getChainId returns 0 without a static chain config#994
gomesalexandre wants to merge 1 commit into
cowprotocol:mainfrom
gomesalexandre:fix_viem_getchainid_static_chain

Conversation

@gomesalexandre

@gomesalexandre gomesalexandre commented Sep 5, 2026

Copy link
Copy Markdown

Does what it says on the box.

ViemAdapter.getChainId() reads this._publicClient.chain?.id ?? 0 - a static property that is undefined whenever the PublicClient is built without a chain (a valid, common setup: createPublicClient({ transport: http(url) }) for a custom/unlisted RPC endpoint), silently returning chain id 0 instead of the real network id.

Both EthersV5Adapter.getChainId() and EthersV6Adapter.getChainId() instead derive the chain id from a live network call (provider.getNetwork()). Viem itself exposes the matching live action for exactly this - publicClient.getChainId(), which calls eth_chainId on the RPC - and it's already available on the PublicClient type this adapter holds, just wasn't being used.

Fix

Swap this._publicClient.chain?.id ?? 0 for this._publicClient.getChainId(), matching the ethers adapters' live-query semantics and viem's own idiomatic API.

Test

Added packages/contracts-ts/tests/viemAdapterGetChainId.test.ts: constructs a ViemAdapter over a PublicClient with no chain passed (only transport), and asserts getChainId() still returns the real chain id via the live RPC call.

  • Fails on old code: Expected: 11155111, Received: 0
  • Passes on the fix

Ran the full contracts-ts suite locally after the change - 75/75 passing, no regressions. tsc --noEmit and eslint both clean on sdk-viem-adapter.

Risk

Low - one-line swap to an existing, already-typed viem action; only touches a path where the caller omitted chain from their PublicClient, which previously produced a silently wrong (0) value.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected chain ID detection for Viem-based connections without a predefined chain configuration.
    • Chain IDs are now retrieved from the connected network, preventing an incorrect value of 0.

…in config

ViemAdapter.getChainId() read `this._publicClient.chain?.id ?? 0` -
a purely static property that is undefined whenever the PublicClient
is built without a `chain` (e.g. `createPublicClient({ transport:
http(url) })`, a common setup for a custom/unlisted RPC endpoint).
Both ethers adapters instead derive the chain id from a live network
call (`provider.getNetwork()`), and viem itself exposes the matching
live action - `publicClient.getChainId()` (eth_chainId RPC) - which
this adapter already has available and simply wasn't using.

Swap to the live call so viem matches its ethers-v5/ethers-v6
siblings and stops silently reporting chain id 0.
@coderabbitai

coderabbitai Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: 8fd7bc57-a321-4193-925b-92318fb6cd36

📥 Commits

Reviewing files that changed from the base of the PR and between 9057b6d and 92bc42a.

📒 Files selected for processing (2)
  • packages/contracts-ts/tests/viemAdapterGetChainId.test.ts
  • packages/providers/viem-adapter/src/ViemAdapter.ts

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

ViemAdapter.getChainId() now queries the live RPC through viem. A test verifies the actual chain ID when the client has no static chain configuration.

Changes

Viem chain ID resolution

Layer / File(s) Summary
Live chain ID lookup and validation
packages/providers/viem-adapter/src/ViemAdapter.ts, packages/contracts-ts/tests/viemAdapterGetChainId.test.ts
getChainId() now calls viem’s RPC-backed getChainId(). The test covers a client without a configured chain and checks the live chain ID.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 92bc4

Viem clients without static chain configuration now return the RPC’s actual chain ID rather than zero. The affected behavior is covered by an RPC-only regression test, with no remaining merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the Viem adapter bug: getChainId returns 0 when no static chain configuration exists. It directly matches the main code change and regression test.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/contracts-ts/tests/viemAdapterGetChainId.test.ts

Parsing error: Unable to parse the specified 'tsconfig' file. Ensure it's correct and has valid syntax.

packages/app-data/tsconfig.json(2,14): error TS6053: File '@cow-sdk/typescript-config/base.json' not found.

packages/providers/viem-adapter/src/ViemAdapter.ts

Parsing error: Unable to parse the specified 'tsconfig' file. Ensure it's correct and has valid syntax.

packages/app-data/tsconfig.json(2,14): error TS6053: File '@cow-sdk/typescript-config/base.json' not found.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant