Skip to content

Commit

Permalink
refactor: adapt adapters for psp v6 (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra authored Feb 12, 2025
1 parent 5ce53a8 commit e2758d9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
import {PercentageMath} from '../../protocol/libraries/math/PercentageMath.sol';
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';
import {IERC20Detailed} from '../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
import {BaseParaSwapAdapter} from './BaseParaSwapAdapter.sol';

Expand Down Expand Up @@ -49,12 +48,9 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
uint256 maxAmountToSwap,
uint256 amountToReceive
) internal returns (uint256 amountSold, uint256 amountBought) {
(bytes memory buyCalldata, IParaSwapAugustus augustus) = abi.decode(
paraswapData,
(bytes, IParaSwapAugustus)
);
(bytes memory buyCalldata, address augustus) = abi.decode(paraswapData, (bytes, address));

require(AUGUSTUS_REGISTRY.isValidAugustus(address(augustus)), 'INVALID_AUGUSTUS');
require(AUGUSTUS_REGISTRY.isValidAugustus(augustus), 'INVALID_AUGUSTUS');

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

address tokenTransferProxy = augustus.getTokenTransferProxy();
assetToSwapFrom.safeApprove(tokenTransferProxy, maxAmountToSwap);
assetToSwapFrom.safeApprove(augustus, maxAmountToSwap);

if (toAmountOffset != 0) {
// Ensure 256 bit (32 bytes) toAmountOffset value is within bounds of the
Expand All @@ -92,7 +87,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
mstore(add(buyCalldata, add(toAmountOffset, 32)), amountToReceive)
}
}
(bool success, ) = address(augustus).call(buyCalldata);
(bool success, ) = augustus.call(buyCalldata);
if (!success) {
// Copy revert reason from call
assembly {
Expand All @@ -102,7 +97,7 @@ abstract contract BaseParaSwapBuyAdapter is BaseParaSwapAdapter {
}

// Reset allowance
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
assetToSwapFrom.safeApprove(augustus, 0);

uint256 balanceAfterAssetFrom = assetToSwapFrom.balanceOf(address(this));
amountSold = balanceBeforeAssetFrom - balanceAfterAssetFrom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
import {PercentageMath} from '../../protocol/libraries/math/PercentageMath.sol';
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';
import {IERC20Detailed} from '../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
import {BaseParaSwapAdapter} from './BaseParaSwapAdapter.sol';

Expand Down Expand Up @@ -45,7 +44,7 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
function _sellOnParaSwap(
uint256 fromAmountOffset,
bytes memory swapCalldata,
IParaSwapAugustus augustus,
address augustus,
IERC20Detailed assetToSwapFrom,
IERC20Detailed assetToSwapTo,
uint256 amountToSwap,
Expand All @@ -72,9 +71,7 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
require(balanceBeforeAssetFrom >= amountToSwap, 'INSUFFICIENT_BALANCE_BEFORE_SWAP');
uint256 balanceBeforeAssetTo = assetToSwapTo.balanceOf(address(this));

address tokenTransferProxy = augustus.getTokenTransferProxy();
assetToSwapFrom.safeApprove(tokenTransferProxy, 0);
assetToSwapFrom.safeApprove(tokenTransferProxy, amountToSwap);
assetToSwapFrom.safeApprove(augustus, amountToSwap);

if (fromAmountOffset != 0) {
// Ensure 256 bit (32 bytes) fromAmount value is within bounds of the
Expand All @@ -90,7 +87,7 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
mstore(add(swapCalldata, add(fromAmountOffset, 32)), amountToSwap)
}
}
(bool success, ) = address(augustus).call(swapCalldata);
(bool success, ) = augustus.call(swapCalldata);
if (!success) {
// Copy revert reason from call
assembly {
Expand All @@ -99,6 +96,9 @@ abstract contract BaseParaSwapSellAdapter is BaseParaSwapAdapter {
}
}

// Reset allowance
assetToSwapFrom.safeApprove(augustus, 0);

require(
assetToSwapFrom.balanceOf(address(this)) == balanceBeforeAssetFrom - amountToSwap,
'WRONG_BALANCE_AFTER_SWAP'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol
import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
import {BaseParaSwapSellAdapter} from './BaseParaSwapSellAdapter.sol';
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
import {ReentrancyGuard} from '../../dependencies/openzeppelin/ReentrancyGuard.sol';

/**
Expand Down Expand Up @@ -63,12 +62,9 @@ contract ParaSwapLiquiditySwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuar
uint256 minAmountToReceive,
uint256 swapAllBalanceOffset,
bytes memory swapCalldata,
IParaSwapAugustus augustus,
address augustus,
PermitSignature memory permitParams
) = abi.decode(
params,
(IERC20Detailed, uint256, uint256, bytes, IParaSwapAugustus, PermitSignature)
);
) = abi.decode(params, (IERC20Detailed, uint256, uint256, bytes, address, PermitSignature));

_swapLiquidity(
swapAllBalanceOffset,
Expand Down Expand Up @@ -106,7 +102,7 @@ contract ParaSwapLiquiditySwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuar
uint256 minAmountToReceive,
uint256 swapAllBalanceOffset,
bytes calldata swapCalldata,
IParaSwapAugustus augustus,
address augustus,
PermitSignature calldata permitParams
) external nonReentrant {
IERC20WithPermit aToken = IERC20WithPermit(POOL.getReserveAToken(address(assetToSwapFrom)));
Expand Down Expand Up @@ -156,7 +152,7 @@ contract ParaSwapLiquiditySwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuar
function _swapLiquidity(
uint256 swapAllBalanceOffset,
bytes memory swapCalldata,
IParaSwapAugustus augustus,
address augustus,
PermitSignature memory permitParams,
uint256 flashLoanAmount,
uint256 premium,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol
import {SafeMath} from '../../dependencies/openzeppelin/contracts/SafeMath.sol';
import {BaseParaSwapBuyAdapter} from './BaseParaSwapBuyAdapter.sol';
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
import {ReentrancyGuard} from '../../dependencies/openzeppelin/ReentrancyGuard.sol';

/**
Expand Down Expand Up @@ -57,7 +56,7 @@ contract ParaSwapRepayAdapter is BaseParaSwapBuyAdapter, ReentrancyGuard {
* uint256 debtRateMode Rate mode of the debt to be repaid
* bytes paraswapData Paraswap Data
* * bytes buyCallData Call data for augustus
* * IParaSwapAugustus augustus Address of Augustus Swapper
* * address augustus Address of Augustus Swapper
* PermitSignature permitParams Struct containing the permit signatures, set to all zeroes if not used
*/
function executeOperation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.so
import {BaseParaSwapSellAdapter} from './BaseParaSwapSellAdapter.sol';
import {IParaSwapAugustusRegistry} from './interfaces/IParaSwapAugustusRegistry.sol';
import {SafeERC20} from '../../dependencies/openzeppelin/contracts/SafeERC20.sol';
import {IParaSwapAugustus} from './interfaces/IParaSwapAugustus.sol';
import {ReentrancyGuard} from '../../dependencies/openzeppelin/ReentrancyGuard.sol';

contract ParaSwapWithdrawSwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuard {
Expand Down Expand Up @@ -50,7 +49,7 @@ contract ParaSwapWithdrawSwapAdapter is BaseParaSwapSellAdapter, ReentrancyGuard
uint256 minAmountToReceive,
uint256 swapAllBalanceOffset,
bytes calldata swapCalldata,
IParaSwapAugustus augustus,
address augustus,
PermitSignature calldata permitParams
) external nonReentrant {
IERC20WithPermit aToken = IERC20WithPermit(POOL.getReserveAToken(address(assetToSwapFrom)));
Expand Down

This file was deleted.

21 changes: 7 additions & 14 deletions src/contracts/mocks/swap/MockParaSwapAugustus.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

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

contract MockParaSwapAugustus is IParaSwapAugustus {
MockParaSwapTokenTransferProxy immutable TOKEN_TRANSFER_PROXY;
contract MockParaSwapAugustus {
bool _expectingSwap;
address _expectedFromToken;
address _expectedToToken;
Expand All @@ -20,14 +17,6 @@ contract MockParaSwapAugustus is IParaSwapAugustus {
uint256 _expectedToAmountMax;
uint256 _expectedToAmountMin;

constructor() {
TOKEN_TRANSFER_PROXY = new MockParaSwapTokenTransferProxy();
}

function getTokenTransferProxy() external view override returns (address) {
return address(TOKEN_TRANSFER_PROXY);
}

function expectSwap(
address fromToken,
address toToken,
Expand Down Expand Up @@ -72,7 +61,7 @@ contract MockParaSwapAugustus is IParaSwapAugustus {
'From amount out of range'
);
require(_receivedAmount >= toAmount, 'Received amount of tokens are less than expected');
TOKEN_TRANSFER_PROXY.transferFrom(fromToken, msg.sender, address(this), fromAmount);
_transferFrom(fromToken, msg.sender, address(this), fromAmount);
MintableERC20(toToken).mint(_receivedAmount);
IERC20(toToken).transfer(msg.sender, _receivedAmount);
_expectingSwap = false;
Expand All @@ -93,9 +82,13 @@ contract MockParaSwapAugustus is IParaSwapAugustus {
'To amount out of range'
);
require(_fromAmount <= fromAmount, 'From amount of tokens are higher than expected');
TOKEN_TRANSFER_PROXY.transferFrom(fromToken, msg.sender, address(this), _fromAmount);
_transferFrom(fromToken, msg.sender, address(this), _fromAmount);
MintableERC20(toToken).mint(msg.sender, toAmount);
_expectingSwap = false;
return fromAmount;
}

function _transferFrom(address token, address from, address to, uint256 amount) internal {
IERC20(token).transferFrom(from, to, amount);
}
}
16 changes: 0 additions & 16 deletions src/contracts/mocks/swap/MockParaSwapTokenTransferProxy.sol

This file was deleted.

14 changes: 7 additions & 7 deletions tests/extensions/paraswap-adapters/ParaswapAdapters.t.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;

import {ParaSwapLiquiditySwapAdapter, IParaSwapAugustus} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapLiquiditySwapAdapter.sol';
import {ParaSwapLiquiditySwapAdapter} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapLiquiditySwapAdapter.sol';
import {ParaSwapRepayAdapter, IParaSwapAugustusRegistry} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapRepayAdapter.sol';
import {ParaSwapWithdrawSwapAdapter} from '../../../src/contracts/extensions/paraswap-adapters/ParaSwapWithdrawSwapAdapter.sol';
import {BaseParaSwapAdapter} from '../../../src/contracts/extensions/paraswap-adapters/BaseParaSwapAdapter.sol';
Expand Down Expand Up @@ -310,7 +310,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
expectedUsdxAmount,
0,
augustusInput,
IParaSwapAugustus(address(mockParaSwapAugustus)),
address(mockParaSwapAugustus),
emptyPermit
);
}
Expand Down Expand Up @@ -372,7 +372,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
expectedUsdxAmount,
0,
augustusInput,
IParaSwapAugustus(address(mockParaSwapAugustus)),
address(mockParaSwapAugustus),
permitInput
);
}
Expand Down Expand Up @@ -414,7 +414,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
expectedUsdxAmount,
amountToSwap,
augustusInput,
IParaSwapAugustus(address(mockParaSwapAugustus)),
address(mockParaSwapAugustus),
emptyPermit
);
}
Expand Down Expand Up @@ -794,7 +794,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
expectedUsdxAmount,
0,
augustusInput,
IParaSwapAugustus(address(mockParaSwapAugustus)),
address(mockParaSwapAugustus),
emptyPermit
);

Expand Down Expand Up @@ -856,7 +856,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
expectedUsdxAmount,
0,
augustusInput,
IParaSwapAugustus(address(mockParaSwapAugustus)),
address(mockParaSwapAugustus),
permitInput
);

Expand Down Expand Up @@ -899,7 +899,7 @@ contract ParaswapAdaptersTest is TestnetProcedures {
expectedUsdxAmount,
amountToSwap,
augustusInput,
IParaSwapAugustus(address(mockParaSwapAugustus)),
address(mockParaSwapAugustus),
emptyPermit
);

Expand Down

0 comments on commit e2758d9

Please sign in to comment.