Obtain callData
Request endpoint to initiate the swap
Note
Before the swap, wallet address needs to confirm that FROM currency token approves the contract address. The contract address can be fetched by requesting the endpoint
Method 1: Endpoint usage
POST
https://api.bridgers.xyz/api/sswap/swap
The fields with * are required, and the fields without * are optional
Request Body
equipmentNo*
String
Equipment number (This field will be used as the unique identifier of the user, and the third parties can obtain it on their own, such as the first 32 characters of fromAddress, or a string of 32 random numbers and letters)
sourceType
String
Device type (H5/IOS/Android The field is optional, if it is requested directly through the API, it can be empty)
sourceFlag*
String
Source channel (Both sides need to agree on a name to represent the channel of the third parties)
fromTokenAddress*
String
Contract address for the token to sell (It can be obtained from the address field in the currency list)
toTokenAddress*
String
Contract address for the token to receive (It can be obtained from the address field in the currency list)
fromAddress*
String
User address
toAddress*
String
receiving Address
fromTokenChain*
String
Network of the token to sell (It can be obtained from the chain field in the currency list, For details of the support chain, please refer to [Basic information])
toTokenChain*
String
Network of the token to received (It can be obtained from the chain field in the currency list, For details of the support chain, please refer to [Basic information])
fromTokenAmount*
String
Amount of token to sell (with precision, which can be obtained through the fromTokenAmount field in the quote endpoint)
amountOutMin*
String
Amount of token to receive (with precision, which can be obtained through the amountOutMin field in the quote endpoint)
fromCoinCode*
String
Token name for sell token (It can be obtained from the symbol field in the currency list)
toCoinCode*
String
Token name for receive token (It can be obtained from the symbol field in the currency list)
slippage
String
e.g. '0.1'
{
"resCode":"100",
"resMsg":"Success",
"data":{
"txData":{
"data":"0x...", //交易calldata
"to": "0x6b4427caa371627e2a73521a2568fcf1212cd4d9", //合约地址Contract Address
"value":"0x2dd47b4d9a4000"
}
}
}
// Returned value: tx, signer
signer field only exists when deposit SOL assets
tx field only exists when deposit tokens
// tx is Signature Data, signer is an additional signature required by SOL to sign the data
{
"resCode": 100,
"resMsg": "Success",
"data": {
"txData": {
"tx": [ // tx is Signature Data
{
"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 is an additional signature required by SOL to sign the data
"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 example
// 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)
})
}
Postman example
Method 2: Directly request the smart contract
To send a swap message, call the contract's swap() or swapEth() function.
Here is an explanation of the swap() and swapEth() interface:
// @param fromToken - fromToken token's address.
// @param toToken - The target token to be exchanged (obtained from the getToken interface's symbol. e.g. ETH(ARB))
// @param destination - The receiving address for the target token
// @param fromAmount - The amount of the original token
// @param minReturnAmount - The anticipated amount of the target token to be exchanged
function swap(
address fromToken,
string memory toToken,
string memory destination,
uint256 fromAmount,
uint256 minReturnAmount
) external nonReentrant
// @param toToken - The target token to be exchanged (obtained from the getToken interface's symbol. e.g. ETH(ARB))
// @param destination - The receiving address for the target token
// @param minReturnAmount - The anticipated amount of the target token to be exchanged
function swapEth(
string memory toToken,
string memory destination,
uint256 minReturnAmount
)
Mainnet currency swap calls the swapEth() method, and token swap calls the swap() method. EVM Contract call instance:
exports.swapCallData = async (nodeHost,fromTokenChain,fromTokenAddress,fromTokenAmount,toCoinCode,toAddress,amountOutMin) => {
const web3 = new Web3(new Web3.providers.HttpProvider(nodeHost));
const ROUTER_ADDRESS = ''; // contract address
const ROUTE_ABI = require('./abis/BridgersAbi.json'); //ABI Stored inside the contract on chain
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;
}
const fromAddress = '0xCEf7E44D13286e23782E12d654455FA5B35F36f8' //Address of token to sell
const toAddress = '0xCEf7E44D13286e23782E12d654455FA5B35F36f8' //receiving Address
const toTokenCode = 'USDT(BSC)' //Token name for receive token
const amount = '140000000000000000' // Amount of token to sell
const amountOutMin = '53837610794859360000' // Minimum receiving amount
const fromTokenAddress = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' // Contract address for the token to sell
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)
TON Contract call instance:
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")};
}
Last updated