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 yields #2622

Merged
merged 2 commits into from
Feb 10, 2025
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
38 changes: 23 additions & 15 deletions centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DailyPoolState, DailyTrancheState, Pool } from '@centrifuge/centrifuge-js'
import { DailyPoolState, Perquintill, Pool, Token } from '@centrifuge/centrifuge-js'
import { AnchorButton, Box, IconDownload, Select, Shelf, Stack, Tabs, TabsItem, Text } from '@centrifuge/fabric'
import * as React from 'react'
import { useParams } from 'react-router'
Expand Down Expand Up @@ -73,18 +73,22 @@ function calculateTranchePrices(pool: Pool) {
return { juniorTokenPrice, seniorTokenPrice }
}

function getYieldFieldForFilter(tranche: DailyTrancheState, filter: string) {
function getYieldFieldForFilter(
tranche?: Pick<Token, 'yield30DaysAnnualized' | 'yield90DaysAnnualized' | 'yieldYTD' | 'yieldSinceInception'>,
filter?: string
) {
const zero = new Perquintill(0)
switch (filter) {
case '30d':
return tranche.yield30DaysAnnualized || 0
return tranche?.yield30DaysAnnualized || zero
case '90d':
return tranche.yield90DaysAnnualized || 0
return tranche?.yield90DaysAnnualized || zero
case 'ytd':
return tranche.yieldYTD || 0
return tranche?.yieldYTD || zero
case 'all':
return tranche.yieldSinceInception || 0
return tranche?.yieldSinceInception || zero
default:
return 0
return zero
}
}

Expand Down Expand Up @@ -125,14 +129,18 @@ function PoolPerformanceChart() {
? formatBalance(pool?.tranches[pool.tranches.length - 1].tokenPrice || 0, undefined, 5, 5)
: null

const todayJuniorApy = pool?.tranches
?.find((pool) => pool.seniority === 0)
?.[range.value === 'all' ? 'yieldSinceInception' : 'yield30DaysAnnualized']?.toPercent()
const todayJuniorApy = getYieldFieldForFilter(
pool?.tranches?.find((pool) => pool.seniority === 0),
range.value
)
?.toPercent()
.toNumber()

const todaySeniorApy = pool?.tranches
?.find((pool) => pool.seniority === 1)
?.[range.value === 'all' ? 'yieldSinceInception' : 'yield30DaysAnnualized']?.toPercent()
const todaySeniorApy = getYieldFieldForFilter(
pool?.tranches?.find((pool) => pool.seniority === 1),
range.value
)
?.toPercent()
.toNumber()

const trancheTodayPrice = calculateTranchePrices(pool as Pool)
Expand All @@ -150,9 +158,9 @@ function PoolPerformanceChart() {
const seniorTokenPrice = seniorTrancheKey ? day.tranches[seniorTrancheKey]?.price?.toFloat() ?? null : null

const juniorAPY = getYieldFieldForFilter(day.tranches[juniorTrancheKey], range.value)
const formattedJuniorAPY = juniorAPY !== 0 ? juniorAPY.toPercent().toNumber() : 0
const formattedJuniorAPY = juniorAPY.toPercent().toNumber()
const seniorAPY = seniorTrancheKey ? getYieldFieldForFilter(day.tranches[seniorTrancheKey], range.value) : null
const formattedSeniorAPY = seniorAPY !== 0 ? seniorAPY?.toPercent().toNumber() : null
const formattedSeniorAPY = seniorAPY?.toPercent().toNumber()

if (day.timestamp && new Date(day.timestamp).toDateString() === new Date().toDateString()) {
const tranchePrices = calculateTranchePrices(pool as Pool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const TrancheTokenCards = ({
},
},
{
header: 'APY',
header: 'Yield since inception',
align: 'left',
cell: (row: Row) => {
return (
Expand Down
32 changes: 31 additions & 1 deletion centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ export type Tranche = {
currentRiskBuffer: Perquintill
interestRatePerSec: Rate | null
yield30DaysAnnualized: Perquintill | null
yield90DaysAnnualized: Perquintill | null
yieldYTD: Perquintill | null
yieldSinceInception: Perquintill | null
lastUpdatedInterest: string
ratio: Perquintill
Expand Down Expand Up @@ -2117,6 +2119,20 @@ export function getPoolsModule(inst: Centrifuge) {
},
{} as Record<string, string | null>
)
const yield90DaysTrancheId = yieldsAnnualized?.trancheSnapshots.nodes.reduce(
(acc, { yield90DaysAnnualized, trancheId }) => {
acc[trancheId] = yield90DaysAnnualized
return acc
},
{} as Record<string, string | null>
)
const yieldYTDTrancheId = yieldsAnnualized?.trancheSnapshots.nodes.reduce(
(acc, { yieldYTD, trancheId }) => {
acc[trancheId] = yieldYTD
return acc
},
{} as Record<string, string | null>
)
const yieldInceptionTrancheId = yieldsAnnualized?.trancheSnapshots.nodes.reduce(
(acc, { yieldSinceInception, trancheId }) => {
acc[trancheId] = yieldSinceInception
Expand Down Expand Up @@ -2204,6 +2220,12 @@ export function getPoolsModule(inst: Centrifuge) {
yield30DaysAnnualized: yield30DaysTrancheId?.[`${poolId}-${trancheId}`]
? new Perquintill(yield30DaysTrancheId[`${poolId}-${trancheId}`]!)
: null,
yield90DaysAnnualized: yield90DaysTrancheId?.[`${poolId}-${trancheId}`]
? new Perquintill(yield90DaysTrancheId[`${poolId}-${trancheId}`]!)
: null,
yieldYTD: yieldYTDTrancheId?.[`${poolId}-${trancheId}`]
? new Perquintill(yieldYTDTrancheId[`${poolId}-${trancheId}`]!)
: null,
yieldSinceInception: yieldInceptionTrancheId?.[`${poolId}-${trancheId}`]
? new Perquintill(yieldInceptionTrancheId[`${poolId}-${trancheId}`]!)
: null,
Expand Down Expand Up @@ -2343,14 +2365,22 @@ export function getPoolsModule(inst: Centrifuge) {
function getLatestTrancheSnapshots() {
return inst.getSubqueryObservable<{
trancheSnapshots: {
nodes: { yield30DaysAnnualized: string | null; yieldSinceInception: string | null; trancheId: string }[]
nodes: {
yield30DaysAnnualized: string | null
yield90DaysAnnualized: string | null
yieldYTD: string | null
yieldSinceInception: string | null
trancheId: string
}[]
}
}>(
`{
trancheSnapshots(distinct: TRANCHE_ID, orderBy: TIMESTAMP_DESC) {
nodes {
trancheId
yield30DaysAnnualized
yield90DaysAnnualized
yieldYTD
yieldSinceInception
}
}
Expand Down
Loading