Skip to content

Check chainid before viem client#49

Open
aniket866 wants to merge 2 commits intoDjedAlliance:mainfrom
aniket866:check-chainid-before-viem-client
Open

Check chainid before viem client#49
aniket866 wants to merge 2 commits intoDjedAlliance:mainfrom
aniket866:check-chainid-before-viem-client

Conversation

@aniket866
Copy link

@aniket866 aniket866 commented Jan 23, 2026

Sync Check Implemented:

Old Code: Always used await window.ethereum.request({ method: 'eth_chainId' }), forcing an asynchronous wait even if the data was already available.

  • New Code: const chainIdHex = window.ethereum.chainId || await ...

Benefit: It instantly grabs the chainId from the property if populated (which modern MetaMask versions do), effectively making the check synchronous in most cases and avoiding the network round-trip overhead.

@Zahnentferner Please review this PR

Closes #46

Summary by CodeRabbit

  • Chores
    • Optimized application performance by improving rendering efficiency and wallet connection responsiveness.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 23, 2026

📝 Walkthrough

Walkthrough

Enhances NetworkContext with memoized callbacks and context value using useMemo and useCallback, improving reference stability. Updates WalletContext to prioritize synchronous window.ethereum.chainId reads with async fallback for optimized chain ID retrieval. Both changes preserve external API and existing behavior.

Changes

Cohort / File(s) Summary
NetworkContext Memoization
stablepay-sdk/src/contexts/NetworkContext.jsx
Wraps resetState, selectNetwork, selectToken, and resetSelections in useCallback for stable function references; introduces useMemo to memoize context value object, reducing unnecessary Provider re-renders
WalletContext ChainId Optimization
stablepay-sdk/src/contexts/WalletContext.jsx
Updates ensureCorrectNetwork to prefer synchronous window.ethereum.chainId property read over async eth_chainId JSON-RPC request, providing faster chain ID resolution when available

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • StablePay#14: Modifies WalletContext's network and chain ID handling logic in wallet connection and chain-switch flows.
  • StablePay#38: Updates WalletContext's ensureCorrectNetwork function and how current chainId is derived from provider or NetworkContext.
  • StablePay#12: Rewrites WalletContext's network validation and chainId/account flows with provider chainId handling changes.

Suggested reviewers

  • Zahnentferner

Poem

🐰 Memos dance and callbacks bind,
Stable refs we've left behind,
ChainIds sync without the wait,
Faster paths make code so great!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Check chainid before viem client' directly summarizes the main optimization: reading window.ethereum.chainId as a property before falling back to an async request, which is the primary change in WalletContext.jsx and aligns with the PR's stated objective.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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 and usage tips.

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.

Optimization: Sync "Ensure Network" Logic

1 participant