# 获取callData 数据

### 注意

> 发起兑换之前，钱包地址需要确认from币种是否对合约地址进行授权，合约地址在quote接口中已返回

## 方法一：接口调用获取

<mark style="color:green;">`POST`</mark> `https://api.bridgers.xyz/api/sswap/swap`

**以下参数带\*字段为必填，不带\*号为选填**

#### Request Body

| Name                                               | Type    | Description                                                               |
| -------------------------------------------------- | ------- | ------------------------------------------------------------------------- |
| fromTokenAddress<mark style="color:red;">\*</mark> | String  | 出售币种合约地址（可通过获取币种列表中的address字段获得）                                          |
| amountOutMin<mark style="color:red;">\*</mark>     | String  | 预期得到币种数量(带精度，可通过询价接口里的amountOutMin字段获得)                                   |
| equipmentNo<mark style="color:red;">\*</mark>      | String  | 设备号设备号（该字段将作为用户的唯一标识，三方可通过自己的方式获得，如fromAddress的前32位字符，或者32位随机数字与字母结合的字符串） |
| toAddress<mark style="color:red;">\*</mark>        | String  | 接受地址                                                                      |
| toTokenChain<mark style="color:red;">\*</mark>     | String  | 获得币种链（可通过获取币种列表中的chain字段获得，详细支持链的情况可查看【基本说明】）                             |
| fromTokenAmount<mark style="color:red;">\*</mark>  | String  | 出售币种数量(带精度，可通过询价接口里的fromTokenAmount字段获得)                                  |
| fromTokenChain<mark style="color:red;">\*</mark>   | String  | 出售币种链（可通过获取币种列表中的chain字段获得，详细支持链的情况可查看【基本说明】）                             |
| toTokenAddress<mark style="color:red;">\*</mark>   | String  | 获得币种合约地址（可通过获取币种列表中的address字段获得）                                          |
| fromAddress<mark style="color:red;">\*</mark>      | String  | 用户地址                                                                      |
| sourceFlag<mark style="color:red;">\*</mark>       | String  | 渠道来源（需要双方约定一个名称来代表三方的渠道）                                                  |
| fromCoinCode<mark style="color:red;">\*</mark>     | String  | 出售代币名称（可通过获取币种列表中的symbol字段获得）                                             |
| toCoinCode<mark style="color:red;">\*</mark>       | String  | 获得代币名称（可通过获取币种列表中的symbol字段获得）                                             |
| sourceType                                         | String  | 设备类型（H5/IOS/Android 该字段为选填，如通过直接通过API调用，可为空)                              |
| slippage                                           | String  | 滑点，例如：0.1（代表10%）最低接收数量 = amountOutMin \* (1 - slippage)                   |
| fixedRateFlag                                      | Integer | <p>固定费率<br>0：关闭固定费率<br>1 ：开启固定费率,<br>默认: 0</p>                            |

{% tabs %}
{% tab title="200: OK EVM链与TRON链返回结果" %}

```javascript
{
 "resCode":"100",
 "resMsg":"Success",
 "data":{
  "txData":{
   "data":"0x...", //交易calldata
   "to": "0x6b4427caa371627e2a73521a2568fcf1212cd4d9", //合约地址
   "value":"0x2dd47b4d9a4000"
  }
 }
}
```

{% endtab %}

{% tab title="200: OK SOLANA链返回结果" %}

<pre class="language-json"><code class="lang-json">// 返回的数据, 一个 tx, 一个signer,存币为sol时才会有signer字段，
    如果存币为代币, 则只有tx字段
// tx是签名数据 signer 是SOL需要的一个额外的签名,用来签名数据的
{
    "resCode": 100,
    "resMsg": "Success",
    "data": {
        "txData": {
<strong>            "tx": [  // tx是签名数据
</strong>                {
                    "keys": [
                        {
                            "pubkey": "5WtTutYKy6XtVS6FH8VAxHPYTtzKB1rq7LfPR8ySCvW6",
                            "isSigner": true,
                            "isWritable": true
                        },
                        {
                            "pubkey": "Ekg1SKxB61NMwpaccj1Y88mzyH2QKoaGCVnDUZZ3WXc5",
                            "isSigner": true,
                            "isWritable": true
                        }
                    ],
                    "programId": "11111111111111111111111111111111",
                    "data": [
                        0,
                        0,
                        0,
                        0,
                        240,
                        29,
                        31,
                        0,
                        0,
                        0,
                        0,
                        0,
                        165,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        0,
                        6,
                        221,
                        246,
                        225,
                        215,
                        101,
                        161,
                        147,
                        217,
                        203,
                        225,
                        70,
                        206,
                        235,
                        121,
                        172,
                        28,
                        180,
                        133,
                        237,
                        95,
                        91,
                        55,
                        145,
                        58,
                        140,
                        245,
                        133,
                        126,
                        255,
                        0,
                        169
                    ]
                },
                {
                    "keys": [
                        {
                            "pubkey": "5WtTutYKy6XtVS6FH8VAxHPYTtzKB1rq7LfPR8ySCvW6",
                            "isSigner": true,
                            "isWritable": true
                        },
                        {
                            "pubkey": "Ekg1SKxB61NMwpaccj1Y88mzyH2QKoaGCVnDUZZ3WXc5",
                            "isSigner": false,
                            "isWritable": true
                        }
                    ],
                    "programId": "11111111111111111111111111111111",
                    "data": [
                        2,
                        0,
                        0,
                        0,
                        0,
                        202,
                        154,
                        59,
                        0,
                        0,
                        0,
                        0
                    ]
                },
                {
                    "keys": [
                        {
                            "pubkey": "Ekg1SKxB61NMwpaccj1Y88mzyH2QKoaGCVnDUZZ3WXc5",
                            "isSigner": false,
                            "isWritable": true
                        },
                        {
                            "pubkey": "So11111111111111111111111111111111111111112",
                            "isSigner": false,
                            "isWritable": false
                        },
                        {
                            "pubkey": "5WtTutYKy6XtVS6FH8VAxHPYTtzKB1rq7LfPR8ySCvW6",
                            "isSigner": false,
                            "isWritable": false
                        },
                        {
                            "pubkey": "SysvarRent111111111111111111111111111111111",
                            "isSigner": false,
                            "isWritable": false
                        }
                    ],
                    "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                    "data": [
                        1
                    ]
                },
                {
                    "keys": [
                        {
                            "pubkey": "Ekg1SKxB61NMwpaccj1Y88mzyH2QKoaGCVnDUZZ3WXc5",
                            "isSigner": false,
                            "isWritable": true
                        },
                        {
                            "pubkey": "5WtTutYKy6XtVS6FH8VAxHPYTtzKB1rq7LfPR8ySCvW6",
                            "isSigner": true,
                            "isWritable": false
                        },
                        {
                            "pubkey": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                            "isSigner": false,
                            "isWritable": false
                        },
                        {
                            "pubkey": "H71KjNoguKbjSP5uwi8LnR3Huh6pGte6rc3DtxrUzmQB",
                            "isSigner": false,
                            "isWritable": true
                        },
                        {
                            "pubkey": "52nXsHVT861uD8N2EyNhsHBage7d4XFxysD47hcZZiKJ",
                            "isSigner": false,
                            "isWritable": false
                        }
                    ],
                    "programId": "A1kitc8Td5hGFQTkXsencSRBzAT45gKxpgqwkigYFi6g",
                    "data": [
                        2,
                        0,
                        202,
                        154,
                        59,
                        0,
                        0,
                        0,
                        0,
                        42,
                        0,
                        0,
                        0,
                        48,
                        120,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        101,
                        44,
                        0,
                        0,
                        0,
                        53,
                        87,
                        116,
                        84,
                        117,
                        116,
                        89,
                        75,
                        121,
                        54,
                        88,
                        116,
                        86,
                        83,
                        54,
                        70,
                        72,
                        56,
                        86,
                        65,
                        120,
                        72,
                        80,
                        89,
                        84,
                        116,
                        122,
                        75,
                        66,
                        49,
                        114,
                        113,
                        55,
                        76,
                        102,
                        80,
                        82,
                        56,
                        121,
                        83,
                        67,
                        118,
                        87,
                        54,
                        20,
                        0,
                        0,
                        0,
                        51,
                        49,
                        48,
                        50,
                        54,
                        54,
                        54,
                        53,
                        54,
                        54,
                        51,
                        54,
                        55,
                        49,
                        55,
                        53,
                        55,
                        48,
                        48,
                        48,
                        16,
                        0,
                        0,
                        0,
                        85,
                        83,
                        68,
                        84,
                        40,
                        66,
                        83,
                        67,
                        41,
                        124,
                        54,
                        48,
                        121,
                        121,
                        104,
                        110,
                        42,
                        0,
                        0,
                        0,
                        48,
                        120,
                        67,
                        69,
                        102,
                        55,
                        69,
                        52,
                        52,
                        68,
                        49,
                        51,
                        50,
                        56,
                        54,
                        101,
                        50,
                        51,
                        55,
                        56,
                        50,
                        69,
                        49,
                        50,
                        100,
                        54,
                        53,
                        52,
                        52,
                        53,
                        53,
                        70,
                        65,
                        53,
                        66,
                        51,
                        53,
                        70,
                        51,
                        54,
                        102,
                        56
                    ]
                },
                {
                    "keys": [
                        {
                            "pubkey": "Ekg1SKxB61NMwpaccj1Y88mzyH2QKoaGCVnDUZZ3WXc5",
                            "isSigner": false,
                            "isWritable": true
                        },
                        {
                            "pubkey": "5WtTutYKy6XtVS6FH8VAxHPYTtzKB1rq7LfPR8ySCvW6",
                            "isSigner": false,
                            "isWritable": true
                        },
                        {
                            "pubkey": "5WtTutYKy6XtVS6FH8VAxHPYTtzKB1rq7LfPR8ySCvW6",
                            "isSigner": true,
                            "isWritable": false
                        }
                    ],
                    "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
                    "data": [
                        9
                    ]
                }
            ],
            // signer 是SOL需要的一个额外的签名,用来签名数据的
            "signer": "182,16,183,80,22,123,252,74,45,92,252,171,66,45,40,132,60,237,48,145,216,7,46,207,150,92,149,216,93,183,186,156,204,88,2,194,226,51,228,17,225,196,51,0,197,74,129,129,179,131,248,148,175,36,74,145,30,53,21,133,24,117,112,82"
        }
    }
}
</code></pre>

{% endtab %}
{% endtabs %}

### 调用示例

{% tabs %}
{% tab title="JavaScript" %}

```
// swap example
 import { ethers } from 'ethers'
 const provider = new ethers.providers.Web3Provider(window.ethereum, 'any')
 cosnt signer = provider.getSigner()
 const params = {
   fromTokenAddress: '0x55d398326f99059ff775485246999027b3197955',
   toTokenAddress: '0xa71edc38d189767582c38a3145b5873052c3e47a',
   fromAddress: '0x...',  //The wallet address used to send the tokens
   toAddress: '0x...',  //The wallet address used to accept tokens
   fromTokenChain: 'BSC',
   toTokenChain: 'HECO',
   fromTokenAmount: '8000000000000000000',
   amountOutMin:'7884000000000000000',
   fromCoinCode:'USDT(BSC)',
   toCoinCode:'USDT(HECO)',
   equipmentNo:'', // your equipment number
   sourceType:'H5',
   sourceFlag:'widget'
  }
  const res = await axios.post('https://api.bridgers.xyz/api/sswap/swap',params)
  console.log(res)
  if(res.resCode === '100'){
    const transactionParameters = {
        to: res.data.txData.to, // Required except during contract publications.
        from: state.wallet.address, // must match user's active address.
        data: res.data.txData.data,
        value: res.data.txData.value,
        //gasPrice: 5000000000, // 6 gwei
        //gas: new BigNumber(1000000), // 1000000
    }
    signer.sendTransaction(transactionParameters)
      .then((data) => {
        console.log(data.hash)
      })   
  }
```

{% endtab %}
{% endtabs %}

### Postman 示例

![](/files/e6fGbixbs9v8q3K1qv4P)

## 方法二：直接调用合约生成

**要发送交换消息，请调用合约的 swap() 或 swapEth() 函数。**

**合约方法swap和swapEth的解释**

```
// @param fromToken - 兑换的原币种合约地址
// @param toToken - 兑换的目标币种（取getToken接口返回的symbol）
// @param destination - 目标币种的接收地址
// @param fromAmount - 原币种数量
// @param minReturnAmount - 预期兑换的目标币数量
function swap(
        address fromToken,
        string memory toToken,
        string memory destination,
        uint256 fromAmount,
        uint256 minReturnAmount
    ) external nonReentrant

// @param toToken - 兑换的目标币种（取getToken接口返回的symbol）
// @param destination - 目标币种的接收地址
// @param minReturnAmount - 预期兑换的目标币数量
function swapEth(
        string memory toToken, 
        string memory destination, 
        uint256 minReturnAmount
    )
```

**主网币兑换调用swapEth方法，代币兑换调用swap方法**\
**EVM合约调用实例**

{% tabs %}
{% tab title="nodejs" %}

```
exports.swapCallData = async (nodeHost,fromTokenChain,fromTokenAddress,fromTokenAmount,toCoinCode,toAddress,amountOutMin) => {
    const web3 = new Web3(new Web3.providers.HttpProvider(nodeHost));
    const ROUTER_ADDRESS =  ''; //合约地址
    const ROUTE_ABI = require('./abis/BridgersAbi.json'); //ABI 在链上合约中
    const routerContract = new web3.eth.Contract(ROUTE_ABI, ROUTER_ADDRESS);
    let data;
    let value;
    if('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'==fromTokenAddress){
        data = routerContract.methods.swapEth(toCoinCode,toAddress,amountOutMin).encodeABI();
        value = '0x' + new BigNumber(fromTokenAmount).toString(16);
    }else{
        data = routerContract.methods.swap(fromTokenAddress,toCoinCode,toAddress,fromTokenAmount,amountOutMin).encodeABI();
        value = '0x0';
    }

    let res = {
        'data': data,
        'to': ROUTER_ADDRESS,
        'value': value
    };
    return res;
}
```

{% endtab %}

{% tab title="JavaScript" %}

```
const fromAddress = '0xCEf7E44D13286e23782E12d654455FA5B35F36f8' //发币地址 
const toAddress = '0xCEf7E44D13286e23782E12d654455FA5B35F36f8' //收币地址 
const toTokenCode = 'USDT(BSC)' //收币code 名称 
const amount = '140000000000000000' // 发币数量 
const amountOutMin = '53837610794859360000' // 最小得到数量 
const fromTokenAddress = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // 发送币种合约 
const ROUTE_ABI = require('./sswapABI.json') // abi json 
const web3 = new Web3(new Web3.providers.HttpProvider('https://bsc-dataseed1.binance.org/')); 
const ROUTER_ADDRESS = '0x1ed5685f345b2fa564ea4a670de1fde39e484751'; 
const routerContract = new web3.eth.Contract(ROUTE_ABI, ROUTER_ADDRESS); 
let data,value; 
if('0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'== fromTokenAddress){
   data = routerContract.methods.swapEth(toTokenCode,fromAddress,amountOutMin).encodeABI(); 
   value = '0x' + new BigNumber(amount).toString(16); 
}else{ 
  data = routerContract.methods.swap(fromTokenAddress,toTokenCode,toAddress,amount,amountOutMin).encodeABI(); 
  value = '0x0'; 
}
console.log(data,value)
```

{% endtab %}
{% endtabs %}

**TON合约调用实例**

{% tabs %}
{% tab title="nodejs" %}

```
const TonWeb = require('tonweb');
const { Contract, ContractProvider, Sender, Address, Cell, contractAddress, beginCell, SendMode, toNano } = require("@ton/core");
const { TonClient, JettonMaster, WalletContractV4} = require("@ton/ton");
const { getHttpEndpoint } = require("@orbs-network/ton-access")

exports.swapCallData= async(fromAddress, fromTokenAddress, amount, toAddress, amountOutMin, toCoinCode) => {
    const swap = 1783769518;
    const swapETH = 1023744248;
    const transfer = 260734629;
    const gas = toNano("0.06").toString();
    const forwardAmount = toNano("0.01").toString();
    if (fromTokenAddress.toLowerCase() == '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
        const forwardPayload = beginCell()
            .storeUint(swapETH, 32)
            .storeAddress(Address.parse(fromAddress))
            .storeCoins(forwardAmount)
            .storeRef(beginCell().storeUint(0, 32).storeStringTail(JSON.stringify({"fromToken": fromTokenAddress, "toToken":  toCoinCode, "sender": fromAddress, "destination" : toAddress, "minReturnAmount": amountOutMin})).endCell())
            .endCell();
        const body = beginCell()
            .storeUint(0x7362d09c, 32)
            .storeRef(forwardPayload)
            .endCell();
        return {address: sdkInfo.sswapContract.TON, amount: amount, payload: body.toBoc().toString("base64")};
    }
    const swap_forwardPayload = beginCell()
        .storeUint(swap, 32)
        .storeAddress(Address.parse(fromAddress))
        .storeCoins(forwardAmount)
        .storeRef(beginCell().storeUint(0, 32).storeStringTail(JSON.stringify({"fromToken": fromTokenAddress, "toToken":  toCoinCode, "sender": fromAddress, "destination" : toAddress, "minReturnAmount": amountOutMin})).endCell())
        .endCell();

    const swap_body = beginCell()
        .storeUint(transfer, 32)
        .storeUint(0, 64)
        .storeCoins(amount)
        .storeAddress(Address.parse(tonContractAddress))
        .storeAddress(Address.parse(fromAddress))
        .storeBit(0)
        .storeCoins(forwardAmount)
        .storeBit(1)
        .storeRef(swap_forwardPayload)
        .endCell();
    const client = new TonClient({
        endpoint: await getHttpEndpoint({ network: 'mainnet' }),
    })
    const jettonMasterAddress = Address.parse(fromTokenAddress);
    const userAddress = Address.parse(fromAddress);
    const jettonMaster = client.open(JettonMaster.create(jettonMasterAddress));
    const jettonWalletAddress = await jettonMaster.getWalletAddress(userAddress);
    return {address: jettonWalletAddress.toString(), amount: gas, payload: swap_body.toBoc().toString("base64")};
}
```

{% endtab %}
{% endtabs %}

**XRP合约调用实例**

{% tabs %}
{% tab title="nodejs" %}

```
const  {Xumm} = require("xumm");

exports.swap = async (fromAddress, fromTokenAddress, fromTokenDecimals, amount, toAddress, amountOutMin, toCoinCode, xrpTokenCode = '') => {
    let result = '';
    const memoData = {
        "fromToken": fromTokenAddress,
        "toToken": toCoinCode,
        "sender": fromAddress,
        "destination": toAddress,
        "minReturnAmount": amountOutMin,
        "fromAmount": amount
    };
    const buffer = Buffer.from(JSON.stringify(memoData), 'utf-8');
    const hexStr = buffer.toString('hex');
    if (fromTokenAddress == '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
        result = {
            TransactionType: "Payment",
            Destination: bridgersXRPContract,
            Amount: amount,
            Memos: [{Memo: {MemoData: hexStr}}],
        }
    } else {
        let amount1 = new BigNumber(amount).shiftedBy(parseInt(-fromTokenDecimals)).dp(10).toFixed();
        result = {
            TransactionType: "Payment",
            Destination: bridgersXRPContract,
            Amount: {
                currency: xrpTokenCode,
                value: amount1,
                issuer: fromTokenAddress
            },
            Memos: [{Memo: {MemoData: hexStr}}],
        }
    }
    const xumm = new Xumm(API-KEY, API-SECRET);
    let payload = await xumm.payload?.create({
        txjson: result,
        options: {
            force_network: "MAINNET"
        }
    });
    return payload;
}
```

{% endtab %}
{% endtabs %}

*


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-bridgers.bridgers.xyz/bridgers-api-jie-kou/obtain-calldata.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
