asyncapi: 3.0.0

info:
  title: Sugar Rush Streaming API
  version: 1.1.0
  license:
    name: MIT
  description: |
    The Sugar Rush exchange speaks one WebSocket. Streaming market data, your
    account, and order submission all travel over a single connection: logical
    channels are multiplexed with an in-band `subscribe` op, and writes go over
    a `submit` op alongside the reads.

    This document is the machine-readable contract. It is served as
    `application/yaml` at the URL the connection's `welcome` frame advertises, so
    an agent can fetch it and drive the API autonomously. Humans should start at
    the docs site.

    Framing:
      - Client control messages are tagged by `op` (subscribe / unsubscribe /
        submit / hello / usage).
      - EVERY server message is tagged by `event` — the single discriminator a
        client demuxes on. Control events: `welcome` / `subscribed` /
        `submitted` / `error` / `usage`. Data frames carry `event: "data"` plus
        `{ channel, type, data }`, where `type` is `snapshot` (a full state,
        one object) or `update` (a delta). Live updates are coalesced and
        delivered as a JSON ARRAY of data frames.

    Prices:
      - On writes, a price is a human decimal string (e.g. "2.15").
      - On reads, a raw price field is a u128 binary fixed-point value with 60
        fractional bits, as a decimal string (`value / 2^60` is the price). The
        `welcome` frame advertises this in `priceEncoding`, and every raw price
        travels with a pre-rendered human decimal twin: a `*Decimal` sibling
        field, or the third element of an orderbook `[price, amount,
        priceDecimal]` level.

    Timestamps:
      - Every wire timestamp is unix MILLISECONDS (the auth `nonce` included).
        Candle `timestamp` is the bucket's start, in unix ms. The only
        exception is `stats.tps.currentSecond`, which labels a one-second
        counting bucket and is documented on its schema.

defaultContentType: application/json

servers:
  preview:
    host: api.sugar.rush.preview.sundae.fi
    pathname: /ws
    protocol: wss
    description: Preview testnet.

channels:
  connection:
    address: /ws
    title: The WebSocket connection (control plane)
    description: |
      The single socket. On connect the server sends `welcome`. The client then
      multiplexes logical channels over this connection with `subscribe` /
      `unsubscribe`, and submits writes with `submit`. All logical stream
      channels below are carried over this same connection.
    messages:
      welcome:
        $ref: '#/components/messages/Welcome'
      hello:
        $ref: '#/components/messages/Hello'
      subscribe:
        $ref: '#/components/messages/Subscribe'
      unsubscribe:
        $ref: '#/components/messages/Unsubscribe'
      submit:
        $ref: '#/components/messages/Submit'
      usageRequest:
        $ref: '#/components/messages/UsageRequest'
      subscribed:
        $ref: '#/components/messages/Subscribed'
      submitted:
        $ref: '#/components/messages/Submitted'
      error:
        $ref: '#/components/messages/ErrorEvent'
      usage:
        $ref: '#/components/messages/Usage'

  orderbook:
    address: 'orderbook:{market}'
    title: L2 orderbook (public)
    description: |
      Full book on subscribe (`snapshot`), then merged per-price-level deltas
      (`update`, one frame per market per flush window; a level with `0` on
      both sides was removed). Frames are seq-chained for gap detection: the
      snapshot carries `seq`, and each update carries `seq` plus `prevSeq` —
      the `seq` of the previous frame on this subscription. A client that
      receives an update whose `prevSeq` does not equal the last `seq` it
      applied has missed a frame and must resubscribe for a fresh snapshot.
      An update whose `seq` is not greater than the snapshot's `seq` is already
      reflected in the snapshot and can be dropped.
    parameters:
      market:
        $ref: '#/components/parameters/market'
    messages:
      snapshot:
        $ref: '#/components/messages/OrderbookSnapshot'
      update:
        $ref: '#/components/messages/OrderbookUpdate'

  ticker:
    address: 'ticker:{market}'
    title: Ticker — last price, best bid/ask, 24h stats (public)
    parameters:
      market:
        $ref: '#/components/parameters/market'
    messages:
      ticker:
        $ref: '#/components/messages/TickerData'

  candles:
    address: 'candles:{market}:{interval}'
    title: OHLCV candles (public)
    parameters:
      market:
        $ref: '#/components/parameters/market'
      interval:
        $ref: '#/components/parameters/interval'
    messages:
      snapshot:
        $ref: '#/components/messages/CandlesSnapshot'
      update:
        $ref: '#/components/messages/CandleUpdate'

  markets:
    address: markets
    title: Instrument directory (public)
    messages:
      snapshot:
        $ref: '#/components/messages/MarketsSnapshot'

  stats:
    address: stats
    title: Global health + liveness heartbeat (public)
    messages:
      stats:
        $ref: '#/components/messages/StatsData'

  trades:
    address: 'trades:{market}'
    title: Trade tape (public, RESERVED)
    description: |
      Reserved. The channel resolves and can be subscribed, but does not yet
      emit data — a trade tape requires the ledger to emit true two-sided
      executions. The shape is not yet frozen.
    parameters:
      market:
        $ref: '#/components/parameters/market'
    messages: {}

  assets:
    address: assets
    title: Asset directory (public, RESERVED)
    description: |
      Reserved. Resolves but does not yet emit data. Until it ships, per-asset
      metadata (canonical id, alias, decimals) is served inside each `markets`
      entry's `base` / `quote` objects.
    messages: {}

  orders:
    address: orders
    title: Your orders (private — auth required)
    messages:
      snapshot:
        $ref: '#/components/messages/OrdersSnapshot'
      update:
        $ref: '#/components/messages/OrderUpdate'

  account:
    address: account
    title: Your balances + nonce (private — auth required)
    messages:
      snapshot:
        $ref: '#/components/messages/AccountData'

  deposits:
    address: deposits
    title: Your deposit status (private — auth required)
    messages:
      snapshot:
        $ref: '#/components/messages/DepositsSnapshot'
      update:
        $ref: '#/components/messages/DepositUpdate'

operations:
  onWelcome:
    action: receive
    channel:
      $ref: '#/channels/connection'
    summary: The self-describing welcome frame, sent once on connect.
    messages:
      - $ref: '#/channels/connection/messages/welcome'

  hello:
    action: send
    channel:
      $ref: '#/channels/connection'
    summary: Optionally select a protocol version. Without it, the oldest is assumed.
    messages:
      - $ref: '#/channels/connection/messages/hello'

  subscribe:
    action: send
    channel:
      $ref: '#/channels/connection'
    summary: Subscribe to one or more channels. Private channels require `auth`.
    security:
      - $ref: '#/components/securitySchemes/cip8Auth'
    messages:
      - $ref: '#/channels/connection/messages/subscribe'
    reply:
      channel:
        $ref: '#/channels/connection'
      messages:
        - $ref: '#/channels/connection/messages/subscribed'
        - $ref: '#/channels/connection/messages/error'

  unsubscribe:
    action: send
    channel:
      $ref: '#/channels/connection'
    summary: Stop receiving one or more channels.
    messages:
      - $ref: '#/channels/connection/messages/unsubscribe'

  submit:
    action: send
    channel:
      $ref: '#/channels/connection'
    summary: |
      Submit a request to the head over the socket — the streaming twin of
      `POST /head/requests`. `request` is forwarded verbatim; its `l2Payload` is
      the COSE-signed, encrypted transaction envelope, so the edge never sees the
      order before it matches. One op covers every write; the SDK layers
      `createOrder`/`cancelOrder` on top.
    messages:
      - $ref: '#/channels/connection/messages/submit'
    reply:
      channel:
        $ref: '#/channels/connection'
      messages:
        - $ref: '#/channels/connection/messages/submitted'
        - $ref: '#/channels/connection/messages/error'

  usage:
    action: send
    channel:
      $ref: '#/channels/connection'
    summary: Ask for current rate-limit standing.
    messages:
      - $ref: '#/channels/connection/messages/usageRequest'
    reply:
      channel:
        $ref: '#/channels/connection'
      messages:
        - $ref: '#/channels/connection/messages/usage'

  receiveOrderbook:
    action: receive
    channel:
      $ref: '#/channels/orderbook'
    summary: |
      Full book on subscribe, then one merged delta per flush window with the
      changed levels (`0` amount on both sides = level removed), seq-chained
      for gap detection.
    messages:
      - $ref: '#/channels/orderbook/messages/snapshot'
      - $ref: '#/channels/orderbook/messages/update'

  receiveTicker:
    action: receive
    channel:
      $ref: '#/channels/ticker'
    summary: Ticker on subscribe, re-derived on each relevant candle/depth change.
    messages:
      - $ref: '#/channels/ticker/messages/ticker'

  receiveCandles:
    action: receive
    channel:
      $ref: '#/channels/candles'
    summary: Recent candles on subscribe, then one candle per change.
    messages:
      - $ref: '#/channels/candles/messages/snapshot'
      - $ref: '#/channels/candles/messages/update'

  receiveMarkets:
    action: receive
    channel:
      $ref: '#/channels/markets'
    messages:
      - $ref: '#/channels/markets/messages/snapshot'

  receiveStats:
    action: receive
    channel:
      $ref: '#/channels/stats'
    messages:
      - $ref: '#/channels/stats/messages/stats'

  receiveOrders:
    action: receive
    channel:
      $ref: '#/channels/orders'
    security:
      - $ref: '#/components/securitySchemes/cip8Auth'
    messages:
      - $ref: '#/channels/orders/messages/snapshot'
      - $ref: '#/channels/orders/messages/update'

  receiveAccount:
    action: receive
    channel:
      $ref: '#/channels/account'
    security:
      - $ref: '#/components/securitySchemes/cip8Auth'
    messages:
      - $ref: '#/channels/account/messages/snapshot'

  receiveDeposits:
    action: receive
    channel:
      $ref: '#/channels/deposits'
    security:
      - $ref: '#/components/securitySchemes/cip8Auth'
    messages:
      - $ref: '#/channels/deposits/messages/snapshot'
      - $ref: '#/channels/deposits/messages/update'

components:
  securitySchemes:
    cip8Auth:
      type: userPassword
      description: |
        Private channels carry an `auth` object on the `subscribe`: a CIP-8 (COSE)
        signature over `SUGAR-RUSH-WS-AUTH-V1:<accountHex>:<nonce>`, from the
        account's own key or its registered on-ledger delegate. See the `Auth`
        schema.

  parameters:
    market:
      description: A market's canonical id or its symbol (e.g. `DARK-VAN`).
    interval:
      description: Candle interval — one of `1s`, `1m`, `1h`, `1d`.

  messages:
    Welcome:
      name: welcome
      title: welcome
      summary: Sent once on connect; self-describing (versioned spec links).
      payload:
        $ref: '#/components/schemas/Welcome'
    Hello:
      name: hello
      payload:
        $ref: '#/components/schemas/Hello'
    Subscribe:
      name: subscribe
      payload:
        $ref: '#/components/schemas/Subscribe'
    Unsubscribe:
      name: unsubscribe
      payload:
        $ref: '#/components/schemas/Unsubscribe'
    Submit:
      name: submit
      payload:
        $ref: '#/components/schemas/Submit'
    UsageRequest:
      name: usageRequest
      payload:
        $ref: '#/components/schemas/UsageRequest'
    Subscribed:
      name: subscribed
      payload:
        $ref: '#/components/schemas/Subscribed'
    Submitted:
      name: submitted
      payload:
        $ref: '#/components/schemas/Submitted'
    ErrorEvent:
      name: error
      payload:
        $ref: '#/components/schemas/ErrorEvent'
    Usage:
      name: usage
      payload:
        $ref: '#/components/schemas/Usage'
    OrderbookSnapshot:
      name: orderbookSnapshot
      payload:
        $ref: '#/components/schemas/OrderbookSnapshotFrame'
    OrderbookUpdate:
      name: orderbookUpdate
      payload:
        $ref: '#/components/schemas/OrderbookUpdateFrame'
    TickerData:
      name: ticker
      payload:
        $ref: '#/components/schemas/TickerFrame'
    CandlesSnapshot:
      name: candlesSnapshot
      payload:
        $ref: '#/components/schemas/CandlesSnapshotFrame'
    CandleUpdate:
      name: candleUpdate
      payload:
        $ref: '#/components/schemas/CandleFrame'
    MarketsSnapshot:
      name: marketsSnapshot
      payload:
        $ref: '#/components/schemas/MarketsFrame'
    StatsData:
      name: stats
      payload:
        $ref: '#/components/schemas/StatsFrame'
    OrdersSnapshot:
      name: ordersSnapshot
      payload:
        $ref: '#/components/schemas/OrdersFrame'
    OrderUpdate:
      name: orderUpdate
      payload:
        $ref: '#/components/schemas/OrderFrame'
    AccountData:
      name: account
      payload:
        $ref: '#/components/schemas/AccountFrame'
    DepositsSnapshot:
      name: depositsSnapshot
      payload:
        $ref: '#/components/schemas/DepositsFrame'
    DepositUpdate:
      name: depositUpdate
      payload:
        $ref: '#/components/schemas/DepositFrame'

  schemas:
    # ── decimal-string helpers ───────────────────────────────────────────────
    Uint:
      type: string
      pattern: '^[0-9]+$'
      description: A non-negative integer serialized as a decimal string (u64/u128 precision-safe).
    TimestampMs:
      type: string
      pattern: '^[0-9]+$'
      description: An instant as unix MILLISECONDS, serialized as a decimal string.
    Price:
      type: string
      description: |
        A raw price. On writes it is a human decimal string (e.g. "2.15"). On
        reads it is a u128 binary fixed-point value with 60 fractional bits, as
        a decimal string: the price is `value / 2^60`. The `welcome` frame
        advertises this encoding (`priceEncoding`), and every read-side raw
        price is accompanied by a `PriceDecimal` twin.
    PriceDecimal:
      type: string
      description: |
        A price rendered as a human decimal string (e.g. "0.687853001402698396"),
        the same representation the write path accepts. Rounded to at most 18
        fractional digits.

    # ── asset identity ───────────────────────────────────────────────────────
    AssetId:
      description: |
        A Cardano asset, internally tagged. `policyId` and `assetName` are hex.
      oneOf:
        - type: object
          required: [tag]
          properties:
            tag:
              const: Ada
        - type: object
          required: [tag, policyId, assetName]
          properties:
            tag:
              const: NativeToken
            policyId:
              type: string
            assetName:
              type: string
    AssetInfo:
      type: object
      description: One side of a market in the `markets` directory.
      required: [asset, id, decimals]
      properties:
        asset:
          $ref: '#/components/schemas/AssetId'
        id:
          type: string
          description: |
            The canonical asset id string: `ada.lovelace`, or
            `<policyIdHex>.<assetNameHex>` for a native token.
        alias:
          type: [string, 'null']
          description: The human ticker (e.g. `DARK`), unique within a running API; null when the asset name is not a printable ticker.
        decimals:
          type: integer
          description: |
            Decimals for rendering this asset's raw integer amounts as human
            units: `human = raw / 10^decimals`. Applies to every raw amount on
            the wire (orderbook amounts, candle/ticker volume, balances,
            deposit values).
    TradingPair:
      type: object
      required: [base, quote]
      properties:
        base:
          $ref: '#/components/schemas/AssetId'
        quote:
          $ref: '#/components/schemas/AssetId'
    Value:
      type: object
      description: A multi-asset value (raw units).
      required: [assets]
      properties:
        assets:
          type: array
          items:
            type: object
            required: [asset, value]
            properties:
              asset:
                $ref: '#/components/schemas/AssetId'
              value:
                type: integer
                description: Raw amount in the asset's smallest unit (u64, serialized as a JSON number).

    # ── client → server ──────────────────────────────────────────────────────
    Hello:
      type: object
      required: [op]
      properties:
        op:
          const: hello
        version:
          type: integer
    Subscribe:
      type: object
      required: [op, channels]
      properties:
        op:
          const: subscribe
        channels:
          type: array
          items:
            type: string
          examples:
            - ['orderbook:DARK-VAN', 'ticker:DARK-VAN']
            - ['orders', 'account']
        auth:
          $ref: '#/components/schemas/Auth'
    Unsubscribe:
      type: object
      required: [op, channels]
      properties:
        op:
          const: unsubscribe
        channels:
          type: array
          items:
            type: string
    Submit:
      type: object
      required: [op, request]
      properties:
        op:
          const: submit
        id:
          type: string
          description: Optional client correlation id, echoed on the reply.
        request:
          $ref: '#/components/schemas/HeadRequest'
    UsageRequest:
      type: object
      required: [op]
      properties:
        op:
          const: usage
    Auth:
      type: object
      required: [account, nonce]
      description: Proof of account ownership on a private subscribe (CIP-8/COSE).
      properties:
        account:
          type: string
          description: The account id (ed25519 key hash), hex.
        nonce:
          type: integer
          description: Unix milliseconds; bounded to a freshness window.
        coseKey:
          type: string
          description: COSE key, hex. Empty while the server runs with verification off.
        coseSignature:
          type: string
          description: COSE signature over the auth message, hex.
    HeadRequest:
      type: object
      required: [type]
      description: Byte-identical to the `POST /head/requests` body.
      properties:
        type:
          type: string
          enum: [transaction, deposit]
        l2Payload:
          type: string
          description: The COSE-signed, encrypted transaction envelope, hex.
        l1Payload:
          type: string
          description: For `deposit` requests only.
      examples:
        - type: transaction
          l2Payload: '<signed+encrypted by your key>'

    # ── server → client control ──────────────────────────────────────────────
    Welcome:
      type: object
      required: [event, versions]
      properties:
        event:
          const: welcome
        versions:
          type: array
          items:
            type: object
            properties:
              version:
                type: integer
              spec:
                type: string
                format: uri
              status:
                type: string
                enum: [current, deprecated, preview]
        priceEncoding:
          type: object
          description: How raw read-side price fields are encoded on this connection.
          properties:
            format:
              const: binaryFixedPoint
            fractionalBits:
              type: integer
              description: 'The raw price is `value / 2^fractionalBits` (currently 60).'
        attestation:
          type: [object, 'null']
          description: Reserved for a later TDX attestation quote; null on testnet.
    Subscribed:
      type: object
      required: [event, channel]
      properties:
        event:
          const: subscribed
        channel:
          type: string
    Submitted:
      type: object
      required: [event, requestId]
      properties:
        event:
          const: submitted
        id:
          type: string
        requestId:
          type: integer
    ErrorEvent:
      type: object
      required: [event, code]
      properties:
        event:
          const: error
        channel:
          type: string
        id:
          type: string
        code:
          type: string
          examples: [auth_required, auth_invalid, unknown_market, rate_limited, submit_rejected, head_unreachable]
        message:
          type: string
    Usage:
      type: object
      required: [event]
      properties:
        event:
          const: usage
        limits:
          type: object
        used:
          type: object

    # ── data frame envelope ──────────────────────────────────────────────────
    Frame:
      type: object
      description: |
        A data frame. Like every server message it is tagged by `event`
        (`"data"`), so a client demuxes on the one discriminator; `channel` +
        `type` then route the payload.
      required: [event, channel, type, data]
      properties:
        event:
          const: data
        channel:
          type: string
        type:
          type: string
          enum: [snapshot, update]
        data: {}

    # ── per-channel data payloads ────────────────────────────────────────────
    OrderbookSnapshotFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/OrderbookSnapshotData'
    OrderbookUpdateFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/OrderbookUpdateData'
    OrderbookSnapshotData:
      type: object
      description: The full book, consistent up to `seq`.
      required: [market, seq, bids, asks]
      properties:
        market:
          type: string
        seq:
          $ref: '#/components/schemas/Uint'
        bids:
          type: array
          items:
            $ref: '#/components/schemas/PriceLevel'
        asks:
          type: array
          items:
            $ref: '#/components/schemas/PriceLevel'
    OrderbookUpdateData:
      type: object
      description: |
        The levels that changed in one flush window (a level with `0` amount on
        both sides was removed). Seq-chained: `prevSeq` equals the `seq` of the
        previous frame the server sent on this subscription (the snapshot's
        `seq` for the first update). A mismatch with the client's last applied
        `seq` means a missed frame — resubscribe for a fresh snapshot.
      required: [market, seq, prevSeq, bids, asks]
      properties:
        market:
          type: string
        seq:
          $ref: '#/components/schemas/Uint'
        prevSeq:
          $ref: '#/components/schemas/Uint'
        bids:
          type: array
          items:
            $ref: '#/components/schemas/PriceLevel'
        asks:
          type: array
          items:
            $ref: '#/components/schemas/PriceLevel'
    PriceLevel:
      type: array
      description: |
        `[price, amount, priceDecimal]`: the raw fixed-point price and raw
        amount as decimal strings, then the price pre-rendered as a human
        decimal string.
      items:
        type: string
      minItems: 2
      maxItems: 3

    TickerFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/Ticker'
    Ticker:
      type: object
      properties:
        market:
          type: string
        timestamp:
          $ref: '#/components/schemas/TimestampMs'
        lastPrice:
          oneOf: [{$ref: '#/components/schemas/Price'}, {type: 'null'}]
        open24h:
          oneOf: [{$ref: '#/components/schemas/Price'}, {type: 'null'}]
        high24h:
          oneOf: [{$ref: '#/components/schemas/Price'}, {type: 'null'}]
        low24h:
          oneOf: [{$ref: '#/components/schemas/Price'}, {type: 'null'}]
        lastPriceDecimal:
          oneOf: [{$ref: '#/components/schemas/PriceDecimal'}, {type: 'null'}]
        open24hDecimal:
          oneOf: [{$ref: '#/components/schemas/PriceDecimal'}, {type: 'null'}]
        high24hDecimal:
          oneOf: [{$ref: '#/components/schemas/PriceDecimal'}, {type: 'null'}]
        low24hDecimal:
          oneOf: [{$ref: '#/components/schemas/PriceDecimal'}, {type: 'null'}]
        volume24h:
          allOf:
            - $ref: '#/components/schemas/Uint'
          description: Raw base-asset units; divide by 10^(base decimals) from the `markets` directory.
        bestBid:
          oneOf: [{$ref: '#/components/schemas/Price'}, {type: 'null'}]
        bestAsk:
          oneOf: [{$ref: '#/components/schemas/Price'}, {type: 'null'}]
        bestBidDecimal:
          oneOf: [{$ref: '#/components/schemas/PriceDecimal'}, {type: 'null'}]
        bestAskDecimal:
          oneOf: [{$ref: '#/components/schemas/PriceDecimal'}, {type: 'null'}]

    CandlesSnapshotFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              type: array
              items:
                $ref: '#/components/schemas/Candle'
    CandleFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/Candle'
    Candle:
      type: object
      properties:
        key:
          type: string
        pairKey:
          type: string
        granularity:
          type: string
        timestamp:
          allOf:
            - $ref: '#/components/schemas/TimestampMs'
          description: The bucket's start, unix milliseconds.
        open:
          $ref: '#/components/schemas/Price'
        high:
          $ref: '#/components/schemas/Price'
        low:
          $ref: '#/components/schemas/Price'
        close:
          $ref: '#/components/schemas/Price'
        openDecimal:
          $ref: '#/components/schemas/PriceDecimal'
        highDecimal:
          $ref: '#/components/schemas/PriceDecimal'
        lowDecimal:
          $ref: '#/components/schemas/PriceDecimal'
        closeDecimal:
          $ref: '#/components/schemas/PriceDecimal'
        volume:
          allOf:
            - $ref: '#/components/schemas/Uint'
          description: Raw base-asset units; divide by 10^(base decimals) from the `markets` directory.
        tradeCount:
          type: integer

    MarketsFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              type: array
              items:
                $ref: '#/components/schemas/Market'
    Market:
      type: object
      description: A market. `id` is the canonical key; `symbol` is the friendly alias.
      properties:
        id:
          type: string
        key:
          type: string
        symbol:
          type: [string, 'null']
        tradingPair:
          $ref: '#/components/schemas/TradingPair'
        base:
          $ref: '#/components/schemas/AssetInfo'
        quote:
          $ref: '#/components/schemas/AssetInfo'
        takerFee:
          $ref: '#/components/schemas/Ratio'
        makerFee:
          $ref: '#/components/schemas/Ratio'
    Ratio:
      type: object
      properties:
        numerator:
          type: integer
        denominator:
          type: integer

    StatsFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              type: object
              properties:
                tradingEnabled:
                  type: boolean
                tps:
                  oneOf:
                    - $ref: '#/components/schemas/TpsStats'
                    - type: 'null'
    TpsStats:
      type: object
      description: Event-throughput diagnostics, emitted ~1/s (the liveness heartbeat).
      properties:
        id:
          type: integer
        totalEvents:
          $ref: '#/components/schemas/Uint'
        lastEventAt:
          $ref: '#/components/schemas/TimestampMs'
        currentSecond:
          allOf:
            - $ref: '#/components/schemas/Uint'
          description: |
            The one-second counting bucket `currentSecondCount` refers to, as
            unix SECONDS — a bucket label, not an instant; the one deliberate
            exception to the milliseconds rule.
        currentSecondCount:
          type: integer
        lastSecondCount:
          type: integer

    OrdersFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              type: array
              items:
                $ref: '#/components/schemas/Order'
    OrderFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/Order'
    Order:
      type: object
      description: One of your resting/closed orders (owner-scoped by the server).
      properties:
        id:
          $ref: '#/components/schemas/Uint'
        owner:
          type: string
        pair:
          $ref: '#/components/schemas/TradingPair'
        side:
          type: string
          enum: [Bid, Ask]
        price:
          $ref: '#/components/schemas/Price'
        priceDecimal:
          $ref: '#/components/schemas/PriceDecimal'
        originalAmount:
          $ref: '#/components/schemas/Uint'
        remaining:
          $ref: '#/components/schemas/Uint'
        status:
          type: string
          enum: [New, PartiallyFilled, Filled, Cancelled]
        created:
          $ref: '#/components/schemas/TimestampMs'
        destination:
          type: [object, 'null']
          description: Optional L1 destination for settlement (address + optional datum).
        lastExecuted:
          $ref: '#/components/schemas/TimestampMs'

    AccountFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/Account'
    Account:
      type: object
      properties:
        accountId:
          type: string
        delegatedKeys:
          type: array
          items:
            type: string
          description: |
            The account's active delegated (session) key hashes, oldest first —
            at most 20; adding past capacity evicts the oldest. Any of them may
            sign on the account's behalf.
        nonce:
          $ref: '#/components/schemas/Uint'
        uncommittedValue:
          allOf:
            - $ref: '#/components/schemas/Value'
          description: Balances (raw units; decode with the assets' `decimals` from the `markets` directory).

    DepositsFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              type: array
              items:
                $ref: '#/components/schemas/Deposit'
    DepositFrame:
      allOf:
        - $ref: '#/components/schemas/Frame'
        - type: object
          properties:
            data:
              $ref: '#/components/schemas/Deposit'
    Deposit:
      type: object
      properties:
        accountId:
          type: string
        requestId:
          $ref: '#/components/schemas/Uint'
        absorbedInBlock:
          $ref: '#/components/schemas/Uint'
        rejectedInBlock:
          $ref: '#/components/schemas/Uint'
        refundTx:
          type: [array, 'null']
          description: The refund transaction bytes, when the deposit was rejected.
        failureReason:
          type: [string, 'null']
        value:
          allOf:
            - $ref: '#/components/schemas/Value'
          description: The deposited value (raw units).
