|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import React from 'react'; |
| 3 | +import React, { useEffect, useMemo, useState } from 'react'; |
| 4 | +import ReactECharts from 'echarts-for-react'; |
4 | 5 | import styles from './HistoricalPricingChart.module.scss'; |
| 6 | +import { useUnit } from 'effector-react'; |
| 7 | +import { $network } from '@/api/connection'; |
| 8 | +import { getTokenSymbol, toUnit } from '@/utils'; |
| 9 | +import { Network } from '@/types'; |
| 10 | +import { subscanFetch } from '@/subscan'; |
| 11 | + |
| 12 | +type SubscanSaleData = { |
| 13 | + sales_cycle: number; |
| 14 | + start_price: string; |
| 15 | + price: string; |
| 16 | + sellout_price: string; |
| 17 | + cores_sold: number; |
| 18 | + cores_offered: number; |
| 19 | +}; |
| 20 | + |
| 21 | +type SalePoint = { |
| 22 | + cycle: number; |
| 23 | + price: number; |
| 24 | + startPrice: number; |
| 25 | +}; |
| 26 | + |
| 27 | +const fetchHistoricalSalePrices = async (network: Network): Promise<SalePoint[]> => { |
| 28 | + // Get the latest sale to know the current cycle. |
| 29 | + const latest = await subscanFetch<SubscanSaleData>(network, '/api/scan/broker/sale'); |
| 30 | + if (!latest?.sales_cycle) return []; |
| 31 | + |
| 32 | + const currentCycle = latest.sales_cycle; |
| 33 | + const cyclesToFetch = Math.min(currentCycle, 3); |
| 34 | + |
| 35 | + // Fetch the last 3 cycles sequentially to respect rate limits. |
| 36 | + const points: SalePoint[] = []; |
| 37 | + for (let i = cyclesToFetch - 1; i >= 0; i--) { |
| 38 | + const cycle = currentCycle - i; |
| 39 | + const data = await subscanFetch<SubscanSaleData>(network, '/api/scan/broker/sale', { |
| 40 | + sales_cycle: cycle, |
| 41 | + }); |
| 42 | + if (data && data.sales_cycle > 0) { |
| 43 | + points.push({ |
| 44 | + cycle: data.sales_cycle, |
| 45 | + price: parseInt(data.sellout_price || data.price || '0'), |
| 46 | + startPrice: parseInt(data.start_price || '0'), |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return points; |
| 52 | +}; |
5 | 53 |
|
6 | 54 | export default function HistoricalPricingChart() { |
| 55 | + const network = useUnit($network); |
| 56 | + const [data, setData] = useState<SalePoint[]>([]); |
| 57 | + const [loading, setLoading] = useState(true); |
| 58 | + const token = getTokenSymbol(network); |
| 59 | + |
| 60 | + useEffect(() => { |
| 61 | + if (!network) return; |
| 62 | + setLoading(true); |
| 63 | + fetchHistoricalSalePrices(network) |
| 64 | + .then(setData) |
| 65 | + .finally(() => setLoading(false)); |
| 66 | + }, [network]); |
| 67 | + |
| 68 | + const option = useMemo(() => { |
| 69 | + if (!network || !data.length) return null; |
| 70 | + |
| 71 | + const fmt = (raw: number) => toUnit(network, BigInt(raw)); |
| 72 | + const yVals = [ |
| 73 | + ...data.map((d) => Number(fmt(d.price))), |
| 74 | + ...data.map((d) => Number(fmt(d.startPrice))), |
| 75 | + ]; |
| 76 | + const max = Math.max(...yVals, 0); |
| 77 | + const tickCount = 6; |
| 78 | + const step = max > 0 ? max / tickCount : 1; |
| 79 | + const yMax = Math.max(max, step); |
| 80 | + |
| 81 | + return { |
| 82 | + backgroundColor: 'transparent', |
| 83 | + grid: { left: 70, right: 40, top: 40, bottom: 50 }, |
| 84 | + tooltip: { |
| 85 | + trigger: 'axis', |
| 86 | + formatter: (params: any[]) => { |
| 87 | + const p = params[0]; |
| 88 | + const d = data[p?.dataIndex ?? 0]; |
| 89 | + const price = Number(toUnit(network, BigInt(d.price))).toLocaleString(undefined, { |
| 90 | + maximumFractionDigits: 6, |
| 91 | + }); |
| 92 | + const startP = Number(toUnit(network, BigInt(d.startPrice))).toLocaleString(undefined, { |
| 93 | + maximumFractionDigits: 6, |
| 94 | + }); |
| 95 | + return [ |
| 96 | + `Sale cycle: ${d.cycle}`, |
| 97 | + `Sellout price: <b>${price} ${token}</b>`, |
| 98 | + `Start price: <b>${startP} ${token}</b>`, |
| 99 | + ].join('<br/>'); |
| 100 | + }, |
| 101 | + }, |
| 102 | + |
| 103 | + legend: { |
| 104 | + top: 0, |
| 105 | + right: 10, |
| 106 | + textStyle: { color: '#aaa', fontSize: 12 }, |
| 107 | + icon: 'roundRect', |
| 108 | + }, |
| 109 | + xAxis: { |
| 110 | + type: 'category', |
| 111 | + boundaryGap: false, |
| 112 | + data: data.map((d) => `Cycle ${d.cycle}`), |
| 113 | + axisLine: { lineStyle: { color: '#666' } }, |
| 114 | + axisLabel: { |
| 115 | + color: '#aaa', |
| 116 | + fontSize: 10, |
| 117 | + hideOverlap: true, |
| 118 | + }, |
| 119 | + axisTick: { alignWithLabel: true }, |
| 120 | + }, |
| 121 | + |
| 122 | + yAxis: { |
| 123 | + type: 'value', |
| 124 | + min: 0, |
| 125 | + max: yMax, |
| 126 | + interval: step, |
| 127 | + axisLine: { lineStyle: { color: '#666' } }, |
| 128 | + axisLabel: { |
| 129 | + color: '#aaa', |
| 130 | + fontSize: 10, |
| 131 | + formatter: (val: number) => `${val.toFixed(2)} ${token}`, |
| 132 | + }, |
| 133 | + splitLine: { lineStyle: { type: 'dashed', color: 'rgba(136,136,136,0.25)' } }, |
| 134 | + }, |
| 135 | + series: [ |
| 136 | + { |
| 137 | + name: 'Sellout price', |
| 138 | + type: 'line', |
| 139 | + smooth: false, |
| 140 | + showSymbol: true, |
| 141 | + symbolSize: 6, |
| 142 | + lineStyle: { width: 2, color: '#eab308' }, |
| 143 | + itemStyle: { color: '#eab308' }, |
| 144 | + data: data.map((d) => Number(fmt(d.price))), |
| 145 | + }, |
| 146 | + { |
| 147 | + name: 'Start price', |
| 148 | + type: 'line', |
| 149 | + smooth: false, |
| 150 | + showSymbol: false, |
| 151 | + lineStyle: { width: 2, type: 'dashed', color: '#0cc184' }, |
| 152 | + itemStyle: { color: '#0cc184' }, |
| 153 | + data: data.map((d) => Number(fmt(d.startPrice))), |
| 154 | + }, |
| 155 | + ], |
| 156 | + }; |
| 157 | + }, [data, network, token]); |
| 158 | + |
7 | 159 | return ( |
8 | 160 | <div className={styles.card}> |
9 | 161 | <div className={styles.header}> |
10 | 162 | <span className={styles.title}>Last 3 Sale Cycle Prices</span> |
11 | 163 | </div> |
12 | | - <div className={styles.placeholder}> |
13 | | - Historical pricing data is currently unavailable. This feature requires an indexer service. |
14 | | - </div> |
| 164 | + |
| 165 | + {loading ? ( |
| 166 | + <div className={styles.placeholder}>Loading data...</div> |
| 167 | + ) : !data.length || !option ? ( |
| 168 | + <div className={styles.placeholder}>No data found.</div> |
| 169 | + ) : ( |
| 170 | + <ReactECharts option={option} style={{ height: 330, width: '100%' }} /> |
| 171 | + )} |
15 | 172 | </div> |
16 | 173 | ); |
17 | 174 | } |
0 commit comments