Skip to content

Commit

Permalink
CentrifugeJS: Loan present value (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored and JP Angelle committed Sep 15, 2023
1 parent 1a7e035 commit 9afbadc
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2355,6 +2355,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(
Expand All @@ -2366,6 +2367,7 @@ export function getPoolsModule(inst: Centrifuge) {
rateValues,
interestLastUpdated,
rawCurrency,
rawPortfolio,
]) => {
const currency = rawCurrency.toPrimitive() as AssetCurrencyData
const rates = rateValues.toPrimitive() as InterestAccrual[]
Expand All @@ -2385,6 +2387,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
Expand Down Expand Up @@ -2484,6 +2500,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
Expand Down Expand Up @@ -2556,6 +2573,7 @@ export function getPoolsModule(inst: Centrifuge) {
normalizedDebt: new CurrencyBalance(normalizedDebt, currency.decimals),
outstandingPrincipal,
outstandingInterest,
presentValue: portfolio.presentValue,
}
}
)
Expand Down

0 comments on commit 9afbadc

Please sign in to comment.