[Demo Code] Pagination of the data from the `condenser_api.get_account_history` API in ascending or descending order

in #dev3 months ago (edited)
import steem from 'steem';

async function getAccountHistory(username, from, limit) {
  return steem.api.getAccountHistoryAsync(username, from, limit)
}

async function desc() {
  const username = 'ety001';
  const limit = 3;
  let from = -1, page = 5;
  let result, tmp;

  for (let p = 1; p <= page; p++) {
    console.log('from => ', from, 'limit => ', limit)
    result = await getAccountHistory(username, from, limit);
    tmp = result.reverse()
    for(let i = 0; i < limit; i++) {
      console.log("id:", tmp[i][0]);
      console.log("block_num:", tmp[i][1]['block']);
      console.log("trx_in_block:", tmp[i][1]['trx_in_block']);
      console.log("timestamp:", tmp[i][1]['timestamp']);
      console.log("op:", tmp[i][1]['op']);
      console.log("------------------"); 
    }
    console.log(`--------- page${p} ---------`); 
    from = tmp[limit][0];
  }
}

async function asc() {
  const username = 'ety001';
  const limit = 3;
  let from = limit, page = 5;
  let result, tmp;

  for (let p = 1; p <= page; p++) {
    console.log('from => ', from, 'limit => ', limit)
    result = await getAccountHistory(username, from, limit);
    for(let i = 0; i < limit; i++) {
      console.log("id:", result[i][0]);
      console.log("block_num:", result[i][1]['block']);
      console.log("trx_in_block:", result[i][1]['trx_in_block']);
      console.log("timestamp:", result[i][1]['timestamp']);
      console.log("op:", result[i][1]['op']);
      console.log("------------------"); 
    }
    console.log(`--------- page${p} ---------`); 
    from = result[limit][0] + limit;
  }
}

async function main() {
  console.log("======> desc() <======");
  await desc();
  console.log("======> asc() <======");
  await asc();
}

main();
Sort:  

This post has been featured in the latest edition of Steem News...

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 63793.25
ETH 3410.80
USDT 1.00
SBD 2.59