[Deprecated] KardiaChain JS SDK

Note: KardiaChain is now fully compatible with standard web3 client libraries. Developers are recommended to use the standard web3 libraries in previous page instead of this.

This is just a quick introduction to Kardia JS SDK. For more detailed documentation, please refer to https://docs.kardiachain.io/js-sdk/

Getting Started

KardiaChain JS SDK contains 5 modules:

  • Account module is for managing KardiaChain wallet

  • KAI module is for interacting directly with KardiaChain's blockchain network

  • Contract module is for interacting with KardiaChain's smart contract.

  • KRC20 module is for interacting with KRC20 tokens

  • Transaction module is for creating KardiaChain's transaction.

Installation

KardiaChain JS SDK can be installed from npm

npm i kardia-js-sdk

Usage

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

// Account module
const accountModule = kardiaClient.account;

// KAI module
const kaiModule = kardiaClient.kaiChain;

// Smart contract module
const smcModule = kardiaClient.contract;

// KRC20 module
const krcModule = kardiaClient.krc20;

// Transaction module
const transactionModule = kardiaClient.transaction;

Account Module

Generating a wallet

import {KardiaAccount} from 'kardia-js-sdk';

const wallet = KardiaAccount.generateWallet();

// Expected result:
// {
//    address: "YOUR_WALLET_ADDRESS",
//    privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
//    balace: 0
// }

Get KAI balance of a wallet

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

// Account module
const accountModule = kardiaClient.account;

// Get balance
const balance = await accountModule.getBalance('YOUR_WALLET_ADDRESS');

Access wallet by private key

import {KardiaAccount} from 'kardia-js-sdk';

const wallet = KardiaAccount.getWalletFromPK('YOUR_ADDRESS_PRIVATE_KEY');

// Expected result:
// {
//    address: "YOUR_WALLET_ADDRESS",
//    privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
//    balace: 123 // your wallet balance
// }

Access wallet by seed phrase

import {KardiaAccount} from 'kardia-js-sdk';

const wallet = KardiaAccount.getWalletFromMnemonic('YOUR_MNEMONIC_PHRASE');

// Expected result:
// {
//    address: "YOUR_WALLET_ADDRESS",
//    privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
//    balace: 123 // your wallet balance
// }

KAI Module

Event Handler

const topic = keccak256('Transfer(address,address,uint256)');

    const filterId = await kardiaClient.kaiChain.newFilter({
      fromBlock: 200000,
      topics: [topic]
    })

    // To get all logs
    const allLogs = await kardiaClient.kaiChain.getFilterLogs(filterId);

    // To get logs since last poll
    const logs = await kardiaClient.kaiChain.getFilterChanges(filterId);

KRC20 Module

Get KRC20 balance of a wallet

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

const krc20Instance = kardiaClient.krc20;

// Fetch KRC20 token's data from smart contract
await krc20Instance.getFromAddress('KRC20_TOKEN_ADDRESS');

const balance = await krc20Instance.balanceOf('YOUR_WALLET_ADDRESS');
// `balance` will be your wallet's balance, but with token's `decimals` padding.
// To get real ballance, use the following code

const decimals = krc20Instance.getDecimals();
const parsedBalance = balance / 10 ** decimals;

Send a KRC20 transaction

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

const krc20Instance = kardiaClient.krc20;

const txResult = await krc20Instance.transfer(
    'YOUR_WALLET_PRIVATE_KEY',
    'RECEIVER_ADDRESS',
    1 // Amount of tokens to send
);

Transaction Module

Creating a transaction

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

// Get your wallet's latest nonce
const nonce = await kardiaClient.account.getNonce('YOUR_WALLET_ADDRESS');
const txData = {
  to: 'RECEIVER_WALLET_ADDRESS',
  nonce,
  gas: 3000000,      // Gas limit
  gasPrice: 1*10**9,     // Minimum Gas Price = 1 OXY
  value: 231095,    // Amount of KAI to send
};

// Send transaction to network and get transaction hash immediately
const txHash = await kardiaClient.transaction.sendTransaction(
  txData,
  'YOUR_WALLET_PRIVATEKEY'
);

// txHash should be something like '0x0a2db5831c314363a97a79f416061a9daec5230f8b6306cd1c431b467c42f820'

// If you want to wait to the transaction to complete, follow the below code

const txResult = await kardiaClient.transaction.sendTransaction(
  txData,
  'YOUR_WALLET_PRIVATEKEY',
  true,    // Flag to indicate if you want to wait for the transaction to complete
  50000    // Time (in ms) you want to wait for transaction to complete, default will be 300000 (300s)
);

Get transaction detail

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

const txDetail = await kardiaClient.transaction.getTransaction('TRANSACTION_HASH');

Get transaction receipt

import KardiaClient from 'kardia-js-sdk';
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';

const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });

const txReceipt = await kardiaClient.transaction.getTransactionReceipt('TRANSACTION_HASH');

Last updated