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

in dev •  7 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!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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