KardiaChain JS SDK
KardiaChain JS SDK is a wrapper built on top of KardiaChain RCF protocol that helps developers to easily interact with KardiaChain's blockchain network.
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
1
npm i kardia-js-sdk
Copied!

Usage

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
// Account module
7
const accountModule = kardiaClient.account;
8
9
// KAI module
10
const kaiModule = kardiaClient.kaiChain;
11
12
// Smart contract module
13
const smcModule = kardiaClient.contract;
14
15
// KRC20 module
16
const krcModule = kardiaClient.krc20;
17
18
// Transaction module
19
const transactionModule = kardiaClient.transaction;
Copied!

Account Module

Generating a wallet

1
import {KardiaAccount} from 'kardia-js-sdk';
2
3
const wallet = KardiaAccount.generateWallet();
4
5
// Expected result:
6
// {
7
// address: "YOUR_WALLET_ADDRESS",
8
// privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
9
// balace: 0
10
// }
Copied!

Get KAI balance of a wallet

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
// Account module
7
const accountModule = kardiaClient.account;
8
9
// Get balance
10
const balance = await accountModule.getBalance('YOUR_WALLET_ADDRESS');
Copied!

Access wallet by private key

1
import {KardiaAccount} from 'kardia-js-sdk';
2
3
const wallet = KardiaAccount.getWalletFromPK('YOUR_ADDRESS_PRIVATE_KEY');
4
5
// Expected result:
6
// {
7
// address: "YOUR_WALLET_ADDRESS",
8
// privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
9
// balace: 123 // your wallet balance
10
// }
Copied!

Access wallet by seed phrase

1
import {KardiaAccount} from 'kardia-js-sdk';
2
3
const wallet = KardiaAccount.getWalletFromMnemonic('YOUR_MNEMONIC_PHRASE');
4
5
// Expected result:
6
// {
7
// address: "YOUR_WALLET_ADDRESS",
8
// privateKey: "YOUR_ADDRESS_PRIVATE_KEY",
9
// balace: 123 // your wallet balance
10
// }
Copied!

KAI Module

Event Handler

1
const topic = keccak256('Transfer(address,address,uint256)');
2
3
const filterId = await kardiaClient.kaiChain.newFilter({
4
fromBlock: 200000,
5
topics: [topic]
6
})
7
8
// To get all logs
9
const allLogs = await kardiaClient.kaiChain.getFilterLogs(filterId);
10
11
// To get logs since last poll
12
const logs = await kardiaClient.kaiChain.getFilterChanges(filterId);
Copied!

KRC20 Module

Get KRC20 balance of a wallet

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
const krc20Instance = kardiaClient.krc20;
7
8
// Fetch KRC20 token's data from smart contract
9
await krc20Instance.getFromAddress('KRC20_TOKEN_ADDRESS');
10
11
const balance = await krc20Instance.balanceOf('YOUR_WALLET_ADDRESS');
12
// `balance` will be your wallet's balance, but with token's `decimals` padding.
13
// To get real ballance, use the following code
14
15
const decimals = krc20Instance.getDecimals();
16
const parsedBalance = balance / 10 ** decimals;
Copied!

Send a KRC20 transaction

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
const krc20Instance = kardiaClient.krc20;
7
8
const txResult = await krc20Instance.transfer(
9
'YOUR_WALLET_PRIVATE_KEY',
10
'RECEIVER_ADDRESS',
11
1 // Amount of tokens to send
12
);
Copied!

Transaction Module

Creating a transaction

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
// Get your wallet's latest nonce
7
const nonce = await kardiaClient.account.getNonce('YOUR_WALLET_ADDRESS');
8
const txData = {
9
to: 'RECEIVER_WALLET_ADDRESS',
10
nonce,
11
gas: 3000000, // Gas limit
12
gasPrice: 1*10**9, // Minimum Gas Price = 1 OXY
13
value: 231095, // Amount of KAI to send
14
};
15
16
// Send transaction to network and get transaction hash immediately
17
const txHash = await kardiaClient.transaction.sendTransaction(
18
txData,
19
'YOUR_WALLET_PRIVATEKEY'
20
);
21
22
// txHash should be something like '0x0a2db5831c314363a97a79f416061a9daec5230f8b6306cd1c431b467c42f820'
23
24
// If you want to wait to the transaction to complete, follow the below code
25
26
const txResult = await kardiaClient.transaction.sendTransaction(
27
txData,
28
'YOUR_WALLET_PRIVATEKEY',
29
true, // Flag to indicate if you want to wait for the transaction to complete
30
50000 // Time (in ms) you want to wait for transaction to complete, default will be 300000 (300s)
31
);
Copied!

Get transaction detail

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
const txDetail = await kardiaClient.transaction.getTransaction('TRANSACTION_HASH');
Copied!

Get transaction receipt

1
import KardiaClient from 'kardia-js-sdk';
2
const RPC_ENDPOINT = 'YOUR_RPC_ENDPOINT';
3
4
const kardiaClient = new KardiaClient({ endpoint: RPC_ENDPOINT });
5
6
const txReceipt = await kardiaClient.transaction.getTransactionReceipt('TRANSACTION_HASH');
Copied!