Skip to content

Commit

Permalink
Add outstanding user debt (#1116)
Browse files Browse the repository at this point in the history
* Add outstanding user debt

* Fix the trv calculation

* Proper formatting
  • Loading branch information
marshall2112 authored Nov 18, 2024
1 parent 78af988 commit b4bb596
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 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
42 changes: 38 additions & 4 deletions apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export type TlcInfo = {
debtCeiling: number;
daiCircuitBreakerRemaining: BigNumber;
templeCircuitBreakerRemaining: BigNumber;
outstandingUserDebt: number;
trvAvailable: BigNumber;
};

export const MAX_LTV = 85;
Expand Down Expand Up @@ -147,6 +149,7 @@ export const BorrowPage = () => {
const debtPosition = await tlcContract.totalDebtPosition();
const totalUserDebt = debtPosition.totalDebt;
const utilizationRatio = debtPosition.utilizationRatio;
const outstandingUserDebt = debtPosition[2];

// NOTE: We are intentionally rounding here to nearest 1e18
const debtCeiling = totalUserDebt
Expand All @@ -158,6 +161,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 @@ -188,6 +194,8 @@ export const BorrowPage = () => {
strategyBalance: fromAtto(maxAvailableToBorrow),
borrowRate: currentBorrowInterestRate,
liquidationLtv: fromAtto(maxLtv),
outstandingUserDebt: fromAtto(outstandingUserDebt),
trvAvailable: trvAvailable,
daiCircuitBreakerRemaining: circuitBreakers?.daiCircuitBreakerRemaining,
templeCircuitBreakerRemaining:
circuitBreakers?.templeCircuitBreakerRemaining,
Expand Down Expand Up @@ -234,6 +242,8 @@ export const BorrowPage = () => {
tlcInfoFromContracts?.daiCircuitBreakerRemaining || ZERO,
templeCircuitBreakerRemaining:
tlcInfoFromContracts?.templeCircuitBreakerRemaining || ZERO,
outstandingUserDebt: tlcInfoFromContracts?.outstandingUserDebt || 0,
trvAvailable: tlcInfoFromContracts?.trvAvailable || ZERO,
});
} catch (e) {
setMetricsLoading(false);
Expand Down Expand Up @@ -477,14 +487,21 @@ export const BorrowPage = () => {
if (!tlcInfo) return '...';

const availableAsBigNumber = toAtto(tlcInfo.strategyBalance);
let borrowableAmount = tlcInfo.strategyBalance;

if (tlcInfo.daiCircuitBreakerRemaining.lt(availableAsBigNumber)) {
return `$${Number(
fromAtto(tlcInfo.daiCircuitBreakerRemaining)
).toLocaleString()}`;
borrowableAmount = fromAtto(tlcInfo.daiCircuitBreakerRemaining);
}

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

return `$${Number(tlcInfo.strategyBalance).toLocaleString()}`;
return `$${Number(borrowableAmount).toLocaleString('en', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}`;
}, [tlcInfo]);

return (
Expand Down Expand Up @@ -712,12 +729,29 @@ export const BorrowPage = () => {
</LeadMetric>
<BrandParagraph>Current Borrow APY </BrandParagraph>
</MetricContainer>
</Metrics>
<Metrics>
<MetricContainer>
<LeadMetric>
{showLoading ? '...' : prices.tpi.toFixed(2)}
</LeadMetric>
<BrandParagraph>Current TPI</BrandParagraph>
</MetricContainer>
<MetricContainer>
<LeadMetric>
{showLoading
? '...'
: tlcInfo &&
`$${Number(tlcInfo.outstandingUserDebt).toLocaleString(
'en',
{
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}
)}`}
</LeadMetric>
<BrandParagraph>Outstanding User Debt</BrandParagraph>
</MetricContainer>
</Metrics>
<ChartContainer>
<TlcChart />
Expand Down

0 comments on commit b4bb596

Please sign in to comment.