Skip to content

Commit

Permalink
Fix the trv calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall2112 committed Nov 15, 2024
1 parent 27b4fd4 commit 1fe963e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
TlcInfo,
Warning,
} from '../index';
import { fromAtto, toAtto } from 'utils/bigNumber';
import { fromAtto, toAtto, ZERO } from 'utils/bigNumber';
import styled from 'styled-components';
import { ReactNode, useEffect, useMemo, useState } from 'react';

Expand Down Expand Up @@ -67,18 +67,28 @@ export const Borrow: React.FC<IProps> = ({

const userMaxBorrowBigNumber = toAtto(userMaxBorrow);

if (!tlcInfo) {
return { value: userMaxBorrow, isCircuitBreakerActive: false };
}
let returnValue = { value: userMaxBorrow, isCircuitBreakerActive: false };

if (tlcInfo.daiCircuitBreakerRemaining.lt(userMaxBorrowBigNumber)) {
return {
// Check if the dai circuit breaker is active
if (
tlcInfo &&
tlcInfo.daiCircuitBreakerRemaining.lt(userMaxBorrowBigNumber)
) {
returnValue = {
value: fromAtto(tlcInfo.daiCircuitBreakerRemaining),
isCircuitBreakerActive: true,
};
}

return { value: userMaxBorrow, isCircuitBreakerActive: false };
// Check if trvAvailable from the contract is less than the user max borrow
if (tlcInfo && tlcInfo.trvAvailable.lt(userMaxBorrowBigNumber)) {
returnValue = {
value: fromAtto(tlcInfo.trvAvailable || ZERO),
isCircuitBreakerActive: true,
};
}

return returnValue;
}, [tlcInfo, accountPosition, prices.tpi]);

return (
Expand Down
10 changes: 8 additions & 2 deletions apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type TlcInfo = {
daiCircuitBreakerRemaining: BigNumber;
templeCircuitBreakerRemaining: BigNumber;
outstandingUserDebt: number;
trvAvailable: BigNumber;
};

export const MAX_LTV = 85;
Expand Down Expand Up @@ -161,6 +162,9 @@ export const BorrowPage = () => {
const trvContract = new TreasuryReservesVault__factory(signer).attach(
env.contracts.treasuryReservesVault
);

const trvAvailable = await trvContract.totalAvailable(env.contracts.dai);

const strategyAvailalableToBorrowFromTrv =
await trvContract.availableForStrategyToBorrow(
env.contracts.strategies.tlcStrategy,
Expand Down Expand Up @@ -192,6 +196,7 @@ export const BorrowPage = () => {
borrowRate: currentBorrowInterestRate,
liquidationLtv: fromAtto(maxLtv),
outstandingUserDebt: fromAtto(outstandingUserDebt),
trvAvailable: trvAvailable,
daiCircuitBreakerRemaining: circuitBreakers?.daiCircuitBreakerRemaining,
templeCircuitBreakerRemaining:
circuitBreakers?.templeCircuitBreakerRemaining,
Expand Down Expand Up @@ -243,6 +248,7 @@ export const BorrowPage = () => {
templeCircuitBreakerRemaining:
tlcInfoFromContracts?.templeCircuitBreakerRemaining || ZERO,
outstandingUserDebt: tlcInfoFromContracts?.outstandingUserDebt || 0,
trvAvailable: tlcInfoFromContracts?.trvAvailable || ZERO,
});
} catch (e) {
setMetricsLoading(false);
Expand Down Expand Up @@ -492,12 +498,12 @@ export const BorrowPage = () => {
borrowableAmount = fromAtto(tlcInfo.daiCircuitBreakerRemaining);
}

const trvAvailable = tlcInfo.outstandingUserDebt;
const trvAvailable = fromAtto(tlcInfo.trvAvailable);
if (trvAvailable < borrowableAmount) {
borrowableAmount = trvAvailable;
}

return `$${Number(borrowableAmount).toLocaleString()}`;
return `$${Number(borrowableAmount).toFixed(2)}`;
}, [tlcInfo]);

return (
Expand Down

0 comments on commit 1fe963e

Please sign in to comment.