Skip to content

ts-sdk: read the maintenance leverage from the markets API instead of deriving it - #238

Draft
claude[bot] wants to merge 2 commits into
mainfrom
markets-maintenance-margin
Draft

ts-sdk: read the maintenance leverage from the markets API instead of deriving it#238
claude[bot] wants to merge 2 commits into
mainfrom
markets-maintenance-margin

Conversation

@claude

@claude claude Bot commented Jul 8, 2026

Copy link
Copy Markdown

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 the maintenance_leverage multiplier from the markets API (same convention as max_leverage) and derives mm = 1e18 / maintenance_leverage with the same floor division as the existing imRate — staying exact for any per-market configuration — and falls back to im / 2 when the field is absent (older nodes), so behavior against existing deployments is unchanged.

How:

  • types/wire.ts: WireMarketStatic gains optional maintenance_leverage (a bare number, like max_leverage).
  • types/public.ts + codec/decode.ts: decoded onto Market as optional maintenanceLeverage.
  • sync/positions-live.ts: enrichPositions uses imRate(maintenanceLeverage) with the imr / 2n fallback; 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

claude added 2 commits July 8, 2026 15:36
…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
@claude claude Bot changed the title ts-sdk: read margin fractions from the markets API instead of deriving them ts-sdk: read the maintenance leverage from the markets API instead of deriving it Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant