diff --git a/src/contracts/BaseParaSwapBuyAdapter.sol b/src/contracts/BaseParaSwapBuyAdapter.sol index 52e1089..8e16f45 100644 --- a/src/contracts/BaseParaSwapBuyAdapter.sol +++ b/src/contracts/BaseParaSwapBuyAdapter.sol @@ -38,6 +38,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter { * @param maxAmountToSwap Max amount to be swapped * @param amountToReceive Amount to be received from the swap * @return amountSold The amount sold during the swap + * @return amountBought The amount bought during the swap */ function _buyOnParaSwap( uint256 toAmountOffset, @@ -46,7 +47,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter { IERC20Detailed assetToSwapTo, uint256 maxAmountToSwap, uint256 amountToReceive - ) internal returns (uint256 amountSold) { + ) internal returns (uint256 amountSold, uint256 amountBought) { (bytes memory buyCalldata, IParaSwapAugustus augustus) = abi.decode( paraswapData, (bytes, IParaSwapAugustus) @@ -73,7 +74,6 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter { uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this)); address tokenTransferProxy = augustus.getTokenTransferProxy(); - assetToSwapFrom.safeApprove(tokenTransferProxy, 0); assetToSwapFrom.safeApprove(tokenTransferProxy, maxAmountToSwap); if (toAmountOffset != 0) { @@ -98,13 +98,15 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter { revert(0, returndatasize()) } } + // Reset allowance + assetToSwapFrom.safeApprove(tokenTransferProxy, 0); uint256 balanceAfterAssetFrom = assetToSwapFrom.balanceOf(address(this)); amountSold = balanceBeforeAssetFrom - balanceAfterAssetFrom; require(amountSold <= maxAmountToSwap, 'WRONG_BALANCE_AFTER_SWAP'); - uint256 amountReceived = assetToSwapTo.balanceOf(address(this)) - balanceBeforeAssetTo; - require(amountReceived >= amountToReceive, 'INSUFFICIENT_AMOUNT_RECEIVED'); + amountBought = assetToSwapTo.balanceOf(address(this)) - balanceBeforeAssetTo; + require(amountBought >= amountToReceive, 'INSUFFICIENT_AMOUNT_RECEIVED'); - emit Bought(address(assetToSwapFrom), address(assetToSwapTo), amountSold, amountReceived); + emit Bought(address(assetToSwapFrom), address(assetToSwapTo), amountSold, amountBought); } } diff --git a/src/contracts/ParaSwapDebtSwapAdapter.sol b/src/contracts/ParaSwapDebtSwapAdapter.sol index 79eadc6..891a4fe 100644 --- a/src/contracts/ParaSwapDebtSwapAdapter.sol +++ b/src/contracts/ParaSwapDebtSwapAdapter.sol @@ -217,7 +217,7 @@ abstract contract ParaSwapDebtSwapAdapter is IERC20Detailed newDebtAsset, uint256 newDebtAmount ) internal returns (uint256) { - uint256 amountSold = _buyOnParaSwap( + (uint256 amountSold, uint256 amountBought) = _buyOnParaSwap( swapParams.offset, swapParams.paraswapData, newDebtAsset, @@ -234,6 +234,12 @@ abstract contract ParaSwapDebtSwapAdapter is swapParams.debtRateMode, swapParams.user ); + + //transfer excess of old debt asset back to the user, if any + uint256 debtAssetExcess = amountBought - swapParams.debtRepayAmount; + if (debtAssetExcess > 0) { + IERC20WithPermit(swapParams.debtAsset).safeTransfer(swapParams.user, debtAssetExcess); + } return amountSold; }