Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions apps/hyperdrive-trading/src/hyperdrive/getLpApy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { fixed } from "@delvtech/fixed-point-wasm";
import {
appConfig,
findYieldSource,
getRewardsFn,
HyperdriveConfig,
} from "@delvtech/hyperdrive-appconfig";
Expand Down Expand Up @@ -49,12 +50,15 @@
const currentBlock = (await readHyperdrive.drift.getBlock()) as Block;
const currentBlockNumber = currentBlock.blockNumber!;
// Appconfig tells us how many days to look back for historical rates
const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});
const numBlocksForHistoricalRate = isForkChain(hyperdrive.chainId)
? 1000n // roughly 3 hours for cloudchain
: appConfig.chains[hyperdrive.chainId].dailyAverageBlocks *
BigInt(
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod,
);
BigInt(yieldSource.historicalRatePeriod);
const targetFromBlock = currentBlockNumber - numBlocksForHistoricalRate;

let lpApy: bigint | undefined;
Expand All @@ -76,7 +80,7 @@
});
lpApy = lpApyResult.lpApy;
netLpApy = lpApy;
const publicClient = getPublicClient(wagmiConfig as any, {

Check warning on line 83 in apps/hyperdrive-trading/src/hyperdrive/getLpApy.ts

View workflow job for this annotation

GitHub Actions / verify (lint)

Unexpected any. Specify a different type
chainId: hyperdrive.chainId,
}) as PublicClient;
const rewardsFn = getRewardsFn({
Expand All @@ -102,8 +106,7 @@

// If we don't have enough blocks to go back 1 full historical period, then
// grab the all-time rate instead.
let ratePeriodDays =
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod;
let ratePeriodDays = yieldSource.historicalRatePeriod;
if (isPoolYoungerThanOneRatePeriod) {
ratePeriodDays = convertMillisecondsToDays(
Date.now() - Number(hyperdrive.initializationTimestamp * 1000n),
Expand Down
18 changes: 14 additions & 4 deletions apps/hyperdrive-trading/src/hyperdrive/getYieldSourceRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {
AppConfig,
findHyperdriveConfig,
findYieldSource,
getRewardsFn,
HyperdriveConfig,
} from "@delvtech/hyperdrive-appconfig";
Expand Down Expand Up @@ -66,11 +67,16 @@

const netRate = await calcNetRate(rate, appConfig, hyperdrive);

const yieldSource = findYieldSource({
appConfig,
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
});

return {
rate,
netRate,
ratePeriodDays:
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod,
ratePeriodDays: yieldSource.historicalRatePeriod,
};
}

Expand All @@ -85,7 +91,7 @@
appConfig,
});
if (rewardsFn) {
const publicClient = getPublicClient(wagmiConfig as any, {

Check warning on line 94 in apps/hyperdrive-trading/src/hyperdrive/getYieldSourceRate.ts

View workflow job for this annotation

GitHub Actions / verify (lint)

Unexpected any. Specify a different type
chainId: hyperdrive.chainId,
}) as PublicClient;
const rewards = await rewardsFn(publicClient);
Expand All @@ -109,8 +115,12 @@
hyperdrive: HyperdriveConfig;
}) {
const blocksPerDay = appConfig.chains[hyperdrive.chainId].dailyAverageBlocks;
const historicalRatePeriod =
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod;
const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});
const historicalRatePeriod = yieldSource.historicalRatePeriod;

const numBlocksForHistoricalRate = isForkChain(hyperdrive.chainId)
? 1000n // roughly 3 hours for cloudchain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AppConfig,
appConfig,
findHyperdriveConfig,
findYieldSource,
} from "@delvtech/hyperdrive-appconfig";
import { ReadHyperdrive } from "@delvtech/hyperdrive-js";
import { useQuery } from "@tanstack/react-query";
Expand Down Expand Up @@ -75,9 +76,12 @@ export async function prepareSharesIn({
// If the shares token is pegged to its base token (e.g., stETH to ETH), then
// we need to treat the amount as if it were base. To get the actual shares
// amount then, we convert to the shares used by the pool (eg: lidoShares).
if (
appConfig.yieldSources[hyperdriveConfig.yieldSource].isSharesPeggedToBase
) {
const yieldSource = findYieldSource({
hyperdriveAddress: hyperdriveConfig.address,
hyperdriveChainId: hyperdriveConfig.chainId,
appConfig,
});
if (yieldSource.isSharesPeggedToBase) {
return readHyperdrive.convertToShares({
baseAmount: sharesAmount,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AppConfig,
appConfig,
findHyperdriveConfig,
findYieldSource,
} from "@delvtech/hyperdrive-appconfig";
import { ReadHyperdrive } from "@delvtech/hyperdrive-js";
import { useQuery } from "@tanstack/react-query";
Expand Down Expand Up @@ -81,9 +82,12 @@ export async function prepareSharesOut({
// amount then, we convert to base. For example, when preparing lido shares
// received back from a steth hyperdrive, this will convert lido shares to
// eth, and since 1 eth = 1 steth we return this as the shares value.
if (
appConfig.yieldSources[hyperdriveConfig.yieldSource].isSharesPeggedToBase
) {
const yieldSource = findYieldSource({
hyperdriveAddress: hyperdriveConfig.address,
hyperdriveChainId: hyperdriveConfig.chainId,
appConfig,
});
if (yieldSource.isSharesPeggedToBase) {
return readHyperdrive.convertToBase({
sharesAmount,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { fixed } from "@delvtech/fixed-point-wasm";
import {
appConfig,
findBaseToken,
findYieldSource,
HyperdriveConfig,
} from "@delvtech/hyperdrive-appconfig";
import { calculateAprFromPrice } from "@delvtech/hyperdrive-js";
Expand Down Expand Up @@ -47,9 +48,12 @@ export function OpenLongStats({
hyperdriveAddress: hyperdrive.address,
});

const isBaseAmount =
asBase ||
appConfig.yieldSources[hyperdrive.yieldSource].isSharesPeggedToBase;
const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});
const isBaseAmount = asBase || yieldSource.isSharesPeggedToBase;
const amountPaidInBase = isBaseAmount
? amountPaid
: convertSharesToBase({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
appConfig,
findBaseToken,
findToken,
findYieldSource,
} from "@delvtech/hyperdrive-appconfig";
import { adjustAmountByPercentage } from "@delvtech/hyperdrive-js";
import classNames from "classnames";
Expand Down Expand Up @@ -533,7 +534,11 @@ function LpApyStat({ hyperdrive }: { hyperdrive: HyperdriveConfig }) {
});

const showSkeleton = !lpApy && lpApyStatus === "loading";

const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});
return (
<PrimaryStat
label="LP APY"
Expand All @@ -560,7 +565,7 @@ function LpApyStat({ hyperdrive }: { hyperdrive: HyperdriveConfig }) {
subValue={
vaultRateStatus === "success" && vaultRate ? (
<div>
{appConfig.yieldSources[hyperdrive.yieldSource].shortName} @{" "}
{yieldSource.shortName} @{" "}
{isNewPool
? "✨New✨"
: `${formatRate({ rate: vaultRate.netVaultRate })} APY`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export function ManageLpAndWithdrawalSharesButton({
account,
chainId: hyperdrive.chainId,
});
const yieldSource = appConfig.yieldSources[hyperdrive.yieldSource];

const { withdrawalShares: balanceOfWithdrawalShares } = useWithdrawalShares({
hyperdriveAddress: hyperdrive.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
appConfig,
findBaseToken,
findToken,
findYieldSource,
HyperdriveConfig,
} from "@delvtech/hyperdrive-appconfig";
import { MouseEvent, ReactElement, useState } from "react";
Expand Down Expand Up @@ -284,6 +285,12 @@ export function OpenShortForm({
Date.now() + Number(hyperdrive.poolConfig.positionDuration * 1000n),
);

const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});

// Plausible event props
const formName = "Open Short";
const chainId = hyperdrive.chainId;
Expand Down Expand Up @@ -369,7 +376,7 @@ export function OpenShortForm({
bottomRightElement={
vaultRateStatus === "success" && vaultRate ? (
<>
{appConfig.yieldSources[hyperdrive.yieldSource].shortName} @{" "}
{yieldSource.shortName} @{" "}
{isNewPool
? "✨New✨"
: `${formatRate({ rate: vaultRate.netVaultRate })} APY`}
Expand Down Expand Up @@ -459,7 +466,7 @@ export function OpenShortForm({
<PrimaryStat
label="Exposure Multiplier"
tooltipContent={`This represents how much exposure you get to ${
appConfig.yieldSources[hyperdrive.yieldSource].shortName
yieldSource.shortName
} compared to what you pay to open the short.`}
value={
<span className="text-h3 font-bold">{exposureMultiplier}</span>
Expand Down
14 changes: 12 additions & 2 deletions apps/hyperdrive-trading/src/ui/markets/PoolDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { appConfig, HyperdriveConfig } from "@delvtech/hyperdrive-appconfig";
import {
appConfig,
findYieldSource,
HyperdriveConfig,
} from "@delvtech/hyperdrive-appconfig";
import { ArrowLeftIcon } from "@heroicons/react/16/solid";
import { Link, useSearch } from "@tanstack/react-router";
import classNames from "classnames";
Expand Down Expand Up @@ -31,6 +35,12 @@ export function PoolDetails({
hyperdriveAddress: hyperdrive.address,
});

const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});

return (
<div className="flex w-[515px] flex-col gap-9">
<div className="flex flex-col gap-4">
Expand All @@ -43,7 +53,7 @@ export function PoolDetails({
</Link>
<h1 className="flex items-center gap-2 text-h2">
<AssetStack hyperdriveAddress={hyperdrive.address} />
{appConfig.yieldSources[hyperdrive.yieldSource].shortName}
{yieldSource.shortName}
</h1>
{marketState?.isPaused && (
<CustomBanner description="This market has been paused. You may close your positions, but no new positions may be opened." />
Expand Down
12 changes: 8 additions & 4 deletions apps/hyperdrive-trading/src/ui/markets/PoolRow/PoolRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
appConfig,
findBaseToken,
findToken,
findYieldSource,
HyperdriveConfig,
} from "@delvtech/hyperdrive-appconfig";
import { ClockIcon } from "@heroicons/react/16/solid";
Expand All @@ -25,7 +26,7 @@ export interface PoolRowProps {

export function PoolRow({ hyperdrive }: PoolRowProps): ReactElement {
const navigate = useNavigate();
const { yieldSources, chains } = appConfig;
const { chains } = appConfig;
const chainInfo = chains[hyperdrive.chainId];

const baseToken = findBaseToken({
Expand Down Expand Up @@ -66,6 +67,11 @@ export function PoolRow({ hyperdrive }: PoolRowProps): ReactElement {
decimals: hyperdrive.decimals,
})}`;
}
const yieldSource = findYieldSource({
hyperdriveAddress: hyperdrive.address,
hyperdriveChainId: hyperdrive.chainId,
appConfig,
});

return (
<Well
Expand Down Expand Up @@ -95,9 +101,7 @@ export function PoolRow({ hyperdrive }: PoolRowProps): ReactElement {
<AssetStack hyperdriveAddress={hyperdrive.address} />
</div>
<div className="flex flex-col gap-1.5">
<h4 className="text-left">
{yieldSources[hyperdrive.yieldSource].shortName}
</h4>
<h4 className="text-left">{yieldSource.shortName}</h4>
<div className="flex flex-wrap gap-5 gap-y-20">
<div className="flex items-center gap-1.5 text-sm">
<ClockIcon className="size-4 text-gray-400/60" />{" "}
Expand Down
1 change: 0 additions & 1 deletion packages/hyperdrive-appconfig/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"lodash.camelcase": "^4.3.0",
"lodash.uniqby": "^4.7.0",
"prettier-plugin-organize-imports": "3.2.4",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.0",
"tsup": "^7.2.0",
"tsx": "^4.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PoolConfig } from "@delvtech/hyperdrive-viem";
import { yieldSources } from "src/yieldSources/yieldSources";
import { YieldSourceId } from "src/yieldSources/types";
import { Address } from "viem";

export interface HyperdriveConfig {
Expand All @@ -15,7 +15,7 @@ export interface HyperdriveConfig {
/**
* The yield source backing the pool,
*/
yieldSource: keyof typeof yieldSources;
yieldSource: YieldSourceId;

depositOptions: {
/**
Expand Down
23 changes: 23 additions & 0 deletions packages/hyperdrive-appconfig/src/hyperdrives/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AppConfig } from "src/appconfig/AppConfig";
import { HyperdriveConfig } from "src/hyperdrives/HyperdriveConfig";
import { findToken } from "src/tokens/selectors";
import { TokenConfig } from "src/tokens/types";
import { YieldSourceConfig } from "src/yieldSources/types";
import { Address, zeroAddress } from "viem";

/**
Expand Down Expand Up @@ -31,6 +32,28 @@ export function findHyperdriveConfig({

return hyperdriveConfig;
}

/**
* Finds the YieldSourceConfig for a given hyperdrive
*/
export function findYieldSource({
hyperdriveChainId,
hyperdriveAddress,
appConfig,
}: {
hyperdriveChainId: number;
hyperdriveAddress: Address;
appConfig: AppConfig;
}): YieldSourceConfig {
const hyperdriveConfig = findHyperdriveConfig({
hyperdriveChainId: hyperdriveChainId,
hyperdriveAddress,
hyperdrives: appConfig.hyperdrives,
});

return appConfig.yieldSources[hyperdriveConfig.yieldSource];
}

/**
* Retrieves the base token associated with a specific Hyperdrive address. This
* token is used to denominate both `vaultSharePrice` and `lpSharePrice`, and
Expand Down
16 changes: 6 additions & 10 deletions packages/hyperdrive-appconfig/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,21 @@ export type { ChainConfig } from "src/chains/chains";
export { isMainnetChain } from "src/chains/isMainnetChain";

// appconfig selectors
export { findBaseToken, findHyperdriveConfig } from "src/hyperdrives/selectors";
export {
findBaseToken,
findHyperdriveConfig,
findYieldSource,
} from "src/hyperdrives/selectors";
export { findToken } from "src/tokens/selectors";

// hyperdrive
export type { HyperdriveConfig } from "src/hyperdrives/HyperdriveConfig";

// tokens
export {
AERO_ICON_URL,
EETH_ICON_URL,
EURC_ICON_URL,
USDC_ICON_URL,
WELL_ICON_URL,
} from "src/tokens/tokenIconsUrls";
export type { TokenConfig } from "src/tokens/types";

// yield sources
export type { YieldSource, YieldSourceId } from "src/yieldSources/types";
export { yieldSources } from "src/yieldSources/yieldSources";
export type { YieldSourceConfig, YieldSourceId } from "src/yieldSources/types";

// rewards
export { getRewardsFn } from "src/rewards/selectors";
Expand Down
Loading
Loading