Skip to content

Commit

Permalink
Fix performance chart after Subquery update (#2342)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored Aug 6, 2024
1 parent a5d4d9e commit 008f70e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ function PoolPerformanceChart() {
const [range, setRange] = React.useState<(typeof rangeFilters)[number]>({ value: 'all', label: 'All' })
const rangeNumber = getRangeNumber(range.value, poolAge) ?? 100

const isSingleTranche = pool?.tranches.length === 1
const data: ChartData[] = React.useMemo(
() =>
truncatedPoolStates?.map((day) => {
const nav = day.poolState.netAssetValue.toDecimal().toNumber()
const price = Object.values(day.tranches).length === 1 ? Object.values(day.tranches)[0].price!.toFloat() : null
const price = (isSingleTranche && Object.values(day.tranches)[0].price?.toFloat()) || null

return { day: new Date(day.timestamp), nav, price }
}) || [],
[truncatedPoolStates]
[isSingleTranche, truncatedPoolStates]
)

const chartData = data.slice(-rangeNumber)
Expand Down
20 changes: 12 additions & 8 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2473,16 +2473,20 @@ export function getPoolsModule(inst: Centrifuge) {
const trancheStates: Record<string, { timestamp: string; tokenPrice: Price }[]> = {}
trancheSnapshots?.forEach((state) => {
const tid = state.tranche.trancheId
const entry = { timestamp: state.timestamp, tokenPrice: new Price(state.tokenPrice) }
const entry = { timestamp: state.timestamp.slice(0, 10), tokenPrice: new Price(state.tokenPrice) }
if (trancheStates[tid]) {
trancheStates[tid].push(entry)
} else {
trancheStates[tid] = [entry]
}
})
const poolSeenDay = new Set<string>()
return {
poolStates:
poolSnapshots?.map((state) => {
poolSnapshots?.flatMap((state) => {
const timestamp = state.timestamp.slice(0, 10)
if (poolSeenDay.has(timestamp)) return []
poolSeenDay.add(timestamp)
const poolState = {
id: state.id,
netAssetValue: new CurrencyBalance(state.netAssetValue, poolCurrency.decimals),
Expand Down Expand Up @@ -2543,8 +2547,8 @@ export function getPoolsModule(inst: Centrifuge) {
const poolValue = new CurrencyBalance(new BN(state?.netAssetValue || '0'), poolCurrency.decimals)

// TODO: This is inefficient, would be better to construct a map indexed by the timestamp
const trancheSnapshotsToday = trancheSnapshots?.filter((t) => t.timestamp === state.timestamp)

const trancheSnapshotsToday = trancheSnapshots?.filter((t) => t.timestamp.slice(0, 10) === timestamp)
if (!trancheSnapshotsToday?.length) return []
const tranches: { [trancheId: string]: DailyTrancheState } = {}
trancheSnapshotsToday?.forEach((tranche) => {
const tid = tranche.tranche.trancheId
Expand Down Expand Up @@ -2599,7 +2603,7 @@ export function getPoolsModule(inst: Centrifuge) {
poolSnapshots: {
nodes: {
netAssetValue: string
periodStart: string
periodId: string
pool: {
currency: {
decimals: number
Expand All @@ -2612,7 +2616,7 @@ export function getPoolsModule(inst: Centrifuge) {
poolSnapshots(first: 1000, orderBy: PERIOD_START_ASC) {
nodes {
netAssetValue
periodStart
periodId
pool {
currency {
decimals
Expand All @@ -2630,8 +2634,8 @@ export function getPoolsModule(inst: Centrifuge) {
}

const mergedMap = new Map()
const formatted = data.poolSnapshots.nodes.map(({ netAssetValue, periodStart, pool }) => ({
dateInMilliseconds: new Date(periodStart).getTime(),
const formatted = data.poolSnapshots.nodes.map(({ netAssetValue, periodId, pool }) => ({
dateInMilliseconds: new Date(periodId).getTime(),
tvl: new CurrencyBalance(new BN(netAssetValue || '0'), pool.currency.decimals).toDecimal(),
}))

Expand Down

0 comments on commit 008f70e

Please sign in to comment.