openapi: 3.0.3
info:
  title: Transfer Agent API
  version: "1.0.0"
  description: |
    REST API for the regulated-securities Transfer Agent (module
    `github.com/liquidity-alt/ta-revision`).

    ## Authentication (dev)
    Write operations identify the acting operator via the `X-Actor-Id` header
    (the maker). Approval and execute flows that enforce maker-checker
    segregation also accept an `X-Checker-Id` header (or `maker`/`checker`
    fields in the request body for corporate-action execution).

    ## Events
    A server-sent events (SSE) stream is served at `GET /events/stream`. Each
    frame is a JSON object `{ "type", "data", "time" }` where `type` is one of
    `operation_update`, `recon_result`, `drift_alert`, `chain_event`,
    `settlement_settled`.
servers:
  - url: http://localhost:8080
    description: Local development

tags:
  - name: Identity
  - name: Compliance
  - name: Lifecycle
  - name: Operations
  - name: Register
  - name: Corporate Actions
  - name: Reconciliation
  - name: Trading
  - name: Assets
  - name: Settlement
  - name: Audit
  - name: Observability

components:
  parameters:
    ActorHeader:
      name: X-Actor-Id
      in: header
      required: false
      description: The acting operator (maker). Required for most write operations.
      schema:
        type: string
        example: operator.alice
    CheckerHeader:
      name: X-Checker-Id
      in: header
      required: false
      description: The checker for maker-checker enforced operations.
      schema:
        type: string
        example: operator.bob
  schemas:
    Error:
      type: object
      properties:
        error:
          type: string
      example:
        error: legal_basis is required

    Position:
      type: object
      properties:
        token_id: { type: string }
        holder_id: { type: string }
        wallet: { type: string }
        balance: { type: string, description: Stringified integer (wei-scale). }
        frozen_amount: { type: string }
        as_of_block: { type: integer, format: int64 }
      required: [token_id, holder_id, wallet, balance, frozen_amount, as_of_block]

    Identity:
      type: object
      properties:
        wallet: { type: string }
        holder_id: { type: string }
        email: { type: string }
        country: { type: string }
        accreditation: { type: string }
        kyc_status: { type: string }
        verdict:
          type: string
          description: Onboarding verdict (returned by register).
        registered:
          type: boolean
          description: Whether the wallet was registered on-chain.
        tx_hash: { type: string }
      required: [wallet, holder_id, country, accreditation]

    RegisterRequest:
      type: object
      properties:
        wallet: { type: string }
        email: { type: string }
        country: { type: string }
        accreditation: { type: string }
      required: [wallet, email, country, accreditation]

    DeregisterRequest:
      type: object
      properties:
        wallet: { type: string }
      required: [wallet]

    EligibilityResult:
      type: object
      properties:
        eligible: { type: boolean }
        wallet: { type: string }
      required: [eligible, wallet]

    ComplianceRule:
      type: object
      properties:
        id: { type: string }
        version: { type: string }
        type: { type: string }
        parameters:
          type: object
          additionalProperties: true
        active_from: { type: string, format: date-time }
        active_to:
          type: string
          format: date-time
          nullable: true
      required: [id, version, type, parameters, active_from]

    CreateRuleRequest:
      type: object
      properties:
        id:
          type: string
          description: Optional — append a new version to this logical rule.
        type: { type: string }
        parameters:
          type: object
          additionalProperties: true
        active_from: { type: string, format: date-time }
        active_to:
          type: string
          format: date-time
          nullable: true
      required: [type, parameters]

    EvaluateRequest:
      type: object
      properties:
        token_id: { type: string }
        from: { type: string }
        to: { type: string }
        amount: { type: string }
      required: [token_id, from, to, amount]

    ComplianceResult:
      type: object
      properties:
        allowed: { type: boolean }
        failed_rule: { type: string }
        reason: { type: string }
        rule_set_version: { type: string }
      required: [allowed, rule_set_version]

    Operation:
      type: object
      properties:
        id: { type: string }
        kind:
          type: string
          enum: [mint, burn, freeze, unfreeze, forced_transfer]
        maker: { type: string }
        checker:
          type: string
          nullable: true
        state:
          type: string
          enum: [pending_approval, approved, rejected, executed]
        tx_hash:
          type: string
          nullable: true
        legal_basis: { type: string }
        payload:
          type: object
          additionalProperties: true
        created_at: { type: string, format: date-time }
      required: [id, kind, maker, state, created_at]

    MintRequest:
      type: object
      properties:
        to: { type: string }
        amount: { type: string }
      required: [to, amount]

    BurnRequest:
      type: object
      properties:
        from: { type: string }
        amount: { type: string }
      required: [from, amount]

    FreezeRequest:
      type: object
      properties:
        address: { type: string }
        frozen: { type: boolean }
        legal_basis: { type: string }
      required: [address, frozen, legal_basis]

    ForcedTransferRequest:
      type: object
      properties:
        from: { type: string }
        to: { type: string }
        amount: { type: string }
        legal_basis: { type: string }
      required: [from, to, amount, legal_basis]

    Distribution:
      type: object
      properties:
        id: { type: string }
        token_id: { type: string }
        record_date_block: { type: integer, format: int64 }
        rate: { type: string }
        status:
          type: string
          enum: [declared, snapshotted, computed, executed]
        total_holders: { type: integer }
        total_amount: { type: string }
      required: [id, token_id, record_date_block, rate, status]

    DistributionWithEntitlements:
      allOf:
        - $ref: '#/components/schemas/Distribution'
        - type: object
          properties:
            entitlements:
              type: array
              items: { $ref: '#/components/schemas/Entitlement' }

    Entitlement:
      type: object
      properties:
        distribution_id: { type: string }
        holder_id: { type: string }
        wallet: { type: string }
        amount: { type: string }
        paid: { type: boolean }
        tx_hash: { type: string }
      required: [distribution_id, holder_id, wallet, amount, paid]

    DeclareDistributionRequest:
      type: object
      properties:
        token_id: { type: string }
        record_date_block: { type: integer, format: int64 }
        rate: { type: string }
      required: [token_id, record_date_block, rate]

    ExecuteDistributionRequest:
      type: object
      properties:
        maker: { type: string }
        checker: { type: string }
      required: [maker, checker]

    ReconRun:
      type: object
      properties:
        id: { type: string }
        token_id: { type: string }
        started: { type: string, format: date-time }
        finished:
          type: string
          format: date-time
          nullable: true
        status:
          type: string
          enum: [ok, drift, running, failed]
      required: [id, token_id, started, status]

    ReconRunRequest:
      type: object
      properties:
        token: { type: string }
      required: [token]

    SettleRequest:
      type: object
      properties:
        trade_id: { type: string }
        token: { type: string, description: Security token address; optional. }
        cash: { type: string, description: Cash token address; optional. }
        seller: { type: string }
        buyer: { type: string }
        security_amount: { type: string }
        cash_amount: { type: string }
      required: [trade_id, seller, buyer, security_amount, cash_amount]

    V42FillOrder:
      type: object
      description: One already-vouched V42 TokenSettlement order intent.
      properties:
        trader: { type: string, description: V42 multisig wallet address. }
        trader_ref: { type: string, description: Logical trader/investor reference; wallet-api resolves the V42 address. }
        asset_out: { type: string }
        max_amount_out: { type: string }
        asset_in: { type: string }
        min_amount_in: { type: string }
        nonce: { type: string, description: Nonce used when this side vouched the order. }
        salt: { type: string, description: Optional bytes32 salt; zero salt when omitted. }
      required: [asset_out, max_amount_out, asset_in, min_amount_in, nonce]

    V42FillRequest:
      type: object
      description: Matched, already-vouched maker/taker pair for TokenSettlement.fillBatch.
      properties:
        trade_id: { type: string }
        seller: { $ref: '#/components/schemas/V42FillOrder' }
        buyer: { $ref: '#/components/schemas/V42FillOrder' }
        fill_asset_amount: { type: string }
        fee_bps: { type: integer, default: 0 }
      required: [trade_id, seller, buyer, fill_asset_amount]

    V42FillResponse:
      type: object
      properties:
        trade_id: { type: string }
        status: { type: string, enum: [submitted, settled] }
        tx_hash: { type: string }
        block_number: { type: integer, format: int64 }
      required: [trade_id, status, tx_hash]

    TradingCapabilities:
      type: object
      properties:
        backend:
          type: string
          example: wallet-api
        routes:
          type: array
          items: { type: string }
      required: [backend, routes]

    TradingProposeRequest:
      type: object
      description: liq-ats order-vouch request. TA delegates to wallet-api Propose.
      properties:
        wallet:
          type: string
          description: V42 wallet address or logical trader/investor reference.
        signer_wallet_id:
          type: string
          description: Optional signer wallet id; empty lets wallet-api resolve by wallet/ref.
        role:
          type: string
          enum: [maker, taker]
        asset_out: { type: string }
        asset_in: { type: string }
        max_amount_out: { type: string }
        min_amount_in: { type: string }
        gas_mode:
          type: string
          enum: [fund, sponsor]
      required: [wallet, role, asset_out, asset_in, max_amount_out, min_amount_in]

    TradingProposeResponse:
      type: object
      properties:
        wallet: { type: string }
        digest: { type: string }
        tx_hash: { type: string }
        nonce: { type: string }
      required: [wallet, tx_hash, nonce]

    BuiltTransaction:
      type: object
      description: Unsigned EVM transaction envelope returned by wallet-api builder routes.
      properties:
        from: { type: string }
        to: { type: string }
        nonce: { type: integer, format: int64 }
        gas: { type: integer, format: int64 }
        gas_price: { type: string }
        value: { type: string }
        data: { type: string }
        chain_id: { type: integer, format: int64 }
        sign_hash:
          type: string
          description: 32-byte EIP-155 signing hash to externally sign and broadcast.
      required: [from, to, nonce, gas, gas_price, value, data, chain_id, sign_hash]

    TradingCancelRequest:
      type: object
      description: >
        Builder-backed on-chain V42 cancel request. TA delegates to wallet-api
        `/trade/cancel-tx`; the caller owns external MPC signing and broadcast.
      properties:
        order_id:
          oneOf:
            - { type: string }
            - { type: integer, format: int64 }
          description: V42 order id emitted by the order vouch.
        wallet:
          type: string
          description: V42 wallet address.
        signer_address:
          type: string
          description: Wallet signer address that must sign the built cancel tx.
        reason: { type: string }
      required: [order_id, wallet, signer_address]

    TradingCancelResponse:
      type: object
      properties:
        order_id: { type: string }
        status:
          type: string
          enum: [built]
        signing_hash: { type: string }
        tx: { $ref: '#/components/schemas/BuiltTransaction' }
      required: [order_id, status, signing_hash, tx]

    CreateAssetRequest:
      type: object
      description: liq-ats-facing asset/regime deployment request. TA delegates to wallet-api CreateAsset.
      properties:
        name: { type: string }
        symbol: { type: string }
        decimals: { type: integer, default: 18 }
        asset_type:
          type: string
          description: Optional wallet-api asset type override.
        to:
          type: string
          description: Optional initial holder.
        amount:
          type: string
          description: Optional initial supply/mint amount.
      required: [name, symbol, decimals]

    Asset:
      type: object
      properties:
        token: { type: string }
        name: { type: string }
        symbol: { type: string }
        decimals: { type: integer }
        asset_type: { type: string }
        compliance: { type: string }
        minted: { type: boolean }
        initial_holder: { type: string }
        initial_supply: { type: string }
        mint_tx: { type: string }
        tx_hash: { type: string }
      required: [token, name, symbol, decimals, asset_type, compliance, tx_hash]

    Settlement:
      type: object
      properties:
        trade_id: { type: string }
        tx_hash: { type: string }
        token: { type: string }
        cash: { type: string }
        seller: { type: string }
        buyer: { type: string }
        security_amount: { type: string }
        cash_amount: { type: string }
        status: { type: string }
        block_number: { type: integer, format: int64 }
      required: [trade_id, status]

    AuditEntry:
      type: object
      properties:
        id: { type: string }
        seq: { type: integer }
        actor: { type: string }
        action: { type: string }
        inputs_hash: { type: string }
        decision: { type: string }
        rule_set_version: { type: string }
        tx_hash: { type: string }
        prev_hash: { type: string }
        entry_hash: { type: string }
        created_at: { type: string, format: date-time }
      required: [id, seq, actor, action, decision, entry_hash, created_at]

    AuditVerifyResult:
      type: object
      properties:
        ok: { type: boolean }
        broken_seq: { type: integer }
      required: [ok, broken_seq]

  responses:
    NotFound:
      description: Resource not found
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }
    BadRequest:
      description: Invalid request body
      content:
        application/json:
          schema: { $ref: '#/components/schemas/Error' }

paths:
  # ---------------------------------------------------------------------------
  # Identity / allowlist
  # ---------------------------------------------------------------------------
  /identity/register:
    post:
      tags: [Identity]
      summary: Onboard a wallet (KYC + allowlist registration)
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/RegisterRequest' }
      responses:
        '201':
          description: Onboarding result with verdict
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Identity' }
        '400': { $ref: '#/components/responses/BadRequest' }

  /identity/deregister:
    post:
      tags: [Identity]
      summary: Remove a wallet from the allowlist
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/DeregisterRequest' }
      responses:
        '200':
          description: Deregistration acknowledged
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Identity' }

  /identity/{wallet}:
    get:
      tags: [Identity]
      summary: Fetch the identity record for a wallet
      parameters:
        - name: wallet
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Identity record
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Identity' }
        '404': { $ref: '#/components/responses/NotFound' }

  /eligibility/{wallet}:
    get:
      tags: [Identity]
      summary: Check transfer eligibility for a wallet
      parameters:
        - name: wallet
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Eligibility verdict
          content:
            application/json:
              schema: { $ref: '#/components/schemas/EligibilityResult' }

  # ---------------------------------------------------------------------------
  # Compliance
  # ---------------------------------------------------------------------------
  /compliance/evaluate:
    post:
      tags: [Compliance]
      summary: Pre-trade compliance evaluation
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/EvaluateRequest' }
      responses:
        '200':
          description: Compliance result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/ComplianceResult' }
        '400': { $ref: '#/components/responses/BadRequest' }

  /compliance/rules:
    get:
      tags: [Compliance]
      summary: List active compliance rules
      responses:
        '200':
          description: Rules
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/ComplianceRule' }
    post:
      tags: [Compliance]
      summary: Create a rule or append a new version
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CreateRuleRequest' }
      responses:
        '201':
          description: Created rule version
          content:
            application/json:
              schema: { $ref: '#/components/schemas/ComplianceRule' }
        '400': { $ref: '#/components/responses/BadRequest' }

  /compliance/rules/{id}/versions:
    get:
      tags: [Compliance]
      summary: Version history for a logical rule
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Ordered rule versions
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/ComplianceRule' }

  # ---------------------------------------------------------------------------
  # Lifecycle (each creates an Operation in pending_approval)
  # ---------------------------------------------------------------------------
  /lifecycle/mint:
    post:
      tags: [Lifecycle]
      summary: Create a mint operation
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/MintRequest' }
      responses:
        '201':
          description: Pending operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }
        '401':
          description: Missing X-Actor-Id header
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /lifecycle/burn:
    post:
      tags: [Lifecycle]
      summary: Create a burn operation
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/BurnRequest' }
      responses:
        '201':
          description: Pending operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }

  /lifecycle/freeze:
    post:
      tags: [Lifecycle]
      summary: Create a freeze/unfreeze operation (legal_basis required)
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/FreezeRequest' }
      responses:
        '201':
          description: Pending operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }
        '422':
          description: legal_basis missing
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /lifecycle/forced-transfer:
    post:
      tags: [Lifecycle]
      summary: Create a forced-transfer operation (legal_basis required)
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/ForcedTransferRequest' }
      responses:
        '201':
          description: Pending operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }
        '422':
          description: legal_basis missing
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  # ---------------------------------------------------------------------------
  # Operations / approvals
  # ---------------------------------------------------------------------------
  /operations:
    get:
      tags: [Operations]
      summary: List operations by state
      parameters:
        - name: state
          in: query
          schema:
            type: string
            enum: [pending_approval, approved, rejected, executed]
            default: pending_approval
      responses:
        '200':
          description: Operations
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/Operation' }

  /operations/{id}:
    get:
      tags: [Operations]
      summary: Fetch a single operation
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }
        '404': { $ref: '#/components/responses/NotFound' }

  /operations/{id}/approve:
    post:
      tags: [Operations]
      summary: Approve a pending operation (checker)
      description: |
        Maker-checker is enforced: the actor named in `X-Actor-Id` cannot be the
        same operator that created the operation (the maker).
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
        - { $ref: '#/components/parameters/ActorHeader' }
        - { $ref: '#/components/parameters/CheckerHeader' }
      responses:
        '200':
          description: Approved operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }
        '409':
          description: Maker attempted to approve their own operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /operations/{id}/reject:
    post:
      tags: [Operations]
      summary: Reject a pending operation
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
        - { $ref: '#/components/parameters/ActorHeader' }
      responses:
        '200':
          description: Rejected operation
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Operation' }

  # ---------------------------------------------------------------------------
  # Register (read model)
  # ---------------------------------------------------------------------------
  /positions:
    get:
      tags: [Register]
      summary: Current positions, optionally filtered by token
      parameters:
        - name: token
          in: query
          schema: { type: string }
      responses:
        '200':
          description: Positions
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/Position' }

  /positions/at:
    get:
      tags: [Register]
      summary: Historical positions at a given block
      parameters:
        - name: token
          in: query
          required: true
          schema: { type: string }
        - name: block
          in: query
          required: true
          schema: { type: integer, format: int64 }
      responses:
        '200':
          description: Positions as of the requested block
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/Position' }

  /holders/{id}/positions:
    get:
      tags: [Register]
      summary: All positions for a holder
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Positions
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/Position' }

  # ---------------------------------------------------------------------------
  # Corporate actions
  # ---------------------------------------------------------------------------
  /distributions:
    post:
      tags: [Corporate Actions]
      summary: Declare a distribution
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/DeclareDistributionRequest' }
      responses:
        '201':
          description: Declared distribution
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Distribution' }

  /distributions/{id}/snapshot:
    post:
      tags: [Corporate Actions]
      summary: Snapshot holders of record at the record date
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
        - { $ref: '#/components/parameters/ActorHeader' }
      responses:
        '200':
          description: Distribution with snapshot taken
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Distribution' }
        '404': { $ref: '#/components/responses/NotFound' }

  /distributions/{id}/compute:
    post:
      tags: [Corporate Actions]
      summary: Compute per-holder entitlements
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
        - { $ref: '#/components/parameters/ActorHeader' }
      responses:
        '200':
          description: Distribution with computed entitlements
          content:
            application/json:
              schema: { $ref: '#/components/schemas/DistributionWithEntitlements' }
        '404': { $ref: '#/components/responses/NotFound' }

  /distributions/{id}/execute:
    post:
      tags: [Corporate Actions]
      summary: Execute payment (maker-checker)
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
        - { $ref: '#/components/parameters/ActorHeader' }
        - { $ref: '#/components/parameters/CheckerHeader' }
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/ExecuteDistributionRequest' }
      responses:
        '200':
          description: Executed distribution with paid entitlements
          content:
            application/json:
              schema: { $ref: '#/components/schemas/DistributionWithEntitlements' }
        '404': { $ref: '#/components/responses/NotFound' }

  /distributions/{id}:
    get:
      tags: [Corporate Actions]
      summary: Fetch a distribution and its entitlements
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Distribution
          content:
            application/json:
              schema: { $ref: '#/components/schemas/DistributionWithEntitlements' }
        '404': { $ref: '#/components/responses/NotFound' }

  # ---------------------------------------------------------------------------
  # Reconciliation
  # ---------------------------------------------------------------------------
  /recon/status:
    get:
      tags: [Reconciliation]
      summary: List recent reconciliation runs
      responses:
        '200':
          description: Runs
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/ReconRun' }

  /recon/run:
    post:
      tags: [Reconciliation]
      summary: Trigger a reconciliation run for a token
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/ReconRunRequest' }
      responses:
        '201':
          description: Started/completed run
          content:
            application/json:
              schema: { $ref: '#/components/schemas/ReconRun' }

  /recon/{id}:
    get:
      tags: [Reconciliation]
      summary: Fetch a reconciliation run
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Run
          content:
            application/json:
              schema: { $ref: '#/components/schemas/ReconRun' }
        '404': { $ref: '#/components/responses/NotFound' }

  # ---------------------------------------------------------------------------
  # Trading / liq-ats routes
  # ---------------------------------------------------------------------------
  /trading/capabilities:
    get:
      tags: [Trading]
      summary: Show the liq-ats trading API layer routes served by TA
      responses:
        '200':
          description: TA trading route capability list
          content:
            application/json:
              schema: { $ref: '#/components/schemas/TradingCapabilities' }

  /trading/propose:
    post:
      tags: [Trading]
      summary: Vouch an order intent through wallet-api
      description: >
        liq-ats-facing TA API route. TA accepts the stable order-vouch request,
        delegates execution to wallet-api `Propose`, and returns the nonce that
        liq-ats must persist and replay at settlement.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/TradingProposeRequest' }
      responses:
        '200':
          description: Vouch result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/TradingProposeResponse' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '422':
          description: wallet-api async job failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /trading/cancel:
    post:
      tags: [Trading]
      summary: Build a V42 cancelOrder transaction through wallet-api
      description: >
        liq-ats-facing TA API route for committed order cancellation. Current
        wallet-api support is builder-backed: TA delegates to wallet-api
        `/trade/cancel-tx` and returns the unsigned tx plus signing hash. The
        caller owns external MPC signing and broadcast until wallet-api exposes
        a server-side cancel submitter.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/TradingCancelRequest' }
      responses:
        '200':
          description: Unsigned cancel transaction built
          content:
            application/json:
              schema: { $ref: '#/components/schemas/TradingCancelResponse' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /trading/settle:
    post:
      tags: [Trading]
      summary: Settle a matched, already-vouched liq-ats order pair
      description: >
        liq-ats-facing TA API route. TA accepts the matched maker/taker fill,
        maps the nested side payload into wallet-api `Settle`, and normalizes the
        result to `{trade_id,status,tx_hash,block_number}`.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/V42FillRequest' }
      responses:
        '200':
          description: V42 fill result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/V42FillResponse' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '422':
          description: wallet-api async job failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /trading/settle/{trade_id}:
    get:
      tags: [Trading]
      summary: Fetch wallet-api settlement status through the TA API layer
      parameters:
        - name: trade_id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Settlement status
          content:
            application/json:
              schema: { $ref: '#/components/schemas/V42FillResponse' }
        '404': { $ref: '#/components/responses/NotFound' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  # ---------------------------------------------------------------------------
  # Asset / regime routes
  # ---------------------------------------------------------------------------
  /assets:
    post:
      tags: [Assets]
      summary: Create a plain ERC-3643 asset through wallet-api
      description: >
        liq-ats-facing asset deployment route. TA sets `asset_type=plain`
        unless the request supplies an explicit `asset_type`, then delegates to
        wallet-api `CreateAsset`.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CreateAssetRequest' }
      responses:
        '200':
          description: Asset deployment result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Asset' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '422':
          description: wallet-api async job failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /assets/regime:
    post:
      tags: [Assets]
      summary: Create a Reg D 506(b) typed asset through wallet-api
      description: >
        Compatibility route for liq-ats. TA defaults `asset_type=regd506b` and
        delegates to wallet-api `CreateAsset`.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CreateAssetRequest' }
      responses:
        '200':
          description: Asset deployment result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Asset' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '422':
          description: wallet-api async job failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /assets/regs:
    post:
      tags: [Assets]
      summary: Create a Reg S typed asset through wallet-api
      description: >
        Compatibility route for liq-ats. TA defaults `asset_type=regs` and
        delegates to wallet-api `CreateAsset`.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CreateAssetRequest' }
      responses:
        '200':
          description: Asset deployment result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Asset' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '422':
          description: wallet-api async job failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /assets/regime/{kind}:
    post:
      tags: [Assets]
      summary: Create a kind-specific regime asset through wallet-api
      description: >
        Compatibility route for liq-ats. TA sets `asset_type` to the path
        `{kind}` and delegates to wallet-api `CreateAsset`.
      parameters:
        - name: kind
          in: path
          required: true
          schema:
            type: string
            enum: [regd506c, regd504, regcf, section4a2, rega1, rega2]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/CreateAssetRequest' }
      responses:
        '200':
          description: Asset deployment result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Asset' }
        '400': { $ref: '#/components/responses/BadRequest' }
        '422':
          description: wallet-api async job failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '502':
          description: wallet-api dependency failure
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  # ---------------------------------------------------------------------------
  # Settlement
  # ---------------------------------------------------------------------------
  /settlement/settle:
    post:
      tags: [Settlement]
      summary: Legacy owner-signed delivery-versus-payment settlement
      parameters: [{ $ref: '#/components/parameters/ActorHeader' }]
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/SettleRequest' }
      responses:
        '201':
          description: Settlement result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Settlement' }
        '422':
          description: Pre-trade compliance rejected the security leg
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /settlement/fill:
    post:
      tags: [Settlement]
      summary: V42 TokenSettlement fill for a matched, already-vouched order pair
      description: >
        Executor path for liq-ats. This is distinct from legacy /settlement/settle:
        callers must provide V42 multisig addresses and the full vouched order
        parameters, including each side's nonce, so ta-revision can reconstruct
        the TokenSettlement digests and submit fillBatch through the TA executor.
      requestBody:
        required: true
        content:
          application/json:
            schema: { $ref: '#/components/schemas/V42FillRequest' }
      responses:
        '200':
          description: V42 fill result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/V42FillResponse' }
        '400':
          description: Invalid fill request
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }
        '422':
          description: Vouch, market, factory, role, or contract precondition failed
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Error' }

  /settlement/{trade_id}:
    get:
      tags: [Settlement]
      summary: Fetch a settlement by trade id
      parameters:
        - name: trade_id
          in: path
          required: true
          schema: { type: string }
      responses:
        '200':
          description: Settlement
          content:
            application/json:
              schema: { $ref: '#/components/schemas/Settlement' }
        '404': { $ref: '#/components/responses/NotFound' }

  # ---------------------------------------------------------------------------
  # Audit
  # ---------------------------------------------------------------------------
  /audit:
    get:
      tags: [Audit]
      summary: Query the audit log
      parameters:
        - name: actor
          in: query
          schema: { type: string }
        - name: action
          in: query
          schema: { type: string }
        - name: limit
          in: query
          schema: { type: integer, default: 50 }
        - name: offset
          in: query
          schema: { type: integer, default: 0 }
      responses:
        '200':
          description: Audit entries
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/AuditEntry' }

  /audit/verify:
    get:
      tags: [Audit]
      summary: Verify the hash chain integrity
      responses:
        '200':
          description: Verification result
          content:
            application/json:
              schema: { $ref: '#/components/schemas/AuditVerifyResult' }

  /audit/export:
    get:
      tags: [Audit]
      summary: Export the full audit log as JSON
      responses:
        '200':
          description: All audit entries
          content:
            application/json:
              schema:
                type: array
                items: { $ref: '#/components/schemas/AuditEntry' }

  # ---------------------------------------------------------------------------
  # Observability
  # ---------------------------------------------------------------------------
  /healthz:
    get:
      tags: [Observability]
      summary: Liveness probe
      responses:
        '200':
          description: ok
          content:
            text/plain:
              schema: { type: string, example: ok }

  /readyz:
    get:
      tags: [Observability]
      summary: Readiness probe (checks DB connectivity)
      responses:
        '200':
          description: ready
          content:
            text/plain:
              schema: { type: string, example: ready }
        '503':
          description: Dependencies not ready

  /metrics:
    get:
      tags: [Observability]
      summary: Prometheus metrics
      responses:
        '200':
          description: Prometheus exposition format
          content:
            text/plain:
              schema: { type: string }

  /events/stream:
    get:
      tags: [Observability]
      summary: Server-sent events stream
      description: |
        Emits JSON frames `{ "type", "data", "time" }`. `type` is one of
        `operation_update`, `recon_result`, `drift_alert`, `chain_event`,
        `settlement_settled`.
      responses:
        '200':
          description: text/event-stream
          content:
            text/event-stream:
              schema: { type: string }
