openapi: 3.0.3
info:
  title: BaiBai API
  version: 0.1.0
  license:
    name: MIT
    url: https://opensource.org/license/mit
  description: |
    REST API for BaiBai gasless L2 swaps.

    Only EIP-712 gasless orders are supported today. Traditional/non-gasless
    orders are coming soon.

    Native ETH is not a gasless order token. Use WETH in quote and order
    requests; callers handle wrap and unwrap transactions.
servers:
  - url: https://alpha.baibai.cx/api/v1
security: []
paths:
  /tokens:
    get:
      operationId: listTokens
      summary: List supported tokens
      parameters:
        - $ref: '#/components/parameters/ChainId'
      responses:
        '200':
          description: Tokens
          headers:
            x-ratelimit-limit:
              $ref: '#/components/headers/RateLimitLimit'
            x-ratelimit-remaining:
              $ref: '#/components/headers/RateLimitRemaining'
            x-ratelimit-reset:
              $ref: '#/components/headers/RateLimitReset'
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Token'
        '400':
          $ref: '#/components/responses/Error'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/Error'
  /quote:
    post:
      operationId: createQuote
      summary: Get quote and EIP-712 typed data
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/QuoteRequest'
      responses:
        '200':
          description: Quote
          headers:
            x-ratelimit-limit:
              $ref: '#/components/headers/RateLimitLimit'
            x-ratelimit-remaining:
              $ref: '#/components/headers/RateLimitRemaining'
            x-ratelimit-reset:
              $ref: '#/components/headers/RateLimitReset'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/QuoteResponse'
        '400':
          $ref: '#/components/responses/Error'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/Error'
  /orders:
    post:
      operationId: createOrder
      summary: Submit signed gasless order
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateOrderRequest'
      responses:
        '201':
          description: Accepted order
          headers:
            x-ratelimit-limit:
              $ref: '#/components/headers/RateLimitLimit'
            x-ratelimit-remaining:
              $ref: '#/components/headers/RateLimitRemaining'
            x-ratelimit-reset:
              $ref: '#/components/headers/RateLimitReset'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CreateOrderResponse'
        '400':
          $ref: '#/components/responses/Error'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/Error'
  /orders/{orderId}:
    get:
      operationId: getOrder
      summary: Get order status
      parameters:
        - in: path
          name: orderId
          required: true
          schema:
            type: string
            minLength: 1
            maxLength: 128
      responses:
        '200':
          description: Order
          headers:
            x-ratelimit-limit:
              $ref: '#/components/headers/RateLimitLimit'
            x-ratelimit-remaining:
              $ref: '#/components/headers/RateLimitRemaining'
            x-ratelimit-reset:
              $ref: '#/components/headers/RateLimitReset'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
        '400':
          $ref: '#/components/responses/Error'
        '404':
          $ref: '#/components/responses/Error'
        '429':
          $ref: '#/components/responses/RateLimited'
        '500':
          $ref: '#/components/responses/Error'
components:
  parameters:
    ChainId:
      in: query
      name: chainId
      required: true
      schema:
        type: integer
        enum:
          - 8453
        example: 8453
  headers:
    RateLimitLimit:
      description: Request limit for the route/window.
      schema:
        type: string
    RateLimitRemaining:
      description: Remaining requests in the current window.
      schema:
        type: string
    RateLimitReset:
      description: Reset timestamp in milliseconds.
      schema:
        type: string
    RetryAfter:
      description: Seconds to wait before retrying.
      schema:
        type: string
  responses:
    Error:
      description: Error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    RateLimited:
      description: Rate limited
      headers:
        x-ratelimit-limit:
          $ref: '#/components/headers/RateLimitLimit'
        x-ratelimit-remaining:
          $ref: '#/components/headers/RateLimitRemaining'
        x-ratelimit-reset:
          $ref: '#/components/headers/RateLimitReset'
        retry-after:
          $ref: '#/components/headers/RetryAfter'
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  schemas:
    Address:
      type: string
      pattern: ^0x[a-fA-F0-9]{40}$
      example: '0x1111111111111111111111111111111111111111'
    Hex:
      type: string
      pattern: ^0x[a-fA-F0-9]+$
    TokenAmount:
      type: string
      pattern: ^[1-9]\d*$
      description: Positive token base units as a decimal string.
      example: '1000000'
    NonNegativeTokenAmount:
      type: string
      pattern: ^(0|[1-9]\d*)$
      description: Token base units as a decimal string, including zero.
      example: '0'
    OrderStatus:
      type: string
      enum:
        - pending
        - filled
        - failed
        - expired
    RoutingPreference:
      type: string
      enum:
        - auto
        - preferBaiBai
        - onlyBaiBai
      default: auto
      description: >-
        Choose the best route, prefer BaiBai when competitive, or only quote
        BaiBai Prop AMM liquidity.
    Token:
      type: object
      required:
        - chainId
        - address
        - symbol
        - name
        - decimals
      properties:
        chainId:
          type: integer
          enum:
            - 8453
        address:
          $ref: '#/components/schemas/Address'
        symbol:
          type: string
          example: USDC
        name:
          type: string
          example: USD Coin
        decimals:
          type: integer
          minimum: 0
          maximum: 255
          example: 6
        logoURI:
          type: string
          format: uri
    QuoteRequest:
      allOf:
        - type: object
          required:
            - chainId
            - from
            - sellToken
            - buyToken
            - kind
            - signingScheme
          properties:
            chainId:
              type: integer
              enum:
                - 8453
              example: 8453
            from:
              $ref: '#/components/schemas/Address'
            receiver:
              $ref: '#/components/schemas/Address'
            sellToken:
              allOf:
                - $ref: '#/components/schemas/Address'
              description: ERC-20 token address. Use WETH instead of native ETH.
            buyToken:
              allOf:
                - $ref: '#/components/schemas/Address'
              description: ERC-20 token address. Use WETH instead of native ETH.
            kind:
              type: string
              enum:
                - sell
                - buy
            sellAmountBeforeFee:
              $ref: '#/components/schemas/TokenAmount'
            buyAmountAfterFee:
              $ref: '#/components/schemas/TokenAmount'
            slippageBps:
              type: integer
              minimum: 1
              maximum: 2000
              default: 50
              example: 50
            routingPreference:
              $ref: '#/components/schemas/RoutingPreference'
            signingScheme:
              type: string
              enum:
                - eip712
        - oneOf:
            - type: object
              required:
                - kind
                - sellAmountBeforeFee
              properties:
                kind:
                  type: string
                  enum:
                    - sell
            - type: object
              required:
                - kind
                - buyAmountAfterFee
              properties:
                kind:
                  type: string
                  enum:
                    - buy
    QuoteResponse:
      type: object
      required:
        - id
        - from
        - expiration
        - quote
        - permit2
      properties:
        id:
          type: string
          example: baibai_quote_123
        from:
          $ref: '#/components/schemas/Address'
        expiration:
          type: string
          format: date-time
        quote:
          $ref: '#/components/schemas/QuoteOrder'
        permit2:
          $ref: '#/components/schemas/Permit2QuoteData'
    Permit2QuoteData:
      type: object
      required:
        - allowanceTarget
        - spender
        - typedData
        - witnessTypeString
      properties:
        allowanceTarget:
          allOf:
            - $ref: '#/components/schemas/Address'
          description: Permit2 contract to approve with the sell token.
        spender:
          allOf:
            - $ref: '#/components/schemas/Address'
          description: BaiBai settlement spender in the EIP-712 typed data.
        typedData:
          type: object
          additionalProperties: true
          description: EIP-712 typed data to sign exactly as returned.
        witnessTypeString:
          type: string
    QuoteOrder:
      type: object
      required:
        - sellToken
        - buyToken
        - sellAmount
        - buyAmount
        - minBuyAmount
        - validTo
        - feeAmount
        - kind
        - partiallyFillable
        - signingScheme
      properties:
        sellToken:
          $ref: '#/components/schemas/Address'
        buyToken:
          $ref: '#/components/schemas/Address'
        receiver:
          $ref: '#/components/schemas/Address'
        sellAmount:
          $ref: '#/components/schemas/TokenAmount'
        buyAmount:
          $ref: '#/components/schemas/TokenAmount'
        minBuyAmount:
          $ref: '#/components/schemas/TokenAmount'
        validTo:
          type: integer
          description: Permit deadline in Unix seconds.
        feeAmount:
          $ref: '#/components/schemas/NonNegativeTokenAmount'
        kind:
          type: string
          enum:
            - sell
            - buy
        partiallyFillable:
          type: boolean
          enum:
            - false
        signingScheme:
          type: string
          enum:
            - eip712
    CreateOrderRequest:
      type: object
      required:
        - quoteId
        - signature
      properties:
        quoteId:
          type: string
          minLength: 1
          maxLength: 128
        signature:
          $ref: '#/components/schemas/Hex'
        clientOrderId:
          type: string
          minLength: 1
          maxLength: 128
          pattern: ^[A-Za-z0-9:_-]+$
          description: >-
            Optional idempotency key. Reusing the same key for the same owner
            returns the existing order.
    CreateOrderResponse:
      type: object
      required:
        - orderId
        - quoteId
        - status
      properties:
        orderId:
          type: string
        quoteId:
          type: string
        status:
          $ref: '#/components/schemas/OrderStatus'
    Order:
      type: object
      required:
        - orderId
        - quoteId
        - status
      properties:
        orderId:
          type: string
        quoteId:
          type: string
        status:
          $ref: '#/components/schemas/OrderStatus'
        sellToken:
          $ref: '#/components/schemas/Address'
        buyToken:
          $ref: '#/components/schemas/Address'
        sellAmount:
          $ref: '#/components/schemas/TokenAmount'
        buyAmount:
          $ref: '#/components/schemas/TokenAmount'
        executedSellAmount:
          $ref: '#/components/schemas/TokenAmount'
        executedBuyAmount:
          $ref: '#/components/schemas/TokenAmount'
        txHash:
          $ref: '#/components/schemas/Hex'
        error:
          type: object
          required:
            - code
            - message
          properties:
            code:
              type: string
            message:
              type: string
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          type: object
          required:
            - code
            - message
          properties:
            code:
              type: string
              enum:
                - BAD_REQUEST
                - NOT_FOUND
                - RATE_LIMITED
                - INTERNAL_ERROR
            message:
              type: string
