Skip to content

Logic checks ethBalance < BigInt(fee) ignores gas costs#126

Open
aniket866 wants to merge 2 commits intoStabilityNexus:mainfrom
aniket866:check-balance
Open

Logic checks ethBalance < BigInt(fee) ignores gas costs#126
aniket866 wants to merge 2 commits intoStabilityNexus:mainfrom
aniket866:check-balance

Conversation

@aniket866
Copy link
Contributor

@aniket866 aniket866 commented Feb 28, 2026

Addressed Issues:

Fixes #123

Description

  • Where? ReceivedInvoice.jsx

  • Issue: checkBalance logic for fees.

- Fix: Logic checks ethBalance < BigInt(fee) but ignores gas costs.

  • Why: A transaction will fail even if they have exactly the fee amount, because they need Fee + Gas

Screenshots/Recordings:

Additional Notes:

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions.
  • If applicable, I have made corresponding changes or additions to the documentation.
  • If applicable, I have made corresponding changes or additions to tests.
  • My changes generate no new warnings or errors.
  • I have joined the Stability Nexus's Discord server and I will share a link to this PR with the project maintainers there.
  • I have read the Contribution Guidelines.
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.

AI Usage Disclosure

Check one of the checkboxes below:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: TODO

⚠️ AI Notice - Important!

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact.

Summary by CodeRabbit

  • Bug Fixes
    • Improved balance verification for ETH and ERC20 payments by accounting for gas fees in requirement checks
    • Updated error messages to clearly display total required amounts including gas fees

@coderabbitai
Copy link

coderabbitai bot commented Feb 28, 2026

Warning

Rate limit exceeded

@aniket866 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 5 minutes and 41 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between ce7d552 and e01931f.

📒 Files selected for processing (1)
  • frontend/src/page/ReceivedInvoice.jsx

Walkthrough

A gasBuffer of 0.002 ETH is introduced to balance checks in the ReceivedInvoice component, addressing cases where ETH and ERC20 payments were insufficient to account for transaction gas costs. Error messages are updated to reflect required amounts including gas.

Changes

Cohort / File(s) Summary
Gas Buffer Implementation
frontend/src/page/ReceivedInvoice.jsx
Added 0.002 ETH gasBuffer to balance validation logic for both native ETH and ERC20 payment flows. Updated error messages with ~ prefix and fixed decimal formatting to communicate gas-inclusive requirements. Added explanatory comments clarifying buffer purpose.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Typescript Lang

Poem

🐰 A buffer so small, yet prevents a fall,
0.002 ETH guards against the gas cost wall,
No more failed dreams when balances align,
With room for the fees, transactions now shine!

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main bug being fixed: gas costs being ignored in balance checks, which directly matches the primary changeset.
Linked Issues check ✅ Passed The code changes address issue #123's requirement to include gas costs in balance checks, ensuring ethBalance >= fee + estimated gas for both ETH and ERC20 payments.
Out of Scope Changes check ✅ Passed All changes in ReceivedInvoice.jsx are directly related to fixing the balance check logic to include gas costs, with no unrelated modifications detected.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
frontend/src/page/ReceivedInvoice.jsx (1)

1-11: ⚠️ Potential issue | 🟡 Minor

Missing "use client" directive for Next.js.

This component uses client-side React hooks (useState, useEffect) and wallet client interactions via wagmi. Per Next.js best practices, add the "use client" directive at the top of the file.

Proposed fix
+"use client";
+
 import Paper from "@mui/material/Paper";
 import Table from "@mui/material/Table";

As per coding guidelines: **/*.{ts,tsx,js,jsx}: NextJS - Ensure that "use client" is being used.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@frontend/src/page/ReceivedInvoice.jsx` around lines 1 - 11, This file
(ReceivedInvoice.jsx) uses client-side React hooks (useState, useEffect) and
wallet interactions (BrowserProvider, Contract, ethers, ChainvoiceABI) but is
missing Next.js's "use client" directive; fix by adding the "use client"
directive as the very first line of the file so the component runs on the client
(ensure it appears before any imports and then keep the existing imports and
component code unchanged).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@frontend/src/page/ReceivedInvoice.jsx`:
- Around line 240-256: Replace the hardcoded gasBuffer in ReceivedInvoice.jsx
with a configurable, chain-aware value (e.g., use a GAS_BUFFER_BY_CHAIN map
keyed by chainId or an env-configurable fallback) and compute gasBuffer =
GAS_BUFFER_BY_CHAIN[chainId] || GAS_BUFFER_BY_CHAIN.default; update the
totalRequired calculation to use that gasBuffer; and externalize the thrown
error string into the i18n resource (use an i18n key like
"errors.insufficient_eth" and format in the message with the required/available
amounts) so user-visible text is not hardcoded.
- Around line 246-257: The totalRequired calculation for native ETH currently
uses a single fee; update it to include the batch fee by adding fee multiplied
by invoiceCount (e.g., BigInt(fee) * BigInt(invoiceCount)) so types align with
ethers.parseUnits(amount.toString(), 18) which returns a BigInt; modify the
expression assigned to totalRequired (and any related comparisons against
balance) to use parsed amount + BigInt(fee) * BigInt(invoiceCount) + gasBuffer
so batch ETH payments are correctly covered.
- Around line 271-282: The balance check currently only uses a single `fee` when
computing `totalEthRequired`; update the logic (in the function that contains
`ethBalance`, `fee`, `gasBuffer` — e.g., `checkBalance`) to accept an
`invoiceCount` (or compute `totalFee = BigInt(fee) * BigInt(invoiceCount)`) and
use `totalFee + gasBuffer` for `totalEthRequired`; also adjust the error message
to show the required ETH based on `totalFee` and ensure the batch payment call
(the code path referenced at the later batch payment around line with
`invoiceCount` / totalFee) passes the `invoiceCount` into this check so batch
payments are validated correctly before attempting the transaction.

---

Outside diff comments:
In `@frontend/src/page/ReceivedInvoice.jsx`:
- Around line 1-11: This file (ReceivedInvoice.jsx) uses client-side React hooks
(useState, useEffect) and wallet interactions (BrowserProvider, Contract,
ethers, ChainvoiceABI) but is missing Next.js's "use client" directive; fix by
adding the "use client" directive as the very first line of the file so the
component runs on the client (ensure it appears before any imports and then keep
the existing imports and component code unchanged).

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b83780d and ce7d552.

📒 Files selected for processing (1)
  • frontend/src/page/ReceivedInvoice.jsx

@SIDDHANTCOOKIE
Copy link
Contributor

@kumawatkaran523 @DengreSarthak @aniket866 both #126 and #124 address the same issue #123, I guess he made two by mistake. @aniket866 I feel you should close one from the two

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.

[BUG]: Logic checks ethBalance < BigInt(fee) ignores gas costs.

2 participants