Skip to content

Commit e2758d9

Browse files
authored
refactor: adapt adapters for psp v6 (#49)
1 parent 5ce53a8 commit e2758d9

File tree

9 files changed

+31
-71
lines changed

9 files changed

+31
-71
lines changed

src/contracts/extensions/paraswap-adapters/BaseParaSwapBuyAdapter.sol

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
66
import {PercentageMath} from '../../protocol/libraries/math/PercentageMath.sol';
77
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';
88
import {IERC20Detailed} from '../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
9-
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
109
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
1110
import {BaseParaSwapAdapter} from './BaseParaSwapAdapter.sol';
1211

@@ -49,12 +48,9 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
4948
uint256 maxAmountToSwap,
5049
uint256 amountToReceive
5150
) internal returns (uint256 amountSold, uint256 amountBought) {
52-
(bytes memory buyCalldata, IParaSwapAugustus augustus) = abi.decode(
53-
paraswapData,
54-
(bytes, IParaSwapAugustus)
55-
);
51+
(bytes memory buyCalldata, address augustus) = abi.decode(paraswapData, (bytes, address));
5652

57-
require(AUGUSTUS_REGISTRY.isValidAugustus(address(augustus)), 'INVALID_AUGUSTUS');
53+
require(AUGUSTUS_REGISTRY.isValidAugustus(augustus), 'INVALID_AUGUSTUS');
5854

5955
{
6056
uint256 fromAssetDecimals = _getDecimals(assetToSwapFrom);
@@ -75,8 +71,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
7571
require(balanceBeforeAssetFrom >= maxAmountToSwap, 'INSUFFICIENT_BALANCE_BEFORE_SWAP');
7672
uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this));
7773

78-
address tokenTransferProxy = augustus.getTokenTransferProxy();
79-
assetToSwapFrom.safeApprove(tokenTransferProxy, maxAmountToSwap);
74+
assetToSwapFrom.safeApprove(augustus, maxAmountToSwap);
8075

8176
if (toAmountOffset != 0) {
8277
// Ensure 256 bit (32 bytes) toAmountOffset value is within bounds of the
@@ -92,7 +87,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
9287
mstore(add(buyCalldata, add(toAmountOffset, 32)), amountToReceive)
9388
}
9489
}
95-
(bool success, ) = address(augustus).call(buyCalldata);
90+
(bool success, ) = augustus.call(buyCalldata);
9691
if (!success) {
9792
// Copy revert reason from call
9893
assembly {
@@ -102,7 +97,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
10297
}
10398

10499
// Reset allowance
105-
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
100+
assetToSwapFrom.safeApprove(augustus, 0);
106101

107102
uint256 balanceAfterAssetFrom = assetToSwapFrom.balanceOf(address(this));
108103
amountSold = balanceBeforeAssetFrom - balanceAfterAssetFrom;

src/contracts/extensions/paraswap-adapters/BaseParaSwapSellAdapter.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
66
import {PercentageMath} from '../../protocol/libraries/math/PercentageMath.sol';
77
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';
88
import {IERC20Detailed} from '../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
9-
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
109
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
1110
import {BaseParaSwapAdapter} from './BaseParaSwapAdapter.sol';
1211

@@ -45,7 +44,7 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
4544
function _sellOnParaSwap(
4645
uint256 fromAmountOffset,
4746
bytes memory swapCalldata,
48-
IParaSwapAugustus augustus,
47+
address augustus,
4948
IERC20Detailed assetToSwapFrom,
5049
IERC20Detailed assetToSwapTo,
5150
uint256 amountToSwap,
@@ -72,9 +71,7 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
7271
require(balanceBeforeAssetFrom >= amountToSwap, 'INSUFFICIENT_BALANCE_BEFORE_SWAP');
7372
uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this));
7473

75-
address tokenTransferProxy = augustus.getTokenTransferProxy();
76-
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
77-
assetToSwapFrom.safeApprove(tokenTransferProxy, amountToSwap);
74+
assetToSwapFrom.safeApprove(augustus, amountToSwap);
7875

7976
if (fromAmountOffset != 0) {
8077
// Ensure 256 bit (32 bytes) fromAmount value is within bounds of the
@@ -90,7 +87,7 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
9087
mstore(add(swapCalldata, add(fromAmountOffset, 32)), amountToSwap)
9188
}
9289
}
93-
(bool success, ) = address(augustus).call(swapCalldata);
90+
(bool success, ) = augustus.call(swapCalldata);
9491
if (!success) {
9592
// Copy revert reason from call
9693
assembly {
@@ -99,6 +96,9 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
9996
}
10097
}
10198

99+
// Reset allowance
100+
assetToSwapFrom.safeApprove(augustus, 0);
101+
102102
require(
103103
assetToSwapFrom.balanceOf(address(this)) == balanceBeforeAssetFrom - amountToSwap,
104104
'WRONG_BALANCE_AFTER_SWAP'

src/contracts/extensions/paraswap-adapters/ParaSwapLiquiditySwapAdapter.sol

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol
88
import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
99
import {BaseParaSwapSellAdapter} from './BaseParaSwapSellAdapter.sol';
1010
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
11-
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
1211
import {ReentrancyGuard} from '../../dependencies/openzeppelin/ReentrancyGuard.sol';
1312

1413
/**
@@ -63,12 +62,9 @@ contract ParaSwapLiquiditySwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuar
6362
uint256 minAmountToReceive,
6463
uint256 swapAllBalanceOffset,
6564
bytes memory swapCalldata,
66-
IParaSwapAugustus augustus,
65+
address augustus,
6766
PermitSignature memory permitParams
68-
) = abi.decode(
69-
params,
70-
(IERC20Detailed, uint256, uint256, bytes, IParaSwapAugustus, PermitSignature)
71-
);
67+
) = abi.decode(params, (IERC20Detailed, uint256, uint256, bytes, address, PermitSignature));
7268

7369
_swapLiquidity(
7470
swapAllBalanceOffset,
@@ -106,7 +102,7 @@ contract ParaSwapLiquiditySwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuar
106102
uint256 minAmountToReceive,
107103
uint256 swapAllBalanceOffset,
108104
bytes calldata swapCalldata,
109-
IParaSwapAugustus augustus,
105+
address augustus,
110106
PermitSignature calldata permitParams
111107
) external nonReentrant {
112108
IERC20WithPermit aToken = IERC20WithPermit(POOL.getReserveAToken(address(assetToSwapFrom)));
@@ -156,7 +152,7 @@ contract ParaSwapLiquiditySwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuar
156152
function _swapLiquidity(
157153
uint256 swapAllBalanceOffset,
158154
bytes memory swapCalldata,
159-
IParaSwapAugustus augustus,
155+
address augustus,
160156
PermitSignature memory permitParams,
161157
uint256 flashLoanAmount,
162158
uint256 premium,

src/contracts/extensions/paraswap-adapters/ParaSwapRepayAdapter.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol
1010
import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
1111
import {BaseParaSwapBuyAdapter} from './BaseParaSwapBuyAdapter.sol';
1212
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
13-
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
1413
import {ReentrancyGuard} from '../../dependencies/openzeppelin/ReentrancyGuard.sol';
1514

1615
/**
@@ -57,7 +56,7 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
5756
* uint256 debtRateMode Rate mode of the debt to be repaid
5857
* bytes paraswapData Paraswap Data
5958
* * bytes buyCallData Call data for augustus
60-
* * IParaSwapAugustus augustus Address of Augustus Swapper
59+
* * address augustus Address of Augustus Swapper
6160
* PermitSignature permitParams Struct containing the permit signatures, set to all zeroes if not used
6261
*/
6362
function executeOperation(

src/contracts/extensions/paraswap-adapters/ParaSwapWithdrawSwapAdapter.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.so
77
import {BaseParaSwapSellAdapter} from './BaseParaSwapSellAdapter.sol';
88
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
99
import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol';
10-
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
1110
import {ReentrancyGuard} from '../../dependencies/openzeppelin/ReentrancyGuard.sol';
1211

1312
contract ParaSwapWithdrawSwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuard {
@@ -50,7 +49,7 @@ contract ParaSwapWithdrawSwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuard
5049
uint256 minAmountToReceive,
5150
uint256 swapAllBalanceOffset,
5251
bytes calldata swapCalldata,
53-
IParaSwapAugustus augustus,
52+
address augustus,
5453
PermitSignature calldata permitParams
5554
) external nonReentrant {
5655
IERC20WithPermit aToken = IERC20WithPermit(POOL.getReserveAToken(address(assetToSwapFrom)));

src/contracts/extensions/paraswap-adapters/interfaces/IParaSwapAugustus.sol

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/contracts/mocks/swap/MockParaSwapAugustus.sol

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.10;
33

4-
import {IParaSwapAugustus} from '../../extensions/paraswap-adapters/interfaces/IParaSwapAugustus.sol';
5-
import {MockParaSwapTokenTransferProxy} from './MockParaSwapTokenTransferProxy.sol';
64
import {IERC20} from '../../dependencies/openzeppelin/contracts/IERC20.sol';
75
import {MintableERC20} from '../tokens/MintableERC20.sol';
86

9-
contract MockParaSwapAugustus is IParaSwapAugustus {
10-
MockParaSwapTokenTransferProxy immutable TOKEN_TRANSFER_PROXY;
7+
contract MockParaSwapAugustus {
118
bool _expectingSwap;
129
address _expectedFromToken;
1310
address _expectedToToken;
@@ -20,14 +17,6 @@ contract MockParaSwapAugustus is IParaSwapAugustus {
2017
uint256 _expectedToAmountMax;
2118
uint256 _expectedToAmountMin;
2219

23-
constructor() {
24-
TOKEN_TRANSFER_PROXY = new MockParaSwapTokenTransferProxy();
25-
}
26-
27-
function getTokenTransferProxy() external view override returns (address) {
28-
return address(TOKEN_TRANSFER_PROXY);
29-
}
30-
3120
function expectSwap(
3221
address fromToken,
3322
address toToken,
@@ -72,7 +61,7 @@ contract MockParaSwapAugustus is IParaSwapAugustus {
7261
'From amount out of range'
7362
);
7463
require(_receivedAmount >= toAmount, 'Received amount of tokens are less than expected');
75-
TOKEN_TRANSFER_PROXY.transferFrom(fromToken, msg.sender, address(this), fromAmount);
64+
_transferFrom(fromToken, msg.sender, address(this), fromAmount);
7665
MintableERC20(toToken).mint(_receivedAmount);
7766
IERC20(toToken).transfer(msg.sender, _receivedAmount);
7867
_expectingSwap = false;
@@ -93,9 +82,13 @@ contract MockParaSwapAugustus is IParaSwapAugustus {
9382
'To amount out of range'
9483
);
9584
require(_fromAmount <= fromAmount, 'From amount of tokens are higher than expected');
96-
TOKEN_TRANSFER_PROXY.transferFrom(fromToken, msg.sender, address(this), _fromAmount);
85+
_transferFrom(fromToken, msg.sender, address(this), _fromAmount);
9786
MintableERC20(toToken).mint(msg.sender, toAmount);
9887
_expectingSwap = false;
9988
return fromAmount;
10089
}
90+
91+
function _transferFrom(address token, address from, address to, uint256 amount) internal {
92+
IERC20(token).transferFrom(from, to, amount);
93+
}
10194
}

src/contracts/mocks/swap/MockParaSwapTokenTransferProxy.sol

Lines changed: 0 additions & 16 deletions
This file was deleted.

tests/extensions/paraswap-adapters/ParaswapAdapters.t.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: BUSL-1.1
22
pragma solidity ^0.8.0;
33

4-
import {ParaSwapLiquiditySwapAdapter, IParaSwapAugustus} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapLiquiditySwapAdapter.sol';
4+
import {ParaSwapLiquiditySwapAdapter} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapLiquiditySwapAdapter.sol';
55
import {ParaSwapRepayAdapter, IParaSwapAugustusRegistry} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapRepayAdapter.sol';
66
import {ParaSwapWithdrawSwapAdapter} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapWithdrawSwapAdapter.sol';
77
import {BaseParaSwapAdapter} from '../../../src/contracts/extensions/paraswap-adapters/BaseParaSwapAdapter.sol';
@@ -310,7 +310,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
310310
expectedUsdxAmount,
311311
0,
312312
augustusInput,
313-
IParaSwapAugustus(address(mockParaSwapAugustus)),
313+
address(mockParaSwapAugustus),
314314
emptyPermit
315315
);
316316
}
@@ -372,7 +372,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
372372
expectedUsdxAmount,
373373
0,
374374
augustusInput,
375-
IParaSwapAugustus(address(mockParaSwapAugustus)),
375+
address(mockParaSwapAugustus),
376376
permitInput
377377
);
378378
}
@@ -414,7 +414,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
414414
expectedUsdxAmount,
415415
amountToSwap,
416416
augustusInput,
417-
IParaSwapAugustus(address(mockParaSwapAugustus)),
417+
address(mockParaSwapAugustus),
418418
emptyPermit
419419
);
420420
}
@@ -794,7 +794,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
794794
expectedUsdxAmount,
795795
0,
796796
augustusInput,
797-
IParaSwapAugustus(address(mockParaSwapAugustus)),
797+
address(mockParaSwapAugustus),
798798
emptyPermit
799799
);
800800

@@ -856,7 +856,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
856856
expectedUsdxAmount,
857857
0,
858858
augustusInput,
859-
IParaSwapAugustus(address(mockParaSwapAugustus)),
859+
address(mockParaSwapAugustus),
860860
permitInput
861861
);
862862

@@ -899,7 +899,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
899899
expectedUsdxAmount,
900900
amountToSwap,
901901
augustusInput,
902-
IParaSwapAugustus(address(mockParaSwapAugustus)),
902+
address(mockParaSwapAugustus),
903903
emptyPermit
904904
);
905905

0 commit comments

Comments
 (0)