-
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.
* Add sorting to pools list * Don't mutate array in place * Fix type errors * Remove unused arg * Remove comment * Address rest of PR comments
- Loading branch information
Showing
10 changed files
with
324 additions
and
191 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"delvtech", | ||
"dnum", | ||
"fixedpointmath", | ||
"hyperdrive", | ||
"hyperwasm", | ||
"ihyperdrive", | ||
"mintable", | ||
|
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { Block, ReadHyperdrive } from "@delvtech/hyperdrive-viem"; | ||
import { appConfig, HyperdriveConfig } from "@hyperdrive/appconfig"; | ||
import { convertMillisecondsToDays } from "src/base/convertMillisecondsToDays"; | ||
import { isForkChain } from "src/chains/isForkChain"; | ||
|
||
export type LpApyResult = { | ||
ratePeriodDays: number; | ||
} & ( | ||
| { | ||
lpApy: bigint; | ||
isNew: false; | ||
} | ||
| { | ||
lpApy: undefined; | ||
isNew: true; | ||
} | ||
); | ||
|
||
export async function getLpApy({ | ||
readHyperdrive, | ||
hyperdrive, | ||
}: { | ||
readHyperdrive: ReadHyperdrive; | ||
hyperdrive: HyperdriveConfig; | ||
}): Promise<LpApyResult> { | ||
const currentBlock = (await readHyperdrive.network.getBlock()) as Block; | ||
const currentBlockNumber = currentBlock.blockNumber!; | ||
|
||
// Appconfig tells us how many days to look back for historical rates | ||
const numBlocksForHistoricalRate = isForkChain(hyperdrive.chainId) | ||
? 1000n // roughly 3 hours for cloudchain | ||
: appConfig.chains[hyperdrive.chainId].dailyAverageBlocks * | ||
BigInt( | ||
appConfig.yieldSources[hyperdrive.yieldSource].historicalRatePeriod, | ||
); | ||
const targetFromBlock = currentBlockNumber - numBlocksForHistoricalRate; | ||
|
||
let lpApy: bigint | undefined; | ||
|
||
const blocksSinceInitialization = | ||
(currentBlockNumber || 0n) - hyperdrive.initializationBlock; | ||
const isYoungerThanOneDay = | ||
blocksSinceInitialization < | ||
appConfig.chains[hyperdrive.chainId].dailyAverageBlocks; | ||
|
||
// if the pool was deployed less than one day ago, it's new. | ||
if (!isYoungerThanOneDay) { | ||
const lpApyResult = await readHyperdrive.getLpApy({ | ||
fromBlock: [31337].includes(hyperdrive.chainId) | ||
? // local devnets don't have a lot of blocks, so start from the beginning | ||
1n | ||
: targetFromBlock, | ||
}); | ||
lpApy = lpApyResult.lpApy; | ||
} | ||
|
||
// Figure out if the pool is younger than 1 rate period | ||
const isPoolYoungerThanOneRatePeriod = | ||
hyperdrive.initializationBlock > targetFromBlock; | ||
|
||
// 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; | ||
if (isPoolYoungerThanOneRatePeriod) { | ||
ratePeriodDays = convertMillisecondsToDays( | ||
Date.now() - Number(hyperdrive.initializationTimestamp * 1000n), | ||
); | ||
} | ||
|
||
return { | ||
lpApy, | ||
ratePeriodDays, | ||
isNew: lpApy === undefined, | ||
} as LpApyResult; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { parseFixed } from "@delvtech/fixed-point-wasm"; | ||
import { ETH_MAGIC_NUMBER } from "src/token/ETH_MAGIC_NUMBER"; | ||
import { gnosis, linea, mainnet } from "viem/chains"; | ||
|
||
export async function getTokenFiatPrice({ | ||
tokenAddress, | ||
chainId, | ||
}: { | ||
tokenAddress: string; | ||
chainId: number; | ||
}): Promise<bigint | undefined> { | ||
// Always use mainnet ETH as the reference for native ETH price, regardless of | ||
// the current chain. | ||
let defiLlamaTokenId = `${defiLlamaChainNameIdentifier[chainId]}:${tokenAddress}`; | ||
if (tokenAddress === ETH_MAGIC_NUMBER) { | ||
defiLlamaTokenId = `ethereum:${ETH_MAGIC_NUMBER}`; | ||
} | ||
|
||
const response = await fetch( | ||
`https://coins.llama.fi/prices/current/${defiLlamaTokenId}`, | ||
); | ||
const data = await response.json(); | ||
const { price } = data.coins[defiLlamaTokenId]; | ||
return parseFixed(price).bigint; | ||
} | ||
|
||
// NOTE: DefiLlama chain name identifier must be lower case. | ||
export const defiLlamaChainNameIdentifier: Record<number, string> = { | ||
[mainnet.id]: "ethereum", | ||
[gnosis.id]: "gnosis", | ||
[linea.id]: "linea", | ||
}; |
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
Oops, something went wrong.