-
Notifications
You must be signed in to change notification settings - Fork 0
Fix missing base leg USD value calculation in neutral-trade drift util #73
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2099,9 +2099,12 @@ async function getTvl(api, driftVaultAddresses) { | |
| if (baseTokenMint) { | ||
| api.add(baseTokenMint, baseBalance); | ||
| } else { | ||
| // e.g. HYPE-PERP: no SPL spot mint; skip base leg to avoid "missing token" | ||
| // console.log(`No spot mint for perp market ${position.market_index} (${meta?.name}); skipping base leg`); | ||
| // TODO: find usd price and api.add(getTokenMintFromMarketIndex(0), usdValue); | ||
| const perpAccount = perpAccountMap[position.market_index]; | ||
| if (perpAccount && perpAccount.data) { | ||
| const price = perpAccount.data.readBigInt64LE(72); | ||
| const usdValue = (position.base_asset_amount * price) / BigInt(10 ** 9); | ||
| api.add(getTokenMintFromMarketIndex(0), usdValue); | ||
| } | ||
|
Comment on lines
+2102
to
+2107
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. Magic numbers with no derivation comments Unlike const CUMULATIVE_FUNDING_OFFSET = 8 + 48 + 32 + 256 + (16 * 15) + 24;the new code uses bare magic numbers Consider adding an explanatory constant and comment, similar to the existing pattern: // Drift v2 PerpMarket layout: 8 (discriminator) + 32 (AMM.oracle pubkey) = 40
const ORACLE_PRICE_OFFSET = 40; // AMM.historical_oracle_data.last_oracle_price
// base_asset_amount is in BASE_PRECISION (1e9), price is in PRICE_PRECISION (1e6)
// USD value in USDC raw (6 decimals) = (base/1e9) * (price/1e6) * 1e6 = base * price / 1e9
const price = perpAccount.data.readBigInt64LE(ORACLE_PRICE_OFFSET);
const usdValue = (position.base_asset_amount * price) / BigInt(10 ** 9); |
||
| } | ||
|
|
||
| const quoteTokenMint = getTokenMintFromMarketIndex(0); | ||
|
|
||
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.
Wrong buffer offset reads 5-minute TWAP, not live oracle price
Based on the Drift v2
PerpMarketaccount layout, theAMMstruct begins right after the 8-byte Anchor discriminator, with the following field order:AMM.oracle(Pubkey, 32 bytes)HistoricalOracleData.last_oracle_price← live oracle priceHistoricalOracleData.last_oracle_confHistoricalOracleData.last_oracle_delayHistoricalOracleData.last_oracle_price_twap(1-hour TWAP)HistoricalOracleData.last_oracle_price_twap_5min← what the code readsHistoricalOracleData.last_oracle_price_twap_tsReading at offset
72picks uplast_oracle_price_twap_5min(the 5-minute time-weighted average price), not the live oracle price described in the PR. During periods of high volatility — exactly the times when HYPE-PERP is most active — this TWAP can diverge significantly from the actual price and will cause the TVL to be measurably incorrect.The live oracle price is at offset
40. If a TWAP is intentionally preferred for stability, that should be documented with a comment.