How to iterate over account history items

in #javascript7 years ago (edited)

Using steem.js, it is easy to iterate over all the history items of an account. However, if you have not done this before, it may be a little tricky, because there is not enough documentation on the web (or maybe I didn’t find it).

Anyway, steem.api.getAccountHistory returns a promise that resolves to a batch of history items. Each item has an ID number (that starts with 1 for the first item in the history). The batch returned is sorted ascendingly, and contains limit + 1 items, starting at from. If you want to start at the most recent item, you can give from a value of -1. Otherwise, from must be larger than or equal to limit.

Here is an example code for getting history items:

    for (let from = -1, limit = 100, going = true; !!from && going; ) {
        let items = await steem.api.getAccountHistory(username, from, Math.min(limit, from));
        from = items[0][0] - 1;
        items.reverse();
        loop:
        for (let item of items) {
            if (someTest(item)) {
                going = false;
                break loop;
            }
            // do something with item
        }
    }

The loop ends if we reach the first item (hence the use of !!from) or if someTest(item) returns true.

Sort:  

It's totally a new thing for me. Thanks for sharing this important information.

Loading...

I didn't know about it. But i am interested to know about it.

nice ♥

your are really good at programming, i also like progaramming , l like c language

Good writing.Its help us .

Sir! Is your account hacked? 3 consecutive posts other than idioms? Never seen it before~!

I am going to post more content like this. I am currently doing a programming project and I will be posting some of the tips that I think may be useful.

I do have an interest in computer programming but it takes too much time to get used to it. But after all its worth to give time to learn it.

1117.jpg