From b4bb5966a63c3164184c6e94db1bd126a7f03bd8 Mon Sep 17 00:00:00 2001 From: Marshall <99344331+marshall2112@users.noreply.github.com> Date: Mon, 18 Nov 2024 17:52:35 -0500 Subject: [PATCH] Add outstanding user debt (#1116) * Add outstanding user debt * Fix the trv calculation * Proper formatting --- .../Core/DappPages/Borrow/TLC/Borrow.tsx | 24 +++++++---- .../Pages/Core/DappPages/Borrow/index.tsx | 42 +++++++++++++++++-- 2 files changed, 55 insertions(+), 11 deletions(-) diff --git a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/TLC/Borrow.tsx b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/TLC/Borrow.tsx index 0f5f6f756..d7c9b109f 100644 --- a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/TLC/Borrow.tsx +++ b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/TLC/Borrow.tsx @@ -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'; @@ -67,18 +67,28 @@ export const Borrow: React.FC = ({ 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 ( diff --git a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx index c3d47543c..f23fd5155 100644 --- a/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx +++ b/apps/dapp/src/components/Pages/Core/DappPages/Borrow/index.tsx @@ -52,6 +52,8 @@ export type TlcInfo = { debtCeiling: number; daiCircuitBreakerRemaining: BigNumber; templeCircuitBreakerRemaining: BigNumber; + outstandingUserDebt: number; + trvAvailable: BigNumber; }; export const MAX_LTV = 85; @@ -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 @@ -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, @@ -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, @@ -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); @@ -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 ( @@ -712,12 +729,29 @@ export const BorrowPage = () => { Current Borrow APY + + {showLoading ? '...' : prices.tpi.toFixed(2)} Current TPI + + + {showLoading + ? '...' + : tlcInfo && + `$${Number(tlcInfo.outstandingUserDebt).toLocaleString( + 'en', + { + minimumFractionDigits: 2, + maximumFractionDigits: 2, + } + )}`} + + Outstanding User Debt +