The index value of the token used in the transaction within our system.
toChain
The nickName value of the Chain used in the transaction within our system.
toAddr
The target user’s address for this transaction.
slippage
The slippage information for this transaction. This value is in ten-thousandths, e.g., 10 (0.1%). When orderType = 1, the slippage is always 0.
execStrategy
Reserved field, pass an empty string by default.
extra
Transaction verification information; the accuracy of this value determines whether the transaction can be successful.
triggerPrice
Reserved field, pass "0" by default.
timeout
Reserved field, pass "0" by default.
channel
chainge
sourceCerts
Fields
Description
fromAmount
Transaction quantity.
fromIndex
The index value of the token used in the transaction within our system.
fromChain
The nickName value of the Chain used in the transaction within our system.
fromAddr
The current address of the user's connected kaspa wallet.
certHash
Transaction hash.
fromPublicKey
The publicKey of the current address.
signature
"123456"
example:
This example is for demonstration purposes only.
import BigNumber from'bignumber.js';import { formatUnits, hexlify, parseUnits, toUtf8Bytes } from'ethers'import { getAggregateQuote, getChain } from'../api.js';constfromAddress='kaspa:qzg4g46sd3hnax9fnjqdc2jfljs39f9ng00ntfhdz28rfwyc8adzuksnutgrq'consttoAddress='kaspa:qzg4g46sd3hnax9fnjqdc2jfljs39f9ng00ntfhdz28rfwyc8adzuksnutgrq'constpublicKey='03915457506c6f3e98a99c80dc2a49fca112a4b343df35a6ed128e34b8983f5a2e'constwallet= {} // The kasWare wallet object.constchannelFeeRate='0'// This information can be obtained through the getAssets APIs.constfromTokenForKAS= { index:'18', symbol:'KAS', decimals:8, address:'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'}consttoTokenForKAS= { index:'103', symbol:'PEPE', decimals:8, address:'PEPE'}// init base datalet supportChainList = []constinitData=async() => {constchains=awaitgetChain() supportChainList = chains}constfromAmount='100'constamount=parseUnits(fromAmount,fromTokenForKAS.decimals).toString()let extra =''let customSlippage ='5'// percentage format, ex: 5%let toExpectAmount =''constfnGetAggregateQuote=async () => {constquoteParams= { fromAmount: amount, fromTokenAddress:fromTokenForKAS.address, fromDecimal:fromTokenForKAS.decimals, fromChain:'KAS', toTokenAddress:toTokenForKAS.address, toDecimal:toTokenForKAS.decimals, toChain:'KAS', channelFeeRate: channelFeeRate, }// quote constquoteResult=awaitgetAggregateQuote(quoteParams)if(quoteResult.code !==0) returnconst { chain,chainDecimal,outAmount,serviceFee,gasFee,slippage } =quoteResult.dataconstreceiveAmount=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 InfoconstexecutionChainObj=supportChainList.find((item) =>item.network === chain)// Calculate the value the user should receive. constreceiveAmountHr=formatUnits(receiveAmount, chainDecimal)constreceiveAmountForExtra=parseUnits(receiveAmountHr,toTokenForKAS.decimals).toString()// 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 || slippageconstminiAmount=BigNumber(receiveAmountHr).multipliedBy(BigNumber((1- (tempSlippage *0.01)))).toFixed(toTokenForKAS.decimals)constminiAmountForExtra=parseUnits(miniAmount,toTokenForKAS.decimals).toString()// 1_Expected value;2_Third party profit ratio;3_version;4_Mini Amount;5_Execution chain extra =`1_${receiveAmountForExtra};2_${channelFeeRate};3_2;4_${miniAmountForExtra};5_${executionChainObj.nickName}` customSlippage = tempSlippage toExpectAmount = receiveAmountForExtra}constfnSubmitOrder=async (tradeHash) => {constsourceCertsObj= { fromAmount: amount, fromIndex:fromTokenForKAS.index.toString(), fromChain:'KAS', fromAddr: fromAddress, certHash: tradeHash, fromPublicKey: publicKey, signature:"123456", };constsourceCertsStr=JSON.stringify(sourceCertsObj);let sourceCertsHex =hexlify(toUtf8Bytes(sourceCertsStr)); sourceCertsHex =sourceCertsHex.substring(2);// The slippage here needs to be converted to basis points (1/10,000).let slippageNumBI =BigNumber(customSlippage)constformatSlippage=slippageNumBI.multipliedBy(BigNumber(100)).toFixed(0)constparams= {"sourceCerts": sourceCertsHex,"orderType":"2","toIndex":toTokenForKAS.index.toString(),"toChain":"KAS","toAddr": toAddress,"slippage": formatSlippage,"execStrategy":"","extra": extra,"triggerPrice":"0","timeout":"0","channel":"chainge"// your channel }let raw =`${sourceCertsObj.certHash}_${sourceCertsObj.fromChain}_${sourceCertsObj.fromIndex}_${sourceCertsObj.fromAmount}_${params.toChain}_${params.toIndex}_${toExpectAmount}_${params.toAddr}`// 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/v1/submitOrder', { method:"POST", headers: {"Content-Type":"application/json",...header }, body:JSON.stringify(params) })constresult=awaitresponse.json()}constfnCore=async () => {// step 1: init base dataawaitinitData()// step 2: quote awaitfnGetAggregateQuote()// 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: submitOrderawaitfnSubmitOrder(txHash)}fnCore()
NOTE:
We have three minter addresses; make sure to distinguish between them when using them.