status:
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
This example is for demonstration purposes only.
import BigNumber from 'bignumber.js';
import { formatUnits, hexlify, parseUnits, toUtf8Bytes } from 'ethers'
const fromAddress = 'kaspa:qzg4g46sd3hnax9fnjqdc2jfljs39f9ng00ntfhdz28rfwyc8adzuksnutgrq'
const publicKey = '03915457506c6f3e98a99c80dc2a49fca112a4b343df35a6ed128e34b8983f5a2e'
const wallet = {} // The kasWare wallet object.
const fromAmount = '10'
const fromAmountSwap = 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 = ''
const fnGetQuote = async () => {
const quoteParams = {
fromTicker: "KAS",
toTicker: "RATS",
fromAmount: fromAmountSwap
}
// quote
const response = await fetch(`https://api2.chainge.finance/fun/quote?fromTicker=${quoteParams.fromTicker}&toTicker=${quoteParams.toTicker}&fromAmount=${quoteParams.fromAmount}`)
const quoteResult = await response.json()
if(quoteResult.code !== 0) return
let { amountOut, serviceFee, gasFee, slippage } = quoteResult.data
// slippage: 5%
slippage = slippage.replace('%', '') // '5'
const receiveAmount = 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.
const receiveAmountHr = 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.
const tempSlippage = customSlippage || slippage
const miniAmountHr = BigNumber(receiveAmountHr).multipliedBy(BigNumber((1 - (tempSlippage * 0.01)))).toFixed(8, BigNumber.ROUND_DOWN)
const miniAmount = parseUnits(miniAmount, 8).toString()
toAmountSwap = receiveAmount
toAmountMinSwap = miniAmount
}
const fnSubmitSwap = async (tradeHash) => {
const params = {
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 = ''
const header = {
Address: fromAddress,
PublicKey: publicKey,
Chain: 'KAS',
Signature: signature
}
const response = await fetch('https://api2.chainge.finance/fun/submitSwap', {
method: "POST",
headers: {
"Content-Type": "application/json",
...header
},
body: JSON.stringify(params)
})
const result = await response.json()
}
const fnGetMinterAddr = async () => {
const response = await fetch('https://api2.chainge.finance/fun/getVault?ticker=KAS')
const result = await response.json()
return result.data.vault
}
const fnCore = async () => {
// step 1: quote
await fnGetQuote()
// step 2: get minter address
const minterAddr = await fnGetMinterAddr()
// 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()
const txHash = ''
// step 4: submitOrder
await fnSubmitSwap(txHash)
}
fnCore()