Skip to content

Comments

PRO-3691 - Re: Warning Message#428

Merged
vignesha22 merged 2 commits intostagingfrom
PRO-3691-Re-Warning_Message
Oct 8, 2025
Merged

PRO-3691 - Re: Warning Message#428
vignesha22 merged 2 commits intostagingfrom
PRO-3691-Re-Warning_Message

Conversation

@vignesha22
Copy link
Contributor

@vignesha22 vignesha22 commented Oct 8, 2025

Description

  • fixed the priorities of the warning message
  • Out of USDC and Out of Gas would be shown as soon as the component loads the balances

How Has This Been Tested?

  • Tested locally

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Summary by CodeRabbit

  • New Features

    • Added automatic checks for minimum stable balance and native-token gas fees before enabling Buy actions.
    • Displays clear, immediate warnings for insufficient stable balance or gas fees.
  • Bug Fixes

    • Prevents proceeding with Buy when required balances are missing, reducing confusing or failing attempts.
    • Aligns on-screen error messages with the new validation states.
  • Tests

    • Updated tests to validate immediate warning behavior without debounce delays.
    • Simplified scenarios to assert low balance and insufficient gas warnings on mount.

@vignesha22 vignesha22 requested review from IAmKio and RanaBug October 8, 2025 14:55
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 8, 2025

Walkthrough

Adds guards and validations in Buy.tsx to depend on walletPortfolioData, enforce a $2 minimum stable balance, and require at least $1 native-token balance for gas (minGasFee). Updates UI error handling accordingly. Tests are rewritten to assert immediate warning states on mount without debounced waits or input interactions.

Changes

Cohort / File(s) Summary of Changes
Buy flow guards & validations
src/apps/pulse/components/Buy/Buy.tsx
Added early return if walletPortfolioData is missing; enforced minimum stable balance check ($2) and minGasFee check based on native-token USD balance (≥ $1); removed prior validation from refreshBuyIntent; updated UI to surface minGasFee errors.
Immediate warning tests
src/apps/pulse/components/Buy/tests/Buy.test.tsx
Reworked tests to assert warnings appear immediately on mount; removed debounced wait and input-driven triggers; simplified mocks relying on preset low-balance/gas data.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant BuyComponent as Buy.tsx
  participant Portfolio as walletPortfolioData
  participant UI as Error/State UI

  User->>BuyComponent: Mount / Refresh
  BuyComponent->>Portfolio: Read balances
  alt walletPortfolioData missing
    BuyComponent->>BuyComponent: Warn & exit early
    BuyComponent->>UI: Show data-missing state
  else Has portfolio data
    BuyComponent->>BuyComponent: Compute stable balances
    alt Max stable < $2
      BuyComponent->>BuyComponent: setMinimumStableBalance(true)
      BuyComponent->>UI: Show min-stable warning
    else Stable OK
      BuyComponent->>BuyComponent: Find native token on target chain
      alt Native not found OR USD < $1
        BuyComponent->>BuyComponent: setMinGasFee(true)
        BuyComponent->>UI: Show min-gas warning
      else Gas OK
        BuyComponent->>BuyComponent: setMinGasFee(false)
        BuyComponent->>UI: Enable actions
      end
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • IAmKio
  • RanaBug

Poem

I thump my paw—preflight checks in place,
Two bucks stable, one for gas—set pace.
Warnings bloom early, no debounce delay,
Tests hop quicker, straightaway.
With tidy guards and signals bright,
This bunny buys just right tonight. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title Check ❓ Inconclusive The title references the issue number and mentions warning messages but is too generic to convey the specific change of displaying “Out of USDC” and “Out of Gas” warnings on component load and adjusting their priority. Please update the title to clearly and concisely summarize the main change, for example “Show Out of USDC and Out of Gas warnings on balance load.”
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed The description adheres to the repository template by including the Description, How Has This Been Tested, Screenshots, and Types of changes sections, clearly summarizing the fix and classifying it as a bug fix. It also notes local testing, satisfying the basic testing documentation requirement.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch PRO-3691-Re-Warning_Message

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.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Oct 8, 2025

Deploying x with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3eef4fe
Status: ✅  Deploy successful!
Preview URL: https://6712d522.x-e62.pages.dev
Branch Preview URL: https://pro-3691-re-warning-message.x-e62.pages.dev

View logs

Copy link
Contributor

@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: 2

🧹 Nitpick comments (2)
src/apps/pulse/components/Buy/tests/Buy.test.tsx (2)

449-527: Add negative test case for when both conditions are satisfied.

While the tests verify warnings appear when conditions are violated, there's no test confirming warnings DON'T appear when stable balance >= $2 AND gas >= $1. This would ensure the validation logic doesn't have false positives.

Add a test case like this:

it('does not show warnings when stable balance and gas are sufficient', async () => {
  const sufficientBalanceData = {
    ...mockWalletPortfolioData,
    result: {
      ...mockWalletPortfolioData.result,
      data: {
        ...mockWalletPortfolioData.result.data,
        total_wallet_balance: 2000,
        assets: [
          // USDC with sufficient balance (>= $2)
          {
            asset: {
              id: 2,
              symbol: 'USDC',
              name: 'USD Coin',
              logo: 'usdc-logo.png',
              decimals: ['6'],
              contracts: [],
              blockchains: [],
            },
            contracts_balances: [
              {
                address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
                chainId: 'evm:1',
                balance: 100,
                balanceRaw: '100000000',
                decimals: 6,
              },
            ],
            cross_chain_balances: {},
            price_change_24h: 0,
            estimated_balance: 100,
            price: 1,
            token_balance: 100,
            allocation: 0.95,
            wallets: ['0x1234567890123456789012345678901234567890'],
          },
          // ETH with sufficient balance for gas (>= $1)
          {
            asset: {
              id: 3,
              symbol: 'ETH',
              name: 'Ethereum',
              logo: 'eth-logo.png',
              decimals: ['18'],
              contracts: [],
              blockchains: [],
            },
            contracts_balances: [
              {
                address: '0x0000000000000000000000000000000000000000',
                chainId: 'evm:1',
                balance: 0.5,
                balanceRaw: '500000000000000000',
                decimals: 18,
              },
            ],
            cross_chain_balances: {},
            price_change_24h: 0,
            estimated_balance: 1500,
            price: 3000,
            token_balance: 0.5,
            allocation: 0.05,
            wallets: ['0x1234567890123456789012345678901234567890'],
          },
        ],
      },
    },
  };

  renderWithProviders({ walletPortfolioData: sufficientBalanceData });

  await waitFor(() => {
    expect(screen.queryByText('You need $2 USDC to trade, deposit USDC')).not.toBeInTheDocument();
    expect(screen.queryByText(/Min\. \$1 .* required on/)).not.toBeInTheDocument();
  });
});

449-527: Consider testing warning priority behavior.

The PR description mentions fixing "priorities" of warning messages. It would be valuable to have a test that verifies when both low stable balance AND low gas conditions exist, only the stable balance warning is shown (since the code returns early at line 194).

Add a test case:

it('shows stable balance warning with priority over gas warning', async () => {
  const bothLowBalanceData = {
    ...mockWalletPortfolioData,
    result: {
      ...mockWalletPortfolioData.result,
      data: {
        ...mockWalletPortfolioData.result.data,
        total_wallet_balance: 1.3,
        assets: [
          // USDC with balance less than $2
          {
            asset: {
              id: 2,
              symbol: 'USDC',
              name: 'USD Coin',
              logo: 'usdc-logo.png',
              decimals: ['6'],
              contracts: [],
              blockchains: [],
            },
            contracts_balances: [
              {
                address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
                chainId: 'evm:1',
                balance: 1,
                balanceRaw: '1000000',
                decimals: 6,
              },
            ],
            cross_chain_balances: {},
            price_change_24h: 0,
            estimated_balance: 1,
            price: 1,
            token_balance: 1,
            allocation: 0.77,
            wallets: ['0x1234567890123456789012345678901234567890'],
          },
          // ETH with insufficient balance for gas (< $1)
          {
            asset: {
              id: 3,
              symbol: 'ETH',
              name: 'Ethereum',
              logo: 'eth-logo.png',
              decimals: ['18'],
              contracts: [],
              blockchains: [],
            },
            contracts_balances: [
              {
                address: '0x0000000000000000000000000000000000000000',
                chainId: 'evm:1',
                balance: 0.0001,
                balanceRaw: '100000000000000',
                decimals: 18,
              },
            ],
            cross_chain_balances: {},
            price_change_24h: 0,
            estimated_balance: 0.3,
            price: 3000,
            token_balance: 0.0001,
            allocation: 0.23,
            wallets: ['0x1234567890123456789012345678901234567890'],
          },
        ],
      },
    },
  };

  renderWithProviders({ walletPortfolioData: bothLowBalanceData });

  await waitFor(() => {
    // Should show stable balance warning (higher priority)
    expect(screen.getByText('You need $2 USDC to trade, deposit USDC')).toBeInTheDocument();
    // Should NOT show gas warning since stable balance check returns early
    expect(screen.queryByText(/Min\. \$1 .* required on/)).not.toBeInTheDocument();
  });
});

Also applies to: 529-607

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3958952 and 3eef4fe.

📒 Files selected for processing (2)
  • src/apps/pulse/components/Buy/Buy.tsx (3 hunks)
  • src/apps/pulse/components/Buy/tests/Buy.test.tsx (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/apps/pulse/components/Buy/Buy.tsx (2)
src/apps/pulse/utils/constants.ts (1)
  • getChainId (28-49)
src/apps/pulse/utils/blockchain.ts (3)
  • isNativeToken (17-18)
  • NativeSymbols (20-27)
  • ChainNames (29-36)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: unit-tests
  • GitHub Check: lint
  • GitHub Check: build
🔇 Additional comments (5)
src/apps/pulse/components/Buy/Buy.tsx (3)

175-178: LGTM! Good defensive guard.

The early return when walletPortfolioData is missing prevents downstream errors and aligns with the PR objective of showing warnings immediately when balance data is unavailable.


212-218: Verify native token balance calculation.

The calculation (nativeToken.balance ?? 0) * (nativeToken.price ?? 0) assumes both fields exist and are correctly populated. If either is undefined or zero when it shouldn't be, the gas fee check could produce false positives.

Consider adding debug logging temporarily to verify the balance calculation is working as expected:

 const nativeTokenUSDBalance =
   (nativeToken.balance ?? 0) * (nativeToken.price ?? 0);
+console.log('[Gas Check]', {
+  symbol: nativeToken.symbol,
+  balance: nativeToken.balance,
+  price: nativeToken.price,
+  usdBalance: nativeTokenUSDBalance,
+  chainId: chainIdOfMaxStableBalance
+});

 if (!nativeTokenUSDBalance || nativeTokenUSDBalance < 1) {
   setMinGasFee(true);
   return;
 }

You can remove this logging once verified in production.


202-206: Use chainNameToChainIdTokensData for chain ID comparison instead of t.chainId.

The Token type has a blockchain: string, not a numeric chainId. Replace:

- Number(getChainId(t.blockchain as MobulaChainNames)) === chainIdOfMaxStableBalance
+ chainNameToChainIdTokensData(t.blockchain) === chainIdOfMaxStableBalance

to ensure a correct numeric comparison and avoid NaN from comma-separated IDs.

Likely an incorrect or invalid review comment.

src/apps/pulse/components/Buy/tests/Buy.test.tsx (2)

521-526: LGTM! Test correctly verifies immediate warning behavior.

The test now properly asserts that the minimum stable balance warning appears immediately when the component mounts with low balance data, which aligns with the PR objective.


601-606: LGTM! Gas fee warning test matches new behavior.

The test correctly verifies that the gas fee warning appears immediately on mount with insufficient gas, matching the new immediate-warning implementation.

Comment on lines +192 to +196
if (maxStableBalance < 2) {
setMinimumStableBalance(true);
return;
}
setMinimumStableBalance(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Early return prevents clearing minGasFee state.

If maxStableBalance < 2, the function returns without ever reaching lines 197-219 where minGasFee state is managed. This means if a user previously had low gas (minGasFee=true) but then acquires stable coins while still having low gas, the minGasFee state won't be updated on subsequent renders.

Consider setting minGasFee(false) before the early return or restructure to always update all warning states:

 if (maxStableBalance < 2) {
   setMinimumStableBalance(true);
+  setMinGasFee(false); // Reset since stable balance takes priority
   return;
 }
 setMinimumStableBalance(false);
📝 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 (maxStableBalance < 2) {
setMinimumStableBalance(true);
return;
}
setMinimumStableBalance(false);
if (maxStableBalance < 2) {
setMinimumStableBalance(true);
setMinGasFee(false); // Reset since stable balance takes priority
return;
}
setMinimumStableBalance(false);
🤖 Prompt for AI Agents
In src/apps/pulse/components/Buy/Buy.tsx around lines 192 to 196, the early
return when maxStableBalance < 2 sets minimumStableBalance but prevents later
logic (lines ~197-219) from updating minGasFee, leaving minGasFee stale; ensure
minGasFee is updated before returning (call setMinGasFee(false) or the
appropriate value) or refactor so warnings (minimumStableBalance and minGasFee)
are computed and set unconditionally (remove the early return and use
conditional branching for only the returnable behavior) so both states are
always updated.

Comment on lines +666 to +667
} else if (minGasFee) {
message = `Min. $1 ${NativeSymbols[maxStableCoinBalance.chainId]} required on ${ChainNames[maxStableCoinBalance.chainId]}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add fallback for missing chainId in symbol/name lookups.

If maxStableCoinBalance.chainId is not in the NativeSymbols or ChainNames mappings (e.g., for a new chain), the message will display "Min. $1 undefined required on undefined".

Apply this diff to add fallback values:

-message = `Min. $1 ${NativeSymbols[maxStableCoinBalance.chainId]} required on ${ChainNames[maxStableCoinBalance.chainId]}`;
+message = `Min. $1 ${NativeSymbols[maxStableCoinBalance.chainId] || 'native token'} required on ${ChainNames[maxStableCoinBalance.chainId] || `chain ${maxStableCoinBalance.chainId}`}`;

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In src/apps/pulse/components/Buy/Buy.tsx around lines 666-667, the message
construction uses NativeSymbols[maxStableCoinBalance.chainId] and
ChainNames[maxStableCoinBalance.chainId] without fallbacks; update the code so
lookups use safe access with defaults (e.g., const symbol =
NativeSymbols[maxStableCoinBalance.chainId] || 'TOKEN'; const chainName =
ChainNames[maxStableCoinBalance.chainId] || 'Unknown chain';) and then build the
message using those variables so it never renders "undefined" for unknown
chains.

@vignesha22 vignesha22 merged commit d1bff3e into staging Oct 8, 2025
6 checks passed
@coderabbitai coderabbitai bot mentioned this pull request Dec 2, 2025
3 tasks
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