create account

How to calculate Steem Power using the API by lantto

View this thread on: hive.blogpeakd.comecency.com
· @lantto · (edited)
$1.45
How to calculate Steem Power using the API
https://www.steemimg.com/images/2016/08/11/steemit_code2968ea.png
<hr>

I've seen a lot of  discussions on how Steem Power is actually calculated. In this post I will show a concrete example using JavaScript and the [steem.ws](https://steem.ws) WebSocket API.

## Formula ##

The forumla for calculating Steem Power is as follows:

`total_vesting_fund_steem * (user's vesting_shares / total_vesting_shares)`

I won't go into details what these are but [here](https://steemit.com/steem/@dantheman/how-to-calculate-the-market-capitalization-of-steem) is a good article outlining the concepts of vests.

## Code ##

This script retrieves the relevant values in the formula and calculates Steem Power for @lantto:

```
var totalVestingShares, totalVestingFundSteem;

var socket = new WebSocket('wss://node.steem.ws'),
    account = 'lantto';

socket.onopen = function(event) {
    socket.send(JSON.stringify({id: 1, method: 'get_dynamic_global_properties', 'params': []}));
}

socket.onmessage = function(event) {
    var steemPower, vestingShares;

    var data = JSON.parse(event.data);

    if (data.id === 1) {
        totalVestingShares = data.result.total_vesting_shares.split(' ')[0];
        totalVestingFundSteem = data.result.total_vesting_fund_steem.split(' ')[0];

        socket.send(JSON.stringify({id: 2, method: 'get_accounts', params: [[account]]}));
    }

    if (data.id === 2) {
        vestingShares = data.result[0].vesting_shares.split(' ')[0];
        steemPower = totalVestingFundSteem * (vestingShares / totalVestingShares);

        console.log(steemPower);
    }
}
```
Go ahead and replace `lantto` with your own username and paste the code into the Chrome DevTools console. It should output your current Steem Power.

This is how Steem Power is calculated everywhere. We can verify this by looking at the [source code](https://github.com/steemit/steemit.com) of [steemit.com](https://steemit.com) where they're using a [utility function very similar to the code above](https://github.com/steemit/steemit.com/blob/f5fa235d6a2edc8f9f7d1213f99457e28c321e33/app/utils/StateFunctions.js#L18-L24).

Let me know if you have any questions!

## Credits ##
[steem.ws](https://steem.ws/) by @xeroc and @jesta
[How to calculate the Market Capitalization of Steem](https://steemit.com/steem/@dantheman/how-to-calculate-the-market-capitalization-of-steem) by @dantheman
[Developers Guide to Steem's Blockchain](https://steemit.com/steem/@furion/developers-guide-to-steem-s-blockchain) by @furion
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorlantto
permlinkhow-to-calculate-steem-power-using-the-api
categorysteem
json_metadata{"tags":["steem","developers","dev","javascript","programming"],"users":["lantto","xeroc","jesta","dantheman","furion"],"links":["https://steem.ws"]}
created2016-08-11 13:08:27
last_update2016-08-11 13:26:18
depth0
children2
last_payout2016-09-11 03:12:18
cashout_time1969-12-31 23:59:59
total_payout_value1.116 HBD
curator_payout_value0.334 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,507
author_reputation1,806,855,037,770
root_title"How to calculate Steem Power using the API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id744,899
net_rshares1,621,198,165,990
author_curate_reward""
vote details (32)
@johanherman ·
Nice. Always convenient to check concrete examples now and then. Cheers!
👍  
properties (23)
authorjohanherman
permlinkre-lantto-how-to-calculate-steem-power-using-the-api-20160811t133923261z
categorysteem
json_metadata{"tags":["steem"]}
created2016-08-11 13:39:24
last_update2016-08-11 13:39:24
depth1
children0
last_payout2016-09-11 03:12:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length72
author_reputation24,003,660
root_title"How to calculate Steem Power using the API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id745,370
net_rshares1,478,729,978
author_curate_reward""
vote details (1)
@khunfarang ·
Awesome. it's working!
properties (22)
authorkhunfarang
permlinkre-lantto-how-to-calculate-steem-power-using-the-api-20170618t114157963z
categorysteem
json_metadata{"tags":["steem"],"app":"steemit/0.1"}
created2017-06-18 11:41:57
last_update2017-06-18 11:41:57
depth1
children0
last_payout2017-06-25 11:41:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation23,724,395,578
root_title"How to calculate Steem Power using the API"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id5,169,259
net_rshares0