Paginated Transactions
1 min
The Node HTTP API limits the amount of returned transaction to at maximum 500. This is to avoid unnecessary resource usage. If you want to fetch more (or also less) transactions you can paginate the requests.
async function listTransactions({account, page, pageSize}) {
// 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()
const firstIndex = page * pageSize
// Now, we call the getAccountTransactions method,
// we use the indexing feature to fetch pages of transactions
const {transactions} = await ledger.account.getAccountTransactions(
{
firstIndex,
lastIndex: firstIndex + pageSize,
includeIndirect: true, // multi out payments are included
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) {
// If the API returns an exception,
// the return error object is of type HttpError
console.error(e);
}
}
// // Our entry point has to be async, as our subsequent calls are.
// // This pattern keeps your app running until all async calls are executed
(async () => {
await listTransactions({
account: 'TS-QAJA-QW5Y-SWVP-4RVP4',
page: 2,
pageSize: 50
});
})();