-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuseAllStrategiesData.ts
69 lines (61 loc) · 2 KB
/
useAllStrategiesData.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
import { useQuery } from "@tanstack/react-query"
import { getAllStrategiesData } from "data/actions/common/getAllStrategiesData"
import { useAccount, usePublicClient } from "wagmi"
import { useAllContracts } from "./useAllContracts"
import { useCoinGeckoPrice } from "./useCoinGeckoPrice"
import { fetchCellarStrategyData } from "queries/get-all-strategies-data"
import { useState, useEffect } from "react"
import { GetAllStrategiesDataQuery } from "data/actions/types"
import { tokenConfig } from "data/tokenConfig"
import { getChainByViemId, supportedChains } from "src/data/chainConfig"
export const useAllStrategiesData = () => {
const client = usePublicClient()
const { chain: viemChain } = useAccount()
const { data: allContracts } = useAllContracts()
const chain = getChainByViemId(viemChain?.name)
const sommToken = tokenConfig.find((token) => {
const compareChain = supportedChains.includes(chain ? chain.id : "")
? "ethereum"
: chain?.id
return (
token.coinGeckoId === "sommelier" &&
token.chain === (compareChain || "ethereum")
)
})!
const { data: sommPrice } = useCoinGeckoPrice(sommToken)
const [cellarData, setcellarData] = useState<
GetAllStrategiesDataQuery | undefined
>(undefined)
const [error, setError] = useState(null)
useEffect(() => {
fetchCellarStrategyData()
.then(({ data, error }) => {
if (error) {
setError(error)
} else {
setcellarData(data)
}
})
.catch((error) => setError(error))
return () => {
setError(null);
setcellarData(undefined);
};
}, [])
const query = useQuery({
queryKey: ["USE_ALL_STRATEGIES_DATA", client?.uid ],
queryFn: async () => {
return await getAllStrategiesData({
allContracts: allContracts!,
sommPrice: sommPrice!,
cellarData: cellarData,
})
},
enabled: !!allContracts && !!sommPrice && !!cellarData,
}
)
return {
...query,
isError: Boolean(error) || query.isError,
}
}