V2-SDK
About
The KoinDX v2-sdk is a node module which can be used to interact with the KoinDX v2 contracts.
Getting Started
Installation
- npm
- yarn
- pnpm
- bun
npm install @koindx/v2-sdk
yarn add @koindx/v2-sdk
pnpm add @koindx/v2-sdk
bun add @koindx/v2-sdk
If you want to interact with the chain, you may want to install bignumber.js, dotenv and koilib as well.
- npm
- yarn
- pnpm
- bun
npm install bignumber.js koilib dotenv
yarn add bignumber.js koilib dotenv
pnpm add bignumber.js koilib dotenv
bun add bignumber.js koilib
Import
Import the package in a Javascript or Typescript file.
Harbinger is a KoinDX Testnet. If you want to work on Mainnet, please use ChainId.MAINNET
.
import { ChainId } from "@koindx/v2-sdk";
console.log(ChainId.HARBINGER);
const SDK = require("@koindx/v2-sdk");
console.log(SDK.ChainId.HARBINGER);
Example
These examples demonstrate the basic usage of the KoinDX v2-sdk.
Fetch Token
import { ChainId, Fetcher } from "@koindx/v2-sdk";
let token = await Fetcher.fetchTokenData(ChainId.HARBINGER,"1FaSvLjQJsCJKq5ybmGsMMQs8RQYyVv8ju");
console.log(token);
Native Assets
KOIN and VHP are predefined assets in the SDK. You can directly create the currency objects without fetching them from the chain.
import { ChainId, TKOIN } from "@koindx/v2-sdk";
const tkoin = new TKOIN(ChainId.HARBINGER);
console.log(tkoin);
Fetch Pair
Only the chain id and two token currency objects are required to fetch the data a token pair from KoinDX. Some return values will be presented as a BigNumber object. The SDK works with BigNumber.js which you might want to use as well.
import { ChainId, Fetcher, TKOIN, VHP } from "@koindx/v2-sdk";
const tkoin = new TKOIN(ChainId.HARBINGER);
const vhp = new VHP(ChainId.HARBINGER);
let pair = await Fetcher.fetchPairData(ChainId.HARBINGER, tkoin, vhp);
console.log(pair);
Add liquidity
import { Pool, ChainId, Fetcher, TKOIN, Percent, VHP } from "@koindx/v2-sdk";
import { Signer } from "koilib"
import BigNumber from "bignumber.js";
import 'dotenv/config'
// define slippage
const slippage = new Percent(new BigNumber("10"))
// create assets
const tkoin = new TKOIN(ChainId.HARBINGER);
const vhp = new VHP(ChainId.HARBINGER)
// create signer
const signer = Signer.fromWif(process.env.ACCOPUNT_PK);
// create pair
const PAIR_VHPKOIN = await Fetcher.fetchPairData(ChainId.HARBINGER, tkoin, vhp)
// create pool
let pool = new Pool(PAIR_VHPKOIN);
let result = pool.addLiquidity(tkoin, new BigNumber("100000"), slippage)
let data = {
from: signer.getAddress(),
receiver: signer.getAddress(),
token_a: result.token_a.address,
token_b: result.token_b.address,
amount_a_desired: result.amount_a_desired.toFixed(0, 1),
amount_b_desired: result.amount_b_desired.toFixed(0, 1),
amount_a_min: result.amount_a_min.toFixed(0, 1),
amount_b_min: result.amount_b_min.toFixed(0, 1)
}
console.log(data)
Trade
This example shows how the Router object is used and how Koilib can be configured to execute a trade through the KoinDX Periphery contract. Besides the V2-SDK and koilib this example code uses dotenv and bignumber.js.
import { ChainId, Fetcher, TKOIN, Percent, CHAIN_TO_PROVIDER_MAP, Router, VHP, PeripheryAbi, CHAIN_TO_ADDRESSES_MAP } from "@koindx/v2-sdk";
import { Signer, Contract, Transaction } from "koilib"
import BigNumber from "bignumber.js";
import 'dotenv/config'
// provider
const provider = CHAIN_TO_PROVIDER_MAP[ChainId.HARBINGER]
// define slippage
const slippage = new Percent(new BigNumber("10"))
// create asset
const tkoin = new TKOIN(ChainId.HARBINGER);
const vhp = new VHP(ChainId.HARBINGER)
// create signer
const signer = Signer.fromWif(process.env.ACCOPUNT_PK);
// set provider
signer.provider = provider
// create pair
const PAIR_VHPKOIN = await Fetcher.fetchPairData(ChainId.HARBINGER, tkoin, vhp)
/**
* TRADE
* */
// create the route by adding the trading pairs and relevant Currencies parameters
// in this example: VHP/KOIN Pair and the input currency VHP
// A route through multiple pairs (vhp to KoinDX token) would look like this:
// let router = new Router([ PAIR_VHPKOIN, PAIR_KOINKOINDX ], vhp, koindx)
let router = new Router([PAIR_VHPKOIN], vhp)
// get the data which will be the parameters for our contract call
let result = router.tradeExactIn(new BigNumber("100000"), slippage)
let data = {
from: signer.getAddress(),
receiver: signer.getAddress(),
amountIn: result.amount_in.toFixed(0, 1),
amountOutMin: result.amount_out_min.toFixed(0, 1),
path: result.path.map(token => token.address)
}
console.log(data)
/**
* FINAL PROCESS
*/
// get the addresses related to the correct chainId
let address = CHAIN_TO_ADDRESSES_MAP[ChainId.HARBINGER]
// setup periphery contract
let periphery = new Contract({
id: address.periphery,
abi: PeripheryAbi,
provider: provider,
})
// set only operation
periphery.options.onlyOperation = true;
// define configs
let configs = { payer: signer.getAddress() };
// create new Transaction
const tx = new Transaction({ signer })
// add a swap_tokens_in operation
tx.pushOperation(periphery.functions.swap_tokens_in(data, configs))
// send the Transaction
const receipt = await tx.send();
await tx.wait();
console.log(receipt)