Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: feat(hooks): Develop a Token-Gated Content/Feature Hook
**Closes #19 **
Description
This pull request introduces
useTokenGatedAccess, a powerful and reusable React hook that serves as the primary tool for implementing token-gated functionality across the application.This hook abstracts away the complexity of on-chain data fetching. It allows any component to easily determine if the connected user meets specific on-chain criteria (such as owning an NFT or holding a minimum balance of a token) and react accordingly. This provides a clean, declarative, and centralized way to manage access to exclusive UI elements, features, or content.
Implementation Details
useTokenGatedAccessHookSignature:
The hook is designed to be flexible, accepting an object with the contract address and the specific ownership criteria:
useTokenGatedAccess({ contractAddress, minBalance?, tokenId? })On-Chain Logic:
useFlarehook to get the connected user'saccountand the networkprovider.ethers.jsto create a generic contract instance.tokenIdis provided, it callsownerOf(tokenId)to verify ERC-721 ownership.minBalanceis provided, it callsbalanceOf(account)to verify ERC-20/ERC-1155 token balance.Reactive State:
{ hasAccess: boolean, isLoading: boolean }.useEffecthook ensures the on-chain check is re-run whenever the user'saccountor the input criteria (contractAddress,tokenId, etc.) change, keeping the UI perfectly in sync with the user's wallet state.Demonstration:
The hook has been demonstrated by gating a sample UI component, which will now only render if the connected user holds the required token.
How to Test
1. Setup
2. Test the "Has Access" Flow
3. Test the "No Access" Flow
4. Test Reactivity