From ce7d552eb96d8daccaf0843b1ae8a3227b52c779 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Sat, 28 Feb 2026 23:49:09 +0530 Subject: [PATCH 1/2] fixing-balance-check --- frontend/src/page/ReceivedInvoice.jsx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/frontend/src/page/ReceivedInvoice.jsx b/frontend/src/page/ReceivedInvoice.jsx index 1bac242f..8a777fda 100644 --- a/frontend/src/page/ReceivedInvoice.jsx +++ b/frontend/src/page/ReceivedInvoice.jsx @@ -233,20 +233,26 @@ function ReceivedInvoice() { return suggestions; }; - // UNIFORM BALANCE CHECK +// UNIFORM BALANCE CHECK const checkBalance = async (tokenAddress, amount, symbol, signer) => { const userAddress = await signer.getAddress(); + + // Add a reasonable buffer for gas fees (e.g., 0.002 ETH). + // You can adjust this value depending on the typical network gas costs you expect. + const gasBuffer = ethers.parseEther("0.002"); if (tokenAddress === ethers.ZeroAddress) { const balance = await signer.provider.getBalance(userAddress); + + // Include gas buffer in the total required ETH calculation const totalRequired = - ethers.parseUnits(amount.toString(), 18) + BigInt(fee); + ethers.parseUnits(amount.toString(), 18) + BigInt(fee) + gasBuffer; if (balance < totalRequired) { const requiredEth = ethers.formatEther(totalRequired); const availableEth = ethers.formatEther(balance); throw new Error( - `Insufficient ETH balance. Required: ${requiredEth} ETH, Available: ${availableEth} ETH` + `Insufficient ETH balance. Required: ~${Number(requiredEth).toFixed(4)} ETH (including gas), Available: ${Number(availableEth).toFixed(4)} ETH` ); } } else { @@ -263,11 +269,15 @@ function ReceivedInvoice() { } const ethBalance = await signer.provider.getBalance(userAddress); - if (ethBalance < BigInt(fee)) { - const requiredEthFee = ethers.formatEther(fee); + + // Add gas buffer for ERC20 payments as well (to cover approval/payment gas + network fee) + const totalEthRequired = BigInt(fee) + gasBuffer; + + if (ethBalance < totalEthRequired) { + const requiredEthFee = ethers.formatEther(totalEthRequired); const availableEth = ethers.formatEther(ethBalance); throw new Error( - `Insufficient ETH for fees. Required: ${requiredEthFee} ETH, Available: ${availableEth} ETH` + `Insufficient ETH for fees and gas. Required: ~${Number(requiredEthFee).toFixed(4)} ETH, Available: ${Number(availableEth).toFixed(4)} ETH` ); } } From e01931f18b523c65c1fd8b2cbc4e6b2d84b7e156 Mon Sep 17 00:00:00 2001 From: Aniket Date: Sun, 1 Mar 2026 00:16:01 +0530 Subject: [PATCH 2/2] Code rabbit follow-up --- frontend/src/page/ReceivedInvoice.jsx | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/frontend/src/page/ReceivedInvoice.jsx b/frontend/src/page/ReceivedInvoice.jsx index 8a777fda..fce355f2 100644 --- a/frontend/src/page/ReceivedInvoice.jsx +++ b/frontend/src/page/ReceivedInvoice.jsx @@ -233,8 +233,8 @@ function ReceivedInvoice() { return suggestions; }; -// UNIFORM BALANCE CHECK - const checkBalance = async (tokenAddress, amount, symbol, signer) => { + // UNIFORM BALANCE CHECK + const checkBalance = async (tokenAddress, amount, symbol, signer, invoiceCount = 1) => { const userAddress = await signer.getAddress(); // Add a reasonable buffer for gas fees (e.g., 0.002 ETH). @@ -246,7 +246,7 @@ function ReceivedInvoice() { // Include gas buffer in the total required ETH calculation const totalRequired = - ethers.parseUnits(amount.toString(), 18) + BigInt(fee) + gasBuffer; + ethers.parseUnits(amount.toString(), 18) + (BigInt(fee) * BigInt(invoiceCount)) + gasBuffer; if (balance < totalRequired) { const requiredEth = ethers.formatEther(totalRequired); @@ -271,7 +271,7 @@ function ReceivedInvoice() { const ethBalance = await signer.provider.getBalance(userAddress); // Add gas buffer for ERC20 payments as well (to cover approval/payment gas + network fee) - const totalEthRequired = BigInt(fee) + gasBuffer; + const totalEthRequired = (BigInt(fee) * BigInt(invoiceCount)) + gasBuffer; if (ethBalance < totalEthRequired) { const requiredEthFee = ethers.formatEther(totalEthRequired); @@ -525,7 +525,8 @@ function ReceivedInvoice() { group.tokenAddress, group.totalAmount, group.symbol, - signer + signer, + group.invoices.length ); } catch (error) { setPaymentError(error.message);