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
composeApi function to create the api instance
api.account.getAccountTransactions function to fetch the transactions from the blockchain
Address Value Object to parse and convert the given address
Amount Value Object to convert between Planck and Signa
ChainTime 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)
}
}
Last updated