@@ -170,12 +170,12 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
170170 * @param buyAllBalanceOffset Set to offset of fromAmount in Augustus calldata if wanting to swap all balance, otherwise 0
171171 * @param paraswapData Paraswap data
172172 * @param permitSignature struct containing the permit signature
173- * @param debtRepayAmount Amount of the debt to be repaid, or maximum amount when repaying entire debt(flash loan amount
173+ * @param debtRepayAmount Amount of the debt to be repaid, or maximum amount when repaying entire debt
174174 * @param premium Fee of the flash loan
175175 * @param initiator Address of the user
176176 * @param collateralAsset Address of token to be swapped
177177 * @param debtAsset Address of debt token to be received from the swap
178- * @param collateralAmount Amount of the reserve to be swapped
178+ * @param collateralAmount Amount of the reserve to be swapped(flash loan amount)
179179 * @param rateMode Rate mode of the debt to be repaid
180180 */
181181
@@ -190,22 +190,14 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
190190 IERC20Detailed debtAsset ,
191191 uint256 collateralAmount ,
192192 uint256 rateMode
193- ) internal {
194- // Repay debt. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
195- IERC20 (debtAsset).safeApprove (address (LENDING_POOL), 0 );
196- IERC20 (debtAsset).safeApprove (address (LENDING_POOL), debtRepayAmount);
197- uint256 repaidAmount = IERC20 (debtAsset).balanceOf (address (this ));
198- LENDING_POOL.repay (address (debtAsset), debtRepayAmount, rateMode, initiator);
199- repaidAmount = repaidAmount.sub (IERC20 (debtAsset).balanceOf (address (this )));
200-
201- uint256 neededForFlashLoanDebt = repaidAmount.add (premium);
202-
203- if (repaidAmount < debtRepayAmount) {
204- collateralAmount = collateralAmount.mul (repaidAmount).div (debtRepayAmount);
205- }
206-
207- // Pull aTokens from user
208- _pullATokenAndWithdraw (address (collateralAsset), initiator, collateralAmount, permitSignature);
193+ ) private {
194+ debtRepayAmount = getDebtRepayAmount (
195+ debtAsset,
196+ rateMode,
197+ buyAllBalanceOffset,
198+ debtRepayAmount,
199+ initiator
200+ );
209201
210202 uint256 amountSold =
211203 _buyOnParaSwap (
@@ -214,18 +206,52 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
214206 collateralAsset,
215207 debtAsset,
216208 collateralAmount,
217- neededForFlashLoanDebt
209+ debtRepayAmount
218210 );
219211
220- uint256 collateralBalanceLeft = collateralAmount - amountSold;
212+ // Repay debt. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
213+ IERC20 (debtAsset).safeApprove (address (LENDING_POOL), 0 );
214+ IERC20 (debtAsset).safeApprove (address (LENDING_POOL), debtRepayAmount);
215+ LENDING_POOL.repay (address (debtAsset), debtRepayAmount, rateMode, initiator);
221216
222- //deposit collateral back in the pool, if left after the swap(buy)
223- if (collateralBalanceLeft > 0 ) {
224- LENDING_POOL.deposit (address (collateralAsset), collateralBalanceLeft, initiator, 0 );
225- }
217+ uint256 neededForFlashLoanRepay = amountSold.add (premium);
218+
219+ // Pull aTokens from user
220+ _pullATokenAndWithdraw (
221+ address (collateralAsset),
222+ initiator,
223+ neededForFlashLoanRepay,
224+ permitSignature
225+ );
226226
227227 // Repay flashloan. Approves for 0 first to comply with tokens that implement the anti frontrunning approval fix.
228- IERC20 (debtAsset).safeApprove (address (LENDING_POOL), 0 );
229- IERC20 (debtAsset).safeApprove (address (LENDING_POOL), debtRepayAmount.add (premium));
228+ IERC20 (collateralAsset).safeApprove (address (LENDING_POOL), 0 );
229+ IERC20 (collateralAsset).safeApprove (address (LENDING_POOL), collateralAmount.add (premium));
230+ }
231+
232+ function getDebtRepayAmount (
233+ IERC20Detailed debtAsset ,
234+ uint256 rateMode ,
235+ uint256 buyAllBalanceOffset ,
236+ uint256 debtRepayAmount ,
237+ address initiator
238+ ) private returns (uint256 ) {
239+ DataTypes.ReserveData memory debtReserveData = _getReserveData (address (debtAsset));
240+
241+ address debtToken =
242+ DataTypes.InterestRateMode (rateMode) == DataTypes.InterestRateMode.STABLE
243+ ? debtReserveData.stableDebtTokenAddress
244+ : debtReserveData.variableDebtTokenAddress;
245+
246+ uint256 currentDebt = IERC20 (debtToken).balanceOf (initiator);
247+
248+ if (buyAllBalanceOffset != 0 ) {
249+ require (currentDebt <= debtRepayAmount, 'INSUFFICIENT_AMOUNT_TO_REPAY ' );
250+ debtRepayAmount = currentDebt;
251+ } else {
252+ require (debtRepayAmount <= currentDebt, 'INVALID_DEBT_REPAY_AMOUNT ' );
253+ }
254+
255+ return debtRepayAmount;
230256 }
231257}
0 commit comments