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';constRPC_ENDPOINT='YOUR_RPC_ENDPOINT';constkardiaClient=newKardiaClient({ endpoint:RPC_ENDPOINT });constkrc20Instance=kardiaClient.krc20;// Fetch KRC20 token's data from smart contractawaitkrc20Instance.getFromAddress('KRC20_TOKEN_ADDRESS');constbalance=awaitkrc20Instance.balanceOf('YOUR_WALLET_ADDRESS');// `balance` will be your wallet's balance, but with token's `decimals` padding.// To get real ballance, use the following codeconstdecimals=awaitkrc20Instance.getDecimals();constbalanceBN=newBigNumber(balance);constdecimalsBN=newBigNumber(10** decimals);constparsedBalance=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';constRPC_ENDPOINT='YOUR_RPC_ENDPOINT';constkardiaClient=newKardiaClient({ endpoint:RPC_ENDPOINT });constkrc20Instance=kardiaClient.krc20;// Fetch KRC20 token's data from smart contractawaitkrc20Instance.getFromAddress('KRC20_TOKEN_ADDRESS');consttxResult=awaitkrc20Instance.transfer('YOUR_WALLET_PRIVATE_KEY','RECEIVER_ADDRESS',1// Amount of tokens to send);