-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuseUserStrategyData.ts
75 lines (70 loc) · 2.36 KB
/
useUserStrategyData.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
import { useQuery } from "@tanstack/react-query"
import { getUserData } from "data/actions/common/getUserData"
import { cellarDataMap } from "data/cellarDataMap"
import { useAccount, useWalletClient } from "wagmi"
import { useAllContracts } from "./useAllContracts"
import { useCoinGeckoPrice } from "./useCoinGeckoPrice"
import { useStrategyData } from "./useStrategyData"
import { useUserBalance } from "./useUserBalance"
import { tokenConfig } from "data/tokenConfig"
export const useUserStrategyData = (strategyAddress: string, chain: string) => {
const { data: walletClient } = useWalletClient()
const { address: userAddress } = useAccount()
const { data: allContracts } = useAllContracts()
const strategyData = useStrategyData(strategyAddress, chain)
const sommToken = tokenConfig.find(
(token) =>
token.coinGeckoId === "sommelier" && token.chain === chain
)!
const sommPrice = useCoinGeckoPrice(sommToken)
const config = Object.values(cellarDataMap).find(
(item) =>
item.config.cellar.address.toLowerCase() ===
strategyAddress.toLowerCase() && item.config.chain.id === chain
)!.config
const isNoDataSource = Boolean(
Object.values(cellarDataMap).find(
(item) => item.config.cellar.address === strategyAddress && item.config.chain.id === chain
)?.config.isNoDataSource
)
const { lpToken } = useUserBalance(config)
const baseAsset = config.baseAsset
const { data: baseAssetPrice } = useCoinGeckoPrice(
baseAsset
)
// if chain is not ethereum, key format is '{address}-{chain}', otherwise it is '{address}'
const key =
strategyAddress +
(config.chain.id !== "ethereum" ? "-" + chain : "")
const query = useQuery({
queryKey: [
"USE_USER_DATA",
{
signer: true,
contractAddress: strategyAddress,
userAddress,
},
],
queryFn: async () => {
return await getUserData({
contracts: allContracts![key],
address: strategyAddress,
strategyData: strategyData.data!,
userAddress: userAddress!,
sommPrice: sommPrice.data ?? "0",
baseAssetPrice: baseAssetPrice ?? "0",
chain: chain,
})
},
enabled:
!!allContracts &&
!!walletClient &&
!!sommPrice.data &&
!!lpToken &&
!!baseAssetPrice &&
!!strategyData.data &&
isNoDataSource === false,
}
)
return query
}