Skip to content

Fix: Fetch token data fetch when wallet connect and not connect#70

Merged
kumawatkaran523 merged 3 commits intoStabilityNexus:mainfrom
Nikuunj:main
Jan 18, 2026
Merged

Fix: Fetch token data fetch when wallet connect and not connect#70
kumawatkaran523 merged 3 commits intoStabilityNexus:mainfrom
Nikuunj:main

Conversation

@Nikuunj
Copy link
Copy Markdown
Contributor

@Nikuunj Nikuunj commented Jan 15, 2026

issue - #63

when user connect -

error when refresh the page -
image

solve -

image

when user not connect -

image

example with using alchemy rpc for sepolia -

image

@Zahnentferner @kumawatkaran523

Summary by CodeRabbit

Release Notes

  • Refactor
    • Restructured token verification to support multi-chain verification with improved error handling for unsupported networks.

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

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jan 15, 2026

📝 Walkthrough

Walkthrough

The PR refactors token verification in CreateInvoice by adding chain-specific verification support. The verifyToken function now accepts an optional targetChainId parameter, implements provider selection logic based on wallet connection status and chain, and includes enhanced error handling for unsupported chains. The verifiedToken structure now includes resolved chainId data.

Changes

Cohort / File(s) Summary
Token Verification Enhancement
frontend/src/page/CreateInvoice.jsx
Updated verifyToken signature to accept optional targetChainId; introduced chain determination logic (prioritizing targetChainId → URL param → account.chainId); added RPC provider selection by chain (wallet provider when connected, otherwise JsonRpcProvider for public RPC); replaced direct on-chain metadata fetch with provider-based retrieval; enhanced error handling for unsupported chains; updated verifiedToken to include resolved chainId; added walletClient to useEffect dependencies.

Sequence Diagram

sequenceDiagram
    actor User
    participant CreateInvoice as CreateInvoice Component
    participant VerifyToken as verifyToken Function
    participant ChainLogic as Chain Determination
    participant ProviderLogic as Provider Selection
    participant Provider as Web3 Provider
    participant TokenContract as Token Contract

    User->>CreateInvoice: Verify token (address, optional chainId)
    CreateInvoice->>VerifyToken: Call with targetChainId
    VerifyToken->>ChainLogic: Determine chainIdToUse
    ChainLogic-->>ChainLogic: Prioritize: targetChainId → URL param → account.chainId
    ChainLogic-->>VerifyToken: Return resolved chainId
    
    VerifyToken->>ProviderLogic: Select provider for chainId
    alt Wallet Connected
        ProviderLogic->>Provider: Use wallet provider
    else Unsupported Chain
        ProviderLogic-->>VerifyToken: Error - unsupported chain
        VerifyToken-->>CreateInvoice: Early return / error
    else Public RPC Available
        ProviderLogic->>Provider: Initialize JsonRpcProvider with RPC URL
    end
    
    Provider->>TokenContract: Fetch symbol, name, decimals
    TokenContract-->>Provider: Return token metadata
    Provider-->>VerifyToken: Token data
    VerifyToken-->>VerifyToken: Parse decimals as Number
    VerifyToken->>CreateInvoice: Return {symbol, name, decimals, chainId}
    CreateInvoice-->>User: Token verified with chain info
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • Zahnentferner

Poem

🐰 Hop through the chains with proper care,
Token verification beyond compare,
Provider selection, left and right,
Each chain's truth shines ever bright,
What a refactor—fluffy and fine! ✨🌕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix: Fetch token data fetch when wallet connect and not connect' is awkwardly phrased and contains a grammatical error ('Fetch token data fetch'), but it does address the main objective of the PR: enabling token data fetching in both connected and disconnected wallet states.

✏️ 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.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@frontend/src/page/CreateInvoice.jsx`:
- Around line 259-276: The code can call new ethers.Contract(address, ERC20_ABI,
provider) with provider undefined if neither isConnected/walletClient nor
chainIdToUse/rpcUrl are available (and walletClient can be undefined even when
isConnected). Fix by validating/initializing provider before constructing the
contract: when isConnected check that walletClient is defined before using new
BrowserProvider(walletClient) (otherwise fall back to a public JsonRpcProvider
or setTokenVerificationState("error") and return), and if !isConnected ensure
chainIdToUse exists and rpcUrls[chainIdToUse] exists; only call
ethers.Contract(address, ERC20_ABI, provider) after provider is guaranteed
non-null.
🧹 Nitpick comments (2)
frontend/src/page/CreateInvoice.jsx (2)

284-284: Remove debug console.log statement.

This debug logging should be removed or converted to conditional debug logging before merging to production.

♻️ Suggested fix
-    console.log([symbol, name, decimals]);

251-258: Consider making RPC URLs configurable.

The hardcoded RPC URLs work but could become stale or rate-limited. For better maintainability, consider moving these to environment variables or a configuration file.

// Example: Use environment variables
const rpcUrls = {
  // ...
};
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5850b22 and 5fe96d4.

📒 Files selected for processing (1)
  • frontend/src/page/CreateInvoice.jsx
🔇 Additional comments (1)
frontend/src/page/CreateInvoice.jsx (1)

160-160: Good addition of walletClient to the dependency array.

This correctly ensures the effect re-runs when the wallet client becomes available, which addresses the page refresh bug where token metadata wasn't fetched because walletClient was still loading.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.

Comment on lines +259 to +276
if (typeof window !== "undefined" && isConnected) {
// Fallback to wallet provider if no chainId specified
provider = new BrowserProvider(walletClient);
// If chainId is available, always use public RPC (works without wallet)
} else if (chainIdToUse) {
const rpcUrl = rpcUrls[chainIdToUse];

if (!rpcUrl) {
console.error(`Unsupported chain ${chainIdToUse}. Supported chains: Ethereum (1), Ethereum Classic (61), Polygon (137), BNB Smart Chain (56), Base (8453), Sepolia (11155111)`);
setTokenVerificationState("error");
return;
}
} catch (error) {
console.error("Verification failed:", error);
setTokenVerificationState("error");
// Use JsonRpcProvider with timeout for faster response
provider = new ethers.JsonRpcProvider(rpcUrl, Number(chainIdToUse));
}
};

const contract = new ethers.Contract(address, ERC20_ABI, provider);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Potential runtime error when provider is not initialized.

If the user is not connected (isConnected is false) and no chainIdToUse is available (no URL chain param and no account.chainId), then provider is never assigned. Line 276 will throw when attempting to create the contract.

Additionally, walletClient can be undefined even when isConnected is true due to async loading from useWalletClient().

🐛 Proposed fix to handle edge cases
     if (typeof window !== "undefined" && isConnected) {
-      // Fallback to wallet provider if no chainId specified
-      provider = new BrowserProvider(walletClient);
+      if (!walletClient) {
+        console.error("Wallet client not ready yet");
+        setTokenVerificationState("error");
+        return;
+      }
+      provider = new BrowserProvider(walletClient);
     // If chainId is available, always use public RPC (works without wallet)
     } else if (chainIdToUse) {
       const rpcUrl = rpcUrls[chainIdToUse];
       
       if (!rpcUrl) {
         console.error(`Unsupported chain ${chainIdToUse}. Supported chains: Ethereum (1), Ethereum Classic (61), Polygon (137), BNB Smart Chain (56), Base (8453), Sepolia (11155111)`);
         setTokenVerificationState("error");
         return;
       }
       
       // Use JsonRpcProvider with timeout for faster response
       provider = new ethers.JsonRpcProvider(rpcUrl, Number(chainIdToUse));
+    } else {
+      console.error("Cannot verify token: no wallet connected and no chain specified");
+      setTokenVerificationState("error");
+      return;
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (typeof window !== "undefined" && isConnected) {
// Fallback to wallet provider if no chainId specified
provider = new BrowserProvider(walletClient);
// If chainId is available, always use public RPC (works without wallet)
} else if (chainIdToUse) {
const rpcUrl = rpcUrls[chainIdToUse];
if (!rpcUrl) {
console.error(`Unsupported chain ${chainIdToUse}. Supported chains: Ethereum (1), Ethereum Classic (61), Polygon (137), BNB Smart Chain (56), Base (8453), Sepolia (11155111)`);
setTokenVerificationState("error");
return;
}
} catch (error) {
console.error("Verification failed:", error);
setTokenVerificationState("error");
// Use JsonRpcProvider with timeout for faster response
provider = new ethers.JsonRpcProvider(rpcUrl, Number(chainIdToUse));
}
};
const contract = new ethers.Contract(address, ERC20_ABI, provider);
if (typeof window !== "undefined" && isConnected) {
if (!walletClient) {
console.error("Wallet client not ready yet");
setTokenVerificationState("error");
return;
}
// Fallback to wallet provider if no chainId specified
provider = new BrowserProvider(walletClient);
// If chainId is available, always use public RPC (works without wallet)
} else if (chainIdToUse) {
const rpcUrl = rpcUrls[chainIdToUse];
if (!rpcUrl) {
console.error(`Unsupported chain ${chainIdToUse}. Supported chains: Ethereum (1), Ethereum Classic (61), Polygon (137), BNB Smart Chain (56), Base (8453), Sepolia (11155111)`);
setTokenVerificationState("error");
return;
}
// Use JsonRpcProvider with timeout for faster response
provider = new ethers.JsonRpcProvider(rpcUrl, Number(chainIdToUse));
} else {
console.error("Cannot verify token: no wallet connected and no chain specified");
setTokenVerificationState("error");
return;
}
const contract = new ethers.Contract(address, ERC20_ABI, provider);
🤖 Prompt for AI Agents
In `@frontend/src/page/CreateInvoice.jsx` around lines 259 - 276, The code can
call new ethers.Contract(address, ERC20_ABI, provider) with provider undefined
if neither isConnected/walletClient nor chainIdToUse/rpcUrl are available (and
walletClient can be undefined even when isConnected). Fix by
validating/initializing provider before constructing the contract: when
isConnected check that walletClient is defined before using new
BrowserProvider(walletClient) (otherwise fall back to a public JsonRpcProvider
or setTokenVerificationState("error") and return), and if !isConnected ensure
chainIdToUse exists and rpcUrls[chainIdToUse] exists; only call
ethers.Contract(address, ERC20_ABI, provider) after provider is guaranteed
non-null.

@Nikuunj Nikuunj mentioned this pull request Jan 16, 2026
@kumawatkaran523
Copy link
Copy Markdown
Contributor

Can you explain bit more what exactly the issue is?

@kumawatkaran523 kumawatkaran523 merged commit 5fe96d4 into StabilityNexus:main Jan 18, 2026
1 check passed
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.

2 participants