-
Notifications
You must be signed in to change notification settings - Fork 88
feat: Yield+ domain model and pure utilities #5504
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
Closed
therealemjy
wants to merge
1
commit into
feat/yield-plus-pr03-yieldplus-infra
from
feat/yield-plus-pr04-domain-utils
Closed
Changes from all commits
Commits
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 hidden or 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,5 @@ | ||
| --- | ||
| "@venusprotocol/evm": patch | ||
| --- | ||
|
|
||
| add Yield+ domain models and pure utilities for position formatting, leverage calculations, and chart candle conversion |
This file contains hidden or 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,184 @@ | ||
| import type { YieldPlusPosition } from 'types'; | ||
| import { convertTokensToMantissa } from 'utilities'; | ||
| import { formatToYieldPlusPosition } from 'utilities/formatToYieldPlusPosition'; | ||
| import fakeAddress, { altAddress } from './address'; | ||
| import { poolData } from './pools'; | ||
|
|
||
| type ApiYieldPlusPosition = { | ||
| pnl: { | ||
| realizedPnlShortAssetMantissa: string; | ||
| realizedPnlUsd: string; | ||
| unrealizedPnlShortAssetMantissa: string; | ||
| unrealizedPnlUsd: string; | ||
| unrealizedPnlPercentage: string; | ||
| entryRatio: string; | ||
| currentRatio: string; | ||
| closeEventsWithPnlData: unknown[]; | ||
| totalShortOpenedMantissa: string; | ||
| totalLongReceivedMantissa: string; | ||
| }; | ||
| positionAccountAddress: string; | ||
| accountAddress: string; | ||
| longVTokenAddress: string; | ||
| shortVTokenAddress: string; | ||
| chainId: string; | ||
| isActive: boolean; | ||
| cycleId: string; | ||
| dsaVTokenAddress: string; | ||
| effectiveLeverageRatio: string; | ||
| capitalUtilization: { | ||
| suppliedPrincipalMantissa: string; | ||
| capitalUtilizedMantissa: string; | ||
| withdrawableCapitalMantissa: string; | ||
| }; | ||
| }; | ||
|
|
||
| const pool = poolData[0]; | ||
| const xvsAsset = pool.assets[0]; | ||
| const usdcAsset = pool.assets[1]; | ||
| const usdtAsset = pool.assets[2]; | ||
| const busdAsset = pool.assets[3]; | ||
|
|
||
| export const apiYieldPlusPositions: ApiYieldPlusPosition[] = [ | ||
| { | ||
| pnl: { | ||
| realizedPnlShortAssetMantissa: '0', | ||
| realizedPnlUsd: '0', | ||
| unrealizedPnlShortAssetMantissa: '0', | ||
| unrealizedPnlUsd: '0', | ||
| unrealizedPnlPercentage: '0', | ||
| entryRatio: '1', | ||
| currentRatio: '1', | ||
| closeEventsWithPnlData: [], | ||
| totalShortOpenedMantissa: '0', | ||
| totalLongReceivedMantissa: '0', | ||
| }, | ||
| positionAccountAddress: fakeAddress, | ||
| accountAddress: fakeAddress, | ||
| longVTokenAddress: usdtAsset.vToken.address, | ||
| shortVTokenAddress: busdAsset.vToken.address, | ||
| chainId: String(busdAsset.vToken.underlyingToken.chainId), | ||
| isActive: true, | ||
| cycleId: '1', | ||
| dsaVTokenAddress: xvsAsset.vToken.address, | ||
| effectiveLeverageRatio: '2', | ||
| capitalUtilization: { | ||
| suppliedPrincipalMantissa: convertTokensToMantissa({ | ||
| value: xvsAsset.userSupplyBalanceTokens, | ||
| token: xvsAsset.vToken.underlyingToken, | ||
| }).toFixed(), | ||
| capitalUtilizedMantissa: '0', | ||
| withdrawableCapitalMantissa: '0', | ||
| }, | ||
| }, | ||
| { | ||
| positionAccountAddress: altAddress, | ||
| accountAddress: altAddress, | ||
| dsaVTokenAddress: usdcAsset.vToken.address, | ||
| longVTokenAddress: usdtAsset.vToken.address, | ||
| shortVTokenAddress: busdAsset.vToken.address, | ||
| chainId: String(busdAsset.vToken.underlyingToken.chainId), | ||
| isActive: true, | ||
| cycleId: '1', | ||
| effectiveLeverageRatio: '3', | ||
| pnl: { | ||
| realizedPnlShortAssetMantissa: '0', | ||
| realizedPnlUsd: '0', | ||
| unrealizedPnlShortAssetMantissa: '0', | ||
| unrealizedPnlUsd: '1.5', | ||
| unrealizedPnlPercentage: '1.2', | ||
| entryRatio: '1.1', | ||
| currentRatio: '1.1', | ||
| closeEventsWithPnlData: [], | ||
| totalShortOpenedMantissa: '0', | ||
| totalLongReceivedMantissa: '0', | ||
| }, | ||
| capitalUtilization: { | ||
| suppliedPrincipalMantissa: convertTokensToMantissa({ | ||
| value: usdcAsset.userSupplyBalanceTokens, | ||
| token: usdcAsset.vToken.underlyingToken, | ||
| }).toFixed(), | ||
| capitalUtilizedMantissa: '0', | ||
| withdrawableCapitalMantissa: '0', | ||
| }, | ||
| }, | ||
| { | ||
| positionAccountAddress: altAddress, | ||
| accountAddress: altAddress, | ||
| dsaVTokenAddress: usdcAsset.vToken.address, | ||
| longVTokenAddress: usdcAsset.vToken.address, | ||
| shortVTokenAddress: usdtAsset.vToken.address, | ||
| chainId: String(usdtAsset.vToken.underlyingToken.chainId), | ||
| isActive: true, | ||
| cycleId: '1', | ||
| effectiveLeverageRatio: '1.5', | ||
| pnl: { | ||
| realizedPnlShortAssetMantissa: '0', | ||
| realizedPnlUsd: '0', | ||
| unrealizedPnlShortAssetMantissa: '0', | ||
| unrealizedPnlUsd: '-0.5', | ||
| unrealizedPnlPercentage: '-0.4', | ||
| entryRatio: '0.95', | ||
| currentRatio: '0.95', | ||
| closeEventsWithPnlData: [], | ||
| totalShortOpenedMantissa: '0', | ||
| totalLongReceivedMantissa: '0', | ||
| }, | ||
| capitalUtilization: { | ||
| suppliedPrincipalMantissa: convertTokensToMantissa({ | ||
| value: usdcAsset.userSupplyBalanceTokens, | ||
| token: usdcAsset.vToken.underlyingToken, | ||
| }).toFixed(), | ||
| capitalUtilizedMantissa: '0', | ||
| withdrawableCapitalMantissa: '0', | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| export const yieldPlusPositions: YieldPlusPosition[] = [ | ||
| formatToYieldPlusPosition({ | ||
| pool, | ||
| chainId: busdAsset.vToken.underlyingToken.chainId, | ||
| positionAccountAddress: fakeAddress, | ||
| dsaVTokenAddress: xvsAsset.vToken.address, | ||
| dsaBalanceMantissa: convertTokensToMantissa({ | ||
| value: xvsAsset.userSupplyBalanceTokens, | ||
| token: xvsAsset.vToken.underlyingToken, | ||
| }), | ||
| longVTokenAddress: usdtAsset.vToken.address, | ||
| shortVTokenAddress: busdAsset.vToken.address, | ||
| leverageFactor: 2, | ||
| unrealizedPnlCents: 0, | ||
| unrealizedPnlPercentage: 0, | ||
| })!, | ||
| formatToYieldPlusPosition({ | ||
| pool, | ||
| chainId: busdAsset.vToken.underlyingToken.chainId, | ||
| positionAccountAddress: altAddress, | ||
| dsaVTokenAddress: usdcAsset.vToken.address, | ||
| dsaBalanceMantissa: convertTokensToMantissa({ | ||
| value: usdcAsset.userSupplyBalanceTokens, | ||
| token: usdcAsset.vToken.underlyingToken, | ||
| }), | ||
| longVTokenAddress: usdtAsset.vToken.address, | ||
| shortVTokenAddress: busdAsset.vToken.address, | ||
| leverageFactor: 3, | ||
| unrealizedPnlCents: 150, | ||
| unrealizedPnlPercentage: 1.2, | ||
| })!, | ||
| formatToYieldPlusPosition({ | ||
| pool, | ||
| chainId: usdtAsset.vToken.underlyingToken.chainId, | ||
| positionAccountAddress: altAddress, | ||
| dsaVTokenAddress: usdcAsset.vToken.address, | ||
| dsaBalanceMantissa: convertTokensToMantissa({ | ||
| value: usdcAsset.userSupplyBalanceTokens, | ||
| token: usdcAsset.vToken.underlyingToken, | ||
| }), | ||
| longVTokenAddress: usdcAsset.vToken.address, | ||
| shortVTokenAddress: usdtAsset.vToken.address, | ||
| leverageFactor: 1.5, | ||
| unrealizedPnlCents: -50, | ||
| unrealizedPnlPercentage: -0.4, | ||
| })!, | ||
| ]; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
39 changes: 39 additions & 0 deletions
39
apps/evm/src/pages/YieldPlus/calculateMaxLeverageFactor/__tests__/index.spec.ts
This file contains hidden or 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,39 @@ | ||
| import { calculateMaxLeverageFactor } from '..'; | ||
|
|
||
| describe('calculateMaxLeverageFactor', () => { | ||
| it('returns the leverage factor derived from the collateral factors and close tolerance', () => { | ||
| const result = calculateMaxLeverageFactor({ | ||
| dsaTokenCollateralFactor: 0.5, | ||
| longTokenCollateralFactor: 0.8, | ||
| proportionalCloseTolerancePercentage: 2, | ||
| }); | ||
|
|
||
| expect(result).toMatchInlineSnapshot('2.31'); | ||
| }); | ||
|
|
||
| it('rounds down to two decimal places', () => { | ||
| const result = calculateMaxLeverageFactor({ | ||
| dsaTokenCollateralFactor: 0.1, | ||
| longTokenCollateralFactor: 0.7, | ||
| proportionalCloseTolerancePercentage: 0, | ||
| }); | ||
|
|
||
| expect(result).toMatchInlineSnapshot('0.33'); | ||
| }); | ||
|
|
||
| it('reduces the maximum leverage when proportional close tolerance increases', () => { | ||
| const lowToleranceResult = calculateMaxLeverageFactor({ | ||
| dsaTokenCollateralFactor: 0.5, | ||
| longTokenCollateralFactor: 0.8, | ||
| proportionalCloseTolerancePercentage: 2, | ||
| }); | ||
| const highToleranceResult = calculateMaxLeverageFactor({ | ||
| dsaTokenCollateralFactor: 0.5, | ||
| longTokenCollateralFactor: 0.8, | ||
| proportionalCloseTolerancePercentage: 10, | ||
| }); | ||
|
|
||
| expect(lowToleranceResult).toMatchInlineSnapshot('2.31'); | ||
| expect(highToleranceResult).toMatchInlineSnapshot('1.78'); | ||
| }); | ||
| }); |
19 changes: 19 additions & 0 deletions
19
apps/evm/src/pages/YieldPlus/calculateMaxLeverageFactor/index.ts
This file contains hidden or 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,19 @@ | ||
| import BigNumber from 'bignumber.js'; | ||
|
|
||
| export interface CalculateMaxLeverageFactorInput { | ||
| dsaTokenCollateralFactor: number; | ||
| longTokenCollateralFactor: number; | ||
| proportionalCloseTolerancePercentage: number; | ||
| } | ||
|
|
||
| export const calculateMaxLeverageFactor = ({ | ||
| dsaTokenCollateralFactor, | ||
| longTokenCollateralFactor, | ||
| proportionalCloseTolerancePercentage, | ||
| }: CalculateMaxLeverageFactorInput) => | ||
| new BigNumber( | ||
| dsaTokenCollateralFactor / | ||
| (1 - longTokenCollateralFactor * (1 - proportionalCloseTolerancePercentage / 100)), | ||
| ) | ||
| .dp(2, BigNumber.ROUND_DOWN) | ||
| .toNumber(); | ||
20 changes: 20 additions & 0 deletions
20
apps/evm/src/pages/YieldPlus/formatLiquidationPriceTokensToReadableValue/index.tsx
This file contains hidden or 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,20 @@ | ||
| import type { TFunction } from 'i18next'; | ||
|
|
||
| import { type FormatTokensToReadableValueInput, formatTokensToReadableValue } from 'utilities'; | ||
|
|
||
| export interface FormatLiquidationPriceTokensToReadableValueInput | ||
| extends FormatTokensToReadableValueInput { | ||
| t: TFunction<'translation', undefined>; | ||
| } | ||
|
|
||
| export const formatLiquidationPriceTokensToReadableValue = ( | ||
| _input: FormatLiquidationPriceTokensToReadableValueInput, | ||
| ) => { | ||
| const { t, ...input } = _input; | ||
|
|
||
| if (input.value?.isLessThanOrEqualTo(0)) { | ||
| return t('yieldPlus.operationForm.invalidLiquidationPrice'); | ||
| } | ||
|
|
||
| return formatTokensToReadableValue({ ...input, addSymbol: false }); | ||
| }; |
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
apps/evm/src/utilities/convertPercentageToBps/__tests__/index.spec.ts
This file contains hidden or 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,11 @@ | ||
| import { convertPercentageToBps } from '..'; | ||
|
|
||
| describe('convertPercentageToBps', () => { | ||
| it('converts whole percentages to bps', () => { | ||
| expect(convertPercentageToBps({ percentage: 50 })).toBe(5000n); | ||
| }); | ||
|
|
||
| it('converts decimal percentages to bps', () => { | ||
| expect(convertPercentageToBps({ percentage: 12.34 })).toBe(1234n); | ||
| }); | ||
| }); |
This file contains hidden or 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,4 @@ | ||
| import BigNumber from 'bignumber.js'; | ||
|
|
||
| export const convertPercentageToBps = ({ percentage }: { percentage: number }) => | ||
| BigInt(new BigNumber(percentage).multipliedBy(100).toFixed(0)); |
Oops, something went wrong.
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.
The denominator is computed entirely in native JS floating-point before being passed to
BigNumber. This discards BigNumber's precision guarantees — for deeply fractional inputs, the intermediate result can already be slightly off before rounding. Additionally, iflongTokenCollateralFactor === 1andproportionalCloseTolerancePercentage === 0, the denominator is exactly0, givingInfinity(which propagates through.dp(2).toNumber()asInfinity). Callers would need to guard against this.Prefer computing entirely in BigNumber: