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