-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve caching of rewards resolver functions and token fiat price (#…
- Loading branch information
1 parent
3e5b5ac
commit 23e4e4c
Showing
6 changed files
with
97 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,69 @@ | ||
import { | ||
AnyReward, | ||
appConfig, | ||
getHyperdriveConfig, | ||
getRewardsFn, | ||
HyperdriveConfig, | ||
} from "@delvtech/hyperdrive-appconfig"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useQuery, UseQueryOptions } from "@tanstack/react-query"; | ||
import { getPublicClient } from "@wagmi/core"; | ||
import { makeQueryKey2 } from "src/base/makeQueryKey"; | ||
import { wagmiConfig } from "src/network/wagmiClient"; | ||
import { PublicClient } from "viem"; | ||
import { Address, PublicClient } from "viem"; | ||
|
||
export function useRewards(hyperdriveConfig: HyperdriveConfig): { | ||
rewards: AnyReward[] | undefined; | ||
status: "error" | "success" | "loading"; | ||
} { | ||
const { data: rewards, status } = useQuery( | ||
makeRewardsQuery({ | ||
hyperdriveAddress: hyperdriveConfig.address, | ||
chainId: hyperdriveConfig.chainId, | ||
}), | ||
); | ||
|
||
return { | ||
rewards, | ||
status, | ||
}; | ||
} | ||
|
||
export function makeRewardsQuery({ | ||
hyperdriveAddress, | ||
chainId, | ||
}: { | ||
hyperdriveAddress: Address; | ||
chainId: number; | ||
}): UseQueryOptions<AnyReward[]> { | ||
const hyperdriveConfig = getHyperdriveConfig({ | ||
hyperdriveChainId: chainId, | ||
hyperdriveAddress, | ||
appConfig, | ||
}); | ||
const rewardsFn = getRewardsFn({ | ||
yieldSourceId: hyperdriveConfig.yieldSource, | ||
appConfig, | ||
}); | ||
|
||
const queryEnabled = !!rewardsFn; | ||
|
||
const { data: rewards, status } = useQuery({ | ||
return { | ||
queryKey: makeQueryKey2({ | ||
namespace: "rewards", | ||
queryId: "rewards", | ||
params: { | ||
chainId: hyperdriveConfig.chainId, | ||
hyperdriveAddress: hyperdriveConfig.address, | ||
chainId, | ||
hyperdriveAddress, | ||
}, | ||
}), | ||
enabled: queryEnabled, | ||
staleTime: Infinity, | ||
queryFn: queryEnabled | ||
? async () => { | ||
const publicClient = getPublicClient(wagmiConfig as any, { | ||
chainId: hyperdriveConfig.chainId, | ||
chainId, | ||
}) as PublicClient; | ||
return rewardsFn(publicClient); | ||
} | ||
: undefined, | ||
}); | ||
|
||
return { | ||
rewards, | ||
status, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters