-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unwind.sol
220 lines (196 loc) · 6.88 KB
/
Unwind.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {IPool} from "aave-v3-core/contracts/interfaces/IPool.sol";
import {DataTypes} from "aave-v3-core/contracts/protocol/libraries/types/DataTypes.sol";
import {IFlashLoanSimpleReceiver, IPoolAddressesProvider} from "aave-v3-core/contracts/flashloan/interfaces/IFlashLoanSimpleReceiver.sol";
using SafeTransferLib for ERC20;
interface IFlashLoanRecipient {
/**
* @dev When `flashLoan` is called on the Vault, it invokes the `receiveFlashLoan` hook on the recipient.
*
* At the time of the call, the Vault will have transferred `amounts` for `tokens` to the recipient. Before this
* call returns, the recipient must have transferred `amounts` plus `feeAmounts` for each token back to the
* Vault, or else the entire flash loan will revert.
*
* `userData` is the same value passed in the `IVault.flashLoan` call.
*/
function receiveFlashLoan(
address[] memory tokens,
uint256[] memory amounts,
uint256[] memory feeAmounts,
bytes memory userData
) external;
}
interface IVault {
function flashLoan(
IFlashLoanRecipient recipient,
address[] memory tokens,
uint256[] memory amounts,
bytes memory userData
) external;
}
contract Unwind is IFlashLoanRecipient, IFlashLoanSimpleReceiver {
error InsufficientOutput(address _token, uint _expected, uint _actual);
IVault constant vault = IVault(0xad68ea482860cd7077a5D0684313dD3a9BC70fbB);
IPool constant pool = IPool(0x794a61358D6845594F94dc1DB02A252b5b4814aD);
bool initiated = false;
address caller = address(0);
function unwind(
address _debtToken,
uint _debtToRepay,
uint _rateMode,
address _collToken,
uint _collToWithdraw,
address _swapper,
bytes calldata _swapData
) external initiate withCaller {
// _balancerFlashLoan(_debtToken, _debtToRepay);
_aaveV3FlashLoan(_debtToken, _debtToRepay);
}
function _aaveV3FlashLoan(address _debtToken, uint _debtToRepay) internal {
pool.flashLoanSimple(
address(this),
_debtToken,
_debtToRepay,
msg.data[4:],
0
);
}
function _balancerFlashLoan(
address _debtToken,
uint _debtToRepay
) internal {
address[] memory tokens = new address[](1);
tokens[0] = _debtToken;
uint[] memory amounts = new uint[](1);
amounts[0] = _debtToRepay;
vault.flashLoan(
IFlashLoanRecipient(this),
tokens,
amounts,
msg.data[4:]
);
}
function _afterFlashLoan(
address _flashBorrowedAsset,
uint _flashBorrowedAmount,
uint _flashBorrowFee,
address _borrowedFrom,
bytes calldata userData
) internal {
(
address _debtToken,
uint _debtToRepay,
uint _rateMode,
address _collToken,
uint _collToWithdraw,
address _swapper,
bytes memory _swapData
) = abi.decode(
userData,
(address, uint, uint, address, uint, address, bytes)
);
// repay debt with flashloaned assets
ERC20(_debtToken).safeApprove(address(pool), _debtToRepay);
pool.repay(_debtToken, _debtToRepay, _rateMode, caller);
// pull aToken from user, withdraw collateral, and transfer back the remainder
DataTypes.ReserveData memory assetReserveData = pool.getReserveData(
_collToken
);
ERC20 collateralAToken = ERC20(assetReserveData.aTokenAddress);
collateralAToken.safeTransferFrom(
caller,
address(this),
_collToWithdraw
);
pool.withdraw(_collToken, _collToWithdraw, address(this));
collateralAToken.safeTransfer(
caller,
collateralAToken.balanceOf(address(this))
);
// swap the collateral for debt token
uint totalRepay = _flashBorrowedAmount + _flashBorrowFee;
ERC20(_collToken).safeApprove(_swapper, _collToWithdraw);
// VERY open. Make sure you don't leave any pending token approvals
// credit delegations or tokens on this contract.
// Revoke after use or approve only required amounts that will
// exhaust all of the approved amount.
(bool success, ) = _swapper.call(_swapData);
if (ERC20(_debtToken).balanceOf(address(this)) < totalRepay)
revert InsufficientOutput(
_debtToken,
totalRepay,
ERC20(_debtToken).balanceOf(address(this))
);
// repay the flashloan
ERC20(_debtToken).safeApprove(address(pool), totalRepay);
// ERC20(_debtToken).safeTransfer(_borrowedFrom, totalRepay);
// transfer back the remainders to the caller
ERC20(_debtToken).safeTransfer(
caller,
ERC20(_debtToken).balanceOf(address(this)) - totalRepay
);
ERC20(_collToken).safeTransfer(
caller,
ERC20(_collToken).balanceOf(address(this))
);
}
function receiveFlashLoan(
address[] memory tokens,
uint256[] memory amounts,
uint256[] memory feeAmounts,
bytes calldata userData
) external override onlyInitiated {
require(msg.sender == address(vault), "Unwind: unauthorized");
require(
tokens.length == amounts.length &&
tokens.length == feeAmounts.length,
"Unwind: invalid arrays"
);
_afterFlashLoan(
tokens[0],
amounts[0],
feeAmounts[0],
address(vault),
userData
);
}
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address initiator,
bytes calldata params
) external override onlyInitiated returns (bool) {
require(msg.sender == address(pool), "Unwind: unauthorized");
require(initiator == address(this), "Unwind: unauthorized");
_afterFlashLoan(asset, amount, premium, initiator, params);
return true;
}
function ADDRESSES_PROVIDER()
external
view
returns (IPoolAddressesProvider)
{
return pool.ADDRESSES_PROVIDER();
}
function POOL() external view returns (IPool) {
return pool;
}
modifier initiate() {
require(!initiated, "Unwind: already initiated");
initiated = true;
_;
initiated = false;
}
modifier onlyInitiated() {
require(initiated, "Unwind: not initiated");
_;
}
modifier withCaller() {
require(caller == address(0), "Unwind: already called");
caller = msg.sender;
_;
caller = address(0);
}
}