CAPS is the Ternoa blockchain token. Transactions made on the Ternoa blockchain are carried out in CAPS. It is used in particular for the creation of NFTs, Encryption, and storage of data over time.
Prerequisites
Before getting started, make sure you have the following ready:
This example shows how to get the total CAPS balance of an address using getTotalBalance. The total balance corresponds to the sum of the free balance and the reserve balance. The balanceToNumber helper is used to format balances from a BN (big number) to a humanized value.
import { balanceToNumber, getTotalBalance } from"ternoa-js";// Public address of the known account we want to useconstADDRESS="5Cf8PBw7QiRFNPBTnUoks9Hvkzn8av1qfcgMtSppJvjYcxp6";constmain=async () => {try {consttotalBalanceBN=awaitgetTotalBalance(ADDRESS);consttotalBalance=balanceToNumber(totalBalanceBN);console.log(`The total balance of ${ADDRESS} is:`, totalBalance); } catch (e) {console.error(e); }};main();
How to get the transferrable CAPS balance of an address
This example shows how to get the transferrable CAPS balance of an address using getTransferrableBalance. The transferrable balance corresponds to the liquid balance of an account; we do not take into account the staked balance nor the balance locked on the governance. The balanceToNumber helper is used to format balances from a BN (big number) to a humanized value.
import { balanceToNumber, getTransferrableBalance } from"ternoa-js";// Public address of the known account we want to useconstADDRESS="5Cf8PBw7QiRFNPBTnUoks9Hvkzn8av1qfcgMtSppJvjYcxp6";constmain=async () => {try {consttransferrableBalanceBN=awaitgetTransferrableBalance(ADDRESS);consttransferrableBalance=balanceToNumber(transferrableBalanceBN);console.log(`The transferrable balance of ${ADDRESS} is:`, transferrableBalance ); } catch (e) {console.error(e); }};main();
How to subscribe to CAPS balance changes
This example shows how to retrieve balance updates.
import { balanceToNumber, getBalances, getRawApi } from"ternoa-js";constADDRESS="5Cf8PBw7QiRFNPBTnUoks9Hvkzn8av1qfcgMtSppJvjYcxp6";constmain=async () => {try {constapi=getRawApi();// Retrieve the initial balance.let previousFree = (awaitgetBalances(ADDRESS)).free;console.log(`Initial balance: ${balanceToNumber(previousFree)}`);// Here we subscribe to any balance changes and update the on-screen valueapi.query.system.account(ADDRESS, ({ data: { free: currentFree } }:any) => {// Calculate the deltaconstchange=currentFree.sub(previousFree);// Only display positive value changes (Since we are pulling `previous` above already,// the initial balance change will also be zero)if (!change.isZero()) {console.log(`New balance: ${balanceToNumber(currentFree)}`);console.log(`Change of ${balanceToNumber(change)}`); previousFree = currentFree; } } ); } catch (e) {console.error(e); }};main();
Support
If you face any trouble, feel free to reach out to our community engineers in our Discord.