Skip to content

Commit 23e4e4c

Browse files
authored
Improve caching of rewards resolver functions and token fiat price (#1700)
1 parent 3e5b5ac commit 23e4e4c

File tree

6 files changed

+97
-68
lines changed

6 files changed

+97
-68
lines changed

apps/hyperdrive-trading/src/hyperdrive/getLpApy.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import {
77
HyperdriveConfig,
88
} from "@delvtech/hyperdrive-appconfig";
99
import { ReadHyperdrive } from "@delvtech/hyperdrive-js";
10-
import { getPublicClient } from "@wagmi/core";
1110
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays";
1211
import { isForkChain } from "src/chains/isForkChain";
13-
import { wagmiConfig } from "src/network/wagmiClient";
14-
import { PublicClient } from "viem";
12+
import { queryClient } from "src/network/queryClient";
13+
import { makeRewardsQuery } from "src/ui/rewards/useRewards";
1514

1615
export type LpApyResult = {
1716
ratePeriodDays: number;
@@ -87,17 +86,18 @@ export async function getLpApy({
8786
netLpApy = lpApy;
8887

8988
// Add rewards APY if available
90-
const publicClient = getPublicClient(wagmiConfig as any, {
91-
chainId: hyperdrive.chainId,
92-
}) as PublicClient;
93-
9489
const rewardsFn = getRewardsFn({
9590
yieldSourceId: hyperdrive.yieldSource,
9691
appConfig,
9792
});
9893

9994
if (rewardsFn) {
100-
const rewards = await rewardsFn(publicClient);
95+
const rewards = await queryClient.fetchQuery(
96+
makeRewardsQuery({
97+
chainId: hyperdrive.chainId,
98+
hyperdriveAddress: hyperdrive.address,
99+
}),
100+
);
101101
rewards?.forEach((reward) => {
102102
if (reward.type === "apy") {
103103
netLpApy = fixed(reward.apy).add(netLpApy as bigint).bigint;

apps/hyperdrive-trading/src/hyperdrive/getYieldSourceRate.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import {
88
HyperdriveConfig,
99
} from "@delvtech/hyperdrive-appconfig";
1010
import { ReadHyperdrive } from "@delvtech/hyperdrive-js";
11-
import { getPublicClient } from "@wagmi/core";
1211
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays";
1312
import { isForkChain } from "src/chains/isForkChain";
14-
import { wagmiConfig } from "src/network/wagmiClient";
15-
import { PublicClient } from "viem";
13+
import { queryClient } from "src/network/queryClient";
14+
import { makeRewardsQuery } from "src/ui/rewards/useRewards";
1615

1716
export async function getYieldSourceRate({
1817
readHyperdrive,
@@ -94,10 +93,12 @@ async function calcNetRate(
9493
appConfig,
9594
});
9695
if (rewardsFn) {
97-
const publicClient = getPublicClient(wagmiConfig as any, {
98-
chainId: hyperdrive.chainId,
99-
}) as PublicClient;
100-
const rewards = await rewardsFn(publicClient);
96+
const rewards = await queryClient.fetchQuery(
97+
makeRewardsQuery({
98+
chainId: hyperdrive.chainId,
99+
hyperdriveAddress: hyperdrive.address,
100+
}),
101+
);
101102
rewards?.forEach((reward) => {
102103
if (reward.type === "apy") {
103104
netRate = fixed(reward.apy).add(

apps/hyperdrive-trading/src/ui/hyperdrive/hooks/usePresentValue.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import { getPublicClient } from "@wagmi/core";
1010
import { ZERO_ADDRESS } from "src/base/constants";
1111
import { makeQueryKey2 } from "src/base/makeQueryKey";
1212
import { isTestnetChain } from "src/chains/isTestnetChain";
13+
import { queryClient } from "src/network/queryClient";
1314
import { wagmiConfig } from "src/network/wagmiClient";
1415
import { useReadHyperdrive } from "src/ui/hyperdrive/hooks/useReadHyperdrive";
15-
import { getTokenFiatPrice } from "src/ui/token/hooks/useTokenFiatPrice";
16+
import { makeTokenFiatPriceQuery } from "src/ui/token/hooks/useTokenFiatPrice";
1617
import { Address, PublicClient } from "viem";
1718

1819
export function usePresentValue({
@@ -88,11 +89,14 @@ export function getPresentValue({
8889
const isFiatSupported =
8990
!isTestnetChain(chainId) && baseToken.address !== ZERO_ADDRESS;
9091
const fiatPricePromise = isFiatSupported
91-
? getTokenFiatPrice({
92-
chainId: baseToken.chainId,
93-
publicClient,
94-
tokenAddress: baseToken.address,
95-
}).catch(() => undefined)
92+
? queryClient
93+
.fetchQuery(
94+
makeTokenFiatPriceQuery({
95+
chainId: baseToken.chainId,
96+
tokenAddress: baseToken.address,
97+
}),
98+
)
99+
.catch(() => undefined)
96100
: Promise.resolve(undefined);
97101

98102
return Promise.all([readHyperdrive.getPresentValue(), fiatPricePromise]).then(

apps/hyperdrive-trading/src/ui/hyperdrive/hooks/useUnpausedPools.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import {
55
} from "@delvtech/hyperdrive-appconfig";
66
import { getHyperdrive } from "@delvtech/hyperdrive-js";
77
import { useQuery } from "@tanstack/react-query";
8-
import { getPublicClient } from "@wagmi/core";
98
import { makeQueryKey2 } from "src/base/makeQueryKey";
109
import { getDrift } from "src/drift/getDrift";
11-
import { wagmiConfig } from "src/network/wagmiClient";
10+
import { queryClient } from "src/network/queryClient";
1211
import { useAppConfigForConnectedChain } from "src/ui/appconfig/useAppConfigForConnectedChain";
13-
import { PublicClient } from "viem";
12+
import { makeRewardsQuery } from "src/ui/rewards/useRewards";
1413
import { useChainId } from "wagmi";
1514

1615
const HIDDEN_POOLS = [
@@ -59,18 +58,23 @@ export function useUnpausedPools(): {
5958
}
6059

6160
// Resolve the rewards information and include it for consumers
62-
const publicClient = getPublicClient(wagmiConfig as any, {
63-
chainId: hyperdrive.chainId,
64-
}) as PublicClient;
65-
6661
const rewardsFn = getRewardsFn({
6762
yieldSourceId: hyperdrive.yieldSource,
6863
appConfig: appConfigForConnectedChain,
6964
});
7065

7166
const rewards = !rewardsFn
7267
? []
73-
: await rewardsFn(publicClient).catch(() => []);
68+
: await (async () => {
69+
return queryClient
70+
.fetchQuery(
71+
makeRewardsQuery({
72+
hyperdriveAddress: hyperdrive.address,
73+
chainId: hyperdrive.chainId,
74+
}),
75+
)
76+
.catch(() => []);
77+
})();
7478

7579
return { ...hyperdrive, rewardsAmount: rewards };
7680
}),
Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,69 @@
11
import {
22
AnyReward,
33
appConfig,
4+
getHyperdriveConfig,
45
getRewardsFn,
56
HyperdriveConfig,
67
} from "@delvtech/hyperdrive-appconfig";
7-
import { useQuery } from "@tanstack/react-query";
8+
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
89
import { getPublicClient } from "@wagmi/core";
910
import { makeQueryKey2 } from "src/base/makeQueryKey";
1011
import { wagmiConfig } from "src/network/wagmiClient";
11-
import { PublicClient } from "viem";
12+
import { Address, PublicClient } from "viem";
1213

1314
export function useRewards(hyperdriveConfig: HyperdriveConfig): {
1415
rewards: AnyReward[] | undefined;
1516
status: "error" | "success" | "loading";
1617
} {
18+
const { data: rewards, status } = useQuery(
19+
makeRewardsQuery({
20+
hyperdriveAddress: hyperdriveConfig.address,
21+
chainId: hyperdriveConfig.chainId,
22+
}),
23+
);
24+
25+
return {
26+
rewards,
27+
status,
28+
};
29+
}
30+
31+
export function makeRewardsQuery({
32+
hyperdriveAddress,
33+
chainId,
34+
}: {
35+
hyperdriveAddress: Address;
36+
chainId: number;
37+
}): UseQueryOptions<AnyReward[]> {
38+
const hyperdriveConfig = getHyperdriveConfig({
39+
hyperdriveChainId: chainId,
40+
hyperdriveAddress,
41+
appConfig,
42+
});
1743
const rewardsFn = getRewardsFn({
1844
yieldSourceId: hyperdriveConfig.yieldSource,
1945
appConfig,
2046
});
2147

2248
const queryEnabled = !!rewardsFn;
23-
24-
const { data: rewards, status } = useQuery({
49+
return {
2550
queryKey: makeQueryKey2({
2651
namespace: "rewards",
2752
queryId: "rewards",
2853
params: {
29-
chainId: hyperdriveConfig.chainId,
30-
hyperdriveAddress: hyperdriveConfig.address,
54+
chainId,
55+
hyperdriveAddress,
3156
},
3257
}),
3358
enabled: queryEnabled,
59+
staleTime: Infinity,
3460
queryFn: queryEnabled
3561
? async () => {
3662
const publicClient = getPublicClient(wagmiConfig as any, {
37-
chainId: hyperdriveConfig.chainId,
63+
chainId,
3864
}) as PublicClient;
3965
return rewardsFn(publicClient);
4066
}
4167
: undefined,
42-
});
43-
44-
return {
45-
rewards,
46-
status,
4768
};
4869
}
Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { appConfig, getPriceOracleFn } from "@delvtech/hyperdrive-appconfig";
2-
import { useQuery } from "@tanstack/react-query";
2+
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
33
import { getPublicClient } from "@wagmi/core";
44
import { ZERO_ADDRESS } from "src/base/constants";
55
import { makeQueryKey2 } from "src/base/makeQueryKey";
@@ -15,47 +15,46 @@ export function useTokenFiatPrice({
1515
}): {
1616
fiatPrice: bigint | undefined;
1717
} {
18+
const { data } = useQuery(makeTokenFiatPriceQuery({ chainId, tokenAddress }));
19+
return { fiatPrice: data };
20+
}
21+
export function makeTokenFiatPriceQuery({
22+
chainId,
23+
tokenAddress,
24+
}: {
25+
chainId: number;
26+
tokenAddress: Address | undefined;
27+
}): UseQueryOptions<bigint> {
1828
const queryEnabled =
1929
!isTestnetChain(chainId) && !!tokenAddress && tokenAddress !== ZERO_ADDRESS;
2030

21-
const { data } = useQuery({
31+
return {
2232
queryKey: makeQueryKey2({
2333
namespace: "tokens",
2434
queryId: "tokenFiatPrice",
2535
params: { chainId, tokenAddress },
2636
}),
37+
staleTime: Infinity,
2738
enabled: queryEnabled,
2839
queryFn: queryEnabled
2940
? async () => {
3041
const publicClient = getPublicClient(wagmiConfig as any, {
3142
chainId,
3243
}) as PublicClient;
3344

34-
return getTokenFiatPrice({ chainId, tokenAddress, publicClient });
45+
const priceOracleFn = getPriceOracleFn({
46+
chainId,
47+
tokenAddress,
48+
appConfig,
49+
});
50+
51+
return priceOracleFn({
52+
chainId,
53+
denomination: "usd",
54+
publicClient,
55+
tokenAddress,
56+
});
3557
}
3658
: undefined,
37-
});
38-
return { fiatPrice: data };
39-
}
40-
export function getTokenFiatPrice({
41-
chainId,
42-
tokenAddress,
43-
publicClient,
44-
}: {
45-
chainId: number;
46-
tokenAddress: Address;
47-
publicClient: PublicClient;
48-
}): Promise<bigint> {
49-
const priceOracleFn = getPriceOracleFn({
50-
chainId,
51-
tokenAddress,
52-
appConfig,
53-
});
54-
55-
return priceOracleFn({
56-
chainId,
57-
denomination: "usd",
58-
publicClient,
59-
tokenAddress,
60-
});
59+
};
6160
}

0 commit comments

Comments
 (0)