{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://402rates.com/schemas/rwa-market-state-v1.json",
  "title": "Rates market state, version 1.0.0",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "meta",
    "market",
    "reference",
    "book",
    "comparison",
    "size_calculation",
    "fees",
    "context"
  ],
  "properties": {
    "meta": {
      "type": "object",
      "required": [
        "capability",
        "schema_version",
        "method_version",
        "market_snapshot_id",
        "prepared_at",
        "evidence_prepared_at",
        "evidence_digest",
        "output_profile",
        "freshness",
        "sources",
        "limitations",
        "rounding",
        "disclaimer"
      ],
      "properties": {
        "capability": {
          "const": "compass.rwa.market_state"
        },
        "schema_version": {
          "const": "1.0.0"
        },
        "method_version": {
          "type": "string"
        },
        "market_snapshot_id": {
          "type": "string",
          "format": "uuid"
        },
        "prepared_at": {
          "type": "string",
          "format": "date-time"
        },
        "evidence_digest": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$"
        },
        "output_profile": {
          "enum": [
            "full",
            "derived_only"
          ]
        },
        "freshness": {
          "type": "object"
        },
        "sources": {
          "type": "array"
        },
        "limitations": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "market": {
      "type": "object",
      "required": [
        "market_id",
        "fund_id",
        "share_class",
        "venue",
        "feedcode",
        "instrument_id",
        "base_currency",
        "quote_currency",
        "evidence"
      ]
    },
    "reference": {
      "type": "object",
      "required": [
        "observation_id",
        "instrument",
        "source_id",
        "value",
        "type",
        "currency",
        "share_class",
        "effective_at",
        "fetched_at"
      ],
      "properties": {
        "value": {
          "type": "string"
        },
        "currency": {
          "type": "string"
        }
      }
    },
    "book": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "best_bid",
        "best_ask",
        "bids",
        "asks",
        "side_presence",
        "depth",
        "clocks",
        "raw_quote_status"
      ],
      "properties": {
        "status": {
          "enum": [
            "two_sided",
            "one_sided"
          ]
        },
        "best_bid": {
          "anyOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "price",
                "quantity"
              ],
              "properties": {
                "price": {
                  "type": "string"
                },
                "quantity": {
                  "type": "string"
                }
              }
            },
            {
              "type": "null"
            }
          ]
        },
        "best_ask": {
          "anyOf": [
            {
              "type": "object",
              "additionalProperties": false,
              "required": [
                "price",
                "quantity"
              ],
              "properties": {
                "price": {
                  "type": "string"
                },
                "quantity": {
                  "type": "string"
                }
              }
            },
            {
              "type": "null"
            }
          ]
        },
        "bids": {
          "anyOf": [
            {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "price",
                  "quantity"
                ],
                "properties": {
                  "price": {
                    "type": "string"
                  },
                  "quantity": {
                    "type": "string"
                  }
                }
              }
            },
            {
              "type": "null"
            }
          ]
        },
        "asks": {
          "anyOf": [
            {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "price",
                  "quantity"
                ],
                "properties": {
                  "price": {
                    "type": "string"
                  },
                  "quantity": {
                    "type": "string"
                  }
                }
              }
            },
            {
              "type": "null"
            }
          ]
        },
        "side_presence": {
          "type": "object"
        },
        "depth": {
          "type": "object"
        },
        "clocks": {
          "type": "object"
        },
        "raw_quote_status": {
          "enum": [
            "included",
            "withheld_by_profile"
          ]
        }
      }
    },
    "comparison": {
      "type": "object",
      "required": [
        "bid_premium_discount_bps",
        "ask_premium_discount_bps",
        "midpoint",
        "midpoint_premium_discount_bps",
        "spread_bps",
        "reference_currency",
        "conversion",
        "reason",
        "time_skew_s"
      ],
      "properties": {
        "bid_premium_discount_bps": {
          "type": [
            "string",
            "null"
          ]
        },
        "ask_premium_discount_bps": {
          "type": [
            "string",
            "null"
          ]
        },
        "midpoint": {
          "type": [
            "string",
            "null"
          ]
        },
        "midpoint_premium_discount_bps": {
          "type": [
            "string",
            "null"
          ]
        },
        "spread_bps": {
          "type": [
            "string",
            "null"
          ]
        }
      }
    },
    "size_calculation": {
      "anyOf": [
        {
          "type": "null"
        },
        {
          "type": "object",
          "required": [
            "scope",
            "side",
            "requested_quantity",
            "observed_filled_quantity",
            "unfilled_quantity",
            "gross_value",
            "gross_currency",
            "vwap",
            "net_value",
            "consumed_levels",
            "status",
            "reason"
          ],
          "properties": {
            "scope": {
              "const": "observed_book_calculation"
            },
            "side": {
              "enum": [
                "buy",
                "sell"
              ]
            },
            "gross_value": {
              "type": "string"
            },
            "vwap": {
              "type": [
                "string",
                "null"
              ]
            },
            "net_value": {
              "type": "null"
            }
          }
        }
      ]
    },
    "fees": {
      "type": "object",
      "required": [
        "components",
        "completeness",
        "net_value",
        "reason"
      ],
      "properties": {
        "net_value": {
          "type": "null"
        }
      }
    },
    "context": {
      "type": "object",
      "required": [
        "realised_return",
        "issuer_figure"
      ]
    }
  }
}
