@@ -114,7 +114,7 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
114114 // Pull aTokens from user
115115 _pullATokenAndWithdraw (address (collateralAsset), msg .sender , collateralAmount, permitSignature);
116116 //buy debt asset using collateral asset
117- uint256 amountSold = _buyOnParaSwap (
117+ ( uint256 amountSold , uint256 amountBought ) = _buyOnParaSwap (
118118 buyAllBalanceOffset,
119119 paraswapData,
120120 collateralAsset,
@@ -127,15 +127,23 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
127127
128128 //deposit collateral back in the pool, if left after the swap(buy)
129129 if (collateralBalanceLeft > 0 ) {
130- IERC20 (collateralAsset).safeApprove (address (POOL), 0 );
131130 IERC20 (collateralAsset).safeApprove (address (POOL), collateralBalanceLeft);
132131 POOL.deposit (address (collateralAsset), collateralBalanceLeft, msg .sender , 0 );
132+ IERC20 (collateralAsset).safeApprove (address (POOL), 0 );
133133 }
134134
135135 // Repay debt. Approves 0 first to comply with tokens that implement the anti frontrunning approval fix
136- IERC20 (debtAsset).safeApprove (address (POOL), 0 );
137136 IERC20 (debtAsset).safeApprove (address (POOL), debtRepayAmount);
138137 POOL.repay (address (debtAsset), debtRepayAmount, debtRateMode, msg .sender );
138+ IERC20 (debtAsset).safeApprove (address (POOL), 0 );
139+
140+ {
141+ //transfer excess of debtAsset back to the user, if any
142+ uint256 debtAssetExcess = amountBought - debtRepayAmount;
143+ if (debtAssetExcess > 0 ) {
144+ IERC20 (debtAsset).safeTransfer (msg .sender , debtAssetExcess);
145+ }
146+ }
139147 }
140148
141149 /**
@@ -170,7 +178,7 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
170178 initiator
171179 );
172180
173- uint256 amountSold = _buyOnParaSwap (
181+ ( uint256 amountSold , uint256 amountBought ) = _buyOnParaSwap (
174182 buyAllBalanceOffset,
175183 paraswapData,
176184 collateralAsset,
@@ -180,9 +188,9 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
180188 );
181189
182190 // Repay debt. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
183- IERC20 (debtAsset).safeApprove (address (POOL), 0 );
184191 IERC20 (debtAsset).safeApprove (address (POOL), debtRepayAmount);
185192 POOL.repay (address (debtAsset), debtRepayAmount, rateMode, initiator);
193+ IERC20 (debtAsset).safeApprove (address (POOL), 0 );
186194
187195 uint256 neededForFlashLoanRepay = amountSold.add (premium);
188196
@@ -194,6 +202,14 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
194202 permitSignature
195203 );
196204
205+ {
206+ //transfer excess of debtAsset back to the user, if any
207+ uint256 debtAssetExcess = amountBought - debtRepayAmount;
208+ if (debtAssetExcess > 0 ) {
209+ IERC20 (debtAsset).safeTransfer (initiator, debtAssetExcess);
210+ }
211+ }
212+
197213 // Repay flashloan. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
198214 IERC20 (collateralAsset).safeApprove (address (POOL), 0 );
199215 IERC20 (collateralAsset).safeApprove (address (POOL), collateralAmount.add (premium));
0 commit comments