KardiaChain JS SDK
  • Overview
  • Getting Started
  • Guides
    • Connecting to KardiaChain
      • Front-end
      • Back-end
    • Working with wallet
    • Sending your first transaction
    • Smart Contract: decentralize application
    • KRC20 tokens
    • KAI Chain module
    • Utilities
  • Reference
    • KardiaClient
    • Account
    • Transaction
    • KAI Chain
    • Smart contract
    • KRC20
    • Objects reference
Powered by GitBook
On this page
  • Get KRC20 balance of a wallet
  • Send a KRC20 transaction

Was this helpful?

  1. Guides

KRC20 tokens

PreviousSmart Contract: decentralize applicationNextKAI Chain module

Last updated 3 years ago

Was this helpful?

KRC20 tokens are blockchain-based assets that have value and can be sent and received. It's similar to Ethereum's ERC20 tokens.

Get KRC20 balance of a wallet

Unlike KAI, each KRC20 token has its own decimals so getting balance is a little more complex. You'll need to:

  • Get balance from KRC20 smart contract

  • Using the decimals of that smart contract, convert the received value. You can use any big number library but we recommend using

import KardiaClient from 'kardia-js-sdk';
import BigNumber from 'bignumber.js';

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 = await krc20Instance.getDecimals();
const balanceBN = new BigNumber(balance);
const decimalsBN = new BigNumber(10 ** decimals);
const parsedBalance = balanceBN.dividedBy(decimalsBN).toFixed(); // parsed balance in string

Send a KRC20 transaction

Sending a KRC20 tokens can be done simply by calling transfer method.

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 txResult = await krc20Instance.transfer(
    'YOUR_WALLET_PRIVATE_KEY',
    'RECEIVER_ADDRESS',
    1 // Amount of tokens to send
);
bignumber.js