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.
npm install kaidex-sdk
yarn add kaidex-sdk
npm install https://github.com/kardiachain/kaidex-sdk
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
})
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.
- params: SMCParams.InvokeSMC
- params: InputParams.AddLiquidity
- returns: SMCParams.CallParams
- Example:
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
})
- params: InputParams.RemoveLiquidity
- returns: SMCParams.CallParams
- Example:
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
})
- params: InputParams.MarketSwap
- returns: SMCParams.CallParams
- Example:
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
})
- params:
tokenAddress: string
walletAddress: string
const tokenBalance = await kaidexClient.getTokenBalance(tokenAddress, walletAddress)
- params:
tokenAddr: string;
decimals: number;
walletAddress: string;
spenderAddress: string;
amountToCheck: number | string;
const isApproved = await kaidexClient.getApprovalState({
tokenAddr,
decimals,
walletAddress,
spenderAddress,
amountToCheck
})
- params: InputParams.CalculatePriceImpact
const priceImpact = await kaidexClient.calculatePriceImpact(inputToken, outputToken, amountIn, amountOut)
- params:
tokenA: string
tokenB: string
const { reserveA, reserveB } = await kaidexClient.getReverses(tokenA, tokenB)
const outputAmount = await kaidexClient.calculateOutputAmount(amount, inputToken, outputToken, inputType)
import {
KaidexOptions,
Token,
PooledTokens,
LiquidityPair,
SMCParams,
InputParams
} from 'kaidex-sdk'
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;
}
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 TradeType {
BUY = 0,
SELL = 1,
}
enum InputType {
EXACT_IN = 0,
EXACT_OUT = 1,
}
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;
}
}
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;
}
}