fix(viem-adapter): getChainId returns 0 without a static chain config - #994
fix(viem-adapter): getChainId returns 0 without a static chain config#994gomesalexandre wants to merge 1 commit into
Conversation
…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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthrough
ChangesViem chain ID resolution
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to 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)
✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Some tools did not complete. Review the errors below. 🔧 ESLint
packages/contracts-ts/tests/viemAdapterGetChainId.test.tsParsing 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 ' packages/providers/viem-adapter/src/ViemAdapter.tsParsing 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 ' 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. Comment |
Does what it says on the box.
ViemAdapter.getChainId()readsthis._publicClient.chain?.id ?? 0- a static property that isundefinedwhenever thePublicClientis built without achain(a valid, common setup:createPublicClient({ transport: http(url) })for a custom/unlisted RPC endpoint), silently returning chain id0instead of the real network id.Both
EthersV5Adapter.getChainId()andEthersV6Adapter.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 callseth_chainIdon the RPC - and it's already available on thePublicClienttype this adapter holds, just wasn't being used.Fix
Swap
this._publicClient.chain?.id ?? 0forthis._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 aViemAdapterover aPublicClientwith nochainpassed (onlytransport), and assertsgetChainId()still returns the real chain id via the live RPC call.Expected: 11155111, Received: 0Ran the full
contracts-tssuite locally after the change - 75/75 passing, no regressions.tsc --noEmitandeslintboth clean onsdk-viem-adapter.Risk
Low - one-line swap to an existing, already-typed viem action; only touches a path where the caller omitted
chainfrom theirPublicClient, which previously produced a silently wrong (0) value.Summary by CodeRabbit
0.