From b813e6e6365bbc3928e8e3215f696f9787e947fd Mon Sep 17 00:00:00 2001 From: katty barroso Date: Tue, 27 Aug 2024 13:59:49 -0500 Subject: [PATCH] work in progress YOY --- centrifuge-app/src/pages/Pools.tsx | 21 ++++-- centrifuge-app/src/utils/useListedPools.ts | 85 ++++++++++++++++++++++ 2 files changed, 98 insertions(+), 8 deletions(-) diff --git a/centrifuge-app/src/pages/Pools.tsx b/centrifuge-app/src/pages/Pools.tsx index c004107075..331d5435e0 100644 --- a/centrifuge-app/src/pages/Pools.tsx +++ b/centrifuge-app/src/pages/Pools.tsx @@ -1,7 +1,7 @@ import { formatBalance } from '@centrifuge/centrifuge-react' -import { Box, IconArrowUpRight, Stack, StatusChip, Text } from '@centrifuge/fabric' +import { Box, IconArrowDown, IconArrowUpRight, Stack, StatusChip, Text } from '@centrifuge/fabric' import * as React from 'react' -import { useListedPools } from '../../src/utils/useListedPools' +import { useListedPools, useYearOverYearGrowth } from '../../src/utils/useListedPools' import { LayoutSection } from '../components/LayoutBase/LayoutSection' import { PoolList } from '../components/PoolList' import { prefetchRoute } from '../components/Root' @@ -10,6 +10,9 @@ import { Dec } from '../utils/Decimal' export default function PoolsPage() { const [, listedTokens] = useListedPools() + const { totalValueLockedGrowth } = useYearOverYearGrowth() + const isPositiveYoy = totalValueLockedGrowth > 0 + const IconComponent = isPositiveYoy ? IconArrowUpRight : IconArrowDown const totalValueLocked = React.useMemo(() => { return ( @@ -41,12 +44,14 @@ export default function PoolsPage() { Total value locked (TVL) - - - - 24% YoY - - + {totalValueLockedGrowth != 0 && ( + + + + {formatBalance(totalValueLockedGrowth ?? 0, '', 2)} YoY + + + )} {formatBalance(totalValueLocked ?? 0, config.baseCurrency)} diff --git a/centrifuge-app/src/utils/useListedPools.ts b/centrifuge-app/src/utils/useListedPools.ts index 7f03ea92dd..c45401a12f 100644 --- a/centrifuge-app/src/utils/useListedPools.ts +++ b/centrifuge-app/src/utils/useListedPools.ts @@ -1,12 +1,14 @@ import { PoolMetadata } from '@centrifuge/centrifuge-js' import BN from 'bn.js' import * as React from 'react' +import { useMemo } from 'react' import { useAddress } from '../utils/useAddress' import { useMetadataMulti } from '../utils/useMetadata' import { usePermissions } from '../utils/usePermissions' import { usePools } from '../utils/usePools' import { getPoolTVL } from './getPoolTVL' import { useTinlakePools } from './tinlake/useTinlakePools' +import { useSubquery } from './useSubquery' const sign = (n: BN) => (n.isZero() ? 0 : n.isNeg() ? -1 : 1) @@ -40,3 +42,86 @@ export function useListedPools() { return [listedPools, listedTokens, isLoading] as const } + +export function useYearOverYearGrowth() { + const { currentYearStart, previousYearStart } = useMemo(() => { + const startOfDayToday = new Date() + startOfDayToday.setHours(0, 0, 0, 0) + + return { + currentYearStart: startOfDayToday.toISOString(), + previousYearStart: new Date(new Date().getFullYear() - 1, 0, 1).toISOString(), + } + }, []) + + const { data } = useSubquery( + `query ($currentYearStart: Datetime!, $previousYearStart: Datetime!) { + pools { + nodes { + poolSnapshots( + filter: { + timestamp: { + greaterThan: $previousYearStart, + lessThan: $currentYearStart + } + }, + ) { + nodes { + netAssetValue + totalReserve + timestamp + } + } + } + } + }`, + { + currentYearStart, + previousYearStart, + }, + { + enabled: !!currentYearStart && !!previousYearStart, + } + ) + + const pools = data?.pools?.nodes + + const flattenedData = pools?.flatMap((pool: any) => pool.poolSnapshots.nodes) || [] + + const groupedByYear = flattenedData.reduce((acc: any, snapshot: any) => { + const year = new Date(snapshot.timestamp).getFullYear() + + if (!acc[year]) { + acc[year] = [] + } + + acc[year].push(snapshot) + + return acc + }, {}) + + const years = Object.keys(groupedByYear).sort() + const previousYear = groupedByYear[years[0]] + const currentYear = groupedByYear[years[1]] + + function getTotalValueLocked(pools: any) { + return pools + ?.reduce((sum: any, pool: any) => { + return sum.add(new BN(pool.netAssetValue)).add(new BN(pool.totalReserve)) + }, new BN(0)) + .div(new BN(10).pow(new BN(24))) + .toString() + } + + const totalValueLocked = getTotalValueLocked(currentYear) ?? 0 + const totalValueLockedOneYearAgo = getTotalValueLocked(previousYear) ?? 0 + + const totalValueLockedGrowth = + previousYear && currentYear + ? ((parseFloat(totalValueLocked) - parseFloat(totalValueLockedOneYearAgo)) / + parseFloat(totalValueLockedOneYearAgo)) * + 100 + : 0 + + return { totalValueLockedGrowth } +}