Skip to content

Add btcli deriv: covered long/short derivative commands#1007

Open
unconst wants to merge 4 commits into
latent-to:mainfrom
unconst:feat/short-long-sandbox
Open

Add btcli deriv: covered long/short derivative commands#1007
unconst wants to merge 4 commits into
latent-to:mainfrom
unconst:feat/short-long-sandbox

Conversation

@unconst

@unconst unconst commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a new btcli deriv command group (alias d) for the covered continuous-unwind long/short derivatives in pallet-subtensor. It drives the real on-chain extrinsics and reads the DerivativesRuntimeApi — no simulation.

The economics in three terms: position input P (your capital / floor) + retained-proceeds buffer R (decays over time as carry) + a fixed liability Q (Alpha, short) / D (TAO, long) that you repay to close.

Commands

All take --network, wallet options, and --netuid; --side short|long selects the side. A tight core loop — easy for a human or an agent.

Info (read-only)

  • quote --netuid N --side ... -a P — preview an open: what you pay, retained buffer, what you owe to close, effective LTV, daily carry, est. close cost.
  • positions --side ... [--netuid N] [--ss58 CK] — your open positions (you paid / buffer / you owe / you'd get P+R / close cost / health).
  • market --netuid N --side ... — whether opens are allowed, base LTV, capacity left, min input, current carry.

Lifecycle (extrinsics)

  • open --netuid N --side ... -a Popen_short / open_long (shows the quote, then confirms).
  • topup --netuid N --side ... -a Xtop_up_short / top_up_long (extends the carry buffer).
  • close --netuid N --side ... [-f frac]close_short / close_long (partial via --fraction, default full).

--json on every command for agent use.

Implementation

  • bittensor_cli/src/commands/deriv/deriv.py — async impls: compose + sign_and_send_extrinsic for writes, query_runtime_api("DerivativesRuntimeApi", ...) for reads, lean rich tables. Per-side dispatch keeps short/long symmetric.
  • Wired into bittensor_cli/cli.py following the existing liquidity pattern.

Outputs are deliberately trimmed to what a trader needs to decide and act; full structured data is available via --json. The permissionless default_* keeper extrinsic is intentionally not surfaced in the CLI (niche, acts on other accounts).

Test plan

Verified end-to-end against a local subtensor dev network (derivatives enabled via sudo):

  • deriv market (short & long)
  • deriv quote (short & long)
  • deriv open (short & long)
  • deriv positions (short & long)
  • deriv topup
  • deriv close -f 0.5 then full close (short & long)

Requires the derivatives pallet on the target chain with the side enabled (adminUtils.sudo_set_shorts_enabled / sudo_set_longs_enabled).

Adds a deterministic, in-process sandbox (no chain, no wallet) for the
covered continuous-unwind long/short model. Implements the spec's
closed-form math against a single shared CPMM pool so users can preview,
open, top-up, advance time on, and close short/long positions and see how
carry, break-even, close cost, and pool price interact.

New command group `deriv` (alias `d`):
  quote / open / topup / close / advance / status / reset

Pure engine in src/commands/deriv/sim.py (no btcli deps); rich render
layer in deriv.py; wired into cli.py.

Co-authored-by: Cursor <cursoragent@cursor.com>
Replaces the local simulator with real on-chain commands that drive the
pallet-subtensor covered continuous-unwind extrinsics and read the
DerivativesRuntimeApi.

Commands (`deriv`, alias `d`), all over --network / wallet / --netuid:
  quote      preview open (quote_open_short/long)
  positions  list your positions (get_*_positions / get_*_position)
  market     per-subnet market state (get_subnet_*_state)
  open       open_short / open_long
  topup      top_up_short / top_up_long
  close      close_short / close_long (partial via --fraction)
  default    permissionless default_short / default_long

Removes the sandbox sim engine.

Co-authored-by: Cursor <cursoragent@cursor.com>
@unconst unconst changed the title Add btcli deriv local long/short sandbox simulator Add btcli deriv: covered long/short derivative commands Jun 18, 2026
unconst and others added 2 commits June 18, 2026 12:33
- Drop `default` from the CLI surface: it's a permissionless keeper op on
  other accounts' dusted positions, not something a user runs to manage
  their own positions. The on-chain extrinsic remains available.
- Trim `positions` from 9 (truncating) columns to 7 plain-language ones:
  you paid P / buffer R / you owe / you'd get P+R / close cost / health.
- Trim `market` to the 5 fields you need to decide to open: open allowed,
  base LTV, capacity left, min input, current carry.
- Trim `quote` to plain terms (you pay / buffer / you owe / LTV / carry /
  close cost); drop internal gross-collateral and escrow rows.

Core surface: quote, positions, market, open, topup, close.

Co-authored-by: Cursor <cursoragent@cursor.com>
`open` now takes the hotkey the same way as other btcli commands
(`--hotkey` / `--hotkey-ss58` / `--wallet.hotkey`, plus a `--validator`
alias). When an SS58 is given, only the coldkey is validated — so the
liability hotkey need not be your own or registered to you. You can stake
to a validator and have the position's Alpha/ TAO liability held (and
repaid at close) on that validator hotkey.

Co-authored-by: Cursor <cursoragent@cursor.com>

@basfroman basfroman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We require signed commits with GPG key
Also, pls rebase this pr to the staging branch other than main

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.

2 participants