SteemJs for Dummies #2 Calculate the user’s Steem Power

image.png

Yesterday, we saw how to use SteemJs library to calculate any user’s current Voting Power. As a reminder, you can take a look at the entire library in its Github repository and take a look at its obscure documentation in here.
Today, we will tackle another problem that will make use three different SteemJs functions:

  • getAccount: You should already be familiar with that one, we used it yesterday to find the time of the last upvote and the remaining Voting Power at that time.
  • getDynamicGlobalProperties : New function that we will explore today.`
  • steem.formatter.vestToSteem : Idem
    But wait, we’re just looking for the Steem Power, surely I can find it in the getAccount response, why would I need other functions here?
    Well, that’s just not how it works. To make it simple, Steem Power is a formatted version of what is called Vesting Shares. Thus, in order to convert Vesting Shares to Steem Power, we need to get some information relative to the whole blockchain and accessible through getDynamicGlobalProperties. Then, we can pass the Vesting Shares and the blockchain necessary information to the formatter in order to obtain the Steem Power.
    Ready? Go!

Obtaining the Vesting Shares

As in our first tutorial, let’s start with the getAccounts function:

steem.api.getAccounts(["stoodkev"], function(err, response){
    console.log(response);
});

Here is a snapshot of the obtained response with the information we are looking for:

image.png

Then, we need to find our Vesting Shares, here you go:

image.png

But wait, there is more! I guess you are familiar with delegations, these Vesting Shares that increase your Steem Power without being yours. That’s what happen when someone lend you Steem Power.
So let’s find the two re are interested in: the received and delegated Vesting Shares:

image.png
image.png

As I showed in the first tutorial, you can get quickly the path to the element you want in the Json object by a right click on that one and Copy Property Path.
Try to get the the three variables by yourself.
Done? So let’s take a look at our code now:

steem.api.getAccounts(["stoodkev"], function(err, response){
    var vesting_shares= response["0"]. vesting_shares;
        var delegated_vesting_shares= response["0"].delegated_vesting_shares;
    var received_vesting_shares=response["0"].received_vesting_shares;
console.log(vesting_shares, delegated_vesting_shares, received_vesting_shares);
});

Dynamic Properties



As mentioned in the introduction, we will need some of the dynamic properties of the blockchain in order to convert our Vesting Shares into Steem Power. Let’s run the following code:

steem.api.getDynamicGlobalProperties(function(err, result) {
    console.log(result);
});

And take a look at the result :

image.png

You will find lots of useful information about the blockchain here, but in our case we are only interested in the total_vesting_shares and total_vesting_fund_steem variables.
So let’s get them and include them in our code:

steem.api.getDynamicGlobalProperties(function(err, result) {
    total_vesting_shares=result.total_vesting_shares;
    total_vesting_fund=result.total_vesting_fund_steem;
    console.log(result);
});

Formatting the result



Looking at SteemJs documentation, here is the definition of the formatter:

var steemPower = steem.formatter.vestToSteem(vestingShares, totalVestingShares, totalVestingFundSteem);
console.log(steemPower);

We also want to calculate the amount of Steem Power being delegated to us. For this, we substract the amount we delegated, to the amount we received.
At the end, you should obtain a result similar to mine:

var vesting_shares, delegated_vesting_shares, received_vesting_shares, total_vesting_shares , total_vesting_fund_steem=null;
steem.api.getAccounts(["stoodkev"], function(err, response){   
    vesting_shares= response["0"]. vesting_shares;
        delegated_vesting_shares= response["0"].delegated_vesting_shares;
    received_vesting_shares=response["0"].received_vesting_shares;
});
steem.api.getDynamicGlobalProperties(function(err, result) {
    total_vesting_shares=result.total_vesting_shares;
    total_vesting_fund=result.total_vesting_fund_steem;  
});
// Handle Promises, when you’re sure the two functions were completed simply do:
var steem_power= steem.formatter.vestToSteem(vesting_shares, total_vesting_shares, total_vesting_fund);
var delegated_steem_power= steem.formatter.vestToSteem((received_vesting_shares.split(' ')[0]-delegated_vesting_shares.split(' ')[0])+' VESTS', total_vesting_shares, total_vesting_fund);
console.log(steem_power,delegated_steem_power);

You will get this result:

image.png

Use the toFixed()method to reduce the number of decimals.

Hope this helps!

@stoodkev



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
13 Comments
Ecency