{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://docs.bullpen.fi/reference/schemas/hyperliquid.balances.schema.json",
  "title": "bullpen hyperliquid balances JSON output",
  "description": "Initial stable schema for the documented main-DEX and --all-dexes balances envelopes.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "schema_version",
    "address",
    "spot_estimated_value",
    "estimated_total_equity",
    "spot_balances"
  ],
  "oneOf": [
    {
      "required": [
        "perp_account_value",
        "perp_total_ntl_pos",
        "perp_margin_used",
        "available_usdc"
      ],
      "not": {
        "anyOf": [
          {
            "required": [
              "dex_scope"
            ]
          },
          {
            "required": [
              "totals"
            ]
          },
          {
            "required": [
              "per_dex"
            ]
          }
        ]
      }
    },
    {
      "required": [
        "dex_scope",
        "totals",
        "per_dex"
      ],
      "properties": {
        "dex_scope": {
          "const": "all"
        }
      },
      "not": {
        "anyOf": [
          {
            "required": [
              "perp_account_value"
            ]
          },
          {
            "required": [
              "perp_total_ntl_pos"
            ]
          },
          {
            "required": [
              "perp_margin_used"
            ]
          },
          {
            "required": [
              "perp_raw_usd"
            ]
          },
          {
            "required": [
              "cross_account_value"
            ]
          },
          {
            "required": [
              "available_usdc"
            ]
          }
        ]
      }
    }
  ],
  "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_balance"
      }
    },
    "perp_account_value": {
      "$ref": "#/$defs/decimal_string"
    },
    "perp_total_ntl_pos": {
      "$ref": "#/$defs/decimal_string"
    },
    "perp_margin_used": {
      "$ref": "#/$defs/decimal_string"
    },
    "perp_raw_usd": {
      "$ref": "#/$defs/decimal_string"
    },
    "cross_account_value": {
      "$ref": "#/$defs/decimal_string"
    },
    "available_usdc": {
      "$ref": "#/$defs/decimal_string"
    },
    "spot_estimated_value": {
      "$ref": "#/$defs/decimal_string"
    },
    "estimated_total_equity": {
      "$ref": "#/$defs/decimal_string"
    },
    "unpriced_spot_balances": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "spot_balances": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/spot_balance"
      }
    }
  },
  "$defs": {
    "decimal_string": {
      "type": "string",
      "pattern": "^-?\\d+(?:\\.\\d+)?$"
    },
    "evm_address": {
      "type": "string",
      "pattern": "^0x[0-9A-Fa-f]{40}$"
    },
    "spot_balance": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "coin",
        "total",
        "hold",
        "available",
        "entry_value",
        "mark_px",
        "estimated_value"
      ],
      "properties": {
        "coin": {
          "type": "string",
          "minLength": 1
        },
        "total": {
          "$ref": "#/$defs/decimal_string"
        },
        "hold": {
          "$ref": "#/$defs/decimal_string"
        },
        "available": {
          "$ref": "#/$defs/decimal_string"
        },
        "entry_value": {
          "$ref": "#/$defs/decimal_string"
        },
        "mark_px": {
          "$ref": "#/$defs/decimal_string"
        },
        "estimated_value": {
          "$ref": "#/$defs/decimal_string"
        }
      }
    },
    "all_dex_totals": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "perp_account_value",
        "perp_total_ntl_pos",
        "perp_margin_used",
        "perp_raw_usd",
        "cross_account_value",
        "cross_maintenance_margin_used",
        "perp_withdrawable",
        "available_usdc"
      ],
      "properties": {
        "perp_account_value": {
          "$ref": "#/$defs/decimal_string"
        },
        "perp_total_ntl_pos": {
          "$ref": "#/$defs/decimal_string"
        },
        "perp_margin_used": {
          "$ref": "#/$defs/decimal_string"
        },
        "perp_raw_usd": {
          "$ref": "#/$defs/decimal_string"
        },
        "cross_account_value": {
          "$ref": "#/$defs/decimal_string"
        },
        "cross_maintenance_margin_used": {
          "$ref": "#/$defs/decimal_string"
        },
        "perp_withdrawable": {
          "$ref": "#/$defs/decimal_string"
        },
        "available_usdc": {
          "$ref": "#/$defs/decimal_string"
        }
      }
    },
    "per_dex_balance": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "label",
        "registry_index",
        "perp_account_value"
      ],
      "properties": {
        "label": {
          "type": "string",
          "minLength": 1
        },
        "dex": {
          "type": "string"
        },
        "registry_index": {
          "type": "integer",
          "minimum": 0
        },
        "perp_account_value": {
          "$ref": "#/$defs/decimal_string"
        }
      }
    }
  }
}
