{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://getzero.dev/contracts/zero.builder_code_authority.v1.schema.json",
  "title": "ZERO Builder Code Authority v1",
  "oneOf": [
    { "$ref": "#/$defs/approval_packet" },
    { "$ref": "#/$defs/authority_receipt" },
    { "$ref": "#/$defs/fill_reconciliation" }
  ],
  "$defs": {
    "address": {
      "type": "string",
      "pattern": "^0x[a-fA-F0-9]{40}$"
    },
    "approval_packet": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "schema_version",
        "authority_schema",
        "action",
        "network",
        "deployment_id",
        "main_wallet",
        "builder",
        "max_fee_tenth_bps",
        "must_be_signed_by",
        "must_not_be_signed_by"
      ],
      "properties": {
        "schema_version": { "const": "zero.builder_fee_approval_packet.v1" },
        "authority_schema": { "const": "https://getzero.dev/contracts/zero.builder_code_authority.v1.schema.json" },
        "action": { "const": "approveBuilderFee" },
        "network": { "enum": ["mainnet", "testnet"] },
        "deployment_id": { "type": "string" },
        "main_wallet": { "$ref": "#/$defs/address" },
        "builder": { "$ref": "#/$defs/address" },
        "max_fee_tenth_bps": { "type": "integer", "minimum": 1, "maximum": 100 },
        "hyperliquid_url": { "type": "string" },
        "must_be_signed_by": { "const": "main_wallet" },
        "must_not_be_signed_by": {
          "type": "array",
          "contains": { "const": "api_wallet" }
        },
        "reconciliation_source": {
          "$ref": "#/$defs/builder_fills_export_ref"
        }
      }
    },
    "authority_receipt": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "schema_version",
        "authority_schema",
        "action",
        "receipt_id",
        "deployment_id",
        "occurred_at",
        "evidence_hash",
        "evidence",
        "public",
        "no_secret_material"
      ],
      "properties": {
        "schema_version": { "const": "zero.builder_code_authority_receipt.v1" },
        "authority_schema": { "const": "https://getzero.dev/contracts/zero.builder_code_authority.v1.schema.json" },
        "action": {
          "enum": [
            "approve_builder_fee_required",
            "approve_builder_fee_verified",
            "revoke_builder_fee_verified",
            "builder_fee_readback_failed",
            "builder_fill_reconciliation"
          ]
        },
        "receipt_id": {
          "type": "string",
          "pattern": "^builder-[a-z_]+-[a-f0-9]{16}$"
        },
        "deployment_id": { "type": "string" },
        "operator_id": { "type": ["string", "null"] },
        "occurred_at": { "type": "string" },
        "evidence_hash": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "evidence": { "type": "object" },
        "public": { "const": false },
        "no_secret_material": { "const": true }
      }
    },
    "builder_fills_export_ref": {
      "type": "object",
      "required": [
        "network",
        "builderAddress",
        "date",
        "url",
        "compressed",
        "format",
        "source"
      ],
      "properties": {
        "network": { "enum": ["mainnet", "testnet"] },
        "builderAddress": { "$ref": "#/$defs/address" },
        "date": { "type": "string", "pattern": "^\\d{8}$" },
        "url": {
          "type": "string",
          "pattern": "^https://stats-data\\.hyperliquid\\.xyz/(Mainnet|Testnet)/builder_fills/0x[a-f0-9]{40}/\\d{8}\\.csv\\.lz4$"
        },
        "compressed": { "const": true },
        "format": { "const": "csv.lz4" },
        "source": { "const": "hyperliquid_builder_fills" }
      }
    },
    "fill_reconciliation": {
      "type": "object",
      "required": [
        "schema_version",
        "builder",
        "user",
        "export_ref",
        "fills_count",
        "matched_fills_count",
        "total_builder_fee_usd",
        "total_fee_usd",
        "earliest_fill_time",
        "latest_fill_time",
        "case_sensitive_builder_address",
        "no_secret_material"
      ],
      "properties": {
        "schema_version": { "const": "zero.builder_fill_reconciliation.v1" },
        "builder": { "$ref": "#/$defs/address" },
        "user": { "oneOf": [{ "$ref": "#/$defs/address" }, { "type": "null" }] },
        "export_ref": { "$ref": "#/$defs/builder_fills_export_ref" },
        "fills_count": { "type": "integer", "minimum": 0 },
        "matched_fills_count": { "type": "integer", "minimum": 0 },
        "total_builder_fee_usd": { "type": ["number", "null"] },
        "total_fee_usd": { "type": ["number", "null"] },
        "earliest_fill_time": { "type": ["string", "null"] },
        "latest_fill_time": { "type": ["string", "null"] },
        "case_sensitive_builder_address": { "const": true },
        "no_secret_material": { "const": true }
      }
    }
  }
}
