mirror of
https://github.com/Instadapp/aave-protocol-v2.git
synced 2024-07-29 21:47:30 +00:00
319 lines
11 KiB
TypeScript
319 lines
11 KiB
TypeScript
import { TestEnv, makeSuite } from './helpers/make-suite';
|
|
import {
|
|
APPROVAL_AMOUNT_LENDING_POOL,
|
|
MAX_UINT_AMOUNT,
|
|
RAY,
|
|
MAX_BORROW_CAP,
|
|
MAX_SUPPLY_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('Borrow Cap', (testEnv: TestEnv) => {
|
|
const { VL_BORROW_CAP_EXCEEDED, RC_INVALID_BORROW_CAP } = 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 mintedMiliAmount = '1000000000000';
|
|
const daiMinted = await miliUnitToPrecision(dai, mintedMiliAmount);
|
|
const wethMinted = await miliUnitToPrecision(weth, mintedMiliAmount);
|
|
const usdcMinted = await miliUnitToPrecision(usdc, mintedMiliAmount);
|
|
await dai.mint(daiMinted);
|
|
await weth.mint(wethMinted);
|
|
await usdc.mint(usdcMinted);
|
|
await dai.connect(user1.signer).mint(daiMinted);
|
|
await weth.connect(user1.signer).mint(wethMinted);
|
|
await usdc.connect(user1.signer).mint(usdcMinted);
|
|
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.getReserveCaps(usdc.address)).borrowCap;
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
expect(usdcBorrowCap).to.be.equal(MAX_BORROW_CAP);
|
|
expect(daiBorrowCap).to.be.equal(MAX_BORROW_CAP);
|
|
let wethBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
let wethSupplyCap = (await helpersContract.getReserveCaps(dai.address)).supplyCap;
|
|
expect(wethBorrowCap).to.be.equal(MAX_BORROW_CAP);
|
|
expect(wethSupplyCap).to.be.equal(MAX_SUPPLY_CAP);
|
|
|
|
const depositedMiliAmount = (1e9).toString();
|
|
|
|
await configurator.setBorrowCap(usdc.address, 0);
|
|
await configurator.setBorrowCap(dai.address, 0);
|
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
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, wethMinted, user1.address, 0);
|
|
await pool.connect(user1.signer).deposit(dai.address, daiMinted, user1.address, 0);
|
|
await pool.connect(user1.signer).deposit(usdc.address, usdcMinted, 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('Should fail to set the borrow cap for usdc and DAI to max cap + 1 Units', async () => {
|
|
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
const newCap = Number(MAX_BORROW_CAP) + 1;
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
expect(usdcBorrowCap).to.be.equal(0);
|
|
expect(daiBorrowCap).to.be.equal(0);
|
|
|
|
await expect(configurator.setBorrowCap(usdc.address, newCap)).to.be.revertedWith(
|
|
RC_INVALID_BORROW_CAP
|
|
);
|
|
await expect(configurator.setBorrowCap(dai.address, newCap)).to.be.revertedWith(
|
|
RC_INVALID_BORROW_CAP
|
|
);
|
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
});
|
|
it('Sets the borrow cap for usdc and DAI to 110 Units', async () => {
|
|
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
const newCap = '110';
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
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.getReserveCaps(usdc.address)).borrowCap;
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
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('Should succeed to borrow 99 variable dai and 99 stable usdc', async () => {
|
|
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
const borrowedAmount = 99;
|
|
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('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.getReserveCaps(usdc.address)).borrowCap;
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
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();
|
|
|
|
await pool.borrow(
|
|
usdc.address,
|
|
await miliUnitToPrecision(usdc, borrowedMilimount),
|
|
1,
|
|
0,
|
|
deployer.address
|
|
);
|
|
|
|
await 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.getReserveCaps(usdc.address)).borrowCap;
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
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);
|
|
});
|
|
it('Raises the borrow cap for usdc and DAI to max cap Units', async () => {
|
|
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
const newCap = MAX_BORROW_CAP;
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
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();
|
|
|
|
await pool.borrow(
|
|
usdc.address,
|
|
await miliUnitToPrecision(usdc, borrowedMilimount),
|
|
1,
|
|
0,
|
|
deployer.address
|
|
);
|
|
|
|
await pool.borrow(
|
|
dai.address,
|
|
await miliUnitToPrecision(dai, borrowedMilimount),
|
|
2,
|
|
0,
|
|
deployer.address
|
|
);
|
|
});
|
|
});
|