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 bignumber.js
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
);