# Core API

<mark style="color:blue;">`GET`</mark> `https://api2.chainge.finance/fun/getVault`

#### Query Parameters

| Name                                     | Type   | Description  |
| ---------------------------------------- | ------ | ------------ |
| ticker<mark style="color:red;">\*</mark> | String | krc20 ticker |

{% tabs %}
{% tab title="200:OK" %}

```json
{
    "code": 0,
    "msg": "success",
    "data": {
        "vault": "kaspa:qp6fny54gn7ydnxaltmmezc60qnleksxu6ctvtjch2dlvkvg8pak6rh33st6w"
    }
}
```

{% endtab %}
{% endtabs %}

<mark style="color:blue;">`GET`</mark> `https://api2.chainge.finance/fun/quote`

#### Query Parameters

| Name                                         | Type   | Description                               |
| -------------------------------------------- | ------ | ----------------------------------------- |
| fromTicker<mark style="color:red;">\*</mark> | String | krc20 ticker                              |
| toTicker<mark style="color:red;">\*</mark>   | String | krc20 ticker                              |
| fromAmount<mark style="color:red;">\*</mark> | String | BigNumber String. ex: 10 kas , 1000000000 |

{% tabs %}
{% tab title="200:OK" %}

```postman_json
{
    "code": 0,
    "msg": "success",
    "data": {
        "amountOut": "75553316920000",
        "amountOutUsd": "1.14",
        "userAmountOut": "38702849054762",
        "serviceFee": "2266599507600",
        "gasFee": "34583868357638",
        "serviceFeeRate": "3%",
        "priceImpact": "0.61%",
        "slippage": "5%"
    }
}
```

{% endtab %}
{% endtabs %}

<mark style="color:blue;">`POST`</mark> `https://api2.chainge.finance/fun/submitSwap`

#### Query Parameters

| Name                                          | Type   | Description                               |
| --------------------------------------------- | ------ | ----------------------------------------- |
| channel<mark style="color:red;">\*</mark>     | String | Unique KEY. ex: knot                      |
| certHash<mark style="color:red;">\*</mark>    | String | Transaction hash                          |
| fromTicker<mark style="color:red;">\*</mark>  | String | krc20 ticker                              |
| fromAmount<mark style="color:red;">\*</mark>  | String | BigNumber String. ex: 10 kas , 1000000000 |
| toTicker<mark style="color:red;">\*</mark>    | String | krc20 ticker                              |
| toAmount<mark style="color:red;">\*</mark>    | String | BigNumber String. ex: 10 kas , 1000000000 |
| toAmountMin<mark style="color:red;">\*</mark> | String | BigNumber String.                         |

**Headers Parameters**

| Name                                        | Type   | Description      |
| ------------------------------------------- | ------ | ---------------- |
| Address<mark style="color:red;">\*</mark>   | String | User address     |
| PublicKey<mark style="color:red;">\*</mark> | String | User publicKey   |
| Chain<mark style="color:red;">\*</mark>     | String | KAS              |
| Signature<mark style="color:red;">\*</mark> | String | Verify signature |

{% tabs %}
{% tab title="200:OK" %}

```
{
    "code": 0,
    "msg": "success",
    "data": {
        "id": "2030000000003",
    }
}
```

{% endtab %}
{% endtabs %}

<mark style="color:blue;">`GET`</mark> `https://api2.chainge.finance/fun/checkSwap`

#### Query Parameters

| Name                                 | Type   | Description                 |
| ------------------------------------ | ------ | --------------------------- |
| id<mark style="color:red;">\*</mark> | String | submitSwap result, order id |

{% tabs %}
{% tab title="200:OK" %}

```json
{
    "code": 0,
    "msg": "success",
    "data": {
        "status": "Succeeded",
        "hash": "dd2a4f0553f38eec8cad797965c2522bb59dd5308ce2a893ec15adfeb82aa19d",
        "since": 1736935562
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
status: &#x20;

***Unknown, Refunding, Pending*** //all regarded as pending&#x20;

***Succeeded*** //order executed successfully&#x20;

***Dropped*** //order dropped for any reasons, manual refund is needed&#x20;

***Refunded*** //order cannot proceed for any reasons, system refunded automatically
{% endhint %}

example:&#x20;

This example is for demonstration purposes only.

```javascript
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()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chainge-finance.gitbook.io/chainge-finance/get-started/knot.meme-api-document/core-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
