{
  "openapi": "3.0.3",
  "info": {
    "title": "SandwichCheck API",
    "version": "0.2.0",
    "description": "Read-only API for transaction-level MEV signals on Ethereum Mainnet and Sepolia."
  },
  "servers": [{"url": "https://sandwichcheck.com"}],
  "paths": {
    "/health": {
      "get": {"summary": "Health check", "responses": {"200": {"description": "Backend is healthy"}}}
    },
    "/api/network": {
      "get": {"summary": "Configured network status", "responses": {"200": {"description": "Network and credential presence (never secrets)"}}}
    },
    "/api/analyze": {
      "get": {
        "summary": "Analyze a Mainnet or Sepolia transaction for supported MEV patterns",
        "parameters": [{"in": "query", "name": "q", "required": true, "schema": {"type": "string"}, "description": "Transaction hash or wallet address"}, {"in": "query", "name": "network", "required": false, "schema": {"type": "string", "enum": ["mainnet", "sepolia"], "default": "mainnet"}}],
        "responses": {"200": {"description": "Forensic classification and evidence"}, "400": {"description": "Invalid transaction hash, address, or network"}, "502": {"description": "Ethereum RPC request failed"}, "503": {"description": "Selected network RPC is not configured"}}
      }
    },
    "/api/agent/check": {
      "post": {
        "summary": "Check an autonomous-wallet transaction policy",
        "description": "Evaluates an allowlisted router, native spend limit, optional simulation verdict, and records an audit ID. Slippage is not evaluated without protocol-specific minimum-output data.",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/AgentPolicyInput"}}}},
        "responses": {"200": {"description": "Policy checks and audit ID"}, "400": {"description": "Invalid policy or transaction hash"}, "404": {"description": "Transaction not found"}}
      }
    },
    "/api/runs": {
      "post": {"summary": "Queue a test run", "responses": {"200": {"description": "Queued run"}}}
    },
    "/api/runs/latest": {
      "get": {"summary": "Get the latest run", "responses": {"200": {"description": "Latest run or null"}}}
    },
    "/api/opportunities": {
      "get": {"summary": "List latest sandwich opportunities", "responses": {"200": {"description": "Newest 50 opportunities"}}},
      "post": {
        "summary": "Record a sandwich-worthy transaction",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/OpportunityInput"}}}},
        "responses": {"200": {"description": "Recorded opportunity"}, "400": {"description": "Missing required fields"}}
      }
    }
  },
  "components": {
    "schemas": {
      "OpportunityInput": {
        "type": "object",
        "required": ["tx_hash", "chain_id", "estimated_profit_wei", "estimated_victim_loss_wei", "detection_source", "minimum_profit_wei"],
        "properties": {
          "tx_hash": {"type": "string", "example": "0xabc123"},
          "chain_id": {"type": "integer", "example": 1},
          "estimated_profit_wei": {"type": "string", "description": "Decimal integer string"},
          "estimated_victim_loss_wei": {"type": "string", "description": "Decimal integer string"},
          "detection_source": {"type": "string", "example": "mined-receipt-window"},
          "minimum_profit_wei": {"type": "string", "description": "Threshold used by the detector"}
        }
      },
      "AgentPolicyInput": {
        "type": "object",
        "required": ["tx_hash"],
        "properties": {
          "tx_hash": {"type": "string", "description": "Mined transaction hash"},
          "network": {"type": "string", "enum": ["mainnet", "sepolia"], "default": "mainnet"},
          "allowed_routers": {"type": "array", "items": {"type": "string"}},
          "max_native_value_wei": {"type": "string", "description": "Maximum native value in decimal wei"},
          "simulated_verdict": {"type": "string", "description": "Verdict returned by a pre-sign simulation"}
        }
      }
    }
  }
}
