模拟交易#
在执行交易之前进行模拟,以了解预期结果和潜在风险。
交易模拟API仅向我们的白名单客户提供。如果您感兴趣,请联系我们 dexapi@okx.com。
请求地址#
GET https://web3.okx.com/api/v6/dex/pre-transaction/simulate
请求参数#
| Parameter | Type | Required | Description | 
|---|---|---|---|
| fromAddress | String | 是 | From 地址。对于 兑换、授权,这是钱包地址 | 
| toAddress | String | 是 | To 地址。 对于 兑换,这是 OKX DEX router 地址。对于 授权,这是代币地址。 | 
| chainIndex | String | 是 | 链的唯一标识。 如 501: Solana,更多可查看这里。目前支持 EVM、SOL、SUI 链,更多的链即将被支持。 | 
| txAmount | String | 否 | 交易金额。默认值: 0。1.对于主链币交易(当fromToken为主链币时,例如 ETH ),txAmount可设置为主链币的数量,或通过 /swap API获取(例如: txAmount = swapResponse.tx.value)。2.对于代币交易,需将 txAmount 设为0。 金额必须使用主链币的基础单位(如ETH对应wei)。 | 
| extJson | Object | 是 | 扩展参数,用于添加 calldata 等信息 | 
| > inputData | String | 是 | Call data。编码规则要求 base58。 | 
| priorityFee | String | 否 | 优先费。只适用于 Solana | 
| gasPrice | String | 否 | Gas price | 
响应参数#
| Parameter | Type | Description | 
|---|---|---|
| intention | String | 交易目的。 有效值: Swap,Token Approval | 
| assetChange | Array | 交易导致的资产变化。 | 
| > assetType | String | 资产类型。 有效值: NATIVE,ERC20,SPLTOKEN,SUITOKEN | 
| > name | String | 资产名称(例如, "Ethereum") | 
| > symbol | String | 资产符号 (例如 "ETH") | 
| > decimals | Number | 精度 | 
| > address | String | 合约地址 | 
| > imageUrl | String | 图片地址 | 
| > rawValue | String | 资产金额。正数代表资产增加/接收,负数代表资产减少/转出。 | 
| gasUsed | String | Gas 消耗 | 
| failReason | String | 失败原因 | 
| risks | Array | 潜在的风险 | 
| > address | String | 地址 | 
| > addressType | String | 地址类型。有效值: contract,eos | 
请求示例#
shell
curl --location --request POST 'https://web3.okx.com/api/v6/dex/pre-transaction/simulate' \
--header 'OK-ACCESS-KEY: your-access-key' \
--header 'OK-ACCESS-SIGN: your-access-sign' \
--header 'OK-ACCESS-PASSPHRASE: your-passphrase' \
--header 'OK-ACCESS-TIMESTAMP: 2025-05-19T10:00:00.000Z' \
--header 'Content-Type: application/json' \
--data-raw '{
  "fromAddress": "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
  "toAddress": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D",
  "chainIndex": "1",
  "txAmount": "0",
  "extJson": {
    "inputData": "0x38ed1739000000000000000000000000000000000000000000000000016345785d8a0000000000000000000000000000000000000000000000000000000000000042ab52c000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000742d35cc6634c0532925a3b844bc454e4438f44e0000000000000000000000000000000000000000000000000000000064794b4b0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
  },
  "gasPrice": "12000000000"
}'
响应示例#
200
{
  "code": "0",
  "data": [
    {
      "intention": "SWAP",
      "assetChange": [
        {
          "assetType": "NATIVE",
          "name": "Ether",
          "symbol": "ETH",
          "decimals": 18,
          "address": "",
          "imageUrl": "",
          "rawValue": "1000000000000000"
        },
        {
          "assetType": "ERC20",
          "name": "USD Coin",
          "symbol": "USDC",
          "decimals": 6,
          "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
          "imageUrl": "",
          "rawValue": "1000000000000000"
        }
      ],
      "gasUsed": "180000",
      "failReason": "",
      "risks": []
    }
  ],
  "msg": "success"
}
