2021-04-29 16:11:57 +00:00
|
|
|
import { TestEnv, makeSuite } from './helpers/make-suite';
|
|
|
|
import {
|
|
|
|
APPROVAL_AMOUNT_LENDING_POOL,
|
|
|
|
MAX_UINT_AMOUNT,
|
|
|
|
RAY,
|
|
|
|
MAX_BORROW_CAP,
|
2021-05-17 11:08:07 +00:00
|
|
|
MAX_SUPPLY_CAP,
|
2021-04-29 16:11:57 +00:00
|
|
|
} 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');
|
|
|
|
|
2021-04-30 07:28:35 +00:00
|
|
|
makeSuite('Borrow Cap', (testEnv: TestEnv) => {
|
2021-04-30 07:36:46 +00:00
|
|
|
const { VL_BORROW_CAP_EXCEEDED, RC_INVALID_BORROW_CAP } = ProtocolErrors;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
2021-05-25 12:39:19 +00:00
|
|
|
const unitParse = async (token: WETH9Mocked | MintableERC20, nb: string) =>
|
2021-04-29 16:11:57 +00:00
|
|
|
BigNumber.from(nb).mul(BigNumber.from('10').pow((await token.decimals()) - 3));
|
2021-05-25 12:39:19 +00:00
|
|
|
it('Reserves should initially have borrow cap disabled (borrowCap = 0)', async () => {
|
2021-04-29 16:11:57 +00:00
|
|
|
const {
|
|
|
|
configurator,
|
|
|
|
weth,
|
|
|
|
pool,
|
|
|
|
dai,
|
|
|
|
usdc,
|
|
|
|
deployer,
|
|
|
|
helpersContract,
|
|
|
|
users: [user1],
|
|
|
|
} = testEnv;
|
2021-05-25 12:39:19 +00:00
|
|
|
|
|
|
|
const mintedAmount = parseEther('1000000000');
|
|
|
|
// minting for main user
|
|
|
|
await dai.mint(mintedAmount);
|
|
|
|
await weth.mint(mintedAmount);
|
|
|
|
await usdc.mint(mintedAmount);
|
|
|
|
// minting for lp user
|
|
|
|
await dai.connect(user1.signer).mint(mintedAmount);
|
|
|
|
await weth.connect(user1.signer).mint(mintedAmount);
|
|
|
|
await usdc.connect(user1.signer).mint(mintedAmount);
|
|
|
|
|
2021-04-29 16:11:57 +00:00
|
|
|
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);
|
2021-05-25 12:39:19 +00:00
|
|
|
|
2021-04-30 14:48:09 +00:00
|
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
2021-05-25 12:39:19 +00:00
|
|
|
expect(usdcBorrowCap).to.be.equal('0');
|
|
|
|
expect(daiBorrowCap).to.be.equal('0');
|
|
|
|
});
|
|
|
|
it('Should be able to borrow 10 Dai stable, 10 USDC variable', async () => {
|
|
|
|
const {
|
|
|
|
configurator,
|
|
|
|
weth,
|
|
|
|
pool,
|
|
|
|
dai,
|
|
|
|
usdc,
|
|
|
|
deployer,
|
|
|
|
helpersContract,
|
|
|
|
users: [user1],
|
|
|
|
} = testEnv;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
2021-05-25 12:39:19 +00:00
|
|
|
const suppliedAmount = 1000;
|
|
|
|
const precisionSuppliedAmount = (suppliedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
|
2021-05-25 12:39:19 +00:00
|
|
|
const borrowedAmount = 10;
|
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
|
2021-05-25 12:39:19 +00:00
|
|
|
// deposit collateral
|
2021-04-29 16:11:57 +00:00
|
|
|
await pool.deposit(
|
|
|
|
weth.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(weth, precisionSuppliedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
deployer.address,
|
|
|
|
0
|
|
|
|
);
|
2021-05-25 12:39:19 +00:00
|
|
|
// user 1 deposit more dai and usdc to be able to borrow
|
|
|
|
await pool
|
|
|
|
.connect(user1.signer)
|
|
|
|
.deposit(dai.address, await unitParse(dai, precisionSuppliedAmount), user1.address, 0);
|
|
|
|
|
|
|
|
await pool
|
|
|
|
.connect(user1.signer)
|
|
|
|
.deposit(usdc.address, await unitParse(usdc, precisionSuppliedAmount), user1.address, 0);
|
|
|
|
|
|
|
|
// borrow
|
|
|
|
await pool.borrow(
|
|
|
|
usdc.address,
|
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
|
|
|
|
await pool.borrow(
|
|
|
|
dai.address,
|
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('Sets the borrow cap for Weth and DAI to 10 Units', async () => {
|
|
|
|
const {
|
|
|
|
configurator,
|
|
|
|
weth,
|
|
|
|
pool,
|
|
|
|
dai,
|
|
|
|
usdc,
|
|
|
|
deployer,
|
|
|
|
helpersContract,
|
|
|
|
users: [user1],
|
|
|
|
} = testEnv;
|
|
|
|
|
|
|
|
await configurator.setBorrowCap(usdc.address, 10);
|
|
|
|
await configurator.setBorrowCap(dai.address, 10);
|
|
|
|
|
|
|
|
const usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
const daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
|
|
|
|
|
|
|
expect(usdcBorrowCap).to.be.equal(10);
|
|
|
|
expect(daiBorrowCap).to.be.equal(10);
|
2021-04-29 16:11:57 +00:00
|
|
|
});
|
|
|
|
it('should fail to borrow any dai or usdc, stable or variable', async () => {
|
|
|
|
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
|
|
const borrowedAmount = 10;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
pool.borrow(
|
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
)
|
|
|
|
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
pool.borrow(
|
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
)
|
|
|
|
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
|
|
|
|
});
|
2021-04-30 07:36:46 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
});
|
2021-05-25 12:39:19 +00:00
|
|
|
it('Sets the borrow cap for usdc and DAI to 120 Units', async () => {
|
2021-04-29 16:11:57 +00:00
|
|
|
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
|
2021-05-25 12:39:19 +00:00
|
|
|
const newCap = '120';
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
|
2021-05-25 12:39:19 +00:00
|
|
|
const usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
const daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
expect(usdcBorrowCap).to.be.equal(newCap);
|
|
|
|
expect(daiBorrowCap).to.be.equal(newCap);
|
|
|
|
});
|
2021-04-30 07:36:46 +00:00
|
|
|
it('Should succeed to borrow 10 stable dai and 10 variable usdc', async () => {
|
2021-04-29 16:11:57 +00:00
|
|
|
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
|
|
const borrowedAmount = 10;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
await pool.borrow(
|
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
|
|
|
|
await pool.borrow(
|
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
});
|
2021-04-30 15:35:46 +00:00
|
|
|
it('should fail to borrow 100 variable dai and 100 stable usdc', async () => {
|
2021-04-29 16:11:57 +00:00
|
|
|
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
|
|
const borrowedAmount = 100;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
pool.borrow(
|
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
)
|
|
|
|
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
pool.borrow(
|
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
)
|
|
|
|
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
|
|
|
|
});
|
2021-04-30 09:02:28 +00:00
|
|
|
it('Should succeed to borrow 99 variable dai and 99 stable usdc', async () => {
|
|
|
|
const { usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
|
|
const borrowedAmount = 99;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-30 09:02:28 +00:00
|
|
|
await pool.borrow(
|
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-30 09:02:28 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
|
|
|
|
await pool.borrow(
|
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-30 09:02:28 +00:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
});
|
2021-04-29 16:11:57 +00:00
|
|
|
it('Raises the borrow cap for usdc and DAI to 1000 Units', async () => {
|
|
|
|
const { configurator, usdc, pool, dai, deployer, helpersContract } = testEnv;
|
|
|
|
const newCap = '1000';
|
2021-04-30 14:48:09 +00:00
|
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
|
2021-04-30 14:48:09 +00:00
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
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;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
|
2021-04-30 10:14:48 +00:00
|
|
|
await pool.borrow(
|
2021-04-29 16:11:57 +00:00
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
|
2021-04-30 10:14:48 +00:00
|
|
|
await pool.borrow(
|
2021-04-29 16:11:57 +00:00
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
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';
|
2021-04-30 14:48:09 +00:00
|
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
|
2021-04-30 14:48:09 +00:00
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
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;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-29 16:11:57 +00:00
|
|
|
|
|
|
|
await expect(
|
|
|
|
pool.borrow(
|
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
)
|
|
|
|
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
pool.borrow(
|
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-29 16:11:57 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
)
|
|
|
|
).to.be.revertedWith(VL_BORROW_CAP_EXCEEDED);
|
|
|
|
});
|
2021-04-30 07:36:46 +00:00
|
|
|
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;
|
2021-04-30 14:48:09 +00:00
|
|
|
let usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
let daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-30 07:36:46 +00:00
|
|
|
|
|
|
|
await configurator.setBorrowCap(usdc.address, newCap);
|
|
|
|
await configurator.setBorrowCap(dai.address, newCap);
|
|
|
|
|
2021-04-30 14:48:09 +00:00
|
|
|
usdcBorrowCap = (await helpersContract.getReserveCaps(usdc.address)).borrowCap;
|
|
|
|
daiBorrowCap = (await helpersContract.getReserveCaps(dai.address)).borrowCap;
|
2021-04-30 07:36:46 +00:00
|
|
|
|
|
|
|
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;
|
2021-05-25 12:39:19 +00:00
|
|
|
const precisionBorrowedAmount = (borrowedAmount * 1000).toString();
|
2021-04-30 07:36:46 +00:00
|
|
|
|
2021-04-30 10:14:48 +00:00
|
|
|
await pool.borrow(
|
2021-04-30 07:36:46 +00:00
|
|
|
usdc.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(usdc, precisionBorrowedAmount),
|
2021-04-30 07:36:46 +00:00
|
|
|
1,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
|
2021-04-30 10:14:48 +00:00
|
|
|
await pool.borrow(
|
2021-04-30 07:36:46 +00:00
|
|
|
dai.address,
|
2021-05-25 12:39:19 +00:00
|
|
|
await unitParse(dai, precisionBorrowedAmount),
|
2021-04-30 07:36:46 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
deployer.address
|
|
|
|
);
|
|
|
|
});
|
2021-04-29 16:11:57 +00:00
|
|
|
});
|