generated from valora-inc/typescript-app-starter
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(earn): Add dailyYieldRatePercentage to beefy dataProps response #620
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d1fc0d4
feat(earn): Add dailyYieldRatePercentage to beefy dataProps response
jh2oman 5c1f311
nit
jh2oman ef67c5b
Merge branch 'main' into daily-yield-rate
jh2oman 10d6d31
extra defense
jh2oman ba45549
more clarity
jh2oman 256f67d
subtype optional
jh2oman 865d6f4
Merge branch 'main' into daily-yield-rate
jh2oman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -21,7 +21,7 @@ import { beefyV2AppMulticallAbi } from './abis/beefy-v2-app-multicall' | |||||
import { | ||||||
BaseBeefyVault, | ||||||
GovVault, | ||||||
getApys, | ||||||
getApyBreakdown, | ||||||
getBeefyPrices, | ||||||
getBeefyVaults, | ||||||
getTvls, | ||||||
|
@@ -30,7 +30,7 @@ import { TFunction } from 'i18next' | |||||
import { networkIdToNativeAssetAddress } from '../../runtime/isNative' | ||||||
|
||||||
type BeefyPrices = Awaited<ReturnType<typeof getBeefyPrices>> | ||||||
type BeefyApys = Awaited<ReturnType<typeof getApys>> | ||||||
type BeefyApyBreakdown = Awaited<ReturnType<typeof getApyBreakdown>> | ||||||
type BeefyTvls = Awaited<ReturnType<typeof getTvls>> | ||||||
|
||||||
// Fetched addresses from https://github.com/beefyfinance/beefy-v2/blob/main/src/config/config.tsx | ||||||
|
@@ -58,14 +58,14 @@ const beefyAppTokenDefinition = ({ | |||||
networkId, | ||||||
vault, | ||||||
prices, | ||||||
apys, | ||||||
apyBreakdown, | ||||||
tvls, | ||||||
t, | ||||||
}: { | ||||||
networkId: NetworkId | ||||||
vault: BaseBeefyVault | ||||||
prices: BeefyPrices | ||||||
apys: BeefyApys | ||||||
apyBreakdown: BeefyApyBreakdown | ||||||
tvls: BeefyTvls | ||||||
t: TFunction | ||||||
}): AppTokenPositionDefinition => { | ||||||
|
@@ -74,7 +74,7 @@ const beefyAppTokenDefinition = ({ | |||||
vault.tokenAddress?.toLowerCase() ?? | ||||||
networkIdToNativeAssetAddress[networkId] | ||||||
const tvl = tvls[vault.id] | ||||||
const apy = apys[vault.id] | ||||||
const apy = apyBreakdown[vault.id].totalApy || 0 | ||||||
return { | ||||||
type: 'app-token-definition', | ||||||
networkId, | ||||||
|
@@ -132,6 +132,10 @@ const beefyAppTokenDefinition = ({ | |||||
manageUrl: `${BEEFY_VAULT_BASE_URL}${vault.id}`, | ||||||
contractCreatedAt: new Date(vault.createdAt * 1000).toISOString(), | ||||||
claimType: ClaimType.Rewards, | ||||||
dailyYieldRatePercentage: getDailyYieldRatePercentage( | ||||||
apyBreakdown[vault.id], | ||||||
vault, | ||||||
), | ||||||
}, | ||||||
availableShortcutIds: ['deposit', 'withdraw', 'swap-deposit'], | ||||||
shortcutTriggerArgs: ({ tokensByTokenId }) => { | ||||||
|
@@ -160,6 +164,63 @@ const beefyAppTokenDefinition = ({ | |||||
} | ||||||
} | ||||||
|
||||||
// Based on https://github.com/beefyfinance/beefy-v2/blob/4413697f3d3cb5e090d8bb6958b621a673f0d739/src/features/data/actions/apy.ts#L46 | ||||||
function getDailyYieldRatePercentage( | ||||||
apyBreakdown: BeefyApyBreakdown[string], | ||||||
vault: BaseBeefyVault, | ||||||
) { | ||||||
// https://github.com/beefyfinance/beefy-v2/blob/4413697f3d3cb5e090d8bb6958b621a673f0d739/src/helpers/apy.ts#L103 | ||||||
const components = [ | ||||||
'vaultApr', | ||||||
'clmApr', | ||||||
'tradingApr', | ||||||
'merklApr', | ||||||
'stellaSwapApr', | ||||||
'liquidStakingApr', | ||||||
'composablePoolApr', | ||||||
'rewardPoolApr', | ||||||
'rewardPoolTradingApr', | ||||||
] | ||||||
|
||||||
let totalDaily = 0 | ||||||
// Calculate the total daily apy from components | ||||||
if (Object.keys(apyBreakdown).some((key) => components.includes(key))) { | ||||||
for (const component of components) { | ||||||
const apr = apyBreakdown[component] | ||||||
if (apr && !isNaN(Number(apr))) { | ||||||
totalDaily += apr / 365 | ||||||
} | ||||||
} | ||||||
} else { | ||||||
// "uncompound" apy to get daily apr | ||||||
totalDaily = _yearlyToDaily(apyBreakdown.totalApy) | ||||||
} | ||||||
|
||||||
// At some point the Beefy team decided to change this 'rewardPoolApr' on this specific vault type to be a soft-boost | ||||||
// instead of being calculated as regular APR, so to be backwards compatible they subtract it from the total daily APR | ||||||
if (vault.type === 'gov' && vault.subType === 'cowcentrated') { | ||||||
// anything in 'rewardPoolApr' (i.e. not in 'rewardPoolTradingApr') is considered a soft-boost on the pool | ||||||
// and is should not be counted towards the daily yield rate | ||||||
const additionalApr = apyBreakdown.rewardPoolApr || 0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you did the opposite change 😄 |
||||||
totalDaily -= additionalApr / 365 | ||||||
} | ||||||
|
||||||
// TODO: handle merkl campaigns which are off-chain rewards | ||||||
// https://github.com/beefyfinance/beefy-v2/blob/4413697f3d3cb5e090d8bb6958b621a673f0d739/src/features/data/actions/apy.ts#L132 | ||||||
|
||||||
return totalDaily * 100 | ||||||
} | ||||||
|
||||||
const _yearlyToDaily = (apy: number) => { | ||||||
const dailyApy = Math.pow(10, Math.log10(apy + 1) / 365) - 1 | ||||||
|
||||||
if (isNaN(dailyApy)) { | ||||||
return 0 | ||||||
} | ||||||
|
||||||
return dailyApy | ||||||
} | ||||||
|
||||||
// CLM = Cowcentrated Liquidity Manager: https://docs.beefy.finance/beefy-products/clm | ||||||
interface ClmVaultBalanceInfo { | ||||||
token0: Address | ||||||
|
@@ -232,7 +293,7 @@ const beefyBaseVaultsPositions = async ({ | |||||
vaults, | ||||||
multicallAddress, | ||||||
prices, | ||||||
apys, | ||||||
apyBreakdown, | ||||||
tvls, | ||||||
t, | ||||||
}: { | ||||||
|
@@ -241,7 +302,7 @@ const beefyBaseVaultsPositions = async ({ | |||||
vaults: BaseBeefyVault[] | ||||||
multicallAddress: Address | ||||||
prices: BeefyPrices | ||||||
apys: BeefyApys | ||||||
apyBreakdown: BeefyApyBreakdown | ||||||
tvls: BeefyPrices | ||||||
t: TFunction | ||||||
}) => { | ||||||
|
@@ -310,7 +371,7 @@ const beefyBaseVaultsPositions = async ({ | |||||
networkId, | ||||||
vault, | ||||||
prices, | ||||||
apys, | ||||||
apyBreakdown, | ||||||
tvls, | ||||||
t, | ||||||
}) | ||||||
|
@@ -427,12 +488,13 @@ const hook: PositionsHook = { | |||||
return [] | ||||||
} | ||||||
|
||||||
const [{ vaults, govVaults }, prices, apys, tvls] = await Promise.all([ | ||||||
getBeefyVaults(networkId), | ||||||
getBeefyPrices(networkId), | ||||||
getApys(), | ||||||
getTvls(networkId), | ||||||
]) | ||||||
const [{ vaults, govVaults }, prices, apyBreakdown, tvls] = | ||||||
await Promise.all([ | ||||||
getBeefyVaults(networkId), | ||||||
getBeefyPrices(networkId), | ||||||
getApyBreakdown(), | ||||||
getTvls(networkId), | ||||||
]) | ||||||
|
||||||
return [ | ||||||
...(await beefyBaseVaultsPositions({ | ||||||
|
@@ -441,7 +503,7 @@ const hook: PositionsHook = { | |||||
vaults, | ||||||
multicallAddress, | ||||||
prices, | ||||||
apys, | ||||||
apyBreakdown, | ||||||
tvls, | ||||||
t, | ||||||
})), | ||||||
|
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vault.type itself can be
cowcentrated
right? could we document what a pool withtype=='gov'
andsubType=='cowncentrated'
is? Is thatOld gov pools
? if so can we make it explicit in the comment?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my comment (and the one about subtype values) was mainly because I was confused why something would have
type=gov
andsubtype=cowcentrated
when type can also be cowcentrated. And I also didn't find any vault like that in the vaults response for arbitrum. If this was just copied from beefy as is, probably fine to leave as is.