{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://synor.io/schemas/rpc.json",
  "title": "Synor RPC SDK",
  "description": "Blockchain RPC client for querying blocks, transactions, and chain state",

  "$defs": {
    "RpcConfig": {
      "type": "object",
      "allOf": [{ "$ref": "common.json#/$defs/SynorConfig" }],
      "properties": {
        "wsEndpoint": {
          "type": "string",
          "format": "uri",
          "description": "WebSocket endpoint for subscriptions"
        },
        "network": {
          "type": "string",
          "enum": ["mainnet", "testnet"],
          "default": "mainnet"
        }
      }
    },

    "BlockHeader": {
      "type": "object",
      "properties": {
        "hash": { "$ref": "common.json#/$defs/BlockHash" },
        "height": { "type": "integer", "minimum": 0 },
        "version": { "type": "integer" },
        "previousHash": { "$ref": "common.json#/$defs/BlockHash" },
        "merkleRoot": { "type": "string" },
        "timestamp": { "$ref": "common.json#/$defs/Timestamp" },
        "difficulty": { "type": "string" },
        "nonce": { "type": "integer" }
      },
      "required": ["hash", "height", "previousHash", "merkleRoot", "timestamp"]
    },

    "Block": {
      "type": "object",
      "allOf": [{ "$ref": "#/$defs/BlockHeader" }],
      "properties": {
        "transactions": {
          "type": "array",
          "items": { "$ref": "common.json#/$defs/TxHash" }
        },
        "size": { "type": "integer" },
        "weight": { "type": "integer" },
        "txCount": { "type": "integer" }
      },
      "required": ["transactions", "txCount"]
    },

    "TransactionStatus": {
      "type": "string",
      "enum": ["pending", "confirmed", "failed", "replaced"]
    },

    "Transaction": {
      "type": "object",
      "properties": {
        "txid": { "$ref": "common.json#/$defs/TxHash" },
        "blockHash": { "$ref": "common.json#/$defs/BlockHash" },
        "blockHeight": { "type": "integer" },
        "confirmations": { "type": "integer" },
        "timestamp": { "$ref": "common.json#/$defs/Timestamp" },
        "status": { "$ref": "#/$defs/TransactionStatus" },
        "raw": { "type": "string" },
        "size": { "type": "integer" },
        "fee": { "$ref": "common.json#/$defs/Amount" },
        "inputs": { "type": "array" },
        "outputs": { "type": "array" }
      },
      "required": ["txid", "status"]
    },

    "GetBlockRequest": {
      "type": "object",
      "properties": {
        "hash": { "$ref": "common.json#/$defs/BlockHash" },
        "height": { "type": "integer" },
        "includeTransactions": { "type": "boolean", "default": true }
      },
      "oneOf": [
        { "required": ["hash"] },
        { "required": ["height"] }
      ]
    },

    "GetTransactionRequest": {
      "type": "object",
      "properties": {
        "txid": { "$ref": "common.json#/$defs/TxHash" }
      },
      "required": ["txid"]
    },

    "SendTransactionRequest": {
      "type": "object",
      "properties": {
        "raw": { "type": "string", "description": "Hex-encoded signed transaction" }
      },
      "required": ["raw"]
    },

    "SendTransactionResponse": {
      "type": "object",
      "properties": {
        "txid": { "$ref": "common.json#/$defs/TxHash" },
        "accepted": { "type": "boolean" }
      },
      "required": ["txid", "accepted"]
    },

    "FeeEstimate": {
      "type": "object",
      "properties": {
        "priority": { "$ref": "common.json#/$defs/Priority" },
        "feeRate": { "$ref": "common.json#/$defs/Amount" },
        "estimatedBlocks": { "type": "integer" }
      },
      "required": ["priority", "feeRate", "estimatedBlocks"]
    },

    "EstimateFeeRequest": {
      "type": "object",
      "properties": {
        "priority": { "$ref": "common.json#/$defs/Priority" },
        "targetBlocks": { "type": "integer", "minimum": 1, "maximum": 100 }
      }
    },

    "ChainInfo": {
      "type": "object",
      "properties": {
        "chain": { "type": "string" },
        "network": { "type": "string" },
        "height": { "type": "integer" },
        "bestBlockHash": { "$ref": "common.json#/$defs/BlockHash" },
        "difficulty": { "type": "string" },
        "medianTime": { "$ref": "common.json#/$defs/Timestamp" },
        "chainWork": { "type": "string" },
        "syncing": { "type": "boolean" },
        "syncProgress": { "type": "number", "minimum": 0, "maximum": 1 }
      },
      "required": ["chain", "network", "height", "bestBlockHash"]
    },

    "MempoolInfo": {
      "type": "object",
      "properties": {
        "size": { "type": "integer" },
        "bytes": { "type": "integer" },
        "usage": { "type": "integer" },
        "maxMempool": { "type": "integer" },
        "minFee": { "$ref": "common.json#/$defs/Amount" }
      },
      "required": ["size", "bytes"]
    },

    "SubscriptionType": {
      "type": "string",
      "enum": ["blocks", "transactions", "address", "mempool"]
    },

    "SubscribeRequest": {
      "type": "object",
      "properties": {
        "type": { "$ref": "#/$defs/SubscriptionType" },
        "address": { "$ref": "common.json#/$defs/Address" }
      },
      "required": ["type"]
    },

    "Subscription": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "type": { "$ref": "#/$defs/SubscriptionType" },
        "createdAt": { "$ref": "common.json#/$defs/Timestamp" }
      },
      "required": ["id", "type"]
    },

    "BlockNotification": {
      "type": "object",
      "properties": {
        "type": { "const": "block" },
        "block": { "$ref": "#/$defs/Block" }
      },
      "required": ["type", "block"]
    },

    "TransactionNotification": {
      "type": "object",
      "properties": {
        "type": { "const": "transaction" },
        "transaction": { "$ref": "#/$defs/Transaction" },
        "address": { "$ref": "common.json#/$defs/Address" }
      },
      "required": ["type", "transaction"]
    }
  }
}
