-
Notifications
You must be signed in to change notification settings - Fork 1
/
hooks.ts
328 lines (282 loc) · 10.3 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { useCallback, useEffect, useState, useMemo } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { getUnixTime, startOfHour, Duration, sub } from 'date-fns'
import { AppState, AppDispatch } from 'state'
import { isAddress } from 'utils'
import { Transaction } from 'state/info/types'
import fetchPoolChartData from 'state/info/queries/pools/chartData'
import fetchPoolTransactions from 'state/info/queries/pools/transactions'
import fetchTokenChartData from 'state/info/queries/tokens/chartData'
import fetchTokenTransactions from 'state/info/queries/tokens/transactions'
import fetchTokenPriceData from 'state/info/queries/tokens/priceData'
import fetchPoolsForToken from 'state/info/queries/tokens/poolsForToken'
import {
updateProtocolData,
updateProtocolChartData,
updateProtocolTransactions,
updatePoolData,
addPoolKeys,
updatePoolChartData,
updatePoolTransactions,
updateTokenData,
addTokenKeys,
addTokenPoolAddresses,
updateTokenChartData,
updateTokenPriceData,
updateTokenTransactions,
} from './actions'
import { ProtocolData, PoolData, TokenData, ChartEntry, PriceChartEntry } from './types'
// Protocol hooks
export const useProtocolData = (): [ProtocolData | undefined, (protocolData: ProtocolData) => void] => {
const protocolData: ProtocolData | undefined = useSelector((state: AppState) => state.info.protocol.overview)
const dispatch = useDispatch<AppDispatch>()
const setProtocolData: (protocolData: ProtocolData) => void = useCallback(
(data: ProtocolData) => dispatch(updateProtocolData({ protocolData: data })),
[dispatch],
)
return [protocolData, setProtocolData]
}
export const useProtocolChartData = (): [ChartEntry[] | undefined, (chartData: ChartEntry[]) => void] => {
const chartData: ChartEntry[] | undefined = useSelector((state: AppState) => state.info.protocol.chartData)
const dispatch = useDispatch<AppDispatch>()
const setChartData: (chartData: ChartEntry[]) => void = useCallback(
(data: ChartEntry[]) => dispatch(updateProtocolChartData({ chartData: data })),
[dispatch],
)
return [chartData, setChartData]
}
export const useProtocolTransactions = (): [Transaction[] | undefined, (transactions: Transaction[]) => void] => {
const transactions: Transaction[] | undefined = useSelector((state: AppState) => state.info.protocol.transactions)
const dispatch = useDispatch<AppDispatch>()
const setTransactions: (transactions: Transaction[]) => void = useCallback(
(transactionsData: Transaction[]) => dispatch(updateProtocolTransactions({ transactions: transactionsData })),
[dispatch],
)
return [transactions, setTransactions]
}
// Pools hooks
export const useAllPoolData = (): {
[address: string]: { data?: PoolData }
} => {
return useSelector((state: AppState) => state.info.pools.byAddress)
}
export const useUpdatePoolData = (): ((pools: PoolData[]) => void) => {
const dispatch = useDispatch<AppDispatch>()
return useCallback((pools: PoolData[]) => dispatch(updatePoolData({ pools })), [dispatch])
}
export const useAddPoolKeys = (): ((addresses: string[]) => void) => {
const dispatch = useDispatch<AppDispatch>()
return useCallback((poolAddresses: string[]) => dispatch(addPoolKeys({ poolAddresses })), [dispatch])
}
export const usePoolDatas = (poolAddresses: string[]): PoolData[] => {
const allPoolData = useAllPoolData()
const addNewPoolKeys = useAddPoolKeys()
const untrackedAddresses = poolAddresses.reduce((accum: string[], address) => {
if (!Object.keys(allPoolData).includes(address)) {
accum.push(address)
}
return accum
}, [])
useEffect(() => {
if (untrackedAddresses) {
addNewPoolKeys(untrackedAddresses)
}
}, [addNewPoolKeys, untrackedAddresses])
const poolsWithData = poolAddresses
.map((address) => {
return allPoolData[address]?.data
})
.filter((pool) => pool)
return poolsWithData
}
export const usePoolChartData = (address: string): ChartEntry[] | undefined => {
const dispatch = useDispatch<AppDispatch>()
const pool = useSelector((state: AppState) => state.info.pools.byAddress[address])
const chartData = pool?.chartData
const [error, setError] = useState(false)
useEffect(() => {
const fetch = async () => {
const { error: fetchError, data } = await fetchPoolChartData(address)
if (!fetchError && data) {
dispatch(updatePoolChartData({ poolAddress: address, chartData: data }))
}
if (fetchError) {
setError(fetchError)
}
}
if (!chartData && !error) {
fetch()
}
}, [address, dispatch, error, chartData])
return chartData
}
export const usePoolTransactions = (address: string): Transaction[] | undefined => {
const dispatch = useDispatch<AppDispatch>()
const pool = useSelector((state: AppState) => state.info.pools.byAddress[address])
const transactions = pool?.transactions
const [error, setError] = useState(false)
useEffect(() => {
const fetch = async () => {
const { error: fetchError, data } = await fetchPoolTransactions(address)
if (fetchError) {
setError(true)
} else {
dispatch(updatePoolTransactions({ poolAddress: address, transactions: data }))
}
}
if (!transactions && !error) {
fetch()
}
}, [address, dispatch, error, transactions])
return transactions
}
// Tokens hooks
export const useAllTokenData = (): {
[address: string]: { data?: TokenData }
} => {
return useSelector((state: AppState) => state.info.tokens.byAddress)
}
export const useUpdateTokenData = (): ((tokens: TokenData[]) => void) => {
const dispatch = useDispatch<AppDispatch>()
return useCallback(
(tokens: TokenData[]) => {
dispatch(updateTokenData({ tokens }))
},
[dispatch],
)
}
export const useAddTokenKeys = (): ((addresses: string[]) => void) => {
const dispatch = useDispatch<AppDispatch>()
return useCallback((tokenAddresses: string[]) => dispatch(addTokenKeys({ tokenAddresses })), [dispatch])
}
export const useTokenDatas = (addresses?: string[]): TokenData[] | undefined => {
const allTokenData = useAllTokenData()
const addNewTokenKeys = useAddTokenKeys()
// if token not tracked yet track it
addresses?.forEach((a) => {
if (!allTokenData[a]) {
addNewTokenKeys([a])
}
})
const tokensWithData = useMemo(() => {
if (!addresses) {
return undefined
}
return addresses
.map((a) => {
return allTokenData[a]?.data
})
.filter((token) => token)
}, [addresses, allTokenData])
return tokensWithData
}
export const useTokenData = (address: string | undefined): TokenData | undefined => {
const allTokenData = useAllTokenData()
const addNewTokenKeys = useAddTokenKeys()
if (!address || !isAddress(address)) {
return undefined
}
// if token not tracked yet track it
if (!allTokenData[address]) {
addNewTokenKeys([address])
}
return allTokenData[address]?.data
}
export const usePoolsForToken = (address: string): string[] | undefined => {
const dispatch = useDispatch<AppDispatch>()
const token = useSelector((state: AppState) => state.info.tokens.byAddress[address])
const poolsForToken = token.poolAddresses
const [error, setError] = useState(false)
useEffect(() => {
const fetch = async () => {
const { error: fetchError, addresses } = await fetchPoolsForToken(address)
if (!fetchError && addresses) {
dispatch(addTokenPoolAddresses({ tokenAddress: address, poolAddresses: addresses }))
}
if (fetchError) {
setError(fetchError)
}
}
if (!poolsForToken && !error) {
fetch()
}
}, [address, dispatch, error, poolsForToken])
return poolsForToken
}
export const useTokenChartData = (address: string): ChartEntry[] | undefined => {
const dispatch = useDispatch<AppDispatch>()
const token = useSelector((state: AppState) => state.info.tokens.byAddress[address])
const { chartData } = token
const [error, setError] = useState(false)
useEffect(() => {
const fetch = async () => {
const { error: fetchError, data } = await fetchTokenChartData(address)
if (!fetchError && data) {
dispatch(updateTokenChartData({ tokenAddress: address, chartData: data }))
}
if (fetchError) {
setError(fetchError)
}
}
if (!chartData && !error) {
fetch()
}
}, [address, dispatch, error, chartData])
return chartData
}
export const useTokenPriceData = (
address: string,
interval: number,
timeWindow: Duration,
): PriceChartEntry[] | undefined => {
const dispatch = useDispatch<AppDispatch>()
const token = useSelector((state: AppState) => state.info.tokens.byAddress[address])
const priceData = token.priceData[interval]
const [error, setError] = useState(false)
// construct timestamps and check if we need to fetch more data
const oldestTimestampFetched = token.priceData.oldestFetchedTimestamp
const utcCurrentTime = getUnixTime(new Date()) * 1000
const startTimestamp = getUnixTime(startOfHour(sub(utcCurrentTime, timeWindow)))
useEffect(() => {
const fetch = async () => {
const { data, error: fetchingError } = await fetchTokenPriceData(address, interval, startTimestamp)
if (data) {
dispatch(
updateTokenPriceData({
tokenAddress: address,
secondsInterval: interval,
priceData: data,
oldestFetchedTimestamp: startTimestamp,
}),
)
}
if (fetchingError) {
setError(true)
}
}
if (!priceData && !error) {
fetch()
}
}, [address, dispatch, error, interval, oldestTimestampFetched, priceData, startTimestamp, timeWindow])
return priceData
}
export const useTokenTransactions = (address: string): Transaction[] | undefined => {
const dispatch = useDispatch<AppDispatch>()
const token = useSelector((state: AppState) => state.info.tokens.byAddress[address])
const { transactions } = token
const [error, setError] = useState(false)
useEffect(() => {
const fetch = async () => {
const { error: fetchError, data } = await fetchTokenTransactions(address)
if (fetchError) {
setError(true)
} else if (data) {
dispatch(updateTokenTransactions({ tokenAddress: address, transactions: data }))
}
}
if (!transactions && !error) {
fetch()
}
}, [address, dispatch, error, transactions])
return transactions
}