{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.bullpen.fi/reference/schemas/hyperliquid.status.schema.json",
  "title": "bullpen hyperliquid status JSON output",
  "description": "Initial stable schema for the documented main-DEX and --all-dexes status envelopes.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "address",
    "positions"
  ],
  "oneOf": [
    {
      "required": [
        "margin_summary"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "dex_scope"
            ]
          },
          {
            "required": [
              "totals"
            ]
          },
          {
            "required": [
              "per_dex"
            ]
          }
        ]
      }
    },
    {
      "required": [
        "dex_scope",
        "totals",
        "per_dex"
      ],
      "properties": {
        "dex_scope": {
          "const": "all"
        }
      },
      "not": {
        "anyOf": [
          {
            "required": [
              "margin_summary"
            ]
          },
          {
            "required": [
              "cross_margin_summary"
            ]
          }
        ]
      }
    }
  ],
  "properties": {
    "schema_version": {
      "type": "integer",
      "const": 1
    },
    "address": {
      "$ref": "#/$defs/evm_address"
    },
    "signer_address": {
      "anyOf": [
        {
          "$ref": "#/$defs/evm_address"
        },
        {
          "type": "null"
        }
      ]
    },
    "dex_scope": {
      "const": "all"
    },
    "totals": {
      "$ref": "#/$defs/all_dex_totals"
    },
    "per_dex": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/per_dex_status"
      }
    },
    "positions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/position"
      }
    },
    "margin_summary": {
      "$ref": "#/$defs/margin_summary"
    },
    "cross_margin_summary": {
      "$ref": "#/$defs/margin_summary"
    }
  },
  "$defs": {
    "decimal_string": {
      "type": "string",
      "pattern": "^-?\\d+(?:\\.\\d+)?$"
    },
    "evm_address": {
      "type": "string",
      "pattern": "^0x[0-9A-Fa-f]{40}$"
    },
    "position": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "coin",
        "side",
        "size",
        "position_value",
        "unrealized_pnl",
        "return_on_equity"
      ],
      "properties": {
        "dex_label": {
          "type": "string"
        },
        "registry_index": {
          "type": "integer",
          "minimum": 0
        },
        "coin": {
          "type": "string",
          "minLength": 1
        },
        "side": {
          "enum": [
            "long",
            "short"
          ]
        },
        "size": {
          "$ref": "#/$defs/decimal_string"
        },
        "entry_px": {
          "$ref": "#/$defs/decimal_string"
        },
        "mark_px": {
          "$ref": "#/$defs/decimal_string"
        },
        "position_value": {
          "$ref": "#/$defs/decimal_string"
        },
        "unrealized_pnl": {
          "$ref": "#/$defs/decimal_string"
        },
        "return_on_equity": {
          "$ref": "#/$defs/decimal_string"
        },
        "leverage": {
          "type": "string"
        },
        "liquidation_px": {
          "$ref": "#/$defs/decimal_string"
        },
        "margin_used": {
          "$ref": "#/$defs/decimal_string"
        }
      }
    },
    "margin_summary": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "accountValue",
        "totalNtlPos",
        "totalRawUsd",
        "totalMarginUsed"
      ],
      "properties": {
        "accountValue": {
          "$ref": "#/$defs/decimal_string"
        },
        "totalNtlPos": {
          "$ref": "#/$defs/decimal_string"
        },
        "totalRawUsd": {
          "$ref": "#/$defs/decimal_string"
        },
        "totalMarginUsed": {
          "$ref": "#/$defs/decimal_string"
        }
      }
    },
    "all_dex_totals": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "account_value",
        "total_ntl_pos",
        "total_raw_usd",
        "total_margin_used",
        "cross_account_value",
        "cross_maintenance_margin_used",
        "withdrawable",
        "unrealized_pnl",
        "position_count"
      ],
      "properties": {
        "account_value": {
          "$ref": "#/$defs/decimal_string"
        },
        "total_ntl_pos": {
          "$ref": "#/$defs/decimal_string"
        },
        "total_raw_usd": {
          "$ref": "#/$defs/decimal_string"
        },
        "total_margin_used": {
          "$ref": "#/$defs/decimal_string"
        },
        "cross_account_value": {
          "$ref": "#/$defs/decimal_string"
        },
        "cross_maintenance_margin_used": {
          "$ref": "#/$defs/decimal_string"
        },
        "withdrawable": {
          "$ref": "#/$defs/decimal_string"
        },
        "unrealized_pnl": {
          "$ref": "#/$defs/decimal_string"
        },
        "position_count": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "per_dex_status": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "label",
        "registry_index",
        "position_count",
        "positions"
      ],
      "properties": {
        "label": {
          "type": "string",
          "minLength": 1
        },
        "dex": {
          "type": "string"
        },
        "registry_index": {
          "type": "integer",
          "minimum": 0
        },
        "position_count": {
          "type": "integer",
          "minimum": 0
        },
        "positions": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/position"
          }
        }
      }
    }
  }
}
