forked from aave-dao/aave-v3-origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParaSwapWithdrawSwapAdapter.sol
84 lines (75 loc) · 3.14 KB
/
ParaSwapWithdrawSwapAdapter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.10;
import {IERC20Detailed} from '../../dependencies/openzeppelin/contracts/IERC20Detailed.sol';
import {IERC20WithPermit} from '../../interfaces/IERC20WithPermit.sol';
import {IPoolAddressesProvider} from '../../interfaces/IPoolAddressesProvider.sol';
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 {
using SafeERC20 for IERC20Detailed;
constructor(
IPoolAddressesProvider addressesProvider,
IParaSwapAugustusRegistry augustusRegistry,
address owner
) BaseParaSwapSellAdapter(addressesProvider, augustusRegistry) {
transferOwnership(owner);
}
function executeOperation(
address,
uint256,
uint256,
address,
bytes calldata
) external override nonReentrant returns (bool) {
revert('NOT_SUPPORTED');
}
/**
* @dev Swaps an amount of an asset to another after a withdraw and transfers the new asset to the user.
* The user should give this contract allowance to pull the ATokens in order to withdraw the underlying asset and perform the swap.
* @param assetToSwapFrom Address of the underlying asset to be swapped from
* @param assetToSwapTo Address of the underlying asset to be swapped to
* @param amountToSwap Amount to be swapped, or maximum amount when swapping all balance
* @param minAmountToReceive Minimum amount to be received from the swap
* @param swapAllBalanceOffset Set to offset of fromAmount in Augustus calldata if wanting to swap all balance, otherwise 0
* @param swapCalldata Calldata for ParaSwap's AugustusSwapper contract
* @param augustus Address of ParaSwap's AugustusSwapper contract
* @param permitParams Struct containing the permit signatures, set to all zeroes if not used
*/
function withdrawAndSwap(
IERC20Detailed assetToSwapFrom,
IERC20Detailed assetToSwapTo,
uint256 amountToSwap,
uint256 minAmountToReceive,
uint256 swapAllBalanceOffset,
bytes calldata swapCalldata,
IParaSwapAugustus augustus,
PermitSignature calldata permitParams
) external nonReentrant {
IERC20WithPermit aToken = IERC20WithPermit(POOL.getReserveAToken(address(assetToSwapFrom)));
if (swapAllBalanceOffset != 0) {
uint256 balance = aToken.balanceOf(msg.sender);
require(balance <= amountToSwap, 'INSUFFICIENT_AMOUNT_TO_SWAP');
amountToSwap = balance;
}
_pullATokenAndWithdraw(
address(assetToSwapFrom),
aToken,
msg.sender,
amountToSwap,
permitParams
);
uint256 amountReceived = _sellOnParaSwap(
swapAllBalanceOffset,
swapCalldata,
augustus,
assetToSwapFrom,
assetToSwapTo,
amountToSwap,
minAmountToReceive
);
assetToSwapTo.safeTransfer(msg.sender, amountReceived);
}
}