Skip to content

Commit 112e4b9

Browse files
committed
update tokenDetail page and update ZLUX token icon
1 parent 2640e39 commit 112e4b9

File tree

3 files changed

+91
-34
lines changed

3 files changed

+91
-34
lines changed

src/components/Tokens/TokenDetails/ChartSection.tsx

+33-22
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { isPricePoint, PricePoint } from 'graphql/data/util'
55
import { TimePeriod } from 'graphql/data/util'
66
import { useAtomValue } from 'jotai/utils'
77
import { pageTimePeriodAtom } from 'pages/TokenDetails'
8-
import { startTransition, Suspense, useMemo } from 'react'
8+
import { startTransition, Suspense, useMemo, useEffect, useState } from 'react'
99

1010
import { PriceChart } from './PriceChart'
1111
import TimePeriodSelector from './TimeSelector'
1212
import { ti } from 'make-plural'
13+
import { left } from '@popperjs/core'
1314

1415
function fillMissingTimestamps(priceHistory: any, interval: number, totalTime: number) {
1516
if (!Array.isArray(priceHistory) || priceHistory.length === 0) {
@@ -20,19 +21,19 @@ function fillMissingTimestamps(priceHistory: any, interval: number, totalTime: n
2021
priceHistory.sort((a, b) => a.timestamp - b.timestamp);
2122
const currentTimestamp = Date.now() / 1000
2223
const filledHistory = [];
23-
24+
2425
for (let i = 0; i < priceHistory.length - 1; i++) {
2526
const current = priceHistory[i];
2627
const next = priceHistory[i + 1];
2728

2829
// Add the current object to the filled array
29-
if(next.timestamp + totalTime >= currentTimestamp) {
30-
if(current.timestamp + totalTime >= currentTimestamp) filledHistory.push(current);
30+
if (next.timestamp + totalTime >= currentTimestamp) {
31+
if (current.timestamp + totalTime >= currentTimestamp) filledHistory.push(current);
3132

3233
// Fill missing timestamps
3334
let timestamp = current.timestamp + interval;
3435
while (timestamp < next.timestamp) {
35-
if(timestamp + totalTime >= currentTimestamp)
36+
if (timestamp + totalTime >= currentTimestamp)
3637
filledHistory.push({
3738
...current, // Copy previous values
3839
timestamp, // Update timestamp
@@ -47,8 +48,8 @@ function fillMissingTimestamps(priceHistory: any, interval: number, totalTime: n
4748
return filledHistory;
4849
}
4950

50-
function getTimeInterval(timePeriod:any) {
51-
switch(timePeriod) {
51+
function getTimeInterval(timePeriod: any) {
52+
switch (timePeriod) {
5253
case 0:
5354
return [300, 3600];
5455
case 1:
@@ -64,23 +65,34 @@ function getTimeInterval(timePeriod:any) {
6465
}
6566

6667
function usePriceHistory(tokenPriceData: TokenPriceQuery, timePeriod: any): PricePoint[] | undefined {
67-
68+
const [priceHistory, setPriceHistory] = useState<PricePoint[] | undefined>(undefined);
6869
const [timeInterval, totalTime] = getTimeInterval(timePeriod);
69-
// Appends the current price to the end of the priceHistory array
70-
const priceHistory = useMemo(() => {
71-
const market = tokenPriceData.token?.market
72-
const priceHistory = market?.priceHistory?.filter(isPricePoint)
73-
// const priceHistory = market?.priceHistory?.filter(isPricePoint)
74-
const currentPrice = market?.price?.value
75-
if (Array.isArray(priceHistory) && currentPrice !== undefined) {
76-
const timestamp = Date.now() / 1000
77-
return fillMissingTimestamps([...priceHistory, { timestamp, value: currentPrice }], timeInterval, totalTime)
78-
}
79-
return priceHistory
80-
}, [tokenPriceData])
8170

82-
return priceHistory
71+
useEffect(() => {
72+
const updatePriceHistory = () => {
73+
const market = tokenPriceData.token?.market;
74+
const history = market?.priceHistory?.filter(isPricePoint);
75+
const currentPrice = market?.price?.value;
76+
77+
if (Array.isArray(history) && currentPrice !== undefined) {
78+
const timestamp = Date.now() / 1000;
79+
const filledHistory = fillMissingTimestamps([...history, { timestamp, value: currentPrice }], timeInterval, totalTime);
80+
setPriceHistory(filledHistory);
81+
} else {
82+
setPriceHistory(history);
83+
}
84+
};
85+
86+
// Update immediately and then at intervals
87+
updatePriceHistory();
88+
const intervalId = setInterval(updatePriceHistory, 12000); // Update every 12 seconds
89+
90+
return () => clearInterval(intervalId); // Cleanup on component unmount
91+
}, [tokenPriceData, timeInterval, totalTime]);
92+
93+
return priceHistory;
8394
}
95+
8496
export default function ChartSection({
8597
tokenPriceQuery,
8698
onChangeTimePeriod,
@@ -112,7 +124,6 @@ function Chart({
112124
// Initializes time period to global & maintain separate time period for subsequent changes
113125
const timePeriod = useAtomValue(pageTimePeriodAtom)
114126
const prices = usePriceHistory(tokenPriceQuery, timePeriod)
115-
console.log(tokenPriceQuery, prices);
116127

117128
return (
118129
<ChartContainer data-testid="chart-container">

src/pages/TokenDetails/index.tsx

+57-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { luxNetClient } from 'graphql/thegraph/apollo'
1717
import { zooNetClient } from 'graphql/thegraph/apollo'
1818

1919
const GetTokenInfo = gql`
20-
query GetTokenInfo($tokenAddress: String!, $days: Int, $hours: Int) {
20+
query GetTokenInfo($tokenAddress: String!, $days: Int, $hours: Int, $mins: Int) {
2121
bundles(first: 10) {
2222
ethPriceUSD
2323
}
@@ -47,6 +47,14 @@ query GetTokenInfo($tokenAddress: String!, $days: Int, $hours: Int) {
4747
volumeUSD
4848
}
4949
50+
token5MinData(first: $mins, orderBy: periodStartUnix, orderDirection: desc) {
51+
id
52+
periodStartUnix
53+
priceUSD # The price of the token in USD for the day
54+
totalValueLockedUSD
55+
volumeUSD
56+
}
57+
5058
# Get all pools involving this token for liquidity details
5159
whitelistPools {
5260
id
@@ -70,15 +78,15 @@ export const pageTimePeriodAtom = atomWithStorage<TimePeriod>('tokenDetailsTimeP
7078
function getQueryParams(timePeriod: any) {
7179
switch (timePeriod) {
7280
case 0:
73-
return [2, 2];
81+
return [2, 2, 12];
7482
case 1:
75-
return [2, 24];
83+
return [2, 30, 2];
7684
case 2:
77-
return [2, 168];
85+
return [2, 168, 2];
7886
case 3:
79-
return [2, 720];
87+
return [2, 720, 2];
8088
case 4:
81-
return [365, 2];
89+
return [365, 2, 2];
8290
}
8391
return [2, 2];
8492
}
@@ -89,36 +97,55 @@ export default function TokenDetailsPage() {
8997
const chain = validateUrlChainParam(chainName)
9098
const isNative = tokenAddress === NATIVE_CHAIN_ID
9199
const [timePeriod, setTimePeriod] = useAtom(pageTimePeriodAtom)
92-
const [totalDays, totalHours] = getQueryParams(timePeriod);
100+
const [totalDays, totalHours, total5Mins] = getQueryParams(timePeriod);
93101
const [address, duration] = useMemo(
94102
/* tokenAddress will always be defined in the path for for this page to render, but useParams will always
95103
return optional arguments; nullish coalescing operator is present here to appease typechecker */
96104
() => [isNative ? getNativeTokenDBAddress(chain) : tokenAddress ?? '', toHistoryDuration(timePeriod)],
97105
[chain, isNative, timePeriod, tokenAddress]
98106
)
99-
const { data: tokenQuery } = useTokenQuery({
107+
const { data: tokenQuery, refetch: refetchTokenQuery } = useTokenQuery({
100108
variables: {
101109
address,
102110
chain,
103111
},
112+
pollInterval: 12000, // 12 seconds
104113
})
105114

106-
const { data: tokenPriceQuery } = useTokenPriceQuery({
115+
const { data: tokenPriceQuery, refetch: refetchTokenPriceQuery } = useTokenPriceQuery({
107116
variables: {
108117
address,
109118
chain,
110119
duration,
111120
},
121+
pollInterval: 12000, // 12 seconds
112122
})
113123

114-
const { data: luxData, loading: luxLoading } = useQuery(GetTokenInfo, {
124+
const { data: luxData, loading: luxLoading, refetch: refetchLuxData } = useQuery(GetTokenInfo, {
115125
client: chainName == 'lux' ? luxNetClient : zooNetClient,
116126
variables: {
117127
tokenAddress: address,
118128
days: totalDays,
119129
hours: totalHours,
130+
mins: total5Mins,
120131
},
132+
pollInterval: 12000, // 12 seconds
121133
})
134+
135+
// Use effect to handle periodic refetching
136+
useEffect(() => {
137+
const intervalId = setInterval(() => {
138+
refetchTokenQuery()
139+
refetchTokenPriceQuery()
140+
if (chainName === 'lux' || chainName === 'zoo') {
141+
refetchLuxData()
142+
}
143+
}, 12000) // 12 seconds interval
144+
145+
// Clear interval on component unmount
146+
return () => clearInterval(intervalId)
147+
}, [refetchTokenQuery, refetchTokenPriceQuery, refetchLuxData, chainName])
148+
122149
if (luxLoading) {
123150
console.log("Loading data...");
124151
} else {
@@ -135,6 +162,20 @@ export default function TokenDetailsPage() {
135162
const priceLow52W = dailyPrices && Math.min(...dailyPrices);
136163
const token = luxData?.token;
137164

165+
// Use effect to handle periodic refetching
166+
useEffect(() => {
167+
const intervalId = setInterval(() => {
168+
refetchTokenQuery()
169+
refetchTokenPriceQuery()
170+
if (chainName === 'lux' || chainName === 'zoo') {
171+
refetchLuxData()
172+
}
173+
}, 12000) // 12 seconds interval
174+
175+
// Clear interval on component unmount
176+
return () => clearInterval(intervalId)
177+
}, [])
178+
138179
// Now, you can assign the JSON data to a variable of type TokenQuery
139180
const transformedTokenDetail: TokenQuery = tokenPriceQuery ?? {
140181
token: {
@@ -192,6 +233,11 @@ export default function TokenDetailsPage() {
192233
timestamp: data.periodStartUnix,
193234
value: parseFloat(data.priceUSD),
194235
}))
236+
const token5MinData = luxData?.token?.token5MinData.filter((data: any) => parseFloat(data.priceUSD) !== 0).map((data: any) => ({
237+
id: `VGltZXN0YW1wZWRBbW91bnQ6MV8x${data.periodStartUnix}_VVNE`, // Encoded version of the timestamped amount
238+
timestamp: data.periodStartUnix,
239+
value: parseFloat(data.priceUSD),
240+
}))
195241

196242
const transformedTokenPriceHistory: TokenPriceQuery = {
197243
token: {
@@ -204,7 +250,7 @@ export default function TokenDetailsPage() {
204250
id: "QW1vdW50OjFfVVNE", // Encoded amount ID
205251
value: parseFloat(ethPriceUSD) * parseFloat(ethPrice),
206252
},
207-
priceHistory: timePeriod < 4 ? tokenHourData : tokenDayData,
253+
priceHistory: timePeriod < 1 ? token5MinData : timePeriod < 4 ? tokenHourData : tokenDayData,
208254
},
209255
},
210256
};

src/tokens-lux/tokens.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export const TOKENS_LUX_LIST = {
163163
"name": "Zoo LUX",
164164
"symbol": "ZLUX",
165165
"decimals": 18,
166-
"logoURI": "https://cdn.lux.network/exchange/icon-png/lux.png",
166+
"logoURI": "https://cdn.lux.network/bridge/currencies/zoo/zlux.svg",
167167
"extensions": {}
168168
},
169169
{

0 commit comments

Comments
 (0)