Chainge Finance
  • The Most Liquid Web 3 Trading Venue
  • 🟡Introduction
    • 💡What Is Chainge?
    • About Chainge
      • 🥇Executive Board
      • 📈Performance Metrics
      • ⛓️Integrated DEXs, Aggregators, and Blockchains
  • 🟡Getting Started
    • 🧭Navigating the Chainge dApp
    • ▶️Video Walkthroughs
    • 🏦Chainge Wallet
  • 🟡Technology
    • 📄Overview
    • 🔺Key Differentiators
    • 🛡️Security
      • 🧩How DCRM Works
  • 🟡Core Features
    • 🔀Cross-Chain Swaps
    • 🌉Bridge To Everywhere
    • 🔑Self-Custodial Wallet
    • ⏱️Limit Orders
    • 🤖AI-Powered Features (Coming Soon)
  • 🟡Governance
    • 🏛️Chainge DAO
  • 🟡Economics
    • 🪙Token (XCHNG)
      • 🛠️Utilities and Functions
      • 📊Tokenomics
      • 🔼Staking
      • 💵Profit Share
  • 🟡Developer Tools
    • 📃Chainge 2.0 API Documentation
      • 🟡Get Chains and Tokens
      • 💲Get Price Quote API
      • 🔗Aggregate Swap
      • 📝Submit order
      • 🎟️Submit Ticket
      • Submit order by Kaspa
      • 💻Github Examples
      • 💰Fee Channel
      • ⚡Earn feature API
    • 📖Knot.meme API Document
      • Get KRC20 Tickers
      • Base API
      • Core API
  • 🟡Official resources
    • 📂Github repositories
    • 🌐Social networks
Powered by GitBook
On this page

Was this helpful?

  1. Developer Tools
  2. Knot.meme API Document

Core API

GET https://api2.chainge.finance/fun/getVault

Query Parameters

Name
Type
Description

ticker*

String

krc20 ticker

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

GET https://api2.chainge.finance/fun/quote

Query Parameters

Name
Type
Description

fromTicker*

String

krc20 ticker

toTicker*

String

krc20 ticker

fromAmount*

String

BigNumber String. ex: 10 kas , 1000000000

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

POST https://api2.chainge.finance/fun/submitSwap

Query Parameters

Name
Type
Description

channel*

String

Unique KEY. ex: knot

certHash*

String

Transaction hash

fromTicker*

String

krc20 ticker

fromAmount*

String

BigNumber String. ex: 10 kas , 1000000000

toTicker*

String

krc20 ticker

toAmount*

String

BigNumber String. ex: 10 kas , 1000000000

toAmountMin*

String

BigNumber String.

Headers Parameters

Name
Type
Description

Address*

String

User address

PublicKey*

String

User publicKey

Chain*

String

KAS

Signature*

String

Verify signature

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

GET https://api2.chainge.finance/fun/checkSwap

Query Parameters

Name
Type
Description

id*

String

submitSwap result, order id

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

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

example:

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

PreviousBase APINextGithub repositories

Last updated 4 months ago

Was this helpful?

🟡
📖