ts-sdk: read the maintenance leverage from the markets API instead of deriving it - #238
Draft
claude[bot] wants to merge 2 commits into
Draft
ts-sdk: read the maintenance leverage from the markets API instead of deriving it#238claude[bot] wants to merge 2 commits into
claude[bot] wants to merge 2 commits into
Conversation
…g them The markets API now serves per-market initial_margin_fraction and maintenance_margin_fraction. Use the served values in the live position math instead of deriving im = 1e18/max_leverage and mm = im/2 locally, falling back to the old derivation when the fields are absent (older nodes). Document the new fields in the JSON-RPC OpenAPI spec. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PrmzWnipiBFymK5YyAKZX8
Review rework: the markets API serves the maintenance margin as a leverage multiplier (maintenance_leverage, same convention as max_leverage) instead of a 1e18-scaled fraction, and no longer sends a separate initial-margin field (it is just 1/max_leverage). Decode the multiplier and derive mm = 1e18 / maintenance_leverage with the same floor division as imRate, keeping the im/2 fallback for older nodes that omit the field. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PrmzWnipiBFymK5YyAKZX8
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Requested via Slack thread
Before, the SDK derived a market's maintenance margin locally from
max_leverage(im = 1e18 / max_leverage,mm = im / 2), so a node serving a maintenance margin different from half the initial margin would make the SDK's live withdrawable-cash math drift from the engine. After, the SDK reads themaintenance_leveragemultiplier from the markets API (same convention asmax_leverage) and derivesmm = 1e18 / maintenance_leveragewith the same floor division as the existingimRate— staying exact for any per-market configuration — and falls back toim / 2when the field is absent (older nodes), so behavior against existing deployments is unchanged.How:
types/wire.ts:WireMarketStaticgains optionalmaintenance_leverage(a bare number, likemax_leverage).types/public.ts+codec/decode.ts: decoded ontoMarketas optionalmaintenanceLeverage.sync/positions-live.ts:enrichPositionsusesimRate(maintenanceLeverage)with theimr / 2nfallback; the "derived from max_leverage" replication note is updated to describe the fallback.doc/api-reference/json-rpc/openapi.yaml: documents the new (optional) field on the markets response.Build + typecheck pass (
npm run build,npm run typecheck).🤖 Generated with Claude Code
https://claude.ai/code/session_01PrmzWnipiBFymK5YyAKZX8