Steem-Js for dummies #1 : How to calculate the current Voting Power

image.png

When I first decided to start programming my SteemPlus extension, I realized that I needed an efficient way to communicate with the Steem blockchain. Since Chrome extensions are basically made of Javascript code, I started to look into Steem-Js.

You can find their Github repository here and the documentation in here.

image.png

As you can see, the documentation basically lists the different functions without much detail regarding the parameters that need to be passed to the functions or what they return. I will thus start posting on a regular basis to explain to new SteemDevs how to use Steem-Js to develop their applications.

On today's menu : calculating the current Voting Power

Very often, you will need to know what is your current Voting Power before performing an action, especially if you are trying to automatize a routine. This is not as straightforward as you might think, so buckle up and start learning Steemjs.

steem.api.getAccounts

Here, we will start with our first Steem-Js function, getAccounts. Let's take a look at what the documentation says:

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

As you can see, it says nameS, so it's an Array. Let's try the following:

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

or, if you want to get more than one account:

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

Check on the console (Right click, Inspect element on Chrome), you will get an Array of the accounts you 've asked for:

image.png

Each account is a json element containing various information about as you can see here:

image.png

Much more information is provided and I invite you to take a moment to take a look at it, you might find some data that you will need later in your project!

Back to our example, in order to compute the current voting power, we need to get two things, the voting power at the moment of the last upvote, and the time elapsed since then. Take a moment and try to find these by yourself.

Found it? The two information you need are:

  • last_vote_time
    image.png
    It gives you a timestamp of the last upvote.

  • voting_power
    image.png
    It gives you a two decimals percentage of your Voting Power at the time of the last upvote.

Thus, it means that I've last voted at 17:01 and was at 92.67% Voting Power at that moment.

Getting the variable you want

Json elements often have arrays inside an object themselves inside another array, etc. It's easy to get lost! If you're on Chrome or Opera (but I guess other browsers have similar features), you can simply ask to copy the path to clipboard.
image.png

This returns ["0"].voting_power

Calculating the Voting Power

First, we need to calculate how many seconds ago the last vote was casted.

  var secondsago = (new Date - new Date(response[0].last_vote_time + "Z")) / 1000;

Then, can add the regenerated Voting Power to the VP at last upvote

    var vpow = response[0].voting_power + (10000 * secondsago / 432000);

Finally, let's show it as real percentage with two decimals, and let's make sure it doesn't go over 100%

    vpow = Math.min(vpow / 100, 100).toFixed(2);

Done! You can now use the Voting Power, as you see fit, for now let's display it on the console:

steem.api.getAccounts(["stoodkev"], function(err, response){
  var secondsago = (new Date - new Date(response[0].last_vote_time + "Z")) / 1000;
   var vpow = response[0].voting_power + (10000 * secondsago / 432000);
    vpow = Math.min(vpow / 100, 100).toFixed(2);
console.log(vpow);
});

Result :
image.png

I hope you liked this tutorial, in next episode, I will show you how to calculate a user Steem Power from its Vesting Shares.

Hope this helps.

@stoodkev



Open Source Contribution posted via Utopian.io

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