Skip to content

Commit 34509b9

Browse files
authored
Show the correct number of days when pool is younger than rate period (#1481)
* Add ratePeriodDays to useLpApy and useVaultRate hooks * Update PoolRow to display the ratePeriodDays * Update other places we display rate period
1 parent b0fec7a commit 34509b9

File tree

6 files changed

+107
-33
lines changed

6 files changed

+107
-33
lines changed
Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,86 @@
11
import { Block, ReadHyperdrive } from "@delvtech/hyperdrive-viem";
2-
import { AppConfig, findHyperdriveConfig } from "@hyperdrive/appconfig";
2+
import {
3+
AppConfig,
4+
findHyperdriveConfig,
5+
HyperdriveConfig,
6+
} from "@hyperdrive/appconfig";
7+
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays";
38
import { isForkChain } from "src/chains/isForkChain";
49

510
export async function getYieldSourceRate(
611
readHyperdrive: ReadHyperdrive,
712
appConfig: AppConfig,
8-
): Promise<bigint> {
13+
): Promise<{ rate: bigint; ratePeriodDays: number }> {
914
const hyperdriveChainId = await readHyperdrive.network.getChainId();
1015
const hyperdrive = findHyperdriveConfig({
1116
hyperdriveChainId,
1217
hyperdriveAddress: readHyperdrive.address,
1318
hyperdrives: appConfig.hyperdrives,
1419
});
20+
21+
const numBlocksForHistoricalRate = getNumBlocksForHistoricalRate({
22+
appConfig,
23+
hyperdrive,
24+
});
25+
26+
const currentBlock = (await readHyperdrive.network.getBlock()) as Block;
27+
const initializationBlock = hyperdrive.initializationBlock;
28+
29+
const isPoolYoungerThanOneRatePeriod =
30+
initializationBlock >
31+
currentBlock.blockNumber! - numBlocksForHistoricalRate;
32+
33+
// If we don't have enough blocks to go back 1 full historical period, then
34+
// grab the all-time rate instead.
35+
if (isPoolYoungerThanOneRatePeriod) {
36+
const blocksSinceInitialization =
37+
currentBlock.blockNumber! - initializationBlock;
38+
39+
const daysSinceInitialization = convertMillisecondsToDays(
40+
Date.now() - Number(hyperdrive.initializationTimestamp * 1000n),
41+
);
42+
43+
return {
44+
rate: await readHyperdrive.getYieldSourceRate({
45+
blockRange: blocksSinceInitialization,
46+
}),
47+
ratePeriodDays: daysSinceInitialization,
48+
};
49+
}
50+
51+
const rate = await readHyperdrive.getYieldSourceRate({
52+
blockRange: numBlocksForHistoricalRate,
53+
});
54+
55+
return {
56+
rate,
57+
ratePeriodDays:
58+
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod,
59+
};
60+
}
61+
62+
function getNumBlocksForHistoricalRate({
63+
appConfig,
64+
hyperdrive,
65+
}: {
66+
appConfig: AppConfig;
67+
hyperdrive: HyperdriveConfig;
68+
}) {
69+
const blocksPerDay = appConfig.chains[hyperdrive.chainId].dailyAverageBlocks;
70+
const historicalRatePeriod =
71+
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod;
72+
1573
const numBlocksForHistoricalRate = isForkChain(hyperdrive.chainId)
1674
? 1000n // roughly 3 hours for cloudchain
17-
: appConfig.chains[hyperdrive.chainId].dailyAverageBlocks *
18-
BigInt(
19-
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod,
20-
);
21-
22-
return (
23-
readHyperdrive
24-
.getYieldSourceRate({
25-
blockRange: numBlocksForHistoricalRate,
26-
})
27-
// If the 24 hour rate doesn't exist, assume the pool was initialized less
28-
// than 24 hours ago and try to get the all-time rate
29-
.catch(async () => {
30-
const currentBlock = (await readHyperdrive.network.getBlock()) as Block;
31-
const initializationBlock = hyperdrive.initializationBlock;
32-
const blocksSinceInitialization =
33-
currentBlock.blockNumber! - initializationBlock;
34-
35-
return readHyperdrive.getYieldSourceRate({
36-
blockRange: blocksSinceInitialization,
37-
});
38-
})
39-
);
75+
: blocksPerDay * BigInt(historicalRatePeriod);
76+
77+
return numBlocksForHistoricalRate;
78+
}
79+
80+
function getDaysSinceInitialization({
81+
hyperdrive,
82+
}: {
83+
hyperdrive: HyperdriveConfig;
84+
}): number {
85+
return Number(hyperdrive.initializationTimestamp * 1000n) - Date.now();
4086
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { Block } from "@delvtech/hyperdrive-viem";
12
import { findHyperdriveConfig } from "@hyperdrive/appconfig";
23
import { useQuery } from "@tanstack/react-query";
4+
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays";
35
import { formatRate } from "src/base/formatRate";
46
import { makeQueryKey } from "src/base/makeQueryKey";
57
import { isForkChain } from "src/chains/isForkChain";
@@ -16,7 +18,9 @@ export function useLpApy({
1618
hyperdriveAddress: Address;
1719
chainId: number;
1820
}): {
19-
lpApy: { lpApy: bigint; formatted: string } | undefined;
21+
lpApy:
22+
| { lpApy: bigint; formatted: string; ratePeriodDays: number }
23+
| undefined;
2024
lpApyStatus: "error" | "success" | "loading";
2125
} {
2226
const { poolInfo: currentPoolInfo } = usePoolInfo({
@@ -58,8 +62,26 @@ export function useLpApy({
5862
blockNumber - numBlocksForHistoricalRate,
5963
});
6064

65+
// Figure out if the pool is younger than 1 rate period
66+
const currentBlock =
67+
(await readHyperdrive.network.getBlock()) as Block;
68+
const isPoolYoungerThanOneRatePeriod =
69+
hyperdrive.initializationBlock >
70+
currentBlock.blockNumber! - numBlocksForHistoricalRate;
71+
72+
// If we don't have enough blocks to go back 1 full historical period, then
73+
// grab the all-time rate instead.
74+
let ratePeriodDays =
75+
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod;
76+
if (isPoolYoungerThanOneRatePeriod) {
77+
ratePeriodDays = convertMillisecondsToDays(
78+
Date.now() - Number(hyperdrive.initializationTimestamp * 1000n),
79+
);
80+
}
81+
6182
return {
6283
lpApy,
84+
ratePeriodDays,
6385
formatted: formatRate(lpApy),
6486
};
6587
}

apps/hyperdrive-trading/src/ui/markets/MarketStats/VariableRateStat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function VariableRateStat({
3333

3434
return (
3535
<Stat
36-
label={`Variable APY (${yieldSource.historicalRatePeriod}d)`}
36+
label={`Variable APY (${vaultRate?.ratePeriodDays}d)`}
3737
description={`The yield rate earned on deposits into ${yieldSource.shortName} in the last ${yieldSource.historicalRatePeriod} days.`}
3838
tooltipPosition="bottom"
3939
value={

apps/hyperdrive-trading/src/ui/markets/MarketStats/YieldStats.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function YieldStats({
5050
</Animated>
5151
<Animated isActive={position === "lp"}>
5252
<Stat
53-
label={`LP APY (${yieldSource.historicalRatePeriod}d)`}
53+
label={lpApy ? `LP APY (${lpApy.ratePeriodDays}d)` : "LP APY"}
5454
value={
5555
<RewardsTooltip
5656
chainId={hyperdrive.chainId}

apps/hyperdrive-trading/src/ui/markets/PoolRow/PoolRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export function PoolRow({
247247
}
248248
/>
249249
<PoolStat
250-
label={`LP APY (${yieldSources[hyperdrive.yieldSource].historicalRatePeriod}d)`}
250+
label={lpApy ? `LP APY (${lpApy.ratePeriodDays}d)` : "LP APY"}
251251
isLoading={lpApyStatus === "loading"}
252252
isNew={isLpApyNew}
253253
value={

apps/hyperdrive-trading/src/ui/vaults/useYieldSourceRate.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export function useYieldSourceRate({
1313
chainId: number;
1414
hyperdriveAddress: Address | undefined;
1515
}): {
16-
vaultRate: { vaultRate: bigint; formatted: string } | undefined;
16+
vaultRate:
17+
| { vaultRate: bigint; formatted: string; ratePeriodDays: number }
18+
| undefined;
1719
vaultRateStatus: "error" | "success" | "loading";
1820
} {
1921
const readHyperdrive = useReadHyperdrive({
@@ -22,22 +24,26 @@ export function useYieldSourceRate({
2224
});
2325
const appConfig = useAppConfig();
2426
const queryEnabled = !!hyperdriveAddress && !!readHyperdrive;
25-
const { data: vaultRate, status: vaultRateStatus } = useQuery({
27+
const { data, status: vaultRateStatus } = useQuery({
2628
enabled: queryEnabled,
2729
queryKey: makeQueryKey("vaultRate", {
2830
chainId,
2931
hyperdriveAddress,
3032
}),
3133
queryFn: queryEnabled
3234
? async () => {
33-
const rate = await getYieldSourceRate(readHyperdrive, appConfig);
35+
const { rate, ratePeriodDays } = await getYieldSourceRate(
36+
readHyperdrive,
37+
appConfig,
38+
);
3439
return {
3540
vaultRate: rate,
3641
formatted: formatRate(rate),
42+
ratePeriodDays,
3743
};
3844
}
3945
: undefined,
4046
});
4147

42-
return { vaultRate, vaultRateStatus };
48+
return { vaultRate: data, vaultRateStatus };
4349
}

0 commit comments

Comments
 (0)