Skip to content

Commit 278e747

Browse files
committed
update txn signer
1 parent d3705a6 commit 278e747

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

packages/backend/src/services/transaction-signer.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,31 @@ export class TransactionSigner {
411411
let usdcAmount = params.estimatedUsdcAmount;
412412
if (transferLog) {
413413
const actualUsdcAmount = BigInt(transferLog.data);
414-
console.info('💰 [TX SIGNER] Actual USDC from previous claim receipt:', actualUsdcAmount.toString());
414+
console.info('💰 [TX SIGNER] Actual USDC from claim receipt:', actualUsdcAmount.toString());
415415
usdcAmount = actualUsdcAmount;
416416
} else {
417-
console.info('💰 [TX SIGNER] Using estimated amount for swap:', usdcAmount.toString());
417+
console.info('💰 [TX SIGNER] Could not parse USDC from claim receipt, checking wallet balance...');
418+
}
419+
420+
// SAFETY: Always verify we have enough USDC balance before swapping
421+
// This prevents failures due to rounding differences or partial amounts
422+
const walletUsdcBalance = await publicClient.readContract({
423+
address: chainConfig.usdcAddress as Hex,
424+
abi: ERC20_ABI,
425+
functionName: 'balanceOf',
426+
args: [this.account.address],
427+
}) as bigint;
428+
429+
console.info('💰 [TX SIGNER] Wallet USDC balance:', walletUsdcBalance.toString());
430+
431+
// Use the minimum of estimated amount and actual balance (with small buffer for gas)
432+
if (walletUsdcBalance < usdcAmount) {
433+
console.warn(`⚠️ [TX SIGNER] Wallet balance (${walletUsdcBalance}) < estimated amount (${usdcAmount}), using actual balance`);
434+
usdcAmount = walletUsdcBalance;
435+
}
436+
437+
if (usdcAmount === 0n) {
438+
throw new Error('No USDC balance available for swap');
418439
}
419440

420441
// Swap USDC to native via Uniswap

0 commit comments

Comments
 (0)