Steem-Js for dummies #1 : How to calculate the current Voting PowersteemCreated with Sketch.

in #utopian-io7 years ago

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

Sort:  

Thank you!!

I've just started developing with Steem.js recently, and it's very exciting how much has been done to make building apps on top of the Steem blockchain relatively simple... But the documentation is so light weight it basically doesn't exist.

This community needs a lot more devs willing to put in the work to explain to newcomers, in everyday language, how Steem.js works and what you can do with it.

Utopian.io is going to pull developers in and give them everything they need. Check out the status today, compare it to next week and compare it to in two weeks and you will probably see that things are scaling quickly. This concepts is just too powerful to not create some dynamic.

thank you so much

This comment has received a 0.04 % upvote from @speedvoter thanks to: @kath1.

That was excellent.
Many thanks for taking time to post.
I'm looking forward to next installment

Thank you for the contribution. It has been approved.

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

Hey @stoodkev I am @utopian-io. I have just super-voted you at 13% Power!

Suggestions https://utopian.io/rules

  • Your contribution is less informative than others in this category.

Achievements

  • Thanks for explaining to me how it works! Beep beep!
  • You have less than 500 followers. Just gave you a gift ;)
  • You are generating more rewards than average for this category. Super!
  • Seems like you contribute quite often. AMAZING!
  • You have a good amount of votes on your contributions. Good job!
  • In total you have more votes than average for this category. Bravo!
  • You have just unlocked 6 achievements. Yeah!
    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
@klye Cast a 100% Vote
@tippy was at ~90.96% vote power at time of vote!
Vote power is Generated via RNG (Random Number Generator)

🤖 @Tippy - STEEM & SBD Text-to-Tip Service - by @KLYE 🤖
( click reply & type @tippy help for commands )

Stealing your open source code to implement in tippy. <3

It's here for that, enjoy =D

Thanks, very helpfull!

Interesting. I've always used this for calculations:

power = voting_power + ((now - lastVote) * 0.023148148);

I know that 10000 * secondsago / 432000 comes out to the same multiplier though. :)

This came in handy, I was getting some discrepancies between the blockchain value and steemd.com (calculated value). It seems the blockchain updates the voting_power lazily. So it's best to calculate.

Indeed! Glad to see this tutorial is still useful!

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 63578.99
ETH 2611.82
USDT 1.00
SBD 2.85