test: added tests for borrow cap

This commit is contained in:
Hadrien Charlanes 2021-04-29 18:11:57 +02:00
parent be1181e26f
commit 41c53842f7
2 changed files with 237 additions and 0 deletions

View File

@ -26,6 +26,7 @@
"test-liquidate-with-collateral": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/flash-liquidation-with-collateral.spec.ts",
"test-liquidate-underlying": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/liquidation-underlying.spec.ts",
"test-configurator": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/configurator.spec.ts",
"test-borrow-cap": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/borrow-cap.spec.ts",
"test-transfers": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/atoken-transfer.spec.ts",
"test-flash": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/flashloan.spec.ts",
"test-liquidate": "hardhat test test-suites/test-aave/__setup.spec.ts test-suites/test-aave/liquidation-atoken.spec.ts",

View File

@ -0,0 +1,236 @@
import { TestEnv, makeSuite } from './helpers/make-suite';
import {
APPROVAL_AMOUNT_LENDING_POOL,
MAX_UINT_AMOUNT,
RAY,
MAX_BORROW_CAP,
} from '../../helpers/constants';
import { ProtocolErrors } from '../../helpers/types';
import { MintableERC20, WETH9, WETH9Mocked } from '../../types';
import { parseEther } from '@ethersproject/units';
import { BigNumber } from '@ethersproject/bignumber';
const { expect } = require('chai');
makeSuite('LendingPoolConfigurator', (testEnv: TestEnv) => {
const { VL_BORROW_CAP_EXCEEDED } = ProtocolErrors;
const miliUnitToPrecision = async (token: WETH9Mocked | MintableERC20, nb: string) =>
BigNumber.from(nb).mul(BigNumber.from('10').pow((await token.decimals()) - 3));
it('Sets the borrow cap for Weth and DAI to 0 Units, deposits weth', async () => {
const {
configurator,
weth,
pool,
dai,
usdc,
deployer,
helpersContract,
users: [user1],
} = testEnv;
const mintedAmount = parseEther('1000000000');
await dai.mint(mintedAmount);
await weth.mint(mintedAmount);
await usdc.mint(mintedAmount);
await dai.connect(user1.signer).mint(mintedAmount);
await weth.connect(user1.signer).mint(mintedAmount);
await usdc.connect(user1.signer).mint(mintedAmount);
await dai.approve(pool.address, MAX_UINT_AMOUNT);
await weth.approve(pool.address, MAX_UINT_AMOUNT);
await usdc.approve(pool.address, MAX_UINT_AMOUNT);
await dai.connect(user1.signer).approve(pool.address, MAX_UINT_AMOUNT);
await weth.connect(user1.signer).approve(pool.address, MAX_UINT_AMOUNT);
await usdc.connect(user1.signer).approve(pool.address, MAX_UINT_AMOUNT);
let usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
let daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
expect(usdcBorrowCap).to.be.equal(MAX_BORROW_CAP);
expect(daiBorrowCap).to.be.equal(MAX_BORROW_CAP);
const depositedMiliAmount = (1e9).toString();
await configurator.setBorrowCap(usdc.address, 0);
await configurator.setBorrowCap(dai.address, 0);
usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
expect(usdcBorrowCap).to.be.equal(0);
expect(daiBorrowCap).to.be.equal(0);
await pool.deposit(
weth.address,
await miliUnitToPrecision(weth, depositedMiliAmount),
deployer.address,
0
);
await pool.connect(user1.signer).deposit(weth.address, mintedAmount, user1.address, 0);
await pool.connect(user1.signer).deposit(dai.address, mintedAmount, user1.address, 0);
await pool.connect(user1.signer).deposit(usdc.address, mintedAmount, user1.address, 0);
});
it('should fail to borrow any dai or usdc, stable or variable', async () => {
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
const borrowedAmount = 10;
const borrowedMilimount = (borrowedAmount * 1000).toString();
await expect(
pool.borrow(
usdc.address,
await miliUnitToPrecision(usdc, borrowedMilimount),
2,
0,
deployer.address
)
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
await expect(
pool.borrow(
dai.address,
await miliUnitToPrecision(dai, borrowedMilimount),
2,
0,
deployer.address
)
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
});
it('Sets the borrow cap for usdc and DAI to 100 Units', async () => {
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
const newCap = '100';
let usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
let daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
expect(usdcBorrowCap).to.be.equal(0);
expect(daiBorrowCap).to.be.equal(0);
await configurator.setBorrowCap(usdc.address, newCap);
await configurator.setBorrowCap(dai.address, newCap);
usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
expect(usdcBorrowCap).to.be.equal(newCap);
expect(daiBorrowCap).to.be.equal(newCap);
});
it('should succeed to borrow 10 stable dai and 10 variable usdc', async () => {
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
const borrowedAmount = 10;
const borrowedMilimount = (borrowedAmount * 1000).toString();
await pool.borrow(
usdc.address,
await miliUnitToPrecision(usdc, borrowedMilimount),
2,
0,
deployer.address
);
await pool.borrow(
dai.address,
await miliUnitToPrecision(dai, borrowedMilimount),
1,
0,
deployer.address
);
});
it('should fail to borrow 100 variable dai and 100 stable usdc', async () => {
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
const borrowedAmount = 100;
const borrowedMilimount = (borrowedAmount * 1000).toString();
await expect(
pool.borrow(
usdc.address,
await miliUnitToPrecision(usdc, borrowedMilimount),
1,
0,
deployer.address
)
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
await expect(
pool.borrow(
dai.address,
await miliUnitToPrecision(dai, borrowedMilimount),
2,
0,
deployer.address
)
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
});
it('Raises the borrow cap for usdc and DAI to 1000 Units', async () => {
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
const newCap = '1000';
let usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
let daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
await configurator.setBorrowCap(usdc.address, newCap);
await configurator.setBorrowCap(dai.address, newCap);
usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
expect(usdcBorrowCap).to.be.equal(newCap);
expect(daiBorrowCap).to.be.equal(newCap);
});
it('should succeed to borrow 100 variable dai and 100 stable usdc', async () => {
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
const borrowedAmount = 100;
const borrowedMilimount = (borrowedAmount * 1000).toString();
pool.borrow(
usdc.address,
await miliUnitToPrecision(usdc, borrowedMilimount),
1,
0,
deployer.address
);
pool.borrow(
dai.address,
await miliUnitToPrecision(dai, borrowedMilimount),
2,
0,
deployer.address
);
});
it('Lowers the borrow cap for usdc and DAI to 200 Units', async () => {
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
const newCap = '200';
let usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
let daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
await configurator.setBorrowCap(usdc.address, newCap);
await configurator.setBorrowCap(dai.address, newCap);
usdcBorrowCap = await helpersContract.getReserveBorrowCap(usdc.address);
daiBorrowCap = await helpersContract.getReserveBorrowCap(dai.address);
expect(usdcBorrowCap).to.be.equal(newCap);
expect(daiBorrowCap).to.be.equal(newCap);
});
it('should fail to borrow 100 variable dai and 100 stable usdc', async () => {
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
const borrowedAmount = 100;
const borrowedMilimount = (borrowedAmount * 1000).toString();
await expect(
pool.borrow(
usdc.address,
await miliUnitToPrecision(usdc, borrowedMilimount),
1,
0,
deployer.address
)
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
await expect(
pool.borrow(
dai.address,
await miliUnitToPrecision(dai, borrowedMilimount),
2,
0,
deployer.address
)
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
});
});