-
Notifications
You must be signed in to change notification settings - Fork 7
add amount to reach util number #609
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
Draft
kanvgupta
wants to merge
2
commits into
development
Choose a base branch
from
feature/lite-200-show-the-current-amount-it-will-take-to-reach-utilisation
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,40 +1,85 @@ | ||
| export const getVaultSupplyApy = (vault: any): number => { | ||
| if (!vault) return 0 | ||
| if ('interestRates' in vault) return vault.interestRates.supplyAPY | ||
| if ('supplyApy1h' in vault) return vault.supplyApy1h ?? 0 | ||
| return 0 | ||
| const asBigint = (value: unknown): bigint | null => | ||
| typeof value === 'bigint' ? value : null | ||
|
|
||
| const asRecord = (value: unknown): Record<string, unknown> | null => | ||
| value && typeof value === 'object' ? value as Record<string, unknown> : null | ||
|
|
||
| export const getVaultSupplyApy = (vault: unknown): number => { | ||
| const source = asRecord(vault) | ||
| if (!source) return 0 | ||
|
|
||
| const interestRates = asRecord(source.interestRates) | ||
| if (typeof interestRates?.supplyAPY === 'number') return interestRates.supplyAPY | ||
| return typeof source.supplyApy1h === 'number' ? source.supplyApy1h : 0 | ||
| } | ||
|
|
||
| export const getVaultBorrowApy = (vault: any): number => { | ||
| if (!vault || !('interestRates' in vault)) return 0 | ||
| return vault.interestRates.borrowAPY | ||
| export const getVaultBorrowApy = (vault: unknown): number => { | ||
| const source = asRecord(vault) | ||
| const interestRates = asRecord(source?.interestRates) | ||
| return typeof interestRates?.borrowAPY === 'number' ? interestRates.borrowAPY : 0 | ||
| } | ||
|
|
||
| export const getVaultAvailableLiquidity = (vault: any): bigint => { | ||
| if (!vault) return 0n | ||
| if (typeof vault.totalAssets === 'bigint' && typeof vault.totalBorrowed === 'bigint') { | ||
| const totalAssets = vault.totalAssets as bigint | ||
| const totalBorrowed = vault.totalBorrowed as bigint | ||
| export const getVaultAvailableLiquidity = (vault: unknown): bigint => { | ||
| const source = asRecord(vault) | ||
| if (!source) return 0n | ||
|
|
||
| const totalAssets = asBigint(source.totalAssets) | ||
| const totalBorrowed = asBigint(source.totalBorrowed) | ||
| if (totalAssets !== null && totalBorrowed !== null) { | ||
| return totalAssets >= totalBorrowed ? totalAssets - totalBorrowed : 0n | ||
| } | ||
| if (typeof vault.availableLiquidity === 'bigint') return vault.availableLiquidity as bigint | ||
| const availableLiquidity = asBigint(source.availableLiquidity) | ||
| if (availableLiquidity !== null) return availableLiquidity | ||
| return 0n | ||
| } | ||
|
|
||
| export const getVaultUtilization = (vault: any): number => { | ||
| if (!vault) return 0 | ||
| const totalAssets = typeof vault.totalAssets === 'bigint' ? vault.totalAssets as bigint : 0n | ||
| const totalBorrowed = typeof vault.totalBorrowed === 'bigint' | ||
| ? vault.totalBorrowed as bigint | ||
| : typeof vault.borrow === 'bigint' | ||
| ? vault.borrow as bigint | ||
| : 0n | ||
| export const getVaultUtilization = (vault: unknown): number => { | ||
| const source = asRecord(vault) | ||
| if (!source) return 0 | ||
|
|
||
| const totalAssets = asBigint(source.totalAssets) ?? 0n | ||
| const totalBorrowed = asBigint(source.totalBorrowed) ?? asBigint(source.borrow) ?? 0n | ||
|
|
||
| if (totalAssets <= 0n || totalBorrowed <= 0n) return 0 | ||
|
|
||
| return Number(((Number(totalBorrowed) / Number(totalAssets)) * 100).toFixed(2)) | ||
| } | ||
|
|
||
| export type VaultUtilizationDelta = { | ||
| amount: bigint | ||
| direction: 'borrow' | 'repay' | 'none' | ||
| } | ||
|
|
||
| export const getVaultUtilizationDeltaActionLabel = (delta: VaultUtilizationDelta | null | undefined): string | null => { | ||
| if (!delta) return null | ||
| if (delta.direction === 'borrow') return 'Borrow' | ||
| if (delta.direction === 'repay') return 'Repay' | ||
| return 'No change' | ||
| } | ||
|
|
||
| export const getVaultUtilizationDelta = (vault: unknown, targetUtilizationPercent: number): VaultUtilizationDelta | null => { | ||
| const source = asRecord(vault) | ||
| if (!source || !Number.isFinite(targetUtilizationPercent)) return null | ||
|
|
||
| const totalAssets = asBigint(source.totalAssets) ?? 0n | ||
| const totalBorrowed = asBigint(source.totalBorrowed) ?? asBigint(source.borrow) ?? 0n | ||
|
|
||
| if (totalAssets <= 0n) return null | ||
|
|
||
| const clampedPercent = Math.min(100, Math.max(0, targetUtilizationPercent)) | ||
| const percentScale = 10_000n | ||
| const utilizationUnits = BigInt(Math.round(clampedPercent * Number(percentScale))) | ||
| const targetBorrowed = totalAssets * utilizationUnits / (100n * percentScale) | ||
|
|
||
| if (targetBorrowed > totalBorrowed) { | ||
| return { amount: targetBorrowed - totalBorrowed, direction: 'borrow' } | ||
| } | ||
| if (targetBorrowed < totalBorrowed) { | ||
| return { amount: totalBorrowed - targetBorrowed, direction: 'repay' } | ||
| } | ||
| return { amount: 0n, direction: 'none' } | ||
| } | ||
|
|
||
| export const formatMarketAvailability = (count: number) => { | ||
| return count ? `Yes in ${count} ${count === 1 ? 'market' : 'markets'}` : 'No' | ||
| } | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Guard APY reads against
NaN/Infinityvalues.typeof x === 'number'still accepts non-finite numbers. Returning those can propagate invalid values into chart/UI math. PreferNumber.isFinite(...)in both APY getters.Proposed patch
export const getVaultSupplyApy = (vault: unknown): number => { const source = asRecord(vault) if (!source) return 0 const interestRates = asRecord(source.interestRates) - if (typeof interestRates?.supplyAPY === 'number') return interestRates.supplyAPY - return typeof source.supplyApy1h === 'number' ? source.supplyApy1h : 0 + if (Number.isFinite(interestRates?.supplyAPY)) return interestRates!.supplyAPY as number + return Number.isFinite(source.supplyApy1h) ? source.supplyApy1h as number : 0 } @@ export const getVaultBorrowApy = (vault: unknown): number => { const source = asRecord(vault) const interestRates = asRecord(source?.interestRates) - return typeof interestRates?.borrowAPY === 'number' ? interestRates.borrowAPY : 0 + return Number.isFinite(interestRates?.borrowAPY) ? interestRates!.borrowAPY as number : 0 }Also applies to: 19-19
🤖 Prompt for AI Agents