📝Submit order
You are going to interact with Contract ABI directly. Here is an example for aggregate swap. Bridge and direct swap work the same way.
// It's an example from ETH on ETH to BNB 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';
// This information can be obtained through the getChain APIs,(builtInMinterProxyV2)
// builtInMinterProxyV2 is for aggregate swap and bridge
// builtInSwapProxy is for direct swap on same chain
const v2ContractForETH = '0x4c5f53015f3adb1b1d15ddf4e17edaae6fa185a5' // here is an example for aggregate cross-chain swap using builtInMinterProxyV2
// This information can be obtained through the getAssets APIs.
const ETHTokenForEth = {
index: '4',
symbol: 'ETH',
decimals: 18,
address: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee',
burnable: false
}
const BNBTokenForBnb = {
index: '124',
symbol: 'BNB',
decimals: 18,
address: '0x4b0f1812e5df2a09796481ff14017e6005508003',
burnable: false
}
const fromAmount = '1'
const amount = parseUnits(fromAmount, ETHTokenForEth.decimals).toString()
const params = {
fromAmount: amount,
fromTokenAddress: ETHTokenForEth.address,
fromDecimal: ETHTokenForEth.decimals,
fromChain: 'ETH',
toTokenAddress: BNBTokenForBnb.address,
toDecimal: BNBTokenForBnb.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}`
const channel = 'chainge'
const toChain = 'BNB' // here is BNB
const toTokenAddress = BNBTokenForBnb.address
const toAddr = userAddress
const shortOrderStr = `${channel}:${toChain}:${toTokenAddress}:${toAddr}:${slippage}:${extra}`
const orderParamsHex = hexlify(toUtf8Bytes(shortOrderStr))
let value = '0'
// Check if it is the native currency.
if(ETHTokenForEth.address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
value = amount
}
const isApproved = await tokenApprove(ETHTokenForEth, v2ContractForETH)
if(!isApproved) return false
const signer = await getSigner()
const writeContract = new ethers.Contract(v2ContractForETH, abi, signer)
const options = {value: value}
const result = await writeContract.vaultOut(ETHTokenForEth.address, amount, ETHTokenForEth.burnable, orderParamsHex, options)
}
Last updated