aave-protocol-v2/test/uniswapAdapters.spec.ts

1940 lines
68 KiB
TypeScript
Raw Normal View History

2020-10-27 12:18:30 +00:00
import {makeSuite, TestEnv} from './helpers/make-suite';
2020-11-02 20:33:00 +00:00
import {
convertToCurrencyDecimals,
getContract,
buildPermitParams,
getSignatureFromTypedData,
} from '../helpers/contracts-helpers';
2020-10-29 20:29:41 +00:00
import {getMockUniswapRouter} from '../helpers/contracts-getters';
2020-10-27 12:18:30 +00:00
import {
deployUniswapLiquiditySwapAdapter,
deployUniswapRepayAdapter,
2020-10-29 20:29:41 +00:00
} from '../helpers/contracts-deployments';
2020-10-27 12:18:30 +00:00
import {MockUniswapV2Router02} from '../types/MockUniswapV2Router02';
import {Zero} from '@ethersproject/constants';
import BigNumber from 'bignumber.js';
2020-11-02 20:33:00 +00:00
import {BRE, evmRevert, evmSnapshot} from '../helpers/misc-utils';
2020-10-27 12:18:30 +00:00
import {ethers} from 'ethers';
import {eContractid} from '../helpers/types';
import {AToken} from '../types/AToken';
import {StableDebtToken} from '../types/StableDebtToken';
2020-11-02 20:33:00 +00:00
import {BUIDLEREVM_CHAINID} from '../helpers/buidler-constants';
import {MAX_UINT_AMOUNT} from '../helpers/constants';
2020-10-27 12:18:30 +00:00
const {parseEther} = ethers.utils;
const {expect} = require('chai');
makeSuite('Uniswap adapters', (testEnv: TestEnv) => {
let mockUniswapRouter: MockUniswapV2Router02;
let evmSnapshotId: string;
before(async () => {
mockUniswapRouter = await getMockUniswapRouter();
});
beforeEach(async () => {
evmSnapshotId = await evmSnapshot();
});
afterEach(async () => {
await evmRevert(evmSnapshotId);
});
describe('BaseUniswapAdapter', () => {
describe('getAmountOut', () => {
it('should return the estimated amountOut for the asset swap', async () => {
const {weth, dai, uniswapLiquiditySwapAdapter} = testEnv;
const amountIn = parseEther('1');
const amountOut = parseEther('2');
await mockUniswapRouter.setAmountOut(amountIn, weth.address, dai.address, amountOut);
expect(
await uniswapLiquiditySwapAdapter.getAmountOut(amountIn, weth.address, dai.address)
).to.be.eq(amountOut);
});
});
describe('getAmountIn', () => {
it('should return the estimated required amountIn for the asset swap', async () => {
const {weth, dai, uniswapLiquiditySwapAdapter} = testEnv;
const amountIn = parseEther('1');
const amountOut = parseEther('2');
await mockUniswapRouter.setAmountIn(amountOut, weth.address, dai.address, amountIn);
expect(
await uniswapLiquiditySwapAdapter.getAmountIn(amountOut, weth.address, dai.address)
).to.be.eq(amountIn);
});
});
});
2020-10-27 12:18:30 +00:00
describe('UniswapLiquiditySwapAdapter', () => {
describe('constructor', () => {
it('should deploy with correct parameters', async () => {
const {addressesProvider} = testEnv;
2020-10-29 20:29:41 +00:00
await deployUniswapLiquiditySwapAdapter([
2020-10-27 12:18:30 +00:00
addressesProvider.address,
2020-10-29 20:29:41 +00:00
mockUniswapRouter.address,
]);
2020-10-27 12:18:30 +00:00
});
it('should revert if not valid addresses provider', async () => {
expect(
2020-10-29 20:29:41 +00:00
deployUniswapLiquiditySwapAdapter([mockUniswapRouter.address, mockUniswapRouter.address])
2020-10-27 12:18:30 +00:00
).to.be.reverted;
});
});
describe('executeOperation', () => {
beforeEach(async () => {
const {users, weth, dai, pool, deployer} = testEnv;
const userAddress = users[0].address;
// Provide liquidity
await dai.mint(parseEther('20000'));
await dai.approve(pool.address, parseEther('20000'));
await pool.deposit(dai.address, parseEther('20000'), deployer.address, 0);
// Make a deposit for user
await weth.mint(parseEther('100'));
await weth.approve(pool.address, parseEther('100'));
await pool.deposit(weth.address, parseEther('100'), userAddress, 0);
});
it('should correctly swap tokens and deposit the out tokens in the pool', async () => {
2020-10-29 20:29:41 +00:00
const {users, weth, oracle, dai, aDai, aWETH, pool, uniswapLiquiditySwapAdapter} = testEnv;
2020-10-27 12:18:30 +00:00
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapLiquiditySwapAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
const params = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
)
.to.emit(uniswapLiquiditySwapAdapter, 'Swapped')
.withArgs(weth.address, dai.address, flashloanAmount.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiAllowance = await dai.allowance(
uniswapLiquiditySwapAdapter.address,
userAddress
);
const userADaiBalance = await aDai.balanceOf(userAddress);
2020-10-29 20:29:41 +00:00
const userAEthBalance = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(adapterDaiAllowance).to.be.eq(Zero);
expect(userADaiBalance).to.be.eq(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gte(userAEthBalanceBefore.sub(liquidityToSwap));
});
2020-11-02 20:33:00 +00:00
it('should correctly swap tokens with permit', async () => {
const {users, weth, oracle, dai, aDai, aWETH, pool, uniswapLiquiditySwapAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
// IMPORTANT: Round down to work equal to solidity to get the correct value for permit call
BigNumber.config({
ROUNDING_MODE: 1, //round down
});
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmountBN = new BigNumber(liquidityToSwap.toString()).div(1.0009);
const flashloanAmount = flashloanAmountBN.toFixed(0);
const flashloanFee = flashloanAmountBN.multipliedBy(9).div(10000);
const amountToPermit = flashloanAmountBN.plus(flashloanFee);
const chainId = BRE.network.config.chainId || BUIDLEREVM_CHAINID;
const deadline = MAX_UINT_AMOUNT;
const nonce = (await aWETH._nonces(userAddress)).toNumber();
const msgParams = buildPermitParams(
chainId,
aWETH.address,
'1',
await aWETH.name(),
userAddress,
uniswapLiquiditySwapAdapter.address,
nonce,
deadline,
amountToPermit.toFixed(0).toString()
);
const ownerPrivateKey = require('../test-wallets.js').accounts[1].secretKey;
if (!ownerPrivateKey) {
throw new Error('INVALID_OWNER_PK');
}
const {v, r, s} = getSignatureFromTypedData(ownerPrivateKey, msgParams);
const params = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
[[dai.address], [expectedDaiAmount], [deadline], [v], [r], [s]]
2020-11-02 20:33:00 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params,
0
)
)
.to.emit(uniswapLiquiditySwapAdapter, 'Swapped')
.withArgs(weth.address, dai.address, flashloanAmount.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiAllowance = await dai.allowance(
uniswapLiquiditySwapAdapter.address,
userAddress
);
const userADaiBalance = await aDai.balanceOf(userAddress);
const userAEthBalance = await aWETH.balanceOf(userAddress);
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(adapterDaiAllowance).to.be.eq(Zero);
expect(userADaiBalance).to.be.eq(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gte(userAEthBalanceBefore.sub(liquidityToSwap));
// Restore round up
BigNumber.config({
ROUNDING_MODE: 0, //round up
});
});
2020-10-30 19:59:25 +00:00
it('should revert if inconsistent params', async () => {
const {users, weth, oracle, dai, aWETH, pool, uniswapLiquiditySwapAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
await aWETH.connect(user).approve(uniswapLiquiditySwapAdapter.address, liquidityToSwap);
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
const params = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address, weth.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-30 19:59:25 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
2020-11-02 20:33:00 +00:00
const params2 = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address, weth.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0, 0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params2,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params3 = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address, weth.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0, 0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params3,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params4 = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address, weth.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
[
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params4,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params5 = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address, weth.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
[
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params5,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params6 = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
[
[dai.address, weth.address],
[expectedDaiAmount, expectedDaiAmount],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params6,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
2020-10-30 19:59:25 +00:00
});
it('should revert if caller not lending pool', async () => {
const {users, weth, oracle, dai, aWETH, uniswapLiquiditySwapAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
await aWETH.connect(user).approve(uniswapLiquiditySwapAdapter.address, liquidityToSwap);
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
const params = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-30 19:59:25 +00:00
);
await expect(
uniswapLiquiditySwapAdapter
.connect(user)
.executeOperation(
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params
)
).to.be.revertedWith('CALLER_MUST_BE_LENDING_POOL');
});
2020-10-27 12:18:30 +00:00
it('should work correctly with tokens of different decimals', async () => {
const {
users,
usdc,
oracle,
dai,
aDai,
uniswapLiquiditySwapAdapter,
pool,
deployer,
} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountUSDCtoSwap = await convertToCurrencyDecimals(usdc.address, '10');
const liquidity = await convertToCurrencyDecimals(usdc.address, '20000');
// Provide liquidity
await usdc.mint(liquidity);
await usdc.approve(pool.address, liquidity);
await pool.deposit(usdc.address, liquidity, deployer.address, 0);
// Make a deposit for user
await usdc.connect(user).mint(amountUSDCtoSwap);
await usdc.connect(user).approve(pool.address, amountUSDCtoSwap);
await pool.connect(user).deposit(usdc.address, amountUSDCtoSwap, userAddress, 0);
const usdcPrice = await oracle.getAssetPrice(usdc.address);
const daiPrice = await oracle.getAssetPrice(dai.address);
// usdc 6
const collateralDecimals = (await usdc.decimals()).toString();
const principalDecimals = (await dai.decimals()).toString();
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountUSDCtoSwap.toString())
.times(
new BigNumber(usdcPrice.toString()).times(new BigNumber(10).pow(principalDecimals))
)
.div(
new BigNumber(daiPrice.toString()).times(new BigNumber(10).pow(collateralDecimals))
)
.toFixed(0)
);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const aUsdcData = await pool.getReserveData(usdc.address);
const aUsdc = await getContract<AToken>(eContractid.AToken, aUsdcData.aTokenAddress);
const aUsdcBalance = await aUsdc.balanceOf(userAddress);
await aUsdc.connect(user).approve(uniswapLiquiditySwapAdapter.address, aUsdcBalance);
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(amountUSDCtoSwap.toString()).div(1.0009).toFixed(0);
const params = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address],
[expectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[usdc.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
)
.to.emit(uniswapLiquiditySwapAdapter, 'Swapped')
.withArgs(usdc.address, dai.address, flashloanAmount.toString(), expectedDaiAmount);
const adapterUsdcBalance = await usdc.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiAllowance = await dai.allowance(
uniswapLiquiditySwapAdapter.address,
userAddress
);
const aDaiBalance = await aDai.balanceOf(userAddress);
expect(adapterUsdcBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(adapterDaiAllowance).to.be.eq(Zero);
expect(aDaiBalance).to.be.eq(expectedDaiAmount);
});
it('should revert when min amount to receive exceeds the max slippage amount', async () => {
const {users, weth, oracle, dai, aWETH, pool, uniswapLiquiditySwapAdapter} = testEnv;
2020-10-27 12:18:30 +00:00
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
const smallExpectedDaiAmount = expectedDaiAmount.div(2);
2020-10-27 12:18:30 +00:00
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapLiquiditySwapAdapter.address, liquidityToSwap);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
const params = ethers.utils.defaultAbiCoder.encode(
['address[]', 'uint256[]', 'uint256[]', 'uint8[]', 'bytes32[]', 'bytes32[]'],
2020-11-02 20:33:00 +00:00
[
[dai.address],
[smallExpectedDaiAmount],
2020-11-02 20:33:00 +00:00
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapLiquiditySwapAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
params,
2020-10-27 12:18:30 +00:00
0
)
).to.be.revertedWith('minAmountOut exceed max slippage');
2020-10-27 12:18:30 +00:00
});
});
describe('swapAndDeposit', () => {
beforeEach(async () => {
const {users, weth, dai, pool, deployer} = testEnv;
const userAddress = users[0].address;
// Provide liquidity
await dai.mint(parseEther('20000'));
await dai.approve(pool.address, parseEther('20000'));
await pool.deposit(dai.address, parseEther('20000'), deployer.address, 0);
// Make a deposit for user
await weth.mint(parseEther('100'));
await weth.approve(pool.address, parseEther('100'));
await pool.deposit(weth.address, parseEther('100'), userAddress, 0);
});
it('should correctly swap tokens and deposit the out tokens in the pool', async () => {
2020-10-29 20:29:41 +00:00
const {users, weth, oracle, dai, aDai, aWETH, uniswapLiquiditySwapAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapLiquiditySwapAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address],
[dai.address],
[amountWETHtoSwap],
[expectedDaiAmount],
[
2020-11-02 20:33:00 +00:00
{
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
]
)
)
.to.emit(uniswapLiquiditySwapAdapter, 'Swapped')
.withArgs(weth.address, dai.address, amountWETHtoSwap.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiAllowance = await dai.allowance(
uniswapLiquiditySwapAdapter.address,
userAddress
);
const userADaiBalance = await aDai.balanceOf(userAddress);
2020-10-29 20:29:41 +00:00
const userAEthBalance = await aWETH.balanceOf(userAddress);
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(adapterDaiAllowance).to.be.eq(Zero);
expect(userADaiBalance).to.be.eq(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gte(userAEthBalanceBefore.sub(liquidityToSwap));
});
2020-11-02 20:33:00 +00:00
it('should correctly swap tokens using permit', async () => {
const {users, weth, oracle, dai, aDai, aWETH, uniswapLiquiditySwapAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
const chainId = BRE.network.config.chainId || BUIDLEREVM_CHAINID;
const deadline = MAX_UINT_AMOUNT;
const nonce = (await aWETH._nonces(userAddress)).toNumber();
const msgParams = buildPermitParams(
chainId,
aWETH.address,
'1',
await aWETH.name(),
userAddress,
uniswapLiquiditySwapAdapter.address,
nonce,
deadline,
liquidityToSwap.toString()
);
const ownerPrivateKey = require('../test-wallets.js').accounts[1].secretKey;
if (!ownerPrivateKey) {
throw new Error('INVALID_OWNER_PK');
}
const {v, r, s} = getSignatureFromTypedData(ownerPrivateKey, msgParams);
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address],
[dai.address],
[amountWETHtoSwap],
[expectedDaiAmount],
[
2020-11-02 20:33:00 +00:00
{
deadline,
v,
r,
s,
},
]
)
2020-11-02 20:33:00 +00:00
)
.to.emit(uniswapLiquiditySwapAdapter, 'Swapped')
.withArgs(weth.address, dai.address, amountWETHtoSwap.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapLiquiditySwapAdapter.address);
const adapterDaiAllowance = await dai.allowance(
uniswapLiquiditySwapAdapter.address,
userAddress
);
const userADaiBalance = await aDai.balanceOf(userAddress);
const userAEthBalance = await aWETH.balanceOf(userAddress);
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(adapterDaiAllowance).to.be.eq(Zero);
expect(userADaiBalance).to.be.eq(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gte(userAEthBalanceBefore.sub(liquidityToSwap));
});
2020-10-30 19:59:25 +00:00
it('should revert if inconsistent params', async () => {
const {users, weth, dai, uniswapLiquiditySwapAdapter, oracle} = testEnv;
2020-10-30 19:59:25 +00:00
const user = users[0].signer;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
2020-10-30 19:59:25 +00:00
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address, dai.address],
[dai.address],
[amountWETHtoSwap],
[expectedDaiAmount],
[
2020-11-02 20:33:00 +00:00
{
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
]
)
2020-11-02 20:33:00 +00:00
).to.be.revertedWith('INCONSISTENT_PARAMS');
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address],
[dai.address, weth.address],
[amountWETHtoSwap],
[expectedDaiAmount],
[
{
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
]
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address],
[dai.address],
[amountWETHtoSwap, amountWETHtoSwap],
[expectedDaiAmount],
[
2020-11-02 20:33:00 +00:00
{
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
]
)
2020-10-30 19:59:25 +00:00
).to.be.revertedWith('INCONSISTENT_PARAMS');
await expect(
uniswapLiquiditySwapAdapter
.connect(user)
2020-11-02 20:33:00 +00:00
.swapAndDeposit(
[weth.address],
[dai.address],
[amountWETHtoSwap],
[expectedDaiAmount],
[]
2020-11-02 20:33:00 +00:00
)
2020-10-30 19:59:25 +00:00
).to.be.revertedWith('INCONSISTENT_PARAMS');
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address],
[dai.address],
[amountWETHtoSwap],
[expectedDaiAmount, expectedDaiAmount],
[
{
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
]
)
2020-10-30 19:59:25 +00:00
).to.be.revertedWith('INCONSISTENT_PARAMS');
});
it('should revert when min amount to receive exceeds the max slippage amount', async () => {
const {users, weth, oracle, dai, aWETH, uniswapLiquiditySwapAdapter} = testEnv;
const user = users[0].signer;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
await mockUniswapRouter.setAmountToReturn(expectedDaiAmount);
const smallExpectedDaiAmount = expectedDaiAmount.div(2);
// User will swap liquidity 10 aEth to aDai
const liquidityToSwap = parseEther('10');
await aWETH.connect(user).approve(uniswapLiquiditySwapAdapter.address, liquidityToSwap);
await expect(
uniswapLiquiditySwapAdapter.connect(user).swapAndDeposit(
[weth.address],
[dai.address],
[amountWETHtoSwap],
[smallExpectedDaiAmount],
[
{
deadline: 0,
v: 0,
r: '0x0000000000000000000000000000000000000000000000000000000000000000',
s: '0x0000000000000000000000000000000000000000000000000000000000000000',
},
]
)
).to.be.revertedWith('minAmountOut exceed max slippage');
});
});
2020-10-27 12:18:30 +00:00
});
describe('UniswapRepayAdapter', () => {
describe('constructor', () => {
it('should deploy with correct parameters', async () => {
const {addressesProvider} = testEnv;
2020-10-29 20:29:41 +00:00
await deployUniswapRepayAdapter([addressesProvider.address, mockUniswapRouter.address]);
2020-10-27 12:18:30 +00:00
});
it('should revert if not valid addresses provider', async () => {
2020-10-29 20:29:41 +00:00
expect(deployUniswapRepayAdapter([mockUniswapRouter.address, mockUniswapRouter.address])).to
2020-10-27 12:18:30 +00:00
.be.reverted;
});
});
describe('executeOperation', () => {
beforeEach(async () => {
const {users, weth, dai, pool, deployer} = testEnv;
const userAddress = users[0].address;
// Provide liquidity
await dai.mint(parseEther('20000'));
await dai.approve(pool.address, parseEther('20000'));
await pool.deposit(dai.address, parseEther('20000'), deployer.address, 0);
// Make a deposit for user
await weth.mint(parseEther('100'));
await weth.approve(pool.address, parseEther('100'));
await pool.deposit(weth.address, parseEther('100'), userAddress, 0);
});
it('should correctly swap tokens and repay debt', async () => {
const {
users,
pool,
weth,
2020-10-29 20:29:41 +00:00
aWETH,
2020-10-27 12:18:30 +00:00
oracle,
dai,
uniswapRepayAdapter,
helpersContract,
} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
const daiStableDebtTokenAddress = (
await helpersContract.getReserveTokensAddresses(dai.address)
).stableDebtTokenAddress;
const daiStableDebtContract = await getContract<StableDebtToken>(
eContractid.StableDebtToken,
daiStableDebtTokenAddress
);
const userDaiStableDebtAmountBefore = await daiStableDebtContract.balanceOf(userAddress);
const liquidityToSwap = amountWETHtoSwap;
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
)
.to.emit(uniswapRepayAdapter, 'Swapped')
.withArgs(weth.address, dai.address, flashloanAmount.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapRepayAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapRepayAdapter.address);
const userDaiStableDebtAmount = await daiStableDebtContract.balanceOf(userAddress);
2020-10-29 20:29:41 +00:00
const userAEthBalance = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(userDaiStableDebtAmountBefore).to.be.gte(expectedDaiAmount);
expect(userDaiStableDebtAmount).to.be.lt(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gte(userAEthBalanceBefore.sub(liquidityToSwap));
});
2020-11-02 20:33:00 +00:00
it('should correctly swap tokens and repay debt with permit', async () => {
const {
users,
pool,
weth,
aWETH,
oracle,
dai,
uniswapRepayAdapter,
helpersContract,
} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
const daiStableDebtTokenAddress = (
await helpersContract.getReserveTokensAddresses(dai.address)
).stableDebtTokenAddress;
const daiStableDebtContract = await getContract<StableDebtToken>(
eContractid.StableDebtToken,
daiStableDebtTokenAddress
);
const userDaiStableDebtAmountBefore = await daiStableDebtContract.balanceOf(userAddress);
const liquidityToSwap = amountWETHtoSwap;
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
// IMPORTANT: Round down to work equal to solidity to get the correct value for permit call
BigNumber.config({
ROUNDING_MODE: 1, //round down
});
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmountBN = new BigNumber(liquidityToSwap.toString()).div(1.0009);
const flashloanAmount = flashloanAmountBN.toFixed(0);
const flashloanFee = flashloanAmountBN.multipliedBy(9).div(10000);
const amountToPermit = flashloanAmountBN.plus(flashloanFee);
const chainId = BRE.network.config.chainId || BUIDLEREVM_CHAINID;
const deadline = MAX_UINT_AMOUNT;
const nonce = (await aWETH._nonces(userAddress)).toNumber();
const msgParams = buildPermitParams(
chainId,
aWETH.address,
'1',
await aWETH.name(),
userAddress,
uniswapRepayAdapter.address,
nonce,
deadline,
amountToPermit.toFixed(0).toString()
);
const ownerPrivateKey = require('../test-wallets.js').accounts[1].secretKey;
if (!ownerPrivateKey) {
throw new Error('INVALID_OWNER_PK');
}
const {v, r, s} = getSignatureFromTypedData(ownerPrivateKey, msgParams);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const params = ethers.utils.defaultAbiCoder.encode(
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[[dai.address], 0, [expectedDaiAmount], [1], [deadline], [v], [r], [s]]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params,
0
)
)
.to.emit(uniswapRepayAdapter, 'Swapped')
.withArgs(weth.address, dai.address, flashloanAmount.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapRepayAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapRepayAdapter.address);
const userDaiStableDebtAmount = await daiStableDebtContract.balanceOf(userAddress);
const userAEthBalance = await aWETH.balanceOf(userAddress);
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(userDaiStableDebtAmountBefore).to.be.gte(expectedDaiAmount);
expect(userDaiStableDebtAmount).to.be.lt(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gte(userAEthBalanceBefore.sub(liquidityToSwap));
// Restore round up
BigNumber.config({
ROUNDING_MODE: 0, //round up
});
});
2020-10-30 19:59:25 +00:00
it('should revert if inconsistent params', async () => {
const {users, pool, weth, aWETH, oracle, dai, uniswapRepayAdapter} = testEnv;
2020-10-27 12:18:30 +00:00
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
2020-10-30 19:59:25 +00:00
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
2020-10-27 12:18:30 +00:00
const liquidityToSwap = amountWETHtoSwap;
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
2020-10-30 19:59:25 +00:00
const params1 = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address, weth.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-30 19:59:25 +00:00
params1,
2020-10-27 12:18:30 +00:00
0
)
2020-10-30 19:59:25 +00:00
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params2 = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount, expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-30 19:59:25 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params2,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params3 = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1, 1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-30 19:59:25 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params3,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
2020-11-02 20:33:00 +00:00
const params4 = ethers.utils.defaultAbiCoder.encode(
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0, 0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params4,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params5 = ethers.utils.defaultAbiCoder.encode(
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0, 0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params5,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params6 = ethers.utils.defaultAbiCoder.encode(
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
[
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params6,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
const params7 = ethers.utils.defaultAbiCoder.encode(
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
[
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
],
]
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params7,
0
)
).to.be.revertedWith('INCONSISTENT_PARAMS');
2020-10-27 12:18:30 +00:00
});
2020-10-30 19:59:25 +00:00
it('should revert if caller not lending pool', async () => {
const {users, pool, weth, aWETH, oracle, dai, uniswapRepayAdapter} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
const liquidityToSwap = amountWETHtoSwap;
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-30 19:59:25 +00:00
);
await expect(
uniswapRepayAdapter
.connect(user)
.executeOperation(
[weth.address],
[flashloanAmount.toString()],
[0],
userAddress,
params
)
).to.be.revertedWith('CALLER_MUST_BE_LENDING_POOL');
});
it('should revert if there is not debt to repay with the specified rate mode', async () => {
2020-10-29 20:29:41 +00:00
const {users, pool, weth, oracle, dai, uniswapRepayAdapter, aWETH} = testEnv;
2020-10-27 12:18:30 +00:00
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
await weth.connect(user).mint(amountWETHtoSwap);
await weth.connect(user).transfer(uniswapRepayAdapter.address, amountWETHtoSwap);
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
2020-10-30 19:59:25 +00:00
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 2, 0, userAddress);
2020-10-27 12:18:30 +00:00
const liquidityToSwap = amountWETHtoSwap;
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
).to.be.reverted;
});
2020-10-30 19:59:25 +00:00
it('should revert if there is not debt to repay', async () => {
const {users, pool, weth, oracle, dai, uniswapRepayAdapter, aWETH} = testEnv;
2020-10-27 12:18:30 +00:00
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
2020-10-30 19:59:25 +00:00
await weth.connect(user).mint(amountWETHtoSwap);
await weth.connect(user).transfer(uniswapRepayAdapter.address, amountWETHtoSwap);
2020-10-27 12:18:30 +00:00
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
const liquidityToSwap = amountWETHtoSwap;
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
2020-10-30 19:59:25 +00:00
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
2020-10-27 12:18:30 +00:00
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
2020-10-30 19:59:25 +00:00
).to.be.reverted;
2020-10-27 12:18:30 +00:00
});
it('should revert when max amount allowed to swap is bigger than max slippage', async () => {
2020-10-29 20:29:41 +00:00
const {users, pool, weth, oracle, dai, aWETH, uniswapRepayAdapter} = testEnv;
2020-10-27 12:18:30 +00:00
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, amountWETHtoSwap);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const bigMaxAmountToSwap = amountWETHtoSwap.mul(2);
const flashloanAmount = new BigNumber(bigMaxAmountToSwap.toString()).div(1.0009).toFixed(0);
await mockUniswapRouter.connect(user).setAmountToSwap(flashloanAmount);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
).to.be.revertedWith('maxAmountToSwap exceed max slippage');
});
it('should swap tokens, repay debt and deposit in pool the left over', async () => {
const {
users,
pool,
weth,
2020-10-29 20:29:41 +00:00
aWETH,
2020-10-27 12:18:30 +00:00
oracle,
dai,
uniswapRepayAdapter,
helpersContract,
} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
const daiStableDebtTokenAddress = (
await helpersContract.getReserveTokensAddresses(dai.address)
).stableDebtTokenAddress;
const daiStableDebtContract = await getContract<StableDebtToken>(
eContractid.StableDebtToken,
daiStableDebtTokenAddress
);
const userDaiStableDebtAmountBefore = await daiStableDebtContract.balanceOf(userAddress);
const liquidityToSwap = amountWETHtoSwap;
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
const actualWEthSwapped = new BigNumber(flashloanAmount.toString())
.multipliedBy(0.995)
.toFixed(0);
const leftOverWeth = new BigNumber(flashloanAmount).minus(actualWEthSwapped);
await mockUniswapRouter.connect(user).setAmountToSwap(actualWEthSwapped);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
0,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
)
.to.emit(uniswapRepayAdapter, 'Swapped')
.withArgs(weth.address, dai.address, actualWEthSwapped.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapRepayAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapRepayAdapter.address);
const userDaiStableDebtAmount = await daiStableDebtContract.balanceOf(userAddress);
2020-10-29 20:29:41 +00:00
const userAEthBalance = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(userDaiStableDebtAmountBefore).to.be.gte(expectedDaiAmount);
expect(userDaiStableDebtAmount).to.be.lt(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gt(userAEthBalanceBefore.sub(liquidityToSwap));
expect(userAEthBalance).to.be.gte(
userAEthBalanceBefore.sub(liquidityToSwap).add(leftOverWeth.toString())
);
});
it('should swap tokens, repay debt and transfer to user the left over', async () => {
const {
users,
pool,
weth,
2020-10-29 20:29:41 +00:00
aWETH,
2020-10-27 12:18:30 +00:00
oracle,
dai,
uniswapRepayAdapter,
helpersContract,
} = testEnv;
const user = users[0].signer;
const userAddress = users[0].address;
const amountWETHtoSwap = await convertToCurrencyDecimals(weth.address, '10');
const daiPrice = await oracle.getAssetPrice(dai.address);
const expectedDaiAmount = await convertToCurrencyDecimals(
dai.address,
new BigNumber(amountWETHtoSwap.toString()).div(daiPrice.toString()).toFixed(0)
);
// Open user Debt
await pool.connect(user).borrow(dai.address, expectedDaiAmount, 1, 0, userAddress);
const daiStableDebtTokenAddress = (
await helpersContract.getReserveTokensAddresses(dai.address)
).stableDebtTokenAddress;
const daiStableDebtContract = await getContract<StableDebtToken>(
eContractid.StableDebtToken,
daiStableDebtTokenAddress
);
const userDaiStableDebtAmountBefore = await daiStableDebtContract.balanceOf(userAddress);
const liquidityToSwap = amountWETHtoSwap;
2020-10-29 20:29:41 +00:00
await aWETH.connect(user).approve(uniswapRepayAdapter.address, liquidityToSwap);
const userAEthBalanceBefore = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
// Subtract the FL fee from the amount to be swapped 0,09%
const flashloanAmount = new BigNumber(liquidityToSwap.toString()).div(1.0009).toFixed(0);
const actualWEthSwapped = new BigNumber(flashloanAmount.toString())
.multipliedBy(0.995)
.toFixed(0);
const leftOverWeth = new BigNumber(flashloanAmount).minus(actualWEthSwapped);
await mockUniswapRouter.connect(user).setAmountToSwap(actualWEthSwapped);
await mockUniswapRouter.connect(user).setAmountToReturn(expectedDaiAmount);
const wethBalanceBefore = await weth.balanceOf(userAddress);
const params = ethers.utils.defaultAbiCoder.encode(
2020-11-02 20:33:00 +00:00
[
'address[]',
'uint256',
'uint256[]',
'uint256[]',
'uint256[]',
'uint8[]',
'bytes32[]',
'bytes32[]',
],
[
[dai.address],
1,
[expectedDaiAmount],
[1],
[0],
[0],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
['0x0000000000000000000000000000000000000000000000000000000000000000'],
]
2020-10-27 12:18:30 +00:00
);
await expect(
pool
.connect(user)
.flashLoan(
uniswapRepayAdapter.address,
[weth.address],
[flashloanAmount.toString()],
2020-10-30 19:59:25 +00:00
[0],
2020-10-29 20:29:41 +00:00
userAddress,
2020-10-27 12:18:30 +00:00
params,
0
)
)
.to.emit(uniswapRepayAdapter, 'Swapped')
.withArgs(weth.address, dai.address, actualWEthSwapped.toString(), expectedDaiAmount);
const adapterWethBalance = await weth.balanceOf(uniswapRepayAdapter.address);
const adapterDaiBalance = await dai.balanceOf(uniswapRepayAdapter.address);
const userDaiStableDebtAmount = await daiStableDebtContract.balanceOf(userAddress);
2020-10-29 20:29:41 +00:00
const userAEthBalance = await aWETH.balanceOf(userAddress);
2020-10-27 12:18:30 +00:00
const wethBalance = await weth.balanceOf(userAddress);
expect(adapterWethBalance).to.be.eq(Zero);
expect(adapterDaiBalance).to.be.eq(Zero);
expect(userDaiStableDebtAmountBefore).to.be.gte(expectedDaiAmount);
expect(userDaiStableDebtAmount).to.be.lt(expectedDaiAmount);
expect(userAEthBalance).to.be.lt(userAEthBalanceBefore);
expect(userAEthBalance).to.be.gt(userAEthBalanceBefore.sub(liquidityToSwap));
expect(wethBalance).to.be.eq(wethBalanceBefore.add(leftOverWeth.toString()));
});
});
});
});