Skip to content

Commit

Permalink
Adds selector for getRewardsFn (#1638)
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyDelott authored Nov 8, 2024
1 parent 6990d61 commit 045f535
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
11 changes: 6 additions & 5 deletions apps/hyperdrive-trading/src/hyperdrive/getLpApy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fixed } from "@delvtech/fixed-point-wasm";
import {
appConfig,
getRewardsFn,
HyperdriveConfig,
rewardFunctions,
} from "@delvtech/hyperdrive-appconfig";
import { Block, ReadHyperdrive } from "@delvtech/hyperdrive-viem";
import { getPublicClient } from "@wagmi/core";
Expand Down Expand Up @@ -78,11 +78,12 @@ export async function getLpApy({
const publicClient = getPublicClient(wagmiConfig as any, {
chainId: hyperdrive.chainId,
}) as PublicClient;
// TODO: Create an appconfig selector to grab the rewards function from a
// given hyperdrive
const rewardsFn = appConfig.yieldSources[hyperdrive.yieldSource].rewardsFn;
const rewardsFn = getRewardsFn({
yieldSourceId: hyperdrive.yieldSource,
appConfig,
});
if (rewardsFn) {
const rewards = await rewardFunctions[rewardsFn](publicClient);
const rewards = await rewardsFn(publicClient);
rewards?.forEach((reward) => {
if (reward.type === "transferableToken") {
netLpApy = fixed(reward.apy).add(
Expand Down
19 changes: 6 additions & 13 deletions apps/hyperdrive-trading/src/hyperdrive/getYieldSourceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { fixed } from "@delvtech/fixed-point-wasm";
import {
AppConfig,
findHyperdriveConfig,
getRewardsFn,
HyperdriveConfig,
rewardFunctions,
} from "@delvtech/hyperdrive-appconfig";
import { Block, ReadHyperdrive } from "@delvtech/hyperdrive-viem";
import { getPublicClient } from "@wagmi/core";
Expand Down Expand Up @@ -79,14 +79,15 @@ async function calcNetRate(
hyperdrive: HyperdriveConfig,
) {
let netRate = rate;
// TODO: Create an appconfig selector to grab the rewards function from a
// given hyperdrive
const rewardsFn = appConfig.yieldSources[hyperdrive.yieldSource].rewardsFn;
const rewardsFn = getRewardsFn({
yieldSourceId: hyperdrive.yieldSource,
appConfig,
});
if (rewardsFn) {
const publicClient = getPublicClient(wagmiConfig as any, {
chainId: hyperdrive.chainId,
}) as PublicClient;
const rewards = await rewardFunctions[rewardsFn](publicClient);
const rewards = await rewardsFn(publicClient);
rewards?.forEach((reward) => {
if (reward.type === "transferableToken") {
netRate = fixed(reward.apy).add(
Expand Down Expand Up @@ -116,11 +117,3 @@ function getNumBlocksForHistoricalRate({

return numBlocksForHistoricalRate;
}

function getDaysSinceInitialization({
hyperdrive,
}: {
hyperdrive: HyperdriveConfig;
}): number {
return Number(hyperdrive.initializationTimestamp * 1000n) - Date.now();
}
10 changes: 6 additions & 4 deletions apps/hyperdrive-trading/src/ui/rewards/useRewards.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
AnyReward,
appConfig,
getRewardsFn,
HyperdriveConfig,
rewardFunctions,
} from "@delvtech/hyperdrive-appconfig";
import { useQuery } from "@tanstack/react-query";
import { getPublicClient } from "@wagmi/core";
Expand All @@ -14,8 +14,10 @@ export function useRewards(hyperdriveConfig: HyperdriveConfig): {
rewards: AnyReward[] | undefined;
status: "error" | "success" | "loading";
} {
const rewardsFn =
appConfig.yieldSources[hyperdriveConfig.yieldSource].rewardsFn;
const rewardsFn = getRewardsFn({
yieldSourceId: hyperdriveConfig.yieldSource,
appConfig,
});

const queryEnabled = !!rewardsFn;

Expand All @@ -30,7 +32,7 @@ export function useRewards(hyperdriveConfig: HyperdriveConfig): {
const publicClient = getPublicClient(wagmiConfig as any, {
chainId: hyperdriveConfig.chainId,
}) as PublicClient;
return rewardFunctions[rewardsFn](publicClient);
return rewardsFn(publicClient);
}
: undefined,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/hyperdrive-appconfig/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export type { YieldSource, YieldSourceId } from "src/yieldSources/types";
export { yieldSources } from "src/yieldSources/yieldSources";

// rewards
export { rewardFunctions } from "src/rewards/rewards";
export { getRewardsFn } from "src/rewards/selectors";
export type {
AnyReward,
InfoReward,
NonTransferableTokenReward,
RewardsResolver as RewardsFn,
RewardsResolver,
TransferableTokenReward,
} from "src/rewards/types";

Expand Down
25 changes: 25 additions & 0 deletions packages/hyperdrive-appconfig/src/rewards/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { AppConfig } from "src/appconfig/AppConfig";
import { rewardFunctions } from "src/rewards/rewards";
import { RewardsResolver } from "src/rewards/types";
import { YieldSourceId } from "src/yieldSources/types";

/**
* Find the rewards resolver for a given yield source. This will return
* `undefined` if no rewards function exists for this yield source.
*/
export function getRewardsFn({
yieldSourceId,
appConfig,
}: {
// TODO: change this type to YieldSourceId once YieldSourceId can be used outside
// of the appconfig package.k
yieldSourceId: string;
appConfig: AppConfig;
}): RewardsResolver | undefined {
// casting this for now because YieldSourceId doesn't work outside of the appconfig package
const yieldSource = appConfig.yieldSources[yieldSourceId as YieldSourceId];
if (!yieldSource.rewardsFn) {
return;
}
return rewardFunctions[yieldSource.rewardsFn];
}

0 comments on commit 045f535

Please sign in to comment.