🔗Aggregate Swap

The purpose of this interface is to provide a convenient method for you to have the ability to exchange directly within the same chain. Here is a complete example.

// It's an example from USDT on BNB to USDC on BNB

import BigNumber from 'bignumber.js';
import { MaxUint256, ethers, formatUnits, hexlify, parseUnits, toUtf8Bytes } from 'ethers'
import { getAggregateQuote, getAggregateSwap, getAssets, getBridgeQuote, getChain } from './api.js';

const USDTTokenForBnb = {
    index: '5',
    symbol: 'USDT',
    decimals: 18,
    address: '0x55d398326f99059ff775485246999027b3197955',
    burnable: false
}

const USDCTokenForBnb = {
    index: '6',
    symbol: 'USDC',
    decimals: 18,
    address: '0x8ac76a51cc950d9822d68b83fe1ad97b32cd580d',
    burnable: false
}

const fromAmount = '5'
const amount = parseUnits(fromAmount, USDTTokenForBnb.decimals).toString()
const params = {
    fromAmount: amount,
    fromTokenAddress: USDTTokenForBnb.address,
    fromDecimal: USDTTokenForBnb.decimals,
    fromChain: 'BNB',
    toTokenAddress: USDCTokenForBnb.address,
    toDecimal: USDCTokenForBnb.decimals,
    toChain: 'BNB',
    channelFeeRate: channelFeeRate,
}

// quote 
const quoteResult = await getAggregateQuote(params)
if(quoteResult.code !== 0) return
const { chain, chainDecimal, outAmount, serviceFee, gasFee, slippage } = quoteResult.data

const receiveAmount = BigInt(outAmount) - BigInt(serviceFee) - BigInt(gasFee)
if(receiveAmount <= BigInt(0)) {
    // The current quote amount cannot cover the fees. Please enter a larger amount.
    return
}
// execution Chain Info
const executionChainObj = supportChainList.find((item) => item.network === chain)

// Calculate the value the user should receive. 
const receiveAmountHr = formatUnits(receiveAmount, chainDecimal)
const receiveAmountForExtra = parseUnits(receiveAmountHr, BNBTokenForBnb.decimals).toString()

// Computed minimum, After calculating the minimum value, we need to convert it to the decimals of the target chain.
const miniAmount = BigNumber(receiveAmountHr).multipliedBy(BigNumber((1 - (slippage * 0.01)))).toString()
const miniAmountForExtra = parseUnits(miniAmount, BNBTokenForBnb.decimals).toString()

// 1_Expected value;2_Third party profit ratio;3_version;4_Mini Amount;5_Execution chain
const extra = `1_${receiveAmountForExtra};2_${channelFeeRate};3_2;4_${miniAmountForExtra};5_${executionChainObj.nickName}`

// If the execution chain is not the chain you selected.
if(executionChainObj.nickName !== 'BNB') {
    // likely method fnTestAggregate

    // This information can be obtained through the getChain APIs,(builtInMinterProxyV2)
    const v2ContractForBNB = '0x99a57ac044e6ce44b7e161a07af0d3e693a75b54'

    const channel = 'chainge'
    const toChain = 'BNB' // here is BNB
    const toTokenAddress = USDCTokenForBnb.address
    const toAddr = userAddress

    const shortOrderStr = `${channel}:${toChain}:${toTokenAddress}:${toAddr}:${slippage}:${extra}`
    const orderParamsHex = hexlify(toUtf8Bytes(shortOrderStr))

    let value = '0'
    if(USDTTokenForBnb.address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
        value = amount
    }
    const isApproved = await tokenApprove(USDTTokenForBnb, v2ContractForBNB)
    if(!isApproved) return false
    const signer = await getSigner()
    const writeContract = new ethers.Contract(v2ContractForBNB, abi, signer)
    const options = {value: value}
    const result = await writeContract.vaultOut(USDTTokenForBnb.address, amount, USDTTokenForBnb.burnable, orderParamsHex, options)
} else {
    // This information can be obtained through the getChain APIs(builtInSwapProxy)
    const v2SwapContractForBNB = '0xbffca20712e0906d1ef74f3e0a7cbe050aa6228a'
    const params = {
        chain: 'BNB',
        aggregator,
        fromTokenAddress: USDTTokenForBnb.address,
        fromDecimal: USDTTokenForBnb.decimals,
        fromAmount: amount,
        toTokenAddress: USDCTokenForBnb.address,
        toDecimal: USDCTokenForBnb.decimals,
        sender: userAddress,
        recipient: v2SwapContractForBNB,
        slippage,
        allowPartialFill: true,
        routeSummary
    }
    const swapResult = await getAggregateSwap(params)
    if(swapResult.code !== 0) return
    const {data, to} = swapResult.data

    const channel = 'chainge'
    const toChain = 'BNB' // here is BNB
    const toTokenAddress = USDCTokenForBnb.address
    const toAddr = userAddress

    const shortOrderStr = `${channel}:${toChain}:${toTokenAddress}:${toAddr}:${slippage}:${extra}`
    const orderParamsHex = hexlify(toUtf8Bytes(shortOrderStr))
    
    let value = '0'
    if(USDTTokenForBnb.address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
        value = amount
    }
    const isApproved = await tokenApprove(USDTTokenForBnb, v2SwapContractForBNB)
    if(!isApproved) return false
    const signer = await getSigner()
    const writeContract = new ethers.Contract(v2SwapContractForBNB, abi, signer)
    const options = {value: value}
    const result = await writeContract.swap(USDTTokenForBnb.address, amount, to, USDCTokenForBnb.address, userAddress, miniAmountForExtra, data, orderParamsHex, options)
}

Last updated