Skip to content

feat: Add useEnsureCorrectChain hook for chain enforcement#144

Merged
Adebesin-Cell merged 3 commits into
mainfrom
feat/chain-switch
Feb 13, 2026
Merged

feat: Add useEnsureCorrectChain hook for chain enforcement#144
Adebesin-Cell merged 3 commits into
mainfrom
feat/chain-switch

Conversation

@Adebesin-Cell
Copy link
Copy Markdown
Member

@Adebesin-Cell Adebesin-Cell commented Feb 13, 2026

Summary

This PR introduces a new React hook, useEnsureCorrectChain, designed to simplify and standardize the process of ensuring a connected wallet is on the correct blockchain network.

The hook provides a clear status state machine and utilities for prompting network switches, significantly improving developer experience when building dApps that require specific chain interactions.

✨ What’s Included

1. New Hook: useEnsureCorrectChain.

A dedicated hook that manages and enforces that the connected wallet is on a required chain.

2. Unified Status Flow

The hook exposes a deterministic status state machine:

idle → wrong-network → switching → correct

This makes it easier to:

  • Drive UI states (banners, modals, disabled buttons)
  • Avoid ad-hoc network checks across components
  • Centralize chain validation logic

3. Programmatic Chain Switching

The hook provides:

  • switchToCorrectChain() — Prompts the wallet to switch to the required network.
  • dismiss() — Allows consumers to temporarily ignore the wrong-network state.

4. Status Change Callback

An optional onStatusChange callback enables reacting to transitions (e.g., open/close modals automatically).

Example use cases:

  • Auto-open a “Wrong Network” modal
  • Track analytics events on network switch
  • Trigger side effects when switching completes

5. Documentation

Added comprehensive README documentation including:

  • Basic usage example
  • Callback integration example
  • Full API reference

🎯 Why

Handling wallet network state is a common requirement across dApps. Previously, this logic had to be implemented manually in each feature or component.

This hook:

  • Reduces duplicated logic
  • Standardizes network enforcement patterns
  • Improves UI consistency
  • Simplifies onboarding for developers

🔄 Backwards Compatibility

No breaking changes.
This is a purely additive feature.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 13, 2026

🦋 Changeset detected

Latest commit: 7e147df

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

This PR includes changesets to release 1 package
Name Type
@everipedia/iq-login 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

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @Adebesin-Cell, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new useEnsureCorrectChain React hook, designed to simplify and standardize the process of ensuring a user's connected wallet is on the correct blockchain network. It provides a clear state machine for network status and offers utilities for guiding users to switch networks, significantly improving the developer experience for building dApps that require specific chain interactions.

Highlights

  • New Hook: useEnsureCorrectChain: Introduced a new React hook, useEnsureCorrectChain, to manage and enforce that the connected wallet is on a specific blockchain network.
  • Unified Status Flow: The hook provides a clear, enumerated status flow (idlewrong-networkswitchingcorrect) to represent the wallet's chain state, simplifying UI logic.
  • Programmatic Chain Switching: Includes a switchToCorrectChain function to programmatically prompt the user to switch to the required network, and a dismiss function to ignore the wrong-network state.
  • Status Change Callback: An optional onStatusChange callback allows developers to react to status transitions, useful for opening or closing modals based on network state.
  • Comprehensive Documentation: Added detailed documentation to the README.md covering basic usage, callback examples, and API reference for the new hook.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • README.md
    • Added a new section for 'Chain Enforcement Hook' documentation.
    • Included basic usage examples for useEnsureCorrectChain.
    • Provided an example demonstrating the onStatusChange callback.
    • Detailed the API reference for useEnsureCorrectChain options and return values.
  • src/client.ts
    • Exported the new useEnsureCorrectChain hook.
    • Exported the associated types: ChainStatus, UseEnsureCorrectChainOptions, and UseEnsureCorrectChainReturn.
  • src/hooks/use-ensure-correct-chain.ts
    • Created a new file implementing the useEnsureCorrectChain hook.
    • Defined the ChainStatus type and interfaces for the hook's options and return values.
    • Implemented state management for chain status using useState and useEffect.
    • Integrated wagmi's useAccount and useSwitchChain hooks for wallet and chain interaction.
    • Added logic for handling network switching, including error handling and user prompts.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new useEnsureCorrectChain hook, which is a great addition for ensuring users are on the correct network. The implementation is solid, but I've identified a few areas for improvement related to React hook best practices and error handling. My suggestions focus on memoizing functions and derived values with useCallback and useMemo for better performance and stability, and refining the error handling strategy to make the hook more robust and easier to consume without causing unhandled promise rejections.

Comment thread src/hooks/use-ensure-correct-chain.ts Outdated
Comment thread src/hooks/use-ensure-correct-chain.ts
Comment thread src/hooks/use-ensure-correct-chain.ts Outdated
@Adebesin-Cell Adebesin-Cell merged commit 1713335 into main Feb 13, 2026
1 check passed
@Adebesin-Cell Adebesin-Cell deleted the feat/chain-switch branch February 13, 2026 15:36
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