> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstream.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Dex - 路由

> 計算代幣兌換的最佳路由，考慮價格影響和手續費。回傳最優路徑和準備好的交易



## OpenAPI

````yaml /zh-Hant/api-reference/openapi-defi-zh-Hant.yaml POST /v2/dex/{chain}/route
openapi: 3.1.0
info:
  title: Defi Service API
  description: Defi Service API ⚡️
  version: '1.0'
  contact:
    name: AI
    url: https://github.com/chainstream-io/defi-service
    email: ai@sx.ai
servers:
  - url: https://api.chainstream.io
    description: Production
security: []
tags:
  - name: Endpoints
    description: ''
paths:
  /v2/dex/{chain}/route:
    post:
      tags:
        - Dex
      summary: Dex - 路由
      description: 計算代幣兌換的最佳路由，考慮價格影響和手續費。回傳最優路徑和準備好的交易
      operationId: route
      parameters:
        - name: chain
          required: true
          in: path
          description: 支援網路列表中的鏈名稱
          schema:
            $ref: '#/components/schemas/ChainSymbol'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SwapRouteInput'
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SwapRouteResponse'
      security:
        - bearer: []
components:
  schemas:
    ChainSymbol:
      type: string
      enum:
        - sol
        - bsc
        - eth
    SwapRouteInput:
      type: object
      properties:
        dex:
          type: string
          description: 交易所使用的 DEX 標識符
          example: jupiter
          enum:
            - jupiter
            - kyberswap
            - raydium
            - pumpfun
            - moonshot
            - candy
            - launchpad
        userAddress:
          type: string
          description: 發起交易的錢包公鑰
          example: oQPnhXAbLbMuKHESaGrbXT17CyvWCpLyERSJA9HCYd7
          minLength: 8
          maxLength: 64
        priorityFee:
          type: string
          description: 優先費（SOL），用於提高交易處理速度
          example: '1000'
        amount:
          type: string
          description: 兌換數量。使用 "auto" 表示全部餘額，或使用百分比如 "50%"
          example: '1000000000'
        swapMode:
          type: string
          description: 兌換方向模式
          enum:
            - ExactIn
            - ExactOut
          example: ExactIn
        slippage:
          type: integer
          description: 滑點容忍百分比
          example: 5
          default: 5
          minimum: 0
          maximum: 100
          format: int64
        inputMint:
          type: string
          description: 輸入代幣鑄造地址
          example: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
        outputMint:
          type: string
          description: 輸出代幣鑄造地址
          example: Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB
        recipientAddress:
          type: string
          description: 兌換的接收錢包地址
          example: '0x0000000000000000000000000000000000000001'
        permit:
          type: string
          description: 兌換的 Permit 資料
          example: 66c85d1637257e890e581f724f69f4d4fc17eee156c0619c4719ed0c66eed0e9
        deadline:
          type: integer
          description: 兌換截止時間戳
          example: 1716806400000
          format: int64
        tipFee:
          type: string
          description: 小費（SOL），用於提高交易處理速度
          example: '1000000'
        isAntiMev:
          type: boolean
          description: 是否啟用防 MEV 保護
          example: true
          default: false
        maxFeePerGas:
          type: string
          description: 每 Gas 單位最大費用（EIP-1559），單位為 wei
          example: '50000000000'
        maxPriorityFeePerGas:
          type: string
          description: 每 Gas 單位最大優先費（EIP-1559），單位為 wei
          example: '2000000000'
        gasPrice:
          type: string
          description: Gas 價格，單位為 wei（傳統交易）
          example: '50000000000'
        gasLimit:
          type: string
          description: 交易 Gas 上限
          example: '300000'
      required:
        - dex
        - userAddress
        - amount
        - swapMode
        - slippage
    SwapRouteResponse:
      type: object
      properties:
        args:
          description: 原始兌換請求參數
          allOf:
            - $ref: '#/components/schemas/SwapRouteInput'
        serializedTx:
          type: string
          description: Base64 編碼的交易
          example: >-
            AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAEDRgYGpQEDAQIABQcICQoLDA0ODwAAAAAAAAAAAAAQERITFBUWFxgZGhscHR4fAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
        routeInfo:
          type: object
          description: 詳細路由資訊
          example:
            inAmount: '1000000000'
            outAmount: '985000000'
            priceImpact: '0.15'
            route:
              - USDC
              - SOL
        elapsedTime:
          type: integer
          description: 請求處理耗時（毫秒）
          example: 245
          format: int64
        recentBlockhash:
          type: string
          description: 兌換交易的最近區塊哈希
          example: JE38e5sYDcpjcfuKA7Madpq8p3A4zc8UQuonCZDbn2yP
        lastValidBlockHeight:
          type: integer
          description: 兌換交易的最後有效區塊高度
          example: 416681972
          format: int64
      required:
        - args
        - serializedTx
        - routeInfo
        - elapsedTime
  securitySchemes:
    bearer:
      scheme: bearer
      bearerFormat: JWT
      type: http

````