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

in #dev8 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();

Thank you for reading. I'm a witness. You could find more on my site https://steem.fans

I would really appreciate your witness vote! You can vote by AuthSteem. Or open https://steemitwallet.com/~witnesses page, input ety001 to vote.

2.gif

Thank you!

Sort:  

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

Coin Marketplace

STEEM 0.25
TRX 0.25
JST 0.040
BTC 94833.25
ETH 3451.78
USDT 1.00
SBD 3.35