How to calculate Steem Power using the API

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@lantto·
0.000 HBD
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
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,