Skip to content

Commit

Permalink
fix(framework): borrow up to available cash in comptroller checks
Browse files Browse the repository at this point in the history
Problem: Sometimes markets don't have huge amounts of cash available,
e.g. on sepolia there's a shortage of WETH. This makes generic checks
fail. However, it's a normal situation, especially on testnets –
WETH wraps Sepolia ETH, so people supply free faucet tokens and borrow
a token that has a higher demand.

Solution: Ensure that the borrowed amount is less than available cash
  • Loading branch information
kkirka committed Nov 28, 2024
1 parent 7dde94d commit 19b3e50
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/vip-framework/checks/checkIsolatedPoolsComptrollers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ const calculateBorrowableAmount = async (
const supplyTokenUSDAmountScaled = suppliedAmount.mul(supplyTokenPrice).div(EXP_SCALE);
const borrowableAmountUSD = supplyTokenUSDAmountScaled.mul(supplyMarketCF).div(EXP_SCALE);
const borrowTokenPriceScaled = borrowTokenPrice.mul(parseUnits("1", borrowUnderlyingDecimals)).div(EXP_SCALE); // scaled to 18 decimals
const borrowTokenAmount = borrowableAmountUSD.div(borrowTokenPriceScaled);

return parseUnits(borrowTokenAmount.toString(), borrowUnderlyingDecimals);
const borrowTokenAmountScaled = borrowableAmountUSD.div(borrowTokenPriceScaled);
const borrowTokenAmount = parseUnits(borrowTokenAmountScaled.toString(), borrowUnderlyingDecimals);
await borrowMarket.accrueInterest();
const cash = await borrowMarket.getCash();
const reserves = await borrowMarket.totalReserves();
const availableCash = cash.sub(reserves).mul(9).div(10); // applying 0.9 factor to account for interests

return borrowTokenAmount.gt(availableCash) ? availableCash : borrowTokenAmount;
};

const runPoolTests = async (pool: PoolMetadata, poolSupplier: string) => {
Expand Down

0 comments on commit 19b3e50

Please sign in to comment.