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

Utilize on chain data for token price pool graph #2356

Merged
merged 4 commits into from
Aug 21, 2024
Merged
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
82 changes: 53 additions & 29 deletions centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,31 @@
const rangeNumber = getRangeNumber(range.value, poolAge) ?? 100

const isSingleTranche = pool?.tranches.length === 1

// querying chain for more accurate data, since data for today from subquery is not necessarily up to date
const todayAssetValue = pool?.nav.total.toDecimal().toNumber() || 0
const todayPrice = pool?.tranches
? formatBalance(pool?.tranches[pool.tranches.length - 1].tokenPrice || 0, undefined, 5, 5)
: null

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

return { day: new Date(day.timestamp), nav, price }
if (day.timestamp && new Date(day.timestamp).toDateString() === new Date().toDateString()) {
return { day: new Date(day.timestamp), nav: todayAssetValue, price: Number(todayPrice) }
}
return { day: new Date(day.timestamp), nav: Number(nav), price: Number(price) }
}) || [],
[isSingleTranche, truncatedPoolStates]

Check warning on line 82 in centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx

View workflow job for this annotation

GitHub Actions / ff-prod / build-app

React Hook React.useMemo has missing dependencies: 'todayAssetValue' and 'todayPrice'. Either include them or remove the dependency array

Check warning on line 82 in centrifuge-app/src/components/Charts/PoolPerformanceChart.tsx

View workflow job for this annotation

GitHub Actions / build-app

React Hook React.useMemo has missing dependencies: 'todayAssetValue' and 'todayPrice'. Either include them or remove the dependency array
)

const today = {
nav: todayAssetValue,
price: todayPrice,
}

const chartData = data.slice(-rangeNumber)

const dataUrl: any = React.useMemo(() => {
Expand Down Expand Up @@ -101,24 +115,21 @@
if (truncatedPoolStates && truncatedPoolStates?.length < 1 && poolAge > 0)
return <Text variant="body2">No data available</Text>

// querying chain for more accurate data, since data for today from subquery is not necessarily up to date
const todayAssetValue = pool?.nav.total.toDecimal().toNumber() || 0
const todayPrice = data.length > 0 ? data[data.length - 1].price : null
const getOneDayPerMonth = (): any[] => {
const seenMonths = new Set<string>()
const result: any[] = []

const today = {
nav: todayAssetValue,
price: todayPrice,
}
chartData.forEach((item) => {
const date = new Date(item.day)
const monthYear = date.toLocaleString('default', { month: 'short', year: 'numeric' })

const getXAxisInterval = () => {
if (rangeNumber <= 30) return 5
if (rangeNumber > 30 && rangeNumber <= 90) {
return 14
}
if (rangeNumber > 90 && rangeNumber <= 180) {
return 30
}
return 45
if (!seenMonths.has(monthYear)) {
seenMonths.add(monthYear)
result.push(item.day)
}
})

return result
}

return (
Expand Down Expand Up @@ -173,17 +184,13 @@
</defs>
<XAxis
dataKey="day"
dy={4}
interval={0}
minTickGap={100000}
tickLine={false}
type="category"
tickFormatter={(tick: number) => {
if (data.length > 180) {
return new Date(tick).toLocaleString('en-US', { month: 'short' })
}
return new Date(tick).toLocaleString('en-US', { day: 'numeric', month: 'short' })
}}
style={{ fontSize: '10px', fill: theme.colors.textSecondary, letterSpacing: '-0.5px' }}
dy={4}
interval={getXAxisInterval()}
tick={<CustomTick tickCount={getOneDayPerMonth().length} />}
ticks={getOneDayPerMonth()}
/>
<YAxis
stroke="none"
Expand Down Expand Up @@ -216,9 +223,9 @@
</Text>
<Text variant="label2">
{name === 'nav' && typeof value === 'number'
? formatBalance(value, 'USD' || '')
? formatBalance(value, 'USD')
: typeof value === 'number'
? formatBalance(value, 'USD' || '', 6)
? formatBalance(value, 'USD', 6)
: '-'}
</Text>
</Shelf>
Expand Down Expand Up @@ -275,4 +282,21 @@
)
}

const CustomTick = ({ x, y, payload }: any) => {
const theme = useTheme()
return (
<g transform={`translate(${x},${y})`}>
<text
style={{ fontSize: '10px', fill: theme.colors.textSecondary, letterSpacing: '-0.5px' }}
x={0}
y={0}
dy={16}
textAnchor="middle"
>
{new Date(payload.value).toLocaleString('en-US', { month: 'short' })}
</text>
</g>
)
}

export default PoolPerformanceChart
Loading