Scale k-prefix hyperqliquid prices by 1000#5168
Conversation
📝 WalkthroughWalkthroughAdds source-slug lookup and per-asset scaling to Hyperliquid BBO timeseries: mapped slugs starting with "k" use factor 1000 to rescale bid/ask price and volume before mid and weighted-mid computations. ChangesHyperliquid BBO Scaling
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
lib/sanbase/hyperliquid/bbo/bbo_prices.ex (2)
88-93: ⚡ Quick winUse function-head pattern matching for
scale/2.This keeps the helper idiomatic and simpler.
Refactor
- defp scale(value, divisor) do - case value do - nil -> nil - _ -> value / divisor - end - end + defp scale(nil, _divisor), do: nil + defp scale(value, divisor), do: value / divisorAs per coding guidelines, "Use pattern matching in function heads for control flow".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/sanbase/hyperliquid/bbo/bbo_prices.ex` around lines 88 - 93, Replace the runtime case branch in scale/2 with function-head pattern matching: add a clause scale(nil, _divisor) that returns nil and a second clause scale(value, divisor) that returns value / divisor, keeping the exact semantics of the existing helper (function name: scale/2).
68-80: ⚡ Quick winMove this rationale to docs instead of inline comments.
Please place this explanation in
@moduledoc/@docrather than#comments.As per coding guidelines, "Avoid adding comments to code".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/sanbase/hyperliquid/bbo/bbo_prices.ex` around lines 68 - 80, Move the long inline comment about Hyperliquid "k" prefix handling out of the body comments and into the module or function documentation: add this rationale to the module `@moduledoc` (or the relevant `@doc` for the functions in the BboPrices module that perform the 1/1000 adjustment) and remove the inlined # comment block. Ensure the `@moduledoc/`@doc mentions the "k" prefix convention, the 1/1000 normalization rule, and why matching on name prefix is used (so readers and generated docs retain the explanation), keeping the code files free of large inline comments.lib/sanbase/project/source_slug_mapping.ex (1)
56-59: ⚡ Quick winAdd an
iexexample to this public function doc.The new public API is documented, but it still needs an example usage block.
Suggested doc update
`@doc` ~s""" Return the source slug (as known to `source`) for the project identified by `project_slug` (Sanbase slug), or `nil` if no mapping exists. + + ## Examples + iex> Sanbase.Project.SourceSlugMapping.get_source_slug("bitcoin", "hyperliquid") + "BTC" """As per coding guidelines, "Include examples in documentation for public functions".
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/sanbase/project/source_slug_mapping.ex` around lines 56 - 59, Add an iex doctest example to the public function's `@doc` for the SourceSlugMapping module: update the docstring for the function (likely source_slug/1) to include an "iex>" example that demonstrates calling SourceSlugMapping.source_slug("project_slug") and shows the expected return (a source slug string or nil) so the documentation contains a runnable usage example and covers both a successful mapping and a nil result.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@lib/sanbase/hyperliquid/bbo/bbo_prices.ex`:
- Around line 47-55: The current ClickhouseRepo.query_transform callback is
scaling both prices and volumes using divisor(slug), which incorrectly alters
book sizes; update the transform so only bid_price and ask_price are passed
through scale(divisor) and leave bid_volume and ask_volume unchanged (or apply
separate, explicit volume-normalization logic if needed); locate the transform
lambda used in ClickhouseRepo.query_transform and remove or replace the
scale(...) calls for bid_volume and ask_volume so only the price values are
scaled.
---
Nitpick comments:
In `@lib/sanbase/hyperliquid/bbo/bbo_prices.ex`:
- Around line 88-93: Replace the runtime case branch in scale/2 with
function-head pattern matching: add a clause scale(nil, _divisor) that returns
nil and a second clause scale(value, divisor) that returns value / divisor,
keeping the exact semantics of the existing helper (function name: scale/2).
- Around line 68-80: Move the long inline comment about Hyperliquid "k" prefix
handling out of the body comments and into the module or function documentation:
add this rationale to the module `@moduledoc` (or the relevant `@doc` for the
functions in the BboPrices module that perform the 1/1000 adjustment) and remove
the inlined # comment block. Ensure the `@moduledoc/`@doc mentions the "k" prefix
convention, the 1/1000 normalization rule, and why matching on name prefix is
used (so readers and generated docs retain the explanation), keeping the code
files free of large inline comments.
In `@lib/sanbase/project/source_slug_mapping.ex`:
- Around line 56-59: Add an iex doctest example to the public function's `@doc`
for the SourceSlugMapping module: update the docstring for the function (likely
source_slug/1) to include an "iex>" example that demonstrates calling
SourceSlugMapping.source_slug("project_slug") and shows the expected return (a
source slug string or nil) so the documentation contains a runnable usage
example and covers both a successful mapping and a nil result.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 59bcd0c1-90a4-4b18-b692-23192cab8488
📒 Files selected for processing (2)
lib/sanbase/hyperliquid/bbo/bbo_prices.exlib/sanbase/project/source_slug_mapping.ex
c49909a to
1c8b45b
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
lib/sanbase/hyperliquid/bbo/bbo_prices.ex (1)
68-90: ⚡ Quick winMove the long inline rationale out of code comments.
This block is valuable context, but it should live in tests/docs/PR notes rather than inline comments to stay consistent with repository standards.
Suggested minimal cleanup
- # Hyperliquid quotes some low-priced assets per 1000 underlying tokens. The - # only signal is a lowercase "k" prefix on the coin name (e.g. "kPEPE", - # "kSHIB", "kBONK", "kLUNC", "kFLOKI"); the convention is "k" for kilo, so - # one quoted contract represents 1000 of the underlying. Raw fields from - # Hyperliquid are therefore: - # - # * `px` — USD per 1000 underlying tokens - # * `sz` — number of contracts (each = 1000 underlying tokens) - # - # To expose values in native-token units while preserving notional - # (`price * volume`), price is divided by 1000 and volume is multiplied by - # 1000 for k-prefixed coins. - # - # This convention is NOT formally documented in the Hyperliquid docs - # (perpetuals info endpoint, tick/lot size, contract specifications). The - # live `POST /info {"type":"meta"}` response exposes `name`, `szDecimals`, - # `maxLeverage`, `marginTableId`, `marginMode` per asset — none of which is - # a price/size multiplier. The encoding was verified empirically against - # `POST /info {"type":"l2Book","coin":"kPEPE"}`: a quoted `px=0.003691` with - # `sz=202527` gives notional `0.003691 * 202527 ≈ $748`, which only matches - # realistic order sizes if `px` is per 1000 PEPE and `sz` is in contracts. - # Matching on the name prefix is the only option short of hardcoding a list, - # which would go stale as Hyperliquid adds new k-assets.As per coding guidelines: "Avoid adding comments to code."
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@lib/sanbase/hyperliquid/bbo/bbo_prices.ex` around lines 68 - 90, The long rationale about Hyperliquid "k" (k-prefixed coins) currently embedded as a multi-paragraph comment in bbo_prices.ex should be removed from the source and relocated to an external doc or test/PR note (e.g., repository docs/ or test fixtures) and replaced with a short one-line comment and a link/reference to that external file; specifically, find the multi-line comment explaining px/sz being per-1000 and the empirical verification and move that content to the chosen doc, leaving a concise comment in the module (the existing k-prefixed coins comment block) that points to the external doc for full rationale.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@lib/sanbase/hyperliquid/bbo/bbo_prices.ex`:
- Around line 68-90: The long rationale about Hyperliquid "k" (k-prefixed coins)
currently embedded as a multi-paragraph comment in bbo_prices.ex should be
removed from the source and relocated to an external doc or test/PR note (e.g.,
repository docs/ or test fixtures) and replaced with a short one-line comment
and a link/reference to that external file; specifically, find the multi-line
comment explaining px/sz being per-1000 and the empirical verification and move
that content to the chosen doc, leaving a concise comment in the module (the
existing k-prefixed coins comment block) that points to the external doc for
full rationale.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e0fad437-54ea-40a2-9878-f71a43138650
📒 Files selected for processing (3)
lib/sanbase/hyperliquid/bbo/bbo_prices.exlib/sanbase/project/source_slug_mapping.extest/sanbase/hyperliquid/bbo/bbo_prices_test.exs
🚧 Files skipped from review as they are similar to previous changes (1)
- lib/sanbase/project/source_slug_mapping.ex
Changes
Ticket
Checklist:
Summary by CodeRabbit
New Features
Tests