Skip to content

Commit

Permalink
fix avg maturity
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser committed Sep 12, 2024
1 parent 10212d1 commit 52f3ac7
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions centrifuge-app/src/utils/useAverageMaturity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActiveLoan } from '@centrifuge/centrifuge-js'
import * as React from 'react'
import { Dec } from './Decimal'
import { daysBetween, formatAge } from './date'
import { formatAge } from './date'
import { useLoans } from './useLoans'

export const useAverageMaturity = (poolId: string) => {
Expand All @@ -11,15 +11,22 @@ export const useAverageMaturity = (poolId: string) => {
const assets = (loans && [...loans].filter((asset) => asset.status === 'Active')) as ActiveLoan[]
const maturityPerAsset = assets.reduce((sum, asset) => {
if ('maturityDate' in asset.pricing && asset.pricing.maturityDate && asset.pricing.valuationMethod !== 'cash') {
return sum.add(Dec(daysBetween(new Date(), asset.pricing.maturityDate)).mul(asset.presentValue.toDecimal()))
return sum.add(
Dec(new Date(asset.pricing.maturityDate).getTime() - Date.now()).mul(asset.presentValue.toDecimal())
)
}
return sum
}, Dec(0))

const totalPresentValue = assets.reduce((sum, asset) => sum.add(asset.presentValue?.toDecimal() || Dec(0)), Dec(0))
const totalPresentValue = assets.reduce((sum, asset) => {
if ('maturityDate' in asset.pricing && asset.pricing.maturityDate && asset.pricing.valuationMethod !== 'cash') {
return sum.add(asset.presentValue?.toDecimal() || Dec(0))
}
return sum
}, Dec(0))

return maturityPerAsset.div(totalPresentValue).toNumber()
}, [loans])

return formatAge(avgMaturity)
return formatAge(avgMaturity / (60 * 60 * 24 * 1000))
}

0 comments on commit 52f3ac7

Please sign in to comment.