From b335dab113a7b768c2b22ec1f140991c9e9cc6b3 Mon Sep 17 00:00:00 2001 From: Onno Visser Date: Tue, 12 Sep 2023 18:52:54 +0200 Subject: [PATCH] CentrifugeJS: Loan present value (#1578) --- centrifuge-js/src/modules/pools.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/centrifuge-js/src/modules/pools.ts b/centrifuge-js/src/modules/pools.ts index 9e13dff04a..31e23e42f6 100644 --- a/centrifuge-js/src/modules/pools.ts +++ b/centrifuge-js/src/modules/pools.ts @@ -2513,6 +2513,7 @@ export function getPoolsModule(inst: Centrifuge) { api.query.interestAccrual.rates(), api.query.interestAccrual.lastUpdated(), api.query.ormlAssetRegistry.metadata((poolValue.toPrimitive() as any).currency), + api.call.loansApi.portfolio(poolId), // TODO: remove loans.activeLoans and use values from this runtime call ]).pipe(take(1)) }), map( @@ -2524,6 +2525,7 @@ export function getPoolsModule(inst: Centrifuge) { rateValues, interestLastUpdated, rawCurrency, + rawPortfolio, ]) => { const currency = rawCurrency.toPrimitive() as AssetCurrencyData const rates = rateValues.toPrimitive() as InterestAccrual[] @@ -2543,6 +2545,20 @@ export function getPoolsModule(inst: Centrifuge) { } }) + const activeLoansPortfolio: Record< + string, + { + presentValue: CurrencyBalance + } + > = {} + + ;(rawPortfolio as any).forEach(([key, value]: [Codec, Codec]) => { + const data = value.toPrimitive() as any + activeLoansPortfolio[String(key.toPrimitive())] = { + presentValue: new CurrencyBalance(data.presentValue, currency.decimals), + } + }) + function getSharedLoanInfo(loan: CreatedLoanData | ActiveLoanData | ClosedLoanData) { const info = 'info' in loan ? loan.info : loan const [collectionId, nftId] = info.collateral @@ -2642,6 +2658,7 @@ export function getPoolsModule(inst: Centrifuge) { new Rate(rate.interestRatePerSec).toApr().toDecimalPlaces(4).toString() === sharedInfo.pricing.interestRate.toDecimal().toString() ) + const portfolio = activeLoansPortfolio[loanId.toString()] const penaltyRate = 'external' in loan.pricing ? loan.pricing.external.interest.penalty @@ -2714,6 +2731,7 @@ export function getPoolsModule(inst: Centrifuge) { normalizedDebt: new CurrencyBalance(normalizedDebt, currency.decimals), outstandingPrincipal, outstandingInterest, + presentValue: portfolio.presentValue, } } )