Update adapters to use the new flashloan interface

This commit is contained in:
Gerardo Nardelli 2020-10-27 11:22:16 -03:00
parent 723e3f82a6
commit 96e74cf707
3 changed files with 45 additions and 48 deletions

View File

@ -28,18 +28,18 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
* The received funds from the swap are then deposited into the protocol on behalf of the user.
* The user should give this contract allowance to pull the ATokens in order to withdraw the underlying asset and
* repay the flash loan.
* @param reserve Address to be swapped
* @param amount Amount of the reserve to be swapped
* @param fee Fee of the flash loan
* @param assets Address to be swapped
* @param amounts Amount of the reserve to be swapped
* @param premiums Fee of the flash loan
* @param params Additional variadic field to include extra params. Expected parameters:
* address assetToSwapTo Address of the reserve to be swapped to and deposited
* address user The address of the user
* uint256 slippage The max slippage percentage allowed for the swap
*/
function executeOperation(
address reserve,
uint256 amount,
uint256 fee,
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
bytes calldata params
) external override returns (bool) {
(
@ -49,14 +49,14 @@ contract UniswapLiquiditySwapAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
) = abi.decode(params, (address, address, uint256));
require(slippage < MAX_SLIPPAGE_PERCENT && slippage >= MIN_SLIPPAGE_PERCENT, 'SLIPPAGE_OUT_OF_RANGE');
uint256 receivedAmount = swapExactTokensForTokens(reserve, assetToSwapTo, amount, slippage);
uint256 receivedAmount = swapExactTokensForTokens(assets[0], assetToSwapTo, amounts[0], slippage);
// Deposit new reserve
IERC20(assetToSwapTo).approve(address(pool), receivedAmount);
pool.deposit(assetToSwapTo, receivedAmount, user, 0);
uint256 flashLoanDebt = amount.add(fee);
pullATokenAndRepayFlashLoan(reserve, user, flashLoanDebt);
uint256 flashLoanDebt = amounts[0].add(premiums[0]);
pullATokenAndRepayFlashLoan(assets[0], user, flashLoanDebt);
return true;
}

View File

@ -28,9 +28,9 @@ contract UniswapRepayAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
* then used to repay a debt on the protocol on behalf of the user.
* The user should give this contract allowance to pull the ATokens in order to withdraw the underlying asset and
* repay the flash loan.
* @param reserve Address to be swapped
* @param amount Amount of the reserve to be swapped
* @param fee Fee of the flash loan
* @param assets Address to be swapped
* @param amounts Amount of the reserve to be swapped
* @param premiums Fee of the flash loan
* @param params Additional variadic field to include extra params. Expected parameters:
* address assetToSwapTo Address of the reserve to be swapped to and deposited
* address user The address of the user
@ -41,9 +41,9 @@ contract UniswapRepayAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
* uint256 rateMode The rate modes of the debt to be repaid
*/
function executeOperation(
address reserve,
uint256 amount,
uint256 fee,
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
bytes calldata params
) external override returns (bool) {
(
@ -54,17 +54,17 @@ contract UniswapRepayAdapter is BaseUniswapAdapter, IFlashLoanReceiver {
uint256 rateMode
) = abi.decode(params, (address, address, uint256, uint256, uint256));
swapTokensForExactTokens(reserve, assetToSwapTo, amount, repayAmount);
swapTokensForExactTokens(assets[0], assetToSwapTo, amounts[0], repayAmount);
// Repay debt
IERC20(assetToSwapTo).approve(address(pool), repayAmount);
pool.repay(assetToSwapTo, repayAmount, rateMode, user);
uint256 flashLoanDebt = amount.add(fee);
pullATokenAndRepayFlashLoan(reserve, user, flashLoanDebt);
uint256 flashLoanDebt = amounts[0].add(premiums[0]);
pullATokenAndRepayFlashLoan(assets[0], user, flashLoanDebt);
// Take care of reserve leftover from the swap
sendLeftOver(reserve, flashLoanDebt, leftOverAction, user);
sendLeftOver(assets[0], flashLoanDebt, leftOverAction, user);
return true;
}

View File

@ -101,8 +101,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -194,8 +194,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
usdc.address,
flashloanAmount.toString(),
[usdc.address],
[flashloanAmount.toString()],
0,
params,
0
@ -259,8 +259,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params1,
0
@ -271,8 +271,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params2,
0
@ -319,8 +319,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -415,8 +415,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -459,7 +459,6 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
const liquidityToSwap = amountWETHtoSwap;
await aEth.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aEth.balanceOf(userAddress);
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
@ -477,8 +476,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -504,7 +503,6 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
const liquidityToSwap = amountWETHtoSwap;
await aEth.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aEth.balanceOf(userAddress);
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
@ -522,8 +520,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -532,7 +530,7 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
});
it('should revert when the received amount is less than expected', async () => {
const {users, pool, weth, oracle, dai, aEth, uniswapRepayAdapter, deployer} = testEnv;
const {users, pool, weth, oracle, dai, aEth, uniswapRepayAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
@ -570,8 +568,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -595,8 +593,7 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
const liquidityToSwap = amountWETHtoSwap;
await aEth.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
await aEth.connect(user).approve(uniswapRepayAdapter.address, amountWETHtoSwap);
// Subtract the FL fee from the amount to be swapped 0,09%
const bigMaxAmountToSwap = amountWETHtoSwap.mul(2);
@ -615,8 +612,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -686,8 +683,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0
@ -776,8 +773,8 @@ makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
weth.address,
flashloanAmount.toString(),
[weth.address],
[flashloanAmount.toString()],
0,
params,
0