From 008f70efcac1ab18d1066e385f169f630f949c7c Mon Sep 17 00:00:00 2001 From: Onno Visser Date: Tue, 6 Aug 2024 12:04:51 +0200 Subject: [PATCH] Fix performance chart after Subquery update (#2342) --- .../Charts/PoolPerformanceChart.tsx | 5 +++-- centrifuge-js/src/modules/pools.ts | 20 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx b/centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx index 4d70ed8f4b..07ffd4bab6 100644 --- a/centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx +++ b/centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx @@ -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) diff --git a/centrifuge-js/src/modules/pools.ts b/centrifuge-js/src/modules/pools.ts index feb70e5a4e..4b859e958f 100644 --- a/centrifuge-js/src/modules/pools.ts +++ b/centrifuge-js/src/modules/pools.ts @@ -2473,16 +2473,20 @@ export function getPoolsModule(inst: Centrifuge) { const trancheStates: Record = {} 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() 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), @@ -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 @@ -2599,7 +2603,7 @@ export function getPoolsModule(inst: Centrifuge) { poolSnapshots: { nodes: { netAssetValue: string - periodStart: string + periodId: string pool: { currency: { decimals: number @@ -2612,7 +2616,7 @@ export function getPoolsModule(inst: Centrifuge) { poolSnapshots(first: 1000, orderBy: PERIOD_START_ASC) { nodes { netAssetValue - periodStart + periodId pool { currency { decimals @@ -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(), }))