@@ -17,7 +17,7 @@ import { luxNetClient } from 'graphql/thegraph/apollo'
17
17
import { zooNetClient } from 'graphql/thegraph/apollo'
18
18
19
19
const GetTokenInfo = gql `
20
- query GetTokenInfo($tokenAddress: String!, $days: Int, $hours: Int) {
20
+ query GetTokenInfo($tokenAddress: String!, $days: Int, $hours: Int, $mins: Int ) {
21
21
bundles(first: 10) {
22
22
ethPriceUSD
23
23
}
@@ -47,6 +47,14 @@ query GetTokenInfo($tokenAddress: String!, $days: Int, $hours: Int) {
47
47
volumeUSD
48
48
}
49
49
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
+
50
58
# Get all pools involving this token for liquidity details
51
59
whitelistPools {
52
60
id
@@ -70,15 +78,15 @@ export const pageTimePeriodAtom = atomWithStorage<TimePeriod>('tokenDetailsTimeP
70
78
function getQueryParams ( timePeriod : any ) {
71
79
switch ( timePeriod ) {
72
80
case 0 :
73
- return [ 2 , 2 ] ;
81
+ return [ 2 , 2 , 12 ] ;
74
82
case 1 :
75
- return [ 2 , 24 ] ;
83
+ return [ 2 , 30 , 2 ] ;
76
84
case 2 :
77
- return [ 2 , 168 ] ;
85
+ return [ 2 , 168 , 2 ] ;
78
86
case 3 :
79
- return [ 2 , 720 ] ;
87
+ return [ 2 , 720 , 2 ] ;
80
88
case 4 :
81
- return [ 365 , 2 ] ;
89
+ return [ 365 , 2 , 2 ] ;
82
90
}
83
91
return [ 2 , 2 ] ;
84
92
}
@@ -89,36 +97,55 @@ export default function TokenDetailsPage() {
89
97
const chain = validateUrlChainParam ( chainName )
90
98
const isNative = tokenAddress === NATIVE_CHAIN_ID
91
99
const [ timePeriod , setTimePeriod ] = useAtom ( pageTimePeriodAtom )
92
- const [ totalDays , totalHours ] = getQueryParams ( timePeriod ) ;
100
+ const [ totalDays , totalHours , total5Mins ] = getQueryParams ( timePeriod ) ;
93
101
const [ address , duration ] = useMemo (
94
102
/* tokenAddress will always be defined in the path for for this page to render, but useParams will always
95
103
return optional arguments; nullish coalescing operator is present here to appease typechecker */
96
104
( ) => [ isNative ? getNativeTokenDBAddress ( chain ) : tokenAddress ?? '' , toHistoryDuration ( timePeriod ) ] ,
97
105
[ chain , isNative , timePeriod , tokenAddress ]
98
106
)
99
- const { data : tokenQuery } = useTokenQuery ( {
107
+ const { data : tokenQuery , refetch : refetchTokenQuery } = useTokenQuery ( {
100
108
variables : {
101
109
address,
102
110
chain,
103
111
} ,
112
+ pollInterval : 12000 , // 12 seconds
104
113
} )
105
114
106
- const { data : tokenPriceQuery } = useTokenPriceQuery ( {
115
+ const { data : tokenPriceQuery , refetch : refetchTokenPriceQuery } = useTokenPriceQuery ( {
107
116
variables : {
108
117
address,
109
118
chain,
110
119
duration,
111
120
} ,
121
+ pollInterval : 12000 , // 12 seconds
112
122
} )
113
123
114
- const { data : luxData , loading : luxLoading } = useQuery ( GetTokenInfo , {
124
+ const { data : luxData , loading : luxLoading , refetch : refetchLuxData } = useQuery ( GetTokenInfo , {
115
125
client : chainName == 'lux' ? luxNetClient : zooNetClient ,
116
126
variables : {
117
127
tokenAddress : address ,
118
128
days : totalDays ,
119
129
hours : totalHours ,
130
+ mins : total5Mins ,
120
131
} ,
132
+ pollInterval : 12000 , // 12 seconds
121
133
} )
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
+
122
149
if ( luxLoading ) {
123
150
console . log ( "Loading data..." ) ;
124
151
} else {
@@ -135,6 +162,20 @@ export default function TokenDetailsPage() {
135
162
const priceLow52W = dailyPrices && Math . min ( ...dailyPrices ) ;
136
163
const token = luxData ?. token ;
137
164
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
+
138
179
// Now, you can assign the JSON data to a variable of type TokenQuery
139
180
const transformedTokenDetail : TokenQuery = tokenPriceQuery ?? {
140
181
token : {
@@ -192,6 +233,11 @@ export default function TokenDetailsPage() {
192
233
timestamp : data . periodStartUnix ,
193
234
value : parseFloat ( data . priceUSD ) ,
194
235
} ) )
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
+ } ) )
195
241
196
242
const transformedTokenPriceHistory : TokenPriceQuery = {
197
243
token : {
@@ -204,7 +250,7 @@ export default function TokenDetailsPage() {
204
250
id : "QW1vdW50OjFfVVNE" , // Encoded amount ID
205
251
value : parseFloat ( ethPriceUSD ) * parseFloat ( ethPrice ) ,
206
252
} ,
207
- priceHistory : timePeriod < 4 ? tokenHourData : tokenDayData ,
253
+ priceHistory : timePeriod < 1 ? token5MinData : timePeriod < 4 ? tokenHourData : tokenDayData ,
208
254
} ,
209
255
} ,
210
256
} ;
0 commit comments