Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CentrifugeJS: Loan present value #1578

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2361,6 +2361,7 @@ export function getPoolsModule(inst: Centrifuge) {
api.query.interestAccrual.rates(),
api.query.interestAccrual.lastUpdated(),
api.query.ormlAssetRegistry.metadata((poolValue.toHuman() 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 @@ -2372,6 +2373,7 @@ export function getPoolsModule(inst: Centrifuge) {
rateValues,
interestLastUpdated,
rawCurrency,
rawPortfolio,
]) => {
const currency = rawCurrency.toHuman() as AssetCurrencyData
const rates = rateValues.toPrimitive() as InterestAccrual[]
Expand All @@ -2391,6 +2393,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 @@ -2490,6 +2506,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 @@ -2562,6 +2579,7 @@ export function getPoolsModule(inst: Centrifuge) {
normalizedDebt: new CurrencyBalance(normalizedDebt, currency.decimals),
outstandingPrincipal,
outstandingInterest,
presentValue: portfolio.presentValue,
}
}
)
Expand Down
Loading