Unknown, Refunding, Pending //all regarded as pending
Succeeded //order executed successfully
Dropped //order dropped for any reasons, manual refund is needed
Refunded //order cannot proceed for any reasons, system refunded automatically
example:
This example is for demonstration purposes only.
import BigNumber from'bignumber.js';import { formatUnits, hexlify, parseUnits, toUtf8Bytes } from'ethers'constfromAddress='kaspa:qzg4g46sd3hnax9fnjqdc2jfljs39f9ng00ntfhdz28rfwyc8adzuksnutgrq'constpublicKey='03915457506c6f3e98a99c80dc2a49fca112a4b343df35a6ed128e34b8983f5a2e'constwallet= {} // The kasWare wallet object.constfromAmount='10'constfromAmountSwap=parseUnits(fromAmount,8).toString(); // The decimals for KRC20 tokens are all set to 8.let customSlippage ='5'// percentage format, ex: 5%let toExpectAmount =''let toAmountSwap =''let toAmountMinSwap =''constfnGetQuote=async () => {constquoteParams= { fromTicker:"KAS", toTicker:"RATS", fromAmount: fromAmountSwap }// quote constresponse=awaitfetch(`https://api2.chainge.finance/fun/quote?fromTicker=${quoteParams.fromTicker}&toTicker=${quoteParams.toTicker}&fromAmount=${quoteParams.fromAmount}`)constquoteResult=awaitresponse.json()if(quoteResult.code !==0) returnlet { amountOut, serviceFee, gasFee, slippage } =quoteResult.data// slippage: 5% slippage =slippage.replace('%','') // '5'constreceiveAmount=BigInt(amountOut) -BigInt(serviceFee) -BigInt(gasFee)if(receiveAmount <=BigInt(0)) {// The current quote amount cannot cover the fees. Please enter a larger amount.return }// Calculate the value the user should receive. constreceiveAmountHr=formatUnits(receiveAmount,8)// Computed minimum, After calculating the minimum value, we need to convert it to the decimals of the target chain.// The slippage here is in percentage format. // The slippage returned by this interface is our recommended value, but you can set your own slippage.consttempSlippage= customSlippage || slippageconstminiAmountHr=BigNumber(receiveAmountHr).multipliedBy(BigNumber((1- (tempSlippage *0.01)))).toFixed(8,BigNumber.ROUND_DOWN)constminiAmount=parseUnits(miniAmount,8).toString() toAmountSwap = receiveAmount toAmountMinSwap = miniAmount}constfnSubmitSwap=async (tradeHash) => {constparams= { fromTicker:"KAS", fromAmount: fromAmountSwap, toTicker:'RATS', toAmount: toAmountSwap, toAmountMin: toAmountMinSwap, certHash: tradeHash, channel:"knot", };let raw =`${params.channel}_${params.certHash}_${params.fromTicker}_${params.fromAmount}_${params.toTicker}_${params.toAmount}_${params.toAmountMin}`// Use the signMessage method of the kasWare wallet to sign a string.// let signature = wallet.signMessage(raw)let signature =''constheader= { Address: fromAddress, PublicKey: publicKey, Chain:'KAS', Signature: signature }constresponse=awaitfetch('https://api2.chainge.finance/fun/submitSwap', { method:"POST", headers: {"Content-Type":"application/json",...header }, body:JSON.stringify(params) })constresult=awaitresponse.json()}constfnGetMinterAddr=async () => {constresponse=awaitfetch('https://api2.chainge.finance/fun/getVault?ticker=KAS')constresult=awaitresponse.json()returnresult.data.vault}constfnCore=async () => {// step 1: quote awaitfnGetQuote()// step 2: get minter addressconstminterAddr=awaitfnGetMinterAddr()// step 3: Send transaction // You need to initiate a transaction to our minter through the wallet, and obtain the hash. This hash will be the transaction hash.// const txHash = await wallet.signKRC20Transaction()consttxHash=''// step 4: submitOrderawaitfnSubmitSwap(txHash)}fnCore()