TUTORIAL - Create a Steem Browser Extension - JavaScript - Basic - Part 10

in #utopian-io7 years ago (edited)

extension_muster.png

What Will I Learn?

This Tutorial Series is all about how to create a browser extension showing steem account data. We want to focus in this part how to get all latest posts of an account and calculate their pending payout value.

Screen Shot 2018-02-17 at 13.16.41.png

Requirements

  • Read my previous part

Difficulty

  • Basic

Tutorial Contents

I will split this tutorial in three components:

  1. Get the last 100 posts of an user
  2. Filter and receive only pending payout posts
  3. Calculate pending payout value and output it

Get the last 100 posts

To get the last posts of a user we will use the API function called steem.api.getDiscussionsByBlog({tag: username, limit: 100}, function(err, posts) {...}. It responds with the latest posts (amount = limit) for the given user (username).
Lets create a new function using this API call

function calculatePendingPayout() {
    steem.api.getDiscussionsByBlog({tag: username, limit: 100}, function(err, posts) {
        console.log(posts);
    });
}

And call the function before we call the votingpower / steempower functions.

        calculatePendingPayout();
        setSteemPower(userData);
        setVotingPower(userData);

Run the extension and check the output
Screen Shot 2018-02-17 at 12.39.05.png

All posts are ordered descending. We take as a limit 100 posts since it would be unusual an account posts more than 11 posts per day.

Filter and receive only pending payout posts

If you look inside the output and look inside the data of a post a property should take your interest - cashout_time. This values indicates wether a post already cashed out or will cashout in future. If a post is already cashed out the value / date will be the reseted to the default date value 1969-12-31T23:59:59. It's a big win for us, since we now a cashout will never happen in the year 1969 !

We use this information to create filter which returns all posts where the year is not 1969 !

steem.api.getDiscussionsByBlog({tag: username, limit: 100}, function(err, posts) {
        var pendingPosts = posts.filter(post => new Date(post.cashout_time).getFullYear() !== 1969);

        console.log(pendingPosts);
});

Try it out and you will only receive the posts which are currently pending!

Calculate pending payout value and output it

Now that we got all the posts we need, we want to calculate their total pending value and display it inside our extension. For that create a new output div inside our index.html

<div id="pending_payout_wrapper">
                <div style='display: inline-block;'>Pending Payout: </div>
                <div style='display: inline-block; float: right' id="pending_payout"></div>
</div>

To calculate the value we simply loop through the pendingPayouts array and take the sum of all pending_payout_value values.

function calculatePendingPayout() {
    steem.api.getDiscussionsByBlog({tag: username, limit: 100}, function(err, posts) {
        var pendingPosts = posts.filter(post => new Date(post.cashout_time).getFullYear() !== 1969);

        var totalPayout = 0;
        pendingPosts.forEach((post) => {
            totalPayout += Number(post.pending_payout_value.replace(' SBD', ''));
        });

        document.getElementById('pending_payout').innerHTML = totalPayout.toFixed(3) + " SBD";
    });
}

Please keep in mind that pending_payout_value is a String and we need to convert it to a valid Number!

Screen Shot 2018-02-17 at 13.16.41.png

Source Code

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @moonrise I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 62890.35
ETH 2544.51
USDT 1.00
SBD 2.94