aave-protocol-v2/test/collateral-swap.spec.ts

199 lines
6.9 KiB
TypeScript
Raw Normal View History

2020-09-07 13:48:46 +00:00
import {makeSuite, TestEnv} from './helpers/make-suite';
import {MockSwapAdapter} from '../types/MockSwapAdapter';
import {getMockSwapAdapter} from '../helpers/contracts-helpers';
import {ProtocolErrors} from '../helpers/types';
import {ethers} from 'ethers';
import {APPROVAL_AMOUNT_LENDING_POOL} from '../helpers/constants';
2020-09-08 10:08:16 +00:00
import {getContractsData, getTxCostAndTimestamp} from './helpers/actions';
import {calcExpectedATokenBalance} from './helpers/utils/calculations';
import {waitForTx} from './__setup.spec';
import {advanceBlock, timeLatest} from '../helpers/misc-utils';
2020-09-07 13:48:46 +00:00
const {expect} = require('chai');
makeSuite('LendingPool SwapDeposit function', (testEnv: TestEnv) => {
2020-09-07 13:48:46 +00:00
let _mockSwapAdapter = {} as MockSwapAdapter;
const {HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD} = ProtocolErrors;
before(async () => {
_mockSwapAdapter = await getMockSwapAdapter();
});
it('Deposits WETH into the reserve', async () => {
const {pool, weth, users} = testEnv;
const amountToDeposit = ethers.utils.parseEther('1');
for (const signer of [weth.signer, users[2].signer]) {
const connectedWETH = weth.connect(signer);
await connectedWETH.mint(amountToDeposit);
await connectedWETH.approve(pool.address, APPROVAL_AMOUNT_LENDING_POOL);
await pool
.connect(signer)
.deposit(weth.address, amountToDeposit, await signer.getAddress(), '0');
2020-09-07 13:48:46 +00:00
}
});
it('User tries to swap more then he can, revert expected', async () => {
2020-09-07 13:48:46 +00:00
const {pool, weth, dai} = testEnv;
await expect(
pool.swapLiquidity(
2020-09-07 13:48:46 +00:00
_mockSwapAdapter.address,
weth.address,
dai.address,
ethers.utils.parseEther('1.1'),
'0x10'
)
).to.be.revertedWith('55');
2020-09-07 13:48:46 +00:00
});
it('User tries to swap asset on equal asset, revert expected', async () => {
const {pool, weth} = testEnv;
await expect(
pool.swapLiquidity(
_mockSwapAdapter.address,
weth.address,
weth.address,
ethers.utils.parseEther('0.1'),
'0x10'
)
).to.be.revertedWith('56');
});
2020-09-07 13:48:46 +00:00
it('User tries to swap more then available on the reserve', async () => {
const {pool, weth, dai, users, aEth, deployer} = testEnv;
2020-09-07 13:48:46 +00:00
await pool.borrow(weth.address, ethers.utils.parseEther('0.1'), 1, 0, deployer.address);
2020-09-07 13:48:46 +00:00
await pool.connect(users[2].signer).withdraw(weth.address, ethers.utils.parseEther('1'));
await expect(
pool.swapLiquidity(
2020-09-07 13:48:46 +00:00
_mockSwapAdapter.address,
weth.address,
dai.address,
ethers.utils.parseEther('1'),
'0x10'
)
).to.be.revertedWith('55');
2020-09-07 13:48:46 +00:00
});
it('User tries to swap correct amount', async () => {
const {pool, weth, dai, aEth, aDai} = testEnv;
2020-09-07 13:48:46 +00:00
const userAddress = await pool.signer.getAddress();
const amountToSwap = ethers.utils.parseEther('0.25');
const amountToReturn = ethers.utils.parseEther('0.5');
await _mockSwapAdapter.setAmountToReturn(amountToReturn);
2020-09-08 10:08:16 +00:00
const {
reserveData: wethReserveDataBefore,
userData: wethUserDataBefore,
} = await getContractsData(weth.address, userAddress, testEnv);
const {reserveData: daiReserveDataBefore, userData: daiUserDataBefore} = await getContractsData(
dai.address,
userAddress,
testEnv
);
2020-09-07 13:48:46 +00:00
const reserveBalanceWETHBefore = await weth.balanceOf(aEth.address);
const reserveBalanceDAIBefore = await dai.balanceOf(aDai.address);
2020-09-08 10:08:16 +00:00
const txReceipt = await waitForTx(
await pool.swapLiquidity(
2020-09-08 10:08:16 +00:00
_mockSwapAdapter.address,
weth.address,
dai.address,
amountToSwap,
'0x10'
)
2020-09-07 13:48:46 +00:00
);
2020-09-08 10:08:16 +00:00
const {txTimestamp} = await getTxCostAndTimestamp(txReceipt);
2020-09-07 13:48:46 +00:00
const userATokenBalanceWETHAfter = await aEth.balanceOf(userAddress);
const userATokenBalanceDAIAfter = await aDai.balanceOf(userAddress);
const reserveBalanceWETHAfter = await weth.balanceOf(aEth.address);
const reserveBalanceDAIAfter = await dai.balanceOf(aDai.address);
expect(userATokenBalanceWETHAfter.toString()).to.be.equal(
2020-09-08 10:08:16 +00:00
calcExpectedATokenBalance(wethReserveDataBefore, wethUserDataBefore, txTimestamp)
.minus(amountToSwap.toString())
.toString(),
2020-09-07 13:48:46 +00:00
'was burned incorrect amount of user funds'
);
expect(userATokenBalanceDAIAfter.toString()).to.be.equal(
2020-09-08 10:08:16 +00:00
calcExpectedATokenBalance(daiReserveDataBefore, daiUserDataBefore, txTimestamp)
.plus(amountToReturn.toString())
.toString(),
2020-09-07 13:48:46 +00:00
'was minted incorrect amount of user funds'
);
expect(reserveBalanceWETHAfter.toString()).to.be.equal(
reserveBalanceWETHBefore.sub(amountToSwap).toString(),
'was sent incorrect amount if reserve funds'
);
expect(reserveBalanceDAIAfter.toString()).to.be.equal(
reserveBalanceDAIBefore.add(amountToReturn).toString(),
'was received incorrect amount if reserve funds'
);
});
2020-09-07 13:59:41 +00:00
it('User tries to drop HF below one', async () => {
const {pool, weth, dai, deployer} = testEnv;
2020-09-07 13:59:41 +00:00
const amountToSwap = ethers.utils.parseEther('0.3');
const amountToReturn = ethers.utils.parseEther('0.5');
await _mockSwapAdapter.setAmountToReturn(amountToReturn);
await pool.borrow(weth.address, ethers.utils.parseEther('0.3'), 1, 0, deployer.address);
2020-09-07 13:59:41 +00:00
await expect(
pool.swapLiquidity(_mockSwapAdapter.address, weth.address, dai.address, amountToSwap, '0x10')
2020-09-07 13:59:41 +00:00
).to.be.revertedWith(HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD);
});
it('Should set usage as collateral to false if no leftovers after swap', async () => {
2020-09-10 10:31:43 +00:00
const {pool, weth, dai, aEth, users} = testEnv;
const userAddress = await pool.signer.getAddress();
2020-09-10 10:31:43 +00:00
// add more liquidity to allow user 0 to swap everything he has
await weth.connect(users[2].signer).mint(ethers.utils.parseEther('1'));
await pool
.connect(users[2].signer)
.deposit(weth.address, ethers.utils.parseEther('1'), users[2].address, '0');
2020-09-10 10:31:43 +00:00
// cleanup borrowings, to be abe to swap whole weth
2020-09-10 10:31:43 +00:00
const amountToRepay = ethers.utils.parseEther('0.5');
await weth.mint(amountToRepay);
await pool.repay(weth.address, amountToRepay, '1', userAddress);
const txTimestamp = (await timeLatest()).plus(100);
const {
reserveData: wethReserveDataBefore,
userData: wethUserDataBefore,
} = await getContractsData(weth.address, userAddress, testEnv);
const amountToSwap = calcExpectedATokenBalance(
wethReserveDataBefore,
wethUserDataBefore,
2020-09-10 10:31:43 +00:00
txTimestamp.plus('1')
);
await advanceBlock(txTimestamp.toNumber());
await pool.swapLiquidity(
_mockSwapAdapter.address,
weth.address,
dai.address,
2020-09-10 10:31:43 +00:00
amountToSwap.toString(),
'0x10'
);
const {userData: wethUserDataAfter} = await getContractsData(
weth.address,
userAddress,
testEnv
);
expect(wethUserDataAfter.usageAsCollateralEnabled).to.be.equal(
false,
'usageAsCollateralEnabled are not set to false'
);
});
2020-09-07 13:48:46 +00:00
});