# Submit order by Kaspa

### Header

<table><thead><tr><th width="189">Fields</th><th>Description</th></tr></thead><tbody><tr><td>Address</td><td>The current address of the user's connected  kaspa wallet.</td></tr><tr><td>PublicKey</td><td>The publicKey of the current address.</td></tr><tr><td>Chain</td><td>Required. KAS</td></tr><tr><td>Signature</td><td>The user's signature of the post body</td></tr></tbody></table>

### Body

<table><thead><tr><th width="191">Fields</th><th>Description</th></tr></thead><tbody><tr><td>sourceCerts</td><td>The hex string of the transaction data.</td></tr><tr><td>orderType</td><td><p>Transaction type.<br>• orderType: 1 represents cross-chain.</p><p>• orderType: 2 represents Aggregate.</p></td></tr><tr><td>toIndex</td><td>The index value of the token used in the transaction within our system.</td></tr><tr><td>toChain</td><td>The nickName value of the Chain used in the transaction within our system.</td></tr><tr><td>toAddr</td><td>The target user’s address for this transaction.</td></tr><tr><td>slippage</td><td>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.</td></tr><tr><td>execStrategy</td><td>Reserved field, pass an empty string by default.</td></tr><tr><td>extra</td><td>Transaction verification information; the accuracy of this value determines whether the transaction can be successful.</td></tr><tr><td>triggerPrice</td><td>Reserved field, pass "0" by default.</td></tr><tr><td>timeout</td><td>Reserved field, pass "0" by default.</td></tr><tr><td>channel</td><td>chainge</td></tr></tbody></table>

**sourceCerts**

<table><thead><tr><th width="189">Fields</th><th>Description</th></tr></thead><tbody><tr><td>fromAmount</td><td>Transaction quantity.</td></tr><tr><td>fromIndex</td><td>The index value of the token used in the transaction within our system.</td></tr><tr><td>fromChain</td><td>The nickName value of the Chain used in the transaction within our system.</td></tr><tr><td>fromAddr</td><td>The current address of the user's connected  kaspa wallet.</td></tr><tr><td>certHash</td><td>Transaction hash.</td></tr><tr><td>fromPublicKey</td><td>The publicKey of the current address.</td></tr><tr><td>signature</td><td>"123456"</td></tr></tbody></table>

example:&#x20;

This example is for demonstration purposes only.

<pre class="language-javascript"><code class="lang-javascript"><strong>import BigNumber from 'bignumber.js';
</strong>import { formatUnits, hexlify, parseUnits, toUtf8Bytes } from 'ethers'
import { getAggregateQuote,  getChain } from '../api.js';

const fromAddress = 'kaspa:qzg4g46sd3hnax9fnjqdc2jfljs39f9ng00ntfhdz28rfwyc8adzuksnutgrq'
const toAddress = 'kaspa:qzg4g46sd3hnax9fnjqdc2jfljs39f9ng00ntfhdz28rfwyc8adzuksnutgrq'
const publicKey = '03915457506c6f3e98a99c80dc2a49fca112a4b343df35a6ed128e34b8983f5a2e'
const wallet = {} // The kasWare wallet object.

const channelFeeRate = '0'
// This information can be obtained through the getAssets APIs.
const fromTokenForKAS = {
    index: '18',
    symbol: 'KAS',
    decimals: 8,
    address: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
}

const toTokenForKAS = {
    index: '103',
    symbol: 'PEPE',
    decimals: 8,
    address: 'PEPE'
}

// init base data
let supportChainList = []
const initData = async() => {
    const chains = await getChain()
    supportChainList = chains
}


const fromAmount = '100'
const amount = parseUnits(fromAmount, fromTokenForKAS.decimals).toString()
let extra = ''
let customSlippage = '5' // percentage format, ex: 5%
let toExpectAmount = ''

const fnGetAggregateQuote = async () => {
    const quoteParams = {
        fromAmount: amount,
        fromTokenAddress: fromTokenForKAS.address,
        fromDecimal: fromTokenForKAS.decimals,
        fromChain: 'KAS',
        toTokenAddress: toTokenForKAS.address,
        toDecimal: toTokenForKAS.decimals,
        toChain: 'KAS',
        channelFeeRate: channelFeeRate,
    }

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

    const receiveAmount = BigInt(outAmount) - BigInt(serviceFee) - BigInt(gasFee)
    if(receiveAmount &#x3C;= 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, 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.
    const tempSlippage = customSlippage || slippage
    const miniAmount = BigNumber(receiveAmountHr).multipliedBy(BigNumber((1 - (tempSlippage * 0.01)))).toFixed(toTokenForKAS.decimals)
    const miniAmountForExtra = 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
}

const fnSubmitOrder = async (tradeHash) => {
    const sourceCertsObj = {
        fromAmount: amount,
        fromIndex: fromTokenForKAS.index.toString(),
        fromChain: 'KAS',    
        fromAddr: fromAddress,
        certHash: tradeHash,
        fromPublicKey: publicKey,
        signature: "123456",
    };
    const sourceCertsStr = 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)
    const formatSlippage = slippageNumBI.multipliedBy(BigNumber(100)).toFixed(0)
    const params = {
        "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 = ''

    const header = {
        Address: fromAddress,
        PublicKey: publicKey,
        Chain: 'KAS',
        Signature: signature
    }

    const response = await fetch('https://api2.chainge.finance/v1/submitOrder', {
        method: "POST",
        headers: {
             "Content-Type": "application/json",
             ...header
        },
        body: JSON.stringify(params)
    })
    const result = await response.json()
}


const fnCore = async () => {
    // step 1: init base data
    await initData()

    // step 2: quote 
    await fnGetAggregateQuote()

    // 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 fnSubmitOrder(txHash)
}

fnCore()
</code></pre>

> ***NOTE:***\
> We have three minter addresses; make sure to distinguish between them when using them.
>
> *KAS TOKEN:*
>
> kaspa:qpgmt2dn8wcqf0436n0kueap7yx82n7raurlj6aqjc3t3wm9y5ssqtg9e4lsm
>
> *CUSDT/CUSDC/CETH/CBTC/CXCHNG TOKEN:*\
> kaspa:qpy03sxk3z22pacz2vkn2nrqeglvptugyqy54xal2skha6xh0cr7wjueueg79\
> \&#xNAN;*KRC20 TOKEN:*\
> kaspa:qz9cqmddjppjyth8rngevfs767m5nvm0480nlgs5ve8d6aegv4g9xzu2tgg0u
