Skip to content

Commit 45a8a9f

Browse files
authored
fix: Transfer excess back and reset allowance (#28)
1 parent 5bb44b2 commit 45a8a9f

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/contracts/BaseParaSwapBuyAdapter.sol

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
3838
* @param maxAmountToSwap Max amount to be swapped
3939
* @param amountToReceive Amount to be received from the swap
4040
* @return amountSold The amount sold during the swap
41+
* @return amountBought The amount bought during the swap
4142
*/
4243
function _buyOnParaSwap(
4344
uint256 toAmountOffset,
@@ -46,7 +47,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
4647
IERC20Detailed assetToSwapTo,
4748
uint256 maxAmountToSwap,
4849
uint256 amountToReceive
49-
) internal returns (uint256 amountSold) {
50+
) internal returns (uint256 amountSold, uint256 amountBought) {
5051
(bytes memory buyCalldata, IParaSwapAugustus augustus) = abi.decode(
5152
paraswapData,
5253
(bytes, IParaSwapAugustus)
@@ -73,7 +74,6 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
7374
uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this));
7475

7576
address tokenTransferProxy = augustus.getTokenTransferProxy();
76-
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
7777
assetToSwapFrom.safeApprove(tokenTransferProxy, maxAmountToSwap);
7878

7979
if (toAmountOffset != 0) {
@@ -98,13 +98,15 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
9898
revert(0, returndatasize())
9999
}
100100
}
101+
// Reset allowance
102+
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
101103

102104
uint256 balanceAfterAssetFrom = assetToSwapFrom.balanceOf(address(this));
103105
amountSold = balanceBeforeAssetFrom - balanceAfterAssetFrom;
104106
require(amountSold <= maxAmountToSwap, 'WRONG_BALANCE_AFTER_SWAP');
105-
uint256 amountReceived = assetToSwapTo.balanceOf(address(this)) - balanceBeforeAssetTo;
106-
require(amountReceived >= amountToReceive, 'INSUFFICIENT_AMOUNT_RECEIVED');
107+
amountBought = assetToSwapTo.balanceOf(address(this)) - balanceBeforeAssetTo;
108+
require(amountBought >= amountToReceive, 'INSUFFICIENT_AMOUNT_RECEIVED');
107109

108-
emit Bought(address(assetToSwapFrom), address(assetToSwapTo), amountSold, amountReceived);
110+
emit Bought(address(assetToSwapFrom), address(assetToSwapTo), amountSold, amountBought);
109111
}
110112
}

src/contracts/ParaSwapDebtSwapAdapter.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ abstract contract ParaSwapDebtSwapAdapter is
217217
IERC20Detailed newDebtAsset,
218218
uint256 newDebtAmount
219219
) internal returns (uint256) {
220-
uint256 amountSold = _buyOnParaSwap(
220+
(uint256 amountSold, uint256 amountBought) = _buyOnParaSwap(
221221
swapParams.offset,
222222
swapParams.paraswapData,
223223
newDebtAsset,
@@ -234,6 +234,12 @@ abstract contract ParaSwapDebtSwapAdapter is
234234
swapParams.debtRateMode,
235235
swapParams.user
236236
);
237+
238+
//transfer excess of old debt asset back to the user, if any
239+
uint256 debtAssetExcess = amountBought - swapParams.debtRepayAmount;
240+
if (debtAssetExcess > 0) {
241+
IERC20WithPermit(swapParams.debtAsset).safeTransfer(swapParams.user, debtAssetExcess);
242+
}
237243
return amountSold;
238244
}
239245

0 commit comments

Comments
 (0)