11import { TransactionReceipt as ViemTransactionReceipt } from 'viem' ;
2- import { safeParseBigInt } from '../helpers' ;
2+ import { safeParseBigInt , getEvmBalance } from '../helpers' ;
33import { jsonifyError } from '@mark/logger' ;
44import {
55 RebalanceOperationStatus ,
@@ -709,14 +709,44 @@ async function executeLeg2And3(
709709
710710 const ptUsdeAddress = tokenPair . ptUSDe ;
711711
712+ // Use actual USDC balance on Mainnet instead of operation.amount to account for
713+ // potential differences from CCIP fees or rounding during the cross-chain transfer
714+ let swapAmount = operation . amount ;
715+ try {
716+ const usdcBalance = await getEvmBalance (
717+ rebalanceConfig ,
718+ MAINNET_CHAIN_ID . toString ( ) ,
719+ recipient ,
720+ usdcAddress ,
721+ USDC_SOLANA_DECIMALS ,
722+ context . prometheus ,
723+ ) ;
724+ const operationAmount = safeParseBigInt ( operation . amount ) ;
725+ if ( usdcBalance < operationAmount ) {
726+ logger . warn ( 'Actual USDC balance on Mainnet is less than operation amount (CCIP fees/rounding)' , {
727+ ...logContext ,
728+ operationAmount : operation . amount ,
729+ actualBalance : usdcBalance . toString ( ) ,
730+ difference : ( operationAmount - usdcBalance ) . toString ( ) ,
731+ } ) ;
732+ swapAmount = usdcBalance . toString ( ) ;
733+ }
734+ } catch ( balanceError ) {
735+ logger . warn ( 'Failed to check actual USDC balance on Mainnet, using operation amount' , {
736+ ...logContext ,
737+ error : jsonifyError ( balanceError ) ,
738+ fallbackAmount : operation . amount ,
739+ } ) ;
740+ }
741+
712742 logger . debug ( 'Leg 2 Pendle swap details' , {
713743 ...logContext ,
714744 storedRecipient,
715745 fallbackRecipient : rebalanceConfig . ownAddress ,
716746 finalRecipient : recipient ,
717747 usdcAddress,
718748 ptUsdeAddress,
719- amountToSwap : operation . amount ,
749+ amountToSwap : swapAmount ,
720750 } ) ;
721751
722752 // Create route for USDC → ptUSDe swap on mainnet (same chain swap)
@@ -728,17 +758,17 @@ async function executeLeg2And3(
728758 } ;
729759
730760 // Get quote from Pendle for USDC → ptUSDe
731- const receivedAmountStr = await pendleAdapter . getReceivedAmount ( operation . amount , pendleRoute ) ;
761+ const receivedAmountStr = await pendleAdapter . getReceivedAmount ( swapAmount , pendleRoute ) ;
732762
733763 logger . info ( 'Received Pendle quote for USDC → ptUSDe swap' , {
734764 ...logContext ,
735- amountToSwap : operation . amount ,
765+ amountToSwap : swapAmount ,
736766 expectedPtUsde : receivedAmountStr ,
737767 route : pendleRoute ,
738768 } ) ;
739769
740770 // Execute the Pendle swap transactions
741- const swapTxRequests = await pendleAdapter . send ( recipient , recipient , operation . amount , pendleRoute ) ;
771+ const swapTxRequests = await pendleAdapter . send ( recipient , recipient , swapAmount , pendleRoute ) ;
742772
743773 if ( ! swapTxRequests . length ) {
744774 logger . error ( 'No swap transactions returned from Pendle adapter' , logContext ) ;
@@ -874,9 +904,9 @@ async function executeLeg2And3(
874904 status : RebalanceOperationStatus . CANCELLED ,
875905 } ) ;
876906
877- logger . info ( 'Marked operation as FAILED due to Leg 2/3 failure' , {
907+ logger . info ( 'Marked operation as CANCELLED due to Leg 2/3 failure' , {
878908 ...logContext ,
879- note : 'Funds are on Mainnet as USDC - manual intervention may be required' ,
909+ note : 'Funds are on Mainnet as USDC or ptUSDe (depending on which leg failed) - manual intervention required' ,
880910 } ) ;
881911 }
882912}
0 commit comments