Cap USD price formatters at MAX_PRICE_E6 (was the stale 1e15) - #2498
Cap USD price formatters at MAX_PRICE_E6 (was the stale 1e15)#24980x-SquidSol wants to merge 1 commit into
Conversation
…1e15 formatUsd / formatUsdPriceE6 rejected only priceE6 > 1e15 ($1B), while the corrected on-chain price cap is MAX_PRICE_E6 = 1e12 ($1M). So a dust/garbage price between $1M and $1B (e.g. an uninitialized short liquidation price) rendered as a real dollar figure instead of "—". Both formatters are price-scale only (prices, spreads) — large USD amounts use separate local formatters — so aligning to MAX_PRICE_E6 is safe. - format: import MAX_PRICE_E6; use it as the absurd-value threshold in both formatUsd and formatUsdPriceE6 - add a regression test (value above $1M -> dash; at/below -> formatted) - update the existing boundary test to the corrected $1M cap (was asserting 1e15) Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
@0x-SquidSol is attempting to deploy a commit to the Khubair Nasir's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe formatter now uses the shared ChangesUSD price cap
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
dcccrypto
left a comment
There was a problem hiding this comment.
Correct — and the first PR in this batch whose tests actually bind the fix.
Suite: 2921 passed / 0 failed.
I checked the "price-scale only" claim exhaustively, because it's the one that bites
Lowering a display threshold from $1B to $1M turns any larger value into —, so
a single large-amount consumer would visibly break. Every call site:
Uses the lib/format bigint formatUsd — both price-scale:
| site | argument |
|---|---|
MarketStatsCard.tsx:141 |
absSpread (bigint price spread) |
| — | no others |
Every formatUsdPriceE6 site is an entry / mark / index / liquidation /
slippage-bound price. All price-scale.
Large USD amounts all go through different functions — six separate local
formatUsd(n: number) helpers, untouched by this change: stake/page.tsx:105
(TVL, stake, pool caps), ProtocolStatsBar.tsx:29 (24h volume, open interest),
LpPositionsPanel.tsx:11 (TVL, redeemable), StatsBar.tsx:6,
TradeHistory.tsx:29, plus the exported lib/market-meta.ts:41.
One that looks like a counter-example and isn't: app/trade/[slab]/layout.tsx:24
calls formatUsd(market.price) — but it imports from @/lib/market-meta, the
number version, not this one. Worth naming explicitly since a grep for
formatUsd( surfaces it right next to the real ones.
So the claim holds. No consumer regresses.
The cap now agrees with the on-chain guard, which is a stronger argument than the PR makes
MAX_PRICE_E6 isn't just "a smaller number" — it's the bound the rest of the
stack already treats as the validity edge:
lib/oraclePrice.ts:16 if (priceE6 > MAX_PRICE_E6) return 0n;
lib/live-market-state.ts:104 if (e6 > 0n && e6 < MAX_SANE_PRICE_E6) markPriceUsd = …A price above $1M is already discarded upstream, so the formatter was the only
layer still willing to render it. This isn't tightening a display policy — it's
removing a disagreement between the formatter and everything that feeds it. Worth
putting in the PR body; it's the reason this is safe rather than merely narrow.
Tests — first time in this run I have nothing to complain about
I reverted both thresholds to 1_000_000_000_000_000n (the bug fully restored):
| test | result |
|---|---|
format-price-cap.test.ts (new) |
1 of 2 fails |
format.test.ts (updated) |
1 fails |
Both catch it. And the edit to the existing test is a strengthening, not an
accommodation — it moves the boundary assertion to the new cap and adds a
formatUsd(1_000_000_000_001n) === "$—" case that didn't exist before. That's
the right way to touch an existing assertion: when a PR edits a test it didn't
write, the question is whether the test got weaker, and here it got sharper.
What
Lower
formatUsd/formatUsdPriceE6's absurd-value threshold from1e15($1B)to
MAX_PRICE_E6(1e12/ $1M), the corrected on-chain price cap.Why
The formatters rejected only
priceE6 > 1e15, so a dust/garbage price between $1Mand $1B (e.g. an uninitialized short liquidation price) rendered as a real dollar
figure instead of "—". Both formatters are price-scale only (prices, spreads);
large USD amounts use separate local
formatUsd(number)helpers, so lowering thecap is safe.
Changes
MAX_PRICE_E6and use it as the threshold in both functions.Testing
npx tsc --noEmit— clean.Notes
formatUsd/formatUsdPriceE6importers are all price-scale(a spread in MarketStatsCard; entry/mark/liq prices elsewhere) — no large-amount
display uses these.
Summary by CodeRabbit