0.2.2 • Published 3 years ago

kaidex-sdk v0.2.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Introduction

KaiDex SDK is a javascript library designed to help interact with KaiDex. It provides tools and utilities to convert clients' input params into KaiDex's smart contract compatible arguments. From which developers can freely build their own applications on top of KaiDex.

I. Install

npm

  npm install kaidex-sdk

yarn

  yarn add kaidex-sdk

From source

  npm install https://github.com/kardiachain/kaidex-sdk

II. Usage

Quick start:

Initially, please provide required endpoints for all the KaiDex smart contracts:

Constructor params: KaidexOptions

import KaidexClient from 'kaidex-sdk';

const kaidexClient = new KaidexClient({
    smcAddresses: {
        router: ROUTER_SMC_ADDRESS,
        limitOrder: LIMIT_ORDER_SMC_ADDRESS,
        factory: FACTORY_SMC_ADDRESS,
        wkai: WKAI_SMC_ADDRESS
    },
    rpcEndpoint: RPC_ENDPOINT
})

2.1 Transactions:

To create transactions in KaiDex SMC, please provide required input-parameters for each method. The client will return processed data, including methodName, arguments, KaiAmount, which is then used to interact with corresponding ABI via kaidexClient.invokeSMC() method.

Invoke SMC: kaidexClient.invokeSMC()

Add liquidity: kaidexClient.addLiquidityCallParameters()

const { methodName, args, amount } = kaidexClient.addLiquidityCallParameters({
    inputAmount,
    outputAmount,
    tokenA,
    tokenB,
    walletAddress,
    slippageTolerance,
    txDeadline
})

const txResponse = await kaidexClient.invokeSMC({
    abi: kaidexClient.abis.router,
    smcAddr: kaidexClient.smcAddresses.router,
    methodName: methodName,
    params: args,
    amount: amount
})

Remove liquidity: kaidexClient.removeLiquidityCallParameters()

const { methodName, args, amount } = await kaidexClient.removeLiquidityCallParameters({
    pair,
    withdrawAmount,
    walletAddress,
    slippageTolerance,
    txDeadline,
})

const txResponse = await kaidexClient.invokeSMC({
    abi: kaidexClient.abis.router,
    smcAddr: kaidexClient.smcAddresses.router,
    methodName: methodName,
    amount: amount,
    params: args
})

Swap tokens: kaidexClient.marketSwapCallParameters

const { methodName, args, amount } = kaidexClient.marketSwapCallParameters({
    amountIn,
    amountOut,
    inputToken,
    outputToken,
    addressTo,
    inputType,
    txDeadline,
    slippageTolerance
})

const txResponse = await kaidexClient.invokeSMC({
    abi: this.abis.router,
    smcAddr: this.smcAddresses.router,
    methodName,
    amount,
    params: args
})

2.2 Utils:

Get token balance: kaidexClient.getTokenBalance()

  • params:
tokenAddress: string
walletAddress: string
const tokenBalance = await kaidexClient.getTokenBalance(tokenAddress, walletAddress)

Check token's approval state: kaidexClient.getApprovalState()

  • params:
tokenAddr: string;
decimals: number;
walletAddress: string;
spenderAddress: string;
amountToCheck: number | string;
const isApproved = await kaidexClient.getApprovalState({
    tokenAddr,
    decimals,
    walletAddress, 
    spenderAddress,
    amountToCheck
})

Check price impact: kaidexClient.calculatePriceImpact()

const priceImpact = await kaidexClient.calculatePriceImpact(inputToken, outputToken, amountIn, amountOut)

Check pair's reserves: kaidexClient.getReverses()

  • params:
tokenA: string
tokenB: string
const { reserveA, reserveB } = await kaidexClient.getReverses(tokenA, tokenB)

Calculate output amount: kaidexClient.calculateOutputAmount()

const outputAmount = await kaidexClient.calculateOutputAmount(amount, inputToken, outputToken, inputType)

III. Types:

Usage

import {
    KaidexOptions,
    Token,
    PooledTokens,
    LiquidityPair,
    SMCParams,
    InputParams
} from 'kaidex-sdk'

Kaidex Options

interface KaidexOptions {
    rpcEndpoint?: string;
    abis?: ABIS;
    smcAddresses?: SmcAddresses;
}

interface ABIS {
    factory?: any;
    krc20?: any;
    limitOrder?: any;
    router?: any;
}

interface SmcAddresses {
    router?: string;
    factory?: string;
    limitOrder?: string;
    wkai?: string;
}

Token and pair

interface Token {
  tokenAddress: string;
  name: string;
  symbol: string;
  decimals: number;
}

interface PooledTokens {
  reserveA: string;
  reserveB: string;
}

interface LiquidityPair {
    balance: string;
    name: string;
    pairAddress: string;
    tokenA: Token;
    tokenB: Token;
    provider: string;
    pooledTokens: PooledTokens;
}

Enum

enum TradeType {
    BUY = 0,
    SELL = 1,
}

enum InputType {
    EXACT_IN = 0,
    EXACT_OUT = 1,
}

SMCParams

namespace SMCParams {
  interface CallParams {
    methodName: string;
    args: (string | string[] | number)[];
    amount?: number | string;
  }

  interface InvokeParams {
    abi: any;
    smcAddr: string;
    methodName: string;
    params: (string | string[] | number)[];
    amount?: number;
    gasLimit?: number;
    gasPrice?: number;
  }
}

InputParams

namespace InputParams {
    interface CalculateOutputAmount {
        amount: number | string;
        inputToken: Token;
        outputToken: Token;
        inputType: InputType;
    }

    interface CalculatePriceImpact {
        inputToken: Token;
        outputToken: Token;
        amountIn: string;
        amountOut: string;
    }

    interface AddLiquidity {
        inputAmount: string | number;
        outputAmount: string | number;
        tokenA: Token;
        tokenB: Token;
        walletAddress: string;
        slippageTolerance: string | number;
        txDeadline: number;
    }

    interface RemoveLiquidity {
        pair: LiquidityPair;
        withdrawAmount: string;
        walletAddress: string;
        slippageTolerance: string | number;
        txDeadline: number;
    }

    interface MarketSwap {
        amountIn: string;
        amountOut: string;
        inputToken: Token;
        outputToken: Token;
        addressTo: string;
        inputType: InputType;
        txDeadline: number;
        slippageTolerance: string | number;
    }
}
0.2.1

3 years ago

0.2.2

3 years ago

0.2.0

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago