mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
137 lines
4.9 KiB
TypeScript
137 lines
4.9 KiB
TypeScript
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';
|
|
import {getContractsData, getTxCostAndTimestamp} from './helpers/actions';
|
|
import {calcExpectedATokenBalance} from './helpers/utils/calculations';
|
|
import {waitForTx} from './__setup.spec';
|
|
|
|
const {expect} = require('chai');
|
|
|
|
makeSuite('LendingPool CollateralSwap function', (testEnv: TestEnv) => {
|
|
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, '0');
|
|
}
|
|
});
|
|
it('User tries to swap more then he can', async () => {
|
|
const {pool, weth, dai} = testEnv;
|
|
await expect(
|
|
pool.collateralSwap(
|
|
_mockSwapAdapter.address,
|
|
weth.address,
|
|
dai.address,
|
|
ethers.utils.parseEther('1.1'),
|
|
'0x10'
|
|
)
|
|
).to.be.revertedWith('ERC20: burn amount exceeds balance');
|
|
});
|
|
|
|
it('User tries to swap more then available on the reserve', async () => {
|
|
const {pool, weth, dai, users, aEth} = testEnv;
|
|
|
|
await pool.borrow(weth.address, ethers.utils.parseEther('0.1'), 1, 0);
|
|
await pool.connect(users[2].signer).withdraw(weth.address, ethers.utils.parseEther('1'));
|
|
|
|
await expect(
|
|
pool.collateralSwap(
|
|
_mockSwapAdapter.address,
|
|
weth.address,
|
|
dai.address,
|
|
ethers.utils.parseEther('1'),
|
|
'0x10'
|
|
)
|
|
).to.be.revertedWith('SafeMath: subtraction overflow');
|
|
});
|
|
|
|
it('User tries to swap correct amount', async () => {
|
|
const {pool, weth, dai, aEth, aDai} = testEnv;
|
|
const userAddress = await pool.signer.getAddress();
|
|
const amountToSwap = ethers.utils.parseEther('0.25');
|
|
|
|
const amountToReturn = ethers.utils.parseEther('0.5');
|
|
await _mockSwapAdapter.setAmountToReturn(amountToReturn);
|
|
|
|
const {
|
|
reserveData: wethReserveDataBefore,
|
|
userData: wethUserDataBefore,
|
|
} = await getContractsData(weth.address, userAddress, testEnv);
|
|
|
|
const {reserveData: daiReserveDataBefore, userData: daiUserDataBefore} = await getContractsData(
|
|
dai.address,
|
|
userAddress,
|
|
testEnv
|
|
);
|
|
|
|
const reserveBalanceWETHBefore = await weth.balanceOf(aEth.address);
|
|
const reserveBalanceDAIBefore = await dai.balanceOf(aDai.address);
|
|
|
|
const txReceipt = await waitForTx(
|
|
await pool.collateralSwap(
|
|
_mockSwapAdapter.address,
|
|
weth.address,
|
|
dai.address,
|
|
amountToSwap,
|
|
'0x10'
|
|
)
|
|
);
|
|
const {txTimestamp} = await getTxCostAndTimestamp(txReceipt);
|
|
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(
|
|
calcExpectedATokenBalance(wethReserveDataBefore, wethUserDataBefore, txTimestamp)
|
|
.minus(amountToSwap.toString())
|
|
.toString(),
|
|
'was burned incorrect amount of user funds'
|
|
);
|
|
expect(userATokenBalanceDAIAfter.toString()).to.be.equal(
|
|
calcExpectedATokenBalance(daiReserveDataBefore, daiUserDataBefore, txTimestamp)
|
|
.plus(amountToReturn.toString())
|
|
.toString(),
|
|
'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'
|
|
);
|
|
});
|
|
|
|
it('User tries to drop HF below one', async () => {
|
|
const {pool, weth, dai} = testEnv;
|
|
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);
|
|
|
|
await expect(
|
|
pool.collateralSwap(_mockSwapAdapter.address, weth.address, dai.address, amountToSwap, '0x10')
|
|
).to.be.revertedWith(HEALTH_FACTOR_LOWER_THAN_LIQUIDATION_THRESHOLD);
|
|
});
|
|
});
|