Skip to content

Commit

Permalink
Update price field to show latest oracle price instead of settlement …
Browse files Browse the repository at this point in the history
…price
  • Loading branch information
sophialittlejohn committed Oct 10, 2024
1 parent e1e2c56 commit 3c9fc56
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
57 changes: 37 additions & 20 deletions centrifuge-app/src/pages/Loan/PricingValues.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Loan, Pool, TinlakeLoan } from '@centrifuge/centrifuge-js'
import { CurrencyBalance, Loan, Pool, TinlakeLoan } from '@centrifuge/centrifuge-js'
import { useCentrifugeApi, useCentrifugeQuery } from '@centrifuge/centrifuge-react'
import { Card, Stack, Text } from '@centrifuge/fabric'
import { utils } from 'ethers'
import { first, map } from 'rxjs'
import { Tooltips } from '../../components/Tooltips'
import { formatDate, getAge } from '../../utils/date'
import { formatBalance, formatPercentage } from '../../utils/formatting'
import { getLatestPrice } from '../../utils/getLatestPrice'
import { TinlakePool } from '../../utils/tinlake/useTinlakePools'
import { useAvailableFinancing } from '../../utils/useLoans'
import { useAssetTransactions } from '../../utils/usePools'
import { MetricsTable } from './MetricsTable'

type Props = {
Expand All @@ -16,9 +17,32 @@ type Props = {

export function PricingValues({ loan, pool }: Props) {
const { pricing } = loan

const assetTransactions = useAssetTransactions(loan.poolId)
const { current: availableFinancing } = useAvailableFinancing(loan.poolId, loan.id)
const api = useCentrifugeApi()

const [oracleCollection] = useCentrifugeQuery(['oracleCollection', pool.id], () =>
api.query.oraclePriceCollection.collection(pool.id).pipe(
first(),
map((data) => {
const info = data.toPrimitive() as any
const currentAssetPrice = Object.entries(info.content)
.filter(([key]) => {
if ('priceId' in pricing && 'isin' in pricing.priceId) {
return utils.toUtf8String(JSON.parse(key).isin) === pricing.priceId.isin
} else {
return JSON.parse(key).poolLoanId[1].toString() === loan.id.toString()
}
})
.map(([_, value]: [string, any]) => {
return value[0] // current price
})
return {
value: CurrencyBalance.fromFloat(currentAssetPrice?.[0] ?? 0, pool.currency.decimals),
timestamp: info.lastUpdated,
}
})
)
)

const isOutstandingDebtOrDiscountedCashFlow =
'valuationMethod' in pricing &&
Expand All @@ -27,19 +51,10 @@ export function PricingValues({ loan, pool }: Props) {
if ('valuationMethod' in pricing && pricing.valuationMethod === 'oracle') {
const today = new Date()
today.setUTCHours(0, 0, 0, 0)
let latestOraclePrice = pricing.oracle[0]
pricing.oracle.forEach((price) => {
if (price.timestamp > latestOraclePrice.timestamp) {
latestOraclePrice = price
}
})

const borrowerAssetTransactions = assetTransactions?.filter(
(assetTransaction) => assetTransaction.asset.id === `${loan.poolId}-${loan.id}`
)
const latestPrice = getLatestPrice(latestOraclePrice, borrowerAssetTransactions, pool.currency.decimals)
const latestPrice = oracleCollection || { value: new CurrencyBalance(0, pool.currency.decimals), timestamp: 0 }

const days = latestPrice.timestamp > 0 ? getAge(new Date(latestPrice.timestamp).toISOString()) : undefined
const priceLastUpdated = days && days.includes('0') ? 'Today' : `${days} ago`

const accruedPrice = 'currentPrice' in loan && loan.currentPrice

Expand All @@ -53,7 +68,7 @@ export function PricingValues({ loan, pool }: Props) {
metrics={[
...('isin' in pricing.priceId ? [{ label: 'ISIN', value: pricing.priceId.isin }] : []),
{
label: `Current price${latestOraclePrice.value.isZero() && latestPrice ? ' (settlement)' : ''}`,
label: `Current price${latestPrice.value.isZero() && latestPrice ? ' (settlement)' : ''}`,
value: accruedPrice
? `${formatBalance(accruedPrice || latestPrice, pool.currency.symbol, 6, 2)}`
: latestPrice
Expand All @@ -64,9 +79,11 @@ export function PricingValues({ loan, pool }: Props) {
label: <Tooltips type="linearAccrual" />,
value: pricing.withLinearPricing ? 'Enabled' : 'Disabled',
},
...(!pricing.withLinearPricing
? [{ label: 'Price last updated', value: days ? `${days} ago` : `Today` }]
: [{ label: 'Last manual price update', value: days ? `${days} ago` : `Today` }]),
...(loan.status === 'Active' && loan.outstandingDebt.toDecimal().lte(0)
? []
: !pricing.withLinearPricing
? [{ label: 'Price last updated', value: priceLastUpdated }]
: [{ label: 'Last manual price update', value: priceLastUpdated }]),
...(pricing.interestRate.gtn(0)
? [
{
Expand Down
22 changes: 0 additions & 22 deletions centrifuge-app/src/utils/getLatestPrice.ts

This file was deleted.

0 comments on commit 3c9fc56

Please sign in to comment.