List Transactions

This example shows how to list transactions for an account

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} = require("@signumjs/core");
const {Amount, ChainTime} = require("@signumjs/util");

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

async function listTransactions(account) {
    // All API calls are asynchronous
    // The recommended pattern is using async/await
    // This makes exception handling easy using try/catch
    try {      
        // we check if incoming account is either a Signum Address, or Numeric Id
        // eventually, we convert to Numeric Id
        const accountId = Address.create(account).getNumericId()

        // Now, we call the getAccountTransactions method,
        // but we want only the 100 most recent transactions, 
        // *not* including multi-out
        const {transactions} = await api.account.getAccountTransactions(
            {
                firstIndex: 0,
                lastIndex: 100,
                includeIndirect: true,
                accountId,
            }
        );

        // now we map the fields we want to print as a table to console then
        // NOTE: for sake of simplicity we do not filter out the multi payments
        const mappedTransactions = transactions.map(t => {
            const hasReceived = t.sender !== accountId
            return {
                date: ChainTime.fromChainTimestamp(t.timestamp).getDate(),
                account: hasReceived ? t.senderRS : t.recipientRS || 'SELF',
                value: `${hasReceived ? '+' : '-'} ${Amount.fromPlanck(t.amountNQT).toString()}`, // convert from Planck value to Signa
                fee: Amount.fromPlanck(t.feeNQT).toString()
            }
        });

        console.table(mappedTransactions, ['date', 'value', 'fee', 'account'])
    } catch (e) {
        console.error(e)
    }
}

Last updated