SwapBox V1 SDK

安装SwapBox-SDK

yarn add swapbox-sdk

初始化SwapBox-SDK

const swapboxSdk = require("swapbox-sdk");
swapboxSdk.mainnet(); // 主网调用
swapboxSdk.testNet(); // 测试网调用

SwapBox-SDK API

/**
* @description 获取swap预估手续费
* @param fromAsset {object} 源链资产(资产查询接口返回的资产信息)`
* @param toAsset {object} 目标链资产 
* @param fromAddress {object} 用户地址 
* @param inputType { 'src' | 'dest' } 当前用户输入的金额类型 
* @param amount {string} 用户需要兑换的金额 
* @param platform {string} 当前平台(传对接平台 SWFT | NABOX | 其他对接平台) 
* @returns {Promise<{orderId: *, crossChainFee: *, amountIn: (*|BigNumber), amountOut: (*|BigNumber)}|{code, message: (string|*)}>} 
 */
const feeInfo = await swapboxSdk.getSwapEstimateFeeInfo(
        {
            chain: 'BSC',
            chainId: '102',
            assetId: '0',
            contractAddress: '0xd0a347e0ebea8f8efc26d539e17853c8e7a721c4',
            channelInfo: {
                'NERVE': { pairAddress: 'TNVTdTSQoL9quSyGJCA9sY8pcMEVy4RN4EjbB' }
            },
            decimals: 18
        },
        {
            chain: 'Heco',
            chainId: '103',
            assetId: '0',
            contractAddress: '0xa8b8a0751b658dc8c69738283b9d4a79c87a3b3e',
            channelInfo: {
                'NERVE': {
                    pairAddress: 'TNVTdTSQoL9quSyGJCA9sY8pcMEVy4RN4EjbB'
                }
            },
            decimals: 18
        },
        '0x3083f7ed267dca41338de3401c4e054db2a1cd2f',
        'src',
        '100',
        'SWFT'
    );
// 返回值
feeInfo = {
    code: '1000', // 1000成功,其余都是失败  
    crossChainFee: '', // 跨链手续费(调用合约会用到)  
    amountIn: '100', // 源链入金  
    amountOut: '99.98', // 用户收到目标链收到的金额  
    orderId: '', // 生成的订单id(调用合约会用到)  
    swapFee: '', // swap收取的手续(调用合约会用到)  
    message: 'success'
}
/** 
 * * @description 发送稳定币交易(调用合约) 
 * * @param fromAddress {string} 当前用户地址 
 * * @param fromAsset {object} 源链资产(资产查询接口返回的资产信息) 
 * * @param toAsset {object} 目标链资产 
 * * @param amountIn {string} 源链入金(通过费率信息接口获取) 
 * * @param amountOut {string} 目标链入金(通过费率信息接口获取) 
 * * @param crossChainFee {string} 跨链手续费(通过费率信息接口获取) 
 * * @param orderId {string} 订单号(通过费率信息接口获取) 
 * * @param signAddress {string} 多签地址(通过后台config接口获取) 
 * * @param swapFee {string} 兑换收取的手续费(通过费率信息接口获取) 
 * * @param platform {string} 当前平台(传对接平台 SWFT | NABOX | 其他对接平台) 
 * * @returns {Promise<{message: string, code: number}|{code: number, hash: *}|{msg: (*|string), code: (number|string|number)}>} 
 * */

const hashRes = swapboxSdk.sendStableSwapTransaction(
    '0x3083f7ed267dca41338de3401c4e054db2a1cd2f',
    {
        chain: 'BSC',
        chainId: '102',
        assetId: '0',
        contractAddress: '0xd0a347e0ebea8f8efc26d539e17853c8e7a721c4',
        channelInfo: {
            'NERVE': { pairAddress: 'TNVTdTSQoL9quSyGJCA9sY8pcMEVy4RN4EjbB',}
        },
        decimals: 18
    }, 
    {
        chain: 'Heco',
        chainId: '103',
        assetId: '0',
        contractAddress: '0xa8b8a0751b658dc8c69738283b9d4a79c87a3b3e',
        channelInfo: {
            'NERVE': { pairAddress: 'TNVTdTSQoL9quSyGJCA9sY8pcMEVy4RN4EjbB' }
        },
        decimals: 18
    },
    feeInfo.amountIn,
    feeInfo.amountOut,
    feeInfo.crossChainFee.toString(),   
    feeInfo.orderId,
    '0xf85f03C3fAAC61ACF7B187513aeF10041029A1b2',
     feeInfo.swapFee.toString() 
    );
    // 返回值
    hashRes = {      
        code: '1000', // 1000成功,其余都是失败      
        hash: '', // 签名过后广播交易的hash      
        message: 'success'
   }
/** 
* @description 交易发出后记录一次hash 
* @param orderId {string}订单号 orderId 
* @param hash {string} 交易hash 
* @returns {Promise<void>} 
*/ 
const res = await swapboxSdk.recordHash(orderId, has);
// 返回值
res = {
    code: 1000,
    msg: '',
    data: {} // 当前订单信息
}

Last updated