Get GTokens balances

Gtoken is a non-fungible utility token used within the Ternoa network, not mineable or tradable with other fungible tokens.

Prerequisites

Before getting started, make sure you have the following ready:

  1. Create a Ternoa account with Alphanet CAPS

  2. Own a GToken on alphanet created using Polkadot js portal.

  3. Install and set up your editor of choice (we will use Visual Studio Code [VSC] in this tutorial)

  4. Install NodeJS v.14+ & NPM

How to get the total GTokens balance of an address

This example shows how to get the total GTokens balance of an address using getAccountAssetBalance().

import {
	initializeApi,
	getAccountAssetBalance,
} from "ternoa-js";

const main = async () => {
	try {
		await initializeApi ();
		
		const ACCOUNT_ADDRESS = "REPLACE_WITH_THE_ADDRESS";
		const GTOKEN_ID = REPLACE_WITH_THE_GTOKEN_ID

		const balance = balance = (await getAccountAssetBalance(
			GTOKEN_ID,
			ACCOUNT_ADDRESS
		))?.toString()
		
		console.log(
			`The GTOKEN balance for address ${ACCOUNT_ADDRESS} is: balance`,
		);
		return balance
	} catch (e) {
		console.error(e);
		process.exit(1)
	} finally {
		process.exit(0)
	}
};

main();

Last updated