Swap API
Swap API 提供 5 个方法,用于获取兑换报价、执行代币兑换、跟踪兑换状态以及查询支 持的代币和链。
方法
| 方法 | 参数 | 返回值 | 说明 |
|---|---|---|---|
getSwapQuote(request) | request: SwapQuoteRequest | ApiResponse<SwapQuote> | 获取代币兑换的价格报价,但不执行兑换。 |
executeSwap(request) | request: SwapExecuteRequest | ApiResponse<SwapResult> | 使用之前获取的报价执行代币兑换。 |
getSwapStatus(swapId) | swapId: string | ApiResponse<SwapResult> | 返回进行中或已完成兑换的当前状态。 |
getSupportedSwapTokens(chainId?) | chainId?: string | ApiResponse<SwapToken[]> | 列出可用于兑换的代币,可选按链筛选。 |
getSupportedSwapChains() | -- | ApiResponse<SwapChain[]> | 列出支持兑换的所有链。 |
类型
SwapQuoteRequest
interface SwapQuoteRequest {
/** Source token contract address. Use "native" for chain-native currency. */
fromToken: string;
/** Destination token contract address. Use "native" for chain-native currency. */
toToken: string;
/** Amount of source token to swap (human-readable, e.g. "1.5"). */
amount: string;
/** Chain ID for the swap. */
chainId: string;
/** Wallet address of the sender. */
senderAddress: string;
/** Maximum acceptable slippage as a decimal (e.g. 0.005 for 0.5%). */
slippage?: number;
}
SwapQuote
interface SwapQuote {
/** Unique quote identifier. Valid for a limited time. */
quoteId: string;
fromToken: string;
toToken: string;
fromAmount: string;
/** Estimated amount of destination token to receive. */
toAmount: string;
/** Estimated USD value of the output. */
toAmountUsd: string;
/** Exchange rate (toToken per fromToken). */
exchangeRate: string;
/** Estimated gas fee in native currency. */
estimatedGas: string;
/** Estimated gas fee in USD. */
estimatedGasUsd: string;
/** Price impact as a decimal. */
priceImpact: number;
/** DEX or aggregator route used. */
route: string;
/** Quote expiry timestamp (ISO 8601). */
expiresAt: string;
}