Skip to content

Commit

Permalink
Merge pull request #286 from VenusProtocol/VEN-1817
Browse files Browse the repository at this point in the history
[VEN-1817] Peckshield audit fix PVE-003
  • Loading branch information
Debugger022 authored Aug 10, 2023
2 parents b235513 + 8534bc9 commit d476c3c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion contracts/Comptroller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ contract Comptroller is
// Skipping the cap check for uncapped coins to save some gas
if (borrowCap != type(uint256).max) {
uint256 totalBorrows = VToken(vToken).totalBorrows();
uint256 nextTotalBorrows = totalBorrows + borrowAmount;
uint256 badDebt = VToken(vToken).badDebt();
uint256 nextTotalBorrows = totalBorrows + borrowAmount + badDebt;
if (nextTotalBorrows > borrowCap) {
revert BorrowCapExceeded(vToken, borrowCap);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/hardhat/Fork/Shortfall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ const grabTokensTo = async (userAddress: string) => {
const trxHolder = await initMainnetUser("0x3DdfA8eC3052539b6C9549F12cEA2C295cfF5296", parseEther("2"));
const usdtHolder = await initMainnetUser("0xF977814e90dA44bFA03b6295A0616a897441aceC", parseEther("2"));

trx.connect(trxHolder).transfer(userAddress, parseUnits("10000", 6));
usdt.connect(usdtHolder).transfer(userAddress, parseUnits("10000", 18));
await trx.connect(trxHolder).transfer(userAddress, parseUnits("10000", 6));
await usdt.connect(usdtHolder).transfer(userAddress, parseUnits("10000", 18));
};

const setupRiskManagementContracts = async () => {
Expand Down Expand Up @@ -113,7 +113,7 @@ const setupRiskManagementContracts = async () => {
MINIMUM_POOL_BAD_DEBT,
ACM,
])) as Shortfall;
shortfall.updatePoolRegistry(POOL_REGISTRY);
await shortfall.updatePoolRegistry(POOL_REGISTRY);
};

const setupTokens = async () => {
Expand Down
41 changes: 41 additions & 0 deletions tests/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,47 @@ describe("Positive Cases", function () {
expect(Number(liquidity)).to.be.closeTo(preComputeLiquidity.toNumber(), Number(convertToUnit(1, 14)));
expect(shortfall).to.equal(0);
});

it("Should revert when borrow cap is reached", async () => {
// Minting amount to account 1 so that it can also heal account
const faucetAmount = convertToUnit(20, 18);
await BTCB.connect(acc1Signer).faucet(faucetAmount);
await BNX.connect(acc1Signer).faucet(faucetAmount);

const mintAmountForAccount1 = convertToUnit(10, 18);
await BTCB.connect(acc1Signer).approve(vBTCB.address, mintAmountForAccount1);

// Minting in vBTCB so that vToken also has sufficient funds
await vBTCB.connect(acc1Signer).mint(mintAmountForAccount1);

// Setting market caps for market
const borrowCap = convertToUnit(10, 18);
await Comptroller.setMarketBorrowCaps([vBTCB.address], [borrowCap]);

const mintAmountForBNX = convertToUnit(1, 16);
await vBNX.connect(acc2Signer).mint(mintAmountForBNX);

const BTCBBorrowAmount = convertToUnit(46, 17);
await vBTCB.connect(acc2Signer).borrow(BTCBBorrowAmount);

// Mining blocks
await mine(300000000);

await BTCB.connect(acc1Signer).approve(vBTCB.address, convertToUnit(10, 18));
await Comptroller.connect(acc1Signer).healAccount(acc2);

// Now another user should not be able to borrow equal to borrowCap - totalBorrows as there is badDebt also
await expect(vBTCB.connect(acc1Signer).borrow(borrowCap)).to.be.revertedWithCustomError(
Comptroller,
"BorrowCapExceeded",
);

// User should be able to borrow equal to borrowCap - badDebt as there is badDebt also.
const amountToBorrow = BigInt(borrowCap) - BigInt((await vBTCB.badDebt()).toString());
await expect(vBTCB.connect(acc1Signer).borrow(amountToBorrow))
.to.emit(vBTCB, "Borrow")
.withArgs(acc1, amountToBorrow, amountToBorrow, amountToBorrow);
});
});
});

Expand Down

0 comments on commit d476c3c

Please sign in to comment.