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

Fix oracle price last updated #2486

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
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
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.

Loading