{
  "openapi": "3.1.0",
  "info": {
    "title": "Greek Options API",
    "version": "0.1.0",
    "description": "Non-custodial HTTP API for listing Greek.fi option markets on Ethereum mainnet, reading indicative prices, preparing wallet approvals, and fetching firm executable RFQ quotes settled through Bebop."
  },
  "servers": [
    {
      "url": "https://api00.greek.fi",
      "description": "Production (Ethereum mainnet)"
    }
  ],
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "X-Api-Key",
        "description": "Optional integrator key (gk_live_...) for dedicated per-integrator RPM quotas and attribution."
      }
    },
    "schemas": {
      "ErrorResponse": {
        "type": "object",
        "required": ["requestId", "error", "upstreamRequestId"],
        "properties": {
          "requestId": { "type": "string", "example": "gk-3f1c2a8e-0000-0000-0000-000000000000" },
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" }
            }
          },
          "upstreamRequestId": { "type": ["string", "null"] }
        }
      },
      "UnsignedTx": {
        "type": "object",
        "required": ["to", "data", "value"],
        "properties": {
          "to": { "type": "string", "pattern": "^0x[0-9a-fA-F]{40}$" },
          "data": { "type": "string", "pattern": "^0x[0-9a-fA-F]*$" },
          "value": { "type": "string", "description": "Wei as a decimal string" }
        }
      },
      "Instrument": {
        "type": "object",
        "required": [
          "instrument", "receipt", "symbol", "underlying", "quote", "collateral",
          "settlement", "collateralSymbol", "decimals", "collateralDecimals",
          "optionType", "strike", "strikeRaw", "expiration", "canSell", "canBuy", "factory"
        ],
        "properties": {
          "instrument": { "type": "string" },
          "receipt": { "type": "string" },
          "name": { "type": "string" },
          "symbol": { "type": "string" },
          "receiptSymbol": { "type": "string" },
          "underlying": { "type": "string" },
          "quote": { "type": "string" },
          "collateral": { "type": "string" },
          "settlement": { "type": "string" },
          "collateralSymbol": { "type": "string" },
          "settlementSymbol": { "type": "string" },
          "decimals": { "type": "integer" },
          "collateralDecimals": { "type": "integer" },
          "settlementDecimals": { "type": "integer" },
          "optionType": { "type": "string", "enum": ["call", "put"] },
          "exerciseStyle": { "type": "string" },
          "strike": { "type": "string" },
          "strikeRaw": { "type": "string" },
          "expiration": { "type": "integer" },
          "exerciseDeadline": { "type": "integer" },
          "exerciseWindowSeconds": { "type": "integer" },
          "canSell": { "type": "boolean" },
          "canBuy": { "type": "boolean" },
          "factory": { "type": "string" },
          "exercisedTotal": { "type": "string" },
          "maxUnderlyingAmount": { "type": ["string", "null"] },
          "lastExerciseAt": { "type": ["integer", "null"] }
        }
      },
      "QuoteResponse": {
        "type": "object",
        "required": ["quoteId", "positionId", "chainId", "instrument", "side", "amount", "premium", "fee", "expiry", "tx"],
        "properties": {
          "requestId": { "type": "string" },
          "quoteId": { "type": "string" },
          "positionId": { "type": "string" },
          "chainId": { "type": "integer", "const": 1 },
          "instrument": { "type": "string" },
          "side": { "type": "string", "enum": ["sell", "buy"] },
          "amount": { "type": "string" },
          "premium": { "type": "string" },
          "fee": { "type": "string" },
          "expiry": { "type": "integer" },
          "issues": { "type": ["array", "null"], "items": { "type": "string" } },
          "tx": { "$ref": "#/components/schemas/UnsignedTx" },
          "iv": { "type": "number" },
          "delta": { "type": "number" },
          "spotPrice": { "type": "number" },
          "display": { "type": "object" }
        }
      },
      "RollResponse": {
        "type": "object",
        "required": ["rollId", "chainId", "close", "open", "netPremium", "fee", "expiry", "tx"],
        "properties": {
          "requestId": { "type": "string" },
          "rollId": { "type": "string" },
          "chainId": { "type": "integer", "const": 1 },
          "close": { "type": "object" },
          "open": { "type": "object" },
          "netPremium": { "type": "string" },
          "collateralDelta": { "type": "string" },
          "fee": { "type": "string" },
          "expiry": { "type": "integer" },
          "issues": { "type": ["array", "null"], "items": { "type": "string" } },
          "tx": { "$ref": "#/components/schemas/UnsignedTx" },
          "display": { "type": "object" },
          "assignmentRisk": { "type": "object" }
        }
      },
      "Position": {
        "type": "object",
        "required": ["positionId", "takerAddress", "instrument", "status"],
        "properties": {
          "positionId": { "type": "string" },
          "openingQuoteId": { "type": "string" },
          "chainId": { "type": "integer" },
          "instrument": { "type": "string" },
          "takerAddress": { "type": "string" },
          "openedAmount": { "type": "string" },
          "closedAmount": { "type": "string" },
          "remainingAmount": { "type": "string" },
          "premiumReceived": { "type": "string" },
          "premiumPaid": { "type": "string" },
          "status": {
            "type": "string",
            "enum": ["open", "closed", "expired_pending_exercise", "exercised", "expired_unexercised", "redeemed"]
          },
          "openingTransactionHash": { "type": "string" },
          "openedAt": { "type": "integer" },
          "updatedAt": { "type": "integer" },
          "rtBalance": { "type": "string" },
          "pnl": { "type": "object" }
        }
      },
      "ReadinessStep": {
        "type": "object",
        "required": ["kind", "description", "tx"],
        "properties": {
          "kind": { "type": "string", "enum": ["wrap", "approve", "addPermissions"] },
          "description": { "type": "string" },
          "tx": { "$ref": "#/components/schemas/UnsignedTx" }
        }
      },
      "ReadinessResponse": {
        "type": "object",
        "required": ["wallet", "trade", "instrument", "ready", "issues", "checks", "steps", "blockNumber"],
        "properties": {
          "wallet": { "type": "string" },
          "trade": { "type": "string", "enum": ["open", "close", "roll"] },
          "positionId": { "type": "string" },
          "instrument": { "type": "string" },
          "amount": { "type": "string" },
          "ready": { "type": "boolean" },
          "canSell": { "type": "boolean" },
          "issues": { "type": "array", "items": { "type": "string" } },
          "checks": { "type": "object" },
          "steps": { "type": "array", "items": { "$ref": "#/components/schemas/ReadinessStep" } },
          "blockNumber": { "type": "integer" }
        }
      }
    }
  },
  "security": [
    {},
    { "ApiKeyAuth": [] }
  ],
  "paths": {
    "/options/ethereum/v1/instruments": {
      "get": {
        "operationId": "listInstruments",
        "summary": "List option instruments",
        "description": "Edge-cached for 60 seconds (stale-while-revalidate 60s).",
        "parameters": [
          { "name": "instrument", "in": "query", "schema": { "type": "string" } },
          { "name": "underlying", "in": "query", "schema": { "type": "string" } },
          { "name": "quote", "in": "query", "schema": { "type": "string" } },
          { "name": "option_type", "in": "query", "schema": { "type": "string", "enum": ["call", "put"] } },
          { "name": "expiration_from", "in": "query", "schema": { "type": "integer" } },
          { "name": "expiration_to", "in": "query", "schema": { "type": "integer" } },
          { "name": "includeExpired", "in": "query", "schema": { "type": "boolean" } },
          { "name": "includeRetiredFactories", "in": "query", "schema": { "type": "boolean" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 200 } },
          { "name": "cursor", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Paginated instruments",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "instruments": { "type": "array", "items": { "$ref": "#/components/schemas/Instrument" } },
                    "nextCursor": { "type": ["string", "null"] }
                  }
                }
              }
            }
          },
          "400": { "description": "Invalid parameter", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/options/ethereum/v1/assets/{underlying}/summary": {
      "get": {
        "operationId": "getAssetSummary",
        "summary": "Indicative pricing summary for an underlying",
        "description": "Edge-cached for 3 seconds. Indicative only, never executable.",
        "parameters": [
          {
            "name": "underlying",
            "in": "path",
            "required": true,
            "description": "Symbol (ETH, cbBTC) or token address",
            "schema": { "type": "string" }
          }
        ],
        "responses": {
          "200": { "description": "Indicative pricing summary", "content": { "application/json": { "schema": { "type": "object" } } } },
          "400": { "description": "Invalid underlying", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/options/ethereum/v1/status": {
      "get": {
        "operationId": "getVenueStatus",
        "summary": "Upstream venue status",
        "description": "Edge-cached for 10 seconds.",
        "responses": {
          "200": {
            "description": "Venue availability",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": { "status": { "type": "string" } }
                }
              }
            }
          }
        }
      }
    },
    "/options/ethereum/v1/quote": {
      "get": {
        "operationId": "getOpenQuote",
        "summary": "Firm sell-to-open quote",
        "description": "Never cached. Requires funded collateral token balance on-chain and zero existing option token balance for the instrument.",
        "parameters": [
          { "name": "instrument", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "amount", "in": "query", "required": true, "description": "Raw collateral units", "schema": { "type": "string" } },
          { "name": "taker_address", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "validate", "in": "query", "required": false, "schema": { "type": "boolean" } }
        ],
        "responses": {
          "200": { "description": "Executable quote", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuoteResponse" } } } },
          "400": { "description": "Validation error", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "403": { "description": "Sanctioned or revoked key", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "409": { "description": "Wallet not funded or holds existing option tokens", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "429": { "description": "Rate limited or duplicate quote", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "451": { "description": "Restricted jurisdiction", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } },
          "503": { "description": "NoValidMakerQuote or UpstreamBusy", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/options/ethereum/v1/positions/{positionId}/quote": {
      "get": {
        "operationId": "getCloseQuote",
        "summary": "Firm buy-to-close quote",
        "description": "Never cached. Requires positive receipt balance on-chain.",
        "parameters": [
          { "name": "positionId", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "amount", "in": "query", "required": false, "description": "Optional partial close amount in raw units", "schema": { "type": "string" } },
          { "name": "validate", "in": "query", "required": false, "schema": { "type": "boolean" } }
        ],
        "responses": {
          "200": { "description": "Executable close quote", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/QuoteResponse" } } } },
          "409": { "description": "Wallet holds no receipt tokens", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/options/ethereum/v1/positions/{positionId}/roll": {
      "get": {
        "operationId": "getRollQuote",
        "summary": "Firm atomic roll quote",
        "description": "Never cached. Closes positionId and opens instrument in one transaction.",
        "parameters": [
          { "name": "positionId", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "instrument", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "validate", "in": "query", "required": false, "schema": { "type": "boolean" } }
        ],
        "responses": {
          "200": { "description": "Executable roll quote", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/RollResponse" } } } },
          "409": { "description": "Wallet holds no receipt tokens", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/options/ethereum/v1/positions/{positionId}": {
      "get": {
        "operationId": "getPosition",
        "summary": "Get position by ID",
        "parameters": [
          { "name": "positionId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Position details", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Position" } } } },
          "404": { "description": "Position not found", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ErrorResponse" } } } }
        }
      }
    },
    "/options/ethereum/v1/positions/{positionId}/transactions": {
      "get": {
        "operationId": "getPositionTransactions",
        "summary": "Get fill history for a position",
        "parameters": [
          { "name": "positionId", "in": "path", "required": true, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Position transactions", "content": { "application/json": { "schema": { "type": "object" } } } }
        }
      }
    },
    "/greek/v1/wallets/{wallet}/positions": {
      "get": {
        "operationId": "listWalletPositions",
        "summary": "List positions for one wallet",
        "parameters": [
          { "name": "wallet", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "minimum": 1, "maximum": 50, "default": 20 } },
          { "name": "cursor", "in": "query", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "Wallet positions",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "positions": { "type": "array", "items": { "$ref": "#/components/schemas/Position" } },
                    "nextCursor": { "type": ["string", "null"] }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/greek/v1/readiness": {
      "get": {
        "operationId": "getReadiness",
        "summary": "Check on-chain readiness and get unsigned setup transactions",
        "parameters": [
          { "name": "wallet", "in": "query", "required": true, "schema": { "type": "string" } },
          { "name": "trade", "in": "query", "required": true, "schema": { "type": "string", "enum": ["open", "close", "roll"] } },
          { "name": "instrument", "in": "query", "required": false, "schema": { "type": "string" } },
          { "name": "amount", "in": "query", "required": false, "schema": { "type": "string" } },
          { "name": "position_id", "in": "query", "required": false, "schema": { "type": "string" } },
          { "name": "premium", "in": "query", "required": false, "schema": { "type": "string" } }
        ],
        "responses": {
          "200": { "description": "Readiness report and setup txs", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/ReadinessResponse" } } } }
        }
      }
    },
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Proxy health and circuit breaker state",
        "responses": {
          "200": {
            "description": "Health status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "ok": { "type": "boolean" },
                    "version": { "type": "string" },
                    "upstream": {
                      "type": "object",
                      "properties": {
                        "lastStatus": { "type": ["integer", "null"] },
                        "lastSuccessAt": { "type": ["integer", "null"] },
                        "breaker": { "type": "string", "enum": ["open", "closed"] },
                        "throttledTakers": { "type": "integer" }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
