Getting Accounts Balance

In this simple example we just get an accounts balance and print it to the console.

In the basic examples we still show how to use SignumJS depending the way you are using SignumJS, i.e. imported via NodeJS package manager or as minified bundle. In case you haven't read about the module system, take a quick look there:

A fully working example is available here

Following functions from SignumJS are used in this example

const {Address, composeApi} = require("@signumjs/core")
const {Amount} = require("@signumjs/util")

// Create the Api instance to connect to a blockchain node
const api = composeApi({
    nodeHost: "https://europe.signum.network"
});

async function getBalance(account) {
    // All API calls are asynchronous
    // The recommended pattern is using async/await
    // This makes exception handling easy using try/catch
    try {
        // We just create an address instance of incoming account string, which
        // might be the numeric id or an address like 'S-ABCD....'
        // It throws an error, if the input is not a valid address or account id
        const address = Address.create(account)
    
        // Now, we call the getAccountBalance method
        const {balanceNQT} = await api.account.getAccountBalance(address.getNumericId());

        // all amounts coming from the blockchain are expressed in Planck 
        // 1 SIGNA = 100000000 Planck
        // so we need to convert them using the Amount Value Object class
        const balance = Amount.fromPlanck(balanceNQT)
        
        // Print it to the console
        console.info(`Balance of ${address.getReedSolomonAddress()} is:`, balance.toString())
    } catch (e) {
        console.error(e.message)
    }
}

Last updated