获取callData 数据
调用接口发起兑换
Last updated
调用接口发起兑换
Last updated
发起兑换之前,钱包地址需要确认from币种是否对合约地址进行授权,合约地址在quote接口中已返回
POST
https://api.bridgers.xyz/api/sswap/swap
以下参数带*字段为必填,不带*号为选填
Name | Type | Description |
---|---|---|
{
"resCode":"100",
"resMsg":"Success",
"data":{
"txData":{
"data":"0x...", //交易calldata
"to": "0x6b4427caa371627e2a73521a2568fcf1212cd4d9", //合约地址
"value":"0x2dd47b4d9a4000"
}
}
}
// 返回的数据, 一个 tx, 一个signer,存币为sol时才会有signer字段,
如果存币为代币, 则只有tx字段
// tx是签名数据 signer 是SOL需要的一个额外的签名,用来签名数据的
{
"resCode": 100,
"resMsg": "Success",
"data": {
"txData": {
"tx": [ // tx是签名数据
{
"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"
}
}
}
// 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)
})
}
要发送交换消息,请调用合约的 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合约调用实例
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;
}
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)
TON合约调用实例
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")};
}
fromTokenAddress*
String
出售币种合约地址(可通过获取币种列表中的address字段获得)
amountOutMin*
String
得到币种数量(带精度,可通过询价接口里的amountOutMin字段获得)
equipmentNo*
String
设备号设备号(该字段将作为用户的唯一标识,三方可通过自己的方式获得,如fromAddress的前32位字符,或者32位随机数字与字母结合的字符串)
toAddress*
String
接受地址
toTokenChain*
String
获得币种链(可通过获取币种列表中的chain字段获得,详细支持链的情况可查看【基本说明】)
fromTokenAmount*
String
出售币种数量(带精度,可通过询价接口里的fromTokenAmount字段获得)
fromTokenChain*
String
出售币种链(可通过获取币种列表中的chain字段获得,详细支持链的情况可查看【基本说明】)
toTokenAddress*
String
获得币种合约地址(可通过获取币种列表中的address字段获得)
fromAddress*
String
用户地址
sourceFlag*
String
渠道来源(需要双方约定一个名称来代表三方的渠道)
fromCoinCode*
String
出售代币名称(可通过获取币种列表中的symbol字段获得)
toCoinCode*
String
获得代币名称(可通过获取币种列表中的symbol字段获得)
sourceType
String
设备类型(H5/IOS/Android 该字段为选填,如通过直接通过API调用,可为空)
slippage
String
滑点,例如:0.1