-
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.
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
- Loading branch information
1 parent
b0fec7a
commit 34509b9
Showing
6 changed files
with
107 additions
and
33 deletions.
There are no files selected for viewing
96 changes: 71 additions & 25 deletions
96
apps/hyperdrive-trading/src/hyperdrive/getYieldSourceRate.ts
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,40 +1,86 @@ | ||
import { Block, ReadHyperdrive } from "@delvtech/hyperdrive-viem"; | ||
import { AppConfig, findHyperdriveConfig } from "@hyperdrive/appconfig"; | ||
import { | ||
AppConfig, | ||
findHyperdriveConfig, | ||
HyperdriveConfig, | ||
} from "@hyperdrive/appconfig"; | ||
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays"; | ||
import { isForkChain } from "src/chains/isForkChain"; | ||
|
||
export async function getYieldSourceRate( | ||
readHyperdrive: ReadHyperdrive, | ||
appConfig: AppConfig, | ||
): Promise<bigint> { | ||
): Promise<{ rate: bigint; ratePeriodDays: number }> { | ||
const hyperdriveChainId = await readHyperdrive.network.getChainId(); | ||
const hyperdrive = findHyperdriveConfig({ | ||
hyperdriveChainId, | ||
hyperdriveAddress: readHyperdrive.address, | ||
hyperdrives: appConfig.hyperdrives, | ||
}); | ||
|
||
const numBlocksForHistoricalRate = getNumBlocksForHistoricalRate({ | ||
appConfig, | ||
hyperdrive, | ||
}); | ||
|
||
const currentBlock = (await readHyperdrive.network.getBlock()) as Block; | ||
const initializationBlock = hyperdrive.initializationBlock; | ||
|
||
const isPoolYoungerThanOneRatePeriod = | ||
initializationBlock > | ||
currentBlock.blockNumber! - numBlocksForHistoricalRate; | ||
|
||
// If we don't have enough blocks to go back 1 full historical period, then | ||
// grab the all-time rate instead. | ||
if (isPoolYoungerThanOneRatePeriod) { | ||
const blocksSinceInitialization = | ||
currentBlock.blockNumber! - initializationBlock; | ||
|
||
const daysSinceInitialization = convertMillisecondsToDays( | ||
Date.now() - Number(hyperdrive.initializationTimestamp * 1000n), | ||
); | ||
|
||
return { | ||
rate: await readHyperdrive.getYieldSourceRate({ | ||
blockRange: blocksSinceInitialization, | ||
}), | ||
ratePeriodDays: daysSinceInitialization, | ||
}; | ||
} | ||
|
||
const rate = await readHyperdrive.getYieldSourceRate({ | ||
blockRange: numBlocksForHistoricalRate, | ||
}); | ||
|
||
return { | ||
rate, | ||
ratePeriodDays: | ||
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod, | ||
}; | ||
} | ||
|
||
function getNumBlocksForHistoricalRate({ | ||
appConfig, | ||
hyperdrive, | ||
}: { | ||
appConfig: AppConfig; | ||
hyperdrive: HyperdriveConfig; | ||
}) { | ||
const blocksPerDay = appConfig.chains[hyperdrive.chainId].dailyAverageBlocks; | ||
const historicalRatePeriod = | ||
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod; | ||
|
||
const numBlocksForHistoricalRate = isForkChain(hyperdrive.chainId) | ||
? 1000n // roughly 3 hours for cloudchain | ||
: appConfig.chains[hyperdrive.chainId].dailyAverageBlocks * | ||
BigInt( | ||
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod, | ||
); | ||
|
||
return ( | ||
readHyperdrive | ||
.getYieldSourceRate({ | ||
blockRange: numBlocksForHistoricalRate, | ||
}) | ||
// If the 24 hour rate doesn't exist, assume the pool was initialized less | ||
// than 24 hours ago and try to get the all-time rate | ||
.catch(async () => { | ||
const currentBlock = (await readHyperdrive.network.getBlock()) as Block; | ||
const initializationBlock = hyperdrive.initializationBlock; | ||
const blocksSinceInitialization = | ||
currentBlock.blockNumber! - initializationBlock; | ||
|
||
return readHyperdrive.getYieldSourceRate({ | ||
blockRange: blocksSinceInitialization, | ||
}); | ||
}) | ||
); | ||
: blocksPerDay * BigInt(historicalRatePeriod); | ||
|
||
return numBlocksForHistoricalRate; | ||
} | ||
|
||
function getDaysSinceInitialization({ | ||
hyperdrive, | ||
}: { | ||
hyperdrive: HyperdriveConfig; | ||
}): number { | ||
return Number(hyperdrive.initializationTimestamp * 1000n) - Date.now(); | ||
} |
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