{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://reso.example/schemas/trading-v1.websocket.schema.json",
  "title": "Reso Trading WebSocket Protocol v1",
  "oneOf": [
    { "$ref": "#/$defs/subscription" },
    { "$ref": "#/$defs/event" },
    { "$ref": "#/$defs/protocolError" }
  ],
  "$defs": {
    "uint64String": { "type": "string", "pattern": "^(0|[1-9][0-9]*)$" },
    "baseUnitString": { "type": "string", "pattern": "^(0|[1-9][0-9]*)$" },
    "outcome": { "enum": ["UP", "DOWN"] },
    "subscription": {
      "type": "object",
      "additionalProperties": false,
      "required": ["op", "channel"],
      "properties": {
        "op": { "const": "SUBSCRIBE" },
        "channel": { "enum": ["MARKET", "USER"] },
        "stream": { "enum": ["BOOK", "LIFECYCLE", "ORACLE"] },
        "marketId": { "type": "string", "minLength": 1 },
        "oraclePolicy": { "const": "btc-minute-oracle-v1" },
        "outcome": { "$ref": "#/$defs/outcome" },
        "afterSequence": { "$ref": "#/$defs/uint64String" }
      },
      "allOf": [
        {
          "if": { "properties": { "channel": { "const": "USER" } }, "required": ["channel"] },
          "then": { "not": { "anyOf": [{ "required": ["stream"] }, { "required": ["marketId"] }, { "required": ["oraclePolicy"] }, { "required": ["outcome"] }] } }
        },
        {
          "if": { "properties": { "stream": { "const": "BOOK" } }, "required": ["stream"] },
          "then": { "required": ["marketId", "outcome"], "not": { "required": ["oraclePolicy"] } }
        },
        {
          "if": { "properties": { "stream": { "const": "LIFECYCLE" } }, "required": ["stream"] },
          "then": { "required": ["marketId"], "not": { "anyOf": [{ "required": ["oraclePolicy"] }, { "required": ["outcome"] }] } }
        },
        {
          "if": { "properties": { "stream": { "const": "ORACLE" } }, "required": ["stream"] },
          "then": { "required": ["oraclePolicy"], "not": { "anyOf": [{ "required": ["marketId"] }, { "required": ["outcome"] }] } }
        }
      ]
    },
    "bookLevel": {
      "type": "object",
      "additionalProperties": false,
      "required": ["priceBaseUnit", "sharesBaseUnit"],
      "properties": {
        "priceBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "sharesBaseUnit": { "$ref": "#/$defs/baseUnitString" }
      }
    },
    "bookSnapshot": {
      "type": "object",
      "additionalProperties": false,
      "required": ["marketId", "outcome", "sequence", "bookHash", "bids", "asks", "snapshotTimeMs", "earliestGtdExpirationMs"],
      "properties": {
        "marketId": { "type": "string" },
        "outcome": { "$ref": "#/$defs/outcome" },
        "sequence": { "$ref": "#/$defs/uint64String" },
        "bookHash": { "type": "string", "pattern": "^0x[0-9a-f]{64}$" },
        "bids": { "type": "array", "items": { "$ref": "#/$defs/bookLevel" } },
        "asks": { "type": "array", "items": { "$ref": "#/$defs/bookLevel" } },
        "snapshotTimeMs": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
        "earliestGtdExpirationMs": {
          "oneOf": [
            { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
            { "type": "null" }
          ]
        }
      }
    },
    "bookDelta": {
      "type": "object",
      "additionalProperties": false,
      "required": ["marketId", "outcome", "previousBookHash", "bookHash", "earliestGtdExpirationMs", "bids", "asks"],
      "properties": {
        "marketId": { "type": "string" },
        "outcome": { "$ref": "#/$defs/outcome" },
        "previousBookHash": { "type": "string", "pattern": "^0x[0-9a-f]{64}$" },
        "bookHash": { "type": "string", "pattern": "^0x[0-9a-f]{64}$" },
        "earliestGtdExpirationMs": {
          "oneOf": [
            { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
            { "type": "null" }
          ]
        },
        "bids": { "type": "array", "items": { "$ref": "#/$defs/bookLevel" } },
        "asks": { "type": "array", "items": { "$ref": "#/$defs/bookLevel" } }
      }
    },
    "publicTrade": {
      "type": "object",
      "additionalProperties": false,
      "required": ["fillId", "marketId", "outcome", "fundingAssetId", "makerOrderId", "takerOrderId", "priceBaseUnit", "sharesBaseUnit", "notionalBaseUnit", "takerFeeBaseUnit", "makerRebateBaseUnit", "platformFeeBaseUnit", "executedAtMs"],
      "properties": {
        "fillId": { "type": "string" },
        "marketId": { "type": "string" },
        "outcome": { "$ref": "#/$defs/outcome" },
        "fundingAssetId": { "type": "string" },
        "makerOrderId": { "type": "string" },
        "takerOrderId": { "type": "string" },
        "priceBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "sharesBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "notionalBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "takerFeeBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "makerRebateBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "platformFeeBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "sellerCostBaseUnit": { "$ref": "#/$defs/baseUnitString" },
        "executedAtMs": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 }
      }
    },
    "lifecycle": {
      "type": "object",
      "additionalProperties": false,
      "required": ["marketId", "configVersion", "previousSettlementStatus", "settlementStatus", "result", "evidenceVersion", "effectiveAtMs"],
      "properties": {
        "marketId": { "type": "string" },
        "configVersion": { "type": "integer", "minimum": 1 },
        "previousSettlementStatus": { "type": ["string", "null"], "enum": [null, "OPEN", "LOCKED", "RESOLVING", "SETTLED", "REFUNDED"] },
        "settlementStatus": { "enum": ["OPEN", "LOCKED", "RESOLVING", "SETTLED", "REFUNDED"] },
        "result": { "type": ["string", "null"], "enum": [null, "UP", "DOWN", "REFUND"] },
        "evidenceVersion": { "type": "integer", "minimum": 1 },
        "effectiveAtMs": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 }
      }
    },
    "accountDelta": {
      "type": "object",
      "additionalProperties": false,
      "required": ["orders", "fills", "balances", "positions", "settlements"],
      "properties": {
        "orders": { "type": "array", "items": { "type": "object" } },
        "fills": { "type": "array", "items": { "type": "object" } },
        "balances": { "type": "array", "items": { "type": "object" } },
        "positions": { "type": "array", "items": { "type": "object" } },
        "settlements": { "type": "array", "items": { "type": "object" } }
      }
    },
    "oraclePrice": {
      "type": "object",
      "additionalProperties": false,
      "required": ["oraclePolicy", "symbol", "currency", "price", "observedAtIso", "sourceCount"],
      "properties": {
        "oraclePolicy": { "const": "btc-minute-oracle-v1" },
        "symbol": { "type": "string", "minLength": 1 },
        "currency": { "type": "string", "minLength": 1 },
        "price": { "type": "number", "exclusiveMinimum": 0 },
        "observedAtIso": { "type": "string", "format": "date-time" },
        "sourceCount": { "type": "integer", "minimum": 1 }
      }
    },
    "heartbeat": {
      "type": "object",
      "additionalProperties": false,
      "required": ["heartbeat"],
      "properties": {
        "heartbeat": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status"],
          "properties": {
            "status": { "enum": ["ARMED", "EXPIRED", "DISARMED"] }
          }
        }
      }
    },
    "event": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "channel", "stream", "scope", "sequence", "eventId", "eventTimeMs", "eventType", "payload"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "channel": { "enum": ["MARKET", "USER"] },
        "stream": { "enum": ["BOOK", "LIFECYCLE", "ORACLE", "ACCOUNT"] },
        "scope": { "type": "string", "minLength": 1 },
        "sequence": { "$ref": "#/$defs/uint64String" },
        "eventId": { "type": "string", "minLength": 1 },
        "eventTimeMs": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
        "eventType": {
          "enum": [
            "ORDERBOOK_SNAPSHOT", "ORDERBOOK_DELTA", "PUBLIC_TRADE",
            "MARKET_STATE_SNAPSHOT", "MARKET_OPENED", "MARKET_LOCKED", "MARKET_RESOLVING", "MARKET_SETTLED", "MARKET_REFUNDED",
            "ORACLE_PRICE_TICK",
            "ACCOUNT_DELTA", "HEARTBEAT_ARMED", "HEARTBEAT_EXPIRED", "HEARTBEAT_DISARMED"
          ]
        },
        "payload": {
          "oneOf": [
            { "$ref": "#/$defs/bookSnapshot" },
            { "$ref": "#/$defs/bookDelta" },
            { "$ref": "#/$defs/publicTrade" },
            { "$ref": "#/$defs/lifecycle" },
            { "$ref": "#/$defs/oraclePrice" },
            { "$ref": "#/$defs/accountDelta" },
            { "$ref": "#/$defs/heartbeat" }
          ]
        }
      },
      "allOf": [
        {
          "if": { "properties": { "eventType": { "const": "ORDERBOOK_SNAPSHOT" } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "MARKET" }, "stream": { "const": "BOOK" }, "payload": { "$ref": "#/$defs/bookSnapshot" } } }
        },
        {
          "if": { "properties": { "eventType": { "const": "ORDERBOOK_DELTA" } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "MARKET" }, "stream": { "const": "BOOK" }, "payload": { "$ref": "#/$defs/bookDelta" } } }
        },
        {
          "if": { "properties": { "eventType": { "const": "PUBLIC_TRADE" } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "MARKET" }, "stream": { "const": "BOOK" }, "payload": { "$ref": "#/$defs/publicTrade" } } }
        },
        {
          "if": { "properties": { "eventType": { "enum": ["MARKET_STATE_SNAPSHOT", "MARKET_OPENED", "MARKET_LOCKED", "MARKET_RESOLVING", "MARKET_SETTLED", "MARKET_REFUNDED"] } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "MARKET" }, "stream": { "const": "LIFECYCLE" }, "payload": { "$ref": "#/$defs/lifecycle" } } }
        },
        {
          "if": { "properties": { "eventType": { "const": "ORACLE_PRICE_TICK" } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "MARKET" }, "stream": { "const": "ORACLE" }, "payload": { "$ref": "#/$defs/oraclePrice" } } }
        },
        {
          "if": { "properties": { "eventType": { "const": "ACCOUNT_DELTA" } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "USER" }, "stream": { "const": "ACCOUNT" }, "payload": { "$ref": "#/$defs/accountDelta" } } }
        },
        {
          "if": { "properties": { "eventType": { "enum": ["HEARTBEAT_ARMED", "HEARTBEAT_EXPIRED", "HEARTBEAT_DISARMED"] } }, "required": ["eventType"] },
          "then": { "properties": { "channel": { "const": "USER" }, "stream": { "const": "ACCOUNT" }, "payload": { "$ref": "#/$defs/heartbeat" } } }
        }
      ]
    },
    "protocolError": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schemaVersion", "error"],
      "properties": {
        "schemaVersion": { "const": 1 },
        "error": {
          "type": "object",
          "additionalProperties": false,
          "required": ["code", "message", "retryable"],
          "properties": {
            "code": { "enum": ["PROTOCOL_ERROR", "LIMIT_EXCEEDED", "RESYNC_REQUIRED", "AUTHENTICATION_FAILED"] },
            "message": { "type": "string" },
            "retryable": { "type": "boolean" }
          }
        }
      }
    }
  }
}
