SignumJS
  • 👋Welcome to SignumJS
  • Getting Started
    • Installation
    • Modules
    • Usage
  • 👩‍💻Reference
    • API Reference
      • Core Module
      • Util Module
      • Crypto Module
      • Http Module
      • Contracts Module
      • Monitor Module
  • 🧾Recipes
    • Basics
      • Getting Accounts Balance
      • List Transactions
      • Show attached messages
      • Send Signa
    • Advanced
Powered by GitBook
On this page
  1. Recipes
  2. Basics

List Transactions

This example shows how to list transactions for an account

PreviousGetting Accounts BalanceNextShow attached messages

Last updated 3 years ago

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

Following functions from SignumJS are used in this example

  • function to create the api instance

  • function to fetch the transactions from the blockchain

  • Value Object to parse and convert the given address

  • Value Object to convert between Planck and Signa

  • Value Object to convert between blockchain timestamp and real date

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)
    }
}
import {Address} from "@signumjs/core";
import {Amount, ChainTime} = from "@signumjs/util";

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

async function listTransactions(account: string): Promise<void> {
    // 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: false,
                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)
    }
}
// you need to have imported in your index.html:
/*
<script src='https://cdn.jsdelivr.net/npm/@signumjs/core/dist/signumjs.min.js'></script>
<script src='https://cdn.jsdelivr.net/npm/@signumjs/util/dist/signumjs.util.min.js'></script>
*/

// Create the Api instance to connect to a blockchain node
const api = sig$.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 = sig$.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: sig$util.ChainTime.fromChainTimestamp(t.timestamp).getDate(),
                account: hasReceived ? t.senderRS : t.recipientRS || 'SELF',
                value: `${hasReceived ? '+' : '-'} ${sig$util.Amount.fromPlanck(t.amountNQT).toString()}`, // convert from Planck value to Signa
                fee: sig$util.Amount.fromPlanck(t.feeNQT).toString()
            }
        });

        console.table(mappedTransactions, ['date', 'value', 'fee', 'account'])
    } catch (e) {
        console.error(e)
    }
}
🧾
Modules
here
composeApi
api.account.getAccountTransactions
Address
Amount
ChainTime