-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusePrice.ts
47 lines (43 loc) · 1.31 KB
/
usePrice.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
import { useQuery } from 'wagmi'
import { useFns } from '@app/utils/FnsProvider'
import { useQueryKeys } from '@app/utils/cacheKeyFactory'
import { yearsToSeconds } from '@app/utils/utils'
export const usePrice = (nameOrNames: string | string[], legacy?: boolean) => {
const { ready, getPrice } = useFns()
const names = Array.isArray(nameOrNames) ? nameOrNames : [nameOrNames]
const type = legacy ? 'legacy' : 'new'
const {
data,
status,
isFetched,
isLoading: loading,
error,
isFetchedAfterMount,
// don't remove this line, it updates the isCachedData state (for some reason) but isn't needed to verify it
// eslint-disable-next-line @typescript-eslint/no-unused-vars
isFetching,
} = useQuery(
useQueryKeys().getPrice(type, names),
async () =>
getPrice(
names.map((n) => n.split('.')[0]),
yearsToSeconds(1),
).then((d) => d || null),
{
enabled: !!(ready && nameOrNames && nameOrNames.length > 0),
},
)
const base = data?.base
const premium = data?.premium
const total = data?.base ? data.base.add(data.premium) : undefined
const hasPremium = data?.premium.gt(0)
return {
base,
premium,
total,
hasPremium,
isCachedData: status === 'success' && isFetched && !isFetchedAfterMount,
loading,
error,
}
}