create account

[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you] by pilcrow

View this thread on: hive.blogpeakd.comecency.com
· @pilcrow · (edited)
$46.55
[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]
A few posts ago we [calculated a user's reputation](https://steemit.com/steemdev/@pilcrow/calculating-a-user-s-steemit-reputation-score-in-javascript-bonus-content-an-angular-pipe-that-does-it-for-you). Next up is calculating your Steem Power. 

<div class="pull-right">https://steemitimages.com/0x0/https://steemitimages.com/DQmYrgnfgXgnYraTmDMLwnJh8YPMJPbko3iwsKuhoFdM7FE/image.png</div>

For this we're going to fetch the user's info again, but also something called Steem's "dynamic global properties",  which includes information about the total vesting shares and the total STEEM fund currently available in the blockchain.

### Getting the user info
Just now I realized that I never properly explained how to fetch a user's info using SteemJS. It's pretty simple:

```
getUser(username) {
    return steem.api.getAccounts([username]);
}
```
<br>
The `getAccounts()` method expects an array so you could theoretically fetch multiple users at once. We only need one right now, so we just wrap the one username in an array (`[name]`). We return the promise, which will resolve when the API call is done.

The important part that we need is the user's `vesting_shares` property:

![](https://steemitimages.com/DQmPip7g6J4bDLZMCUAEpXJaEDova8BPKFXyFSekaTDgYxZ/image.png)
<sub>My vesting shares at the moment of writing this post</sub>

### Vesting shares and Steem Power
For more information about what vesting shares mean see [these](https://steemit.com/steemit/@driv3n/want-to-become-a-steem-whale-steem-power-and-vests-only-tell-part-of-the-story) [three](https://steemit.com/steem/@dantheman/how-to-calculate-the-market-capitalization-of-steem) [posts](https://steemit.com/steem-help/@hisnameisolllie/what-are-vests). 

What's important for now is that your vesting shares are a fraction of the total amount of vesting shares on the Steem blockchain. This means that you are invested in Steem by owning a percentage of the total STEEM it has in its fund. 

In other words, your value in STEEM is the fraction of the total VESTS you own multiplied by the total STEEM in the fund.

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

### Getting the total vesting shares and fund
To know what the total vesting shares and total fund are, you need to call another API endpoint called `getDynamicGlobalProperties()`. 

The result that this method returns is an object containing (among others) the following properties:

```
{
    ...
    total_vesting_fund_steem: "183124826.141 STEEM"
    total_vesting_shares: "377884186141.130073 VESTS"
    ...
}
```

### Calculating the user's Steem Power
Now that we know how to fetch both the user and the dynamic global properties, we have all the information we need. As soon as both calls are resolved (we wait for that using `Promise.all()` we can calculate the user's Steem Power.

As you might have noticed all the properties we need are strings, ending with the word "VESTS" or "STEEM". To calculate we do need to convert those to numbers first.

```
function getSteemPower(username) {
    return Promise.all([
        steem.api.getAccounts([username]),
        steem.api.getDynamicGlobalProperties()
    ]).then(([user, globals]) => {
        const totalSteem = Number(globals.total_vesting_fund_steem.split(' ')[0]);
        const totalVests = Number(globals.total_vesting_shares.split(' ')[0]);
        const userVests = Number(user[0].vesting_shares.split(' ')[0]);

        return totalSteem * (userVests / totalVests);
    });
}
```
<br>
And there you have it! Don't forget that this method returns a promise too, so to use the value you'll have to catch it in another `.then()`.

A working example of this code can be found here: https://jsfiddle.net/u9xzzbqd/

---

## Bonus content: an Angular pipe
Just like when we calculated the user's reputation, calculating the Steem Power is a great candidate for a pipe if you're using Angular. That way we can just put this in our template:

`{{uer.vesting_shares | steemPower}}`

To do that, we need some preparation first. Calling an asynchronous method inside a pipe is not a good idea, so we'll have to fetch the blockchain information beforehand. One important reason for this is that the dynamic global properties are, well, dynamic. Fetching them once and using that value everywhere means that all calculations use the same values. It would be really weird if one part of your page showed 1000 SP, and another part showed 1001 SP because when the second value was calculated it already used a newer (higher) value of the total STEEM fund.

To solve this I created a steemService that fetches the dynamicGlobalProperties once when the application is loaded, and stores that information in the property `steemService.globals`. That way I can access them from anywhere without having to do additional calls. With that information, I can now give you the Angular pipe I used:

**steem-power.pipe.ts**
```
import {Pipe, PipeTransform} from '@angular/core';
import {SteemService} from './steem.service';

@Pipe({
    name: 'steemPower'
})
export class SteemPowerPipe implements PipeTransform {
    private totalSteem;
    private totalShares;

    constructor(private steemService: SteemService) {
        steemService.globals$.subscribe(globals => {
            this.totalSteem = this.toNumber(globals.total_vesting_fund_steem);
            this.totalShares = this.toNumber(globals.total_vesting_shares);
        });
    }

    transform(value: string): string {
        const steemPower = this.totalSteem * (this.toNumber(value) / this.totalShares);

        return steemPower.toLocaleString() + ' SP';
    }

    private toNumber(string) {
        return Number(string.split(' ')[0]);
    }
}
```

As you can see I separated the `toNumber` method to keep clutter out of the main functions, but in essence this does exactly what the jsFiddle example above does. The difference is that now we can use it in our templates a lot easier.

---
I hope this was useful, let me know if you have any comments and/or questions!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorpilcrow
permlinktutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you
categorysteemdev
json_metadata{"tags":["steemdev","steemit","javascript","angular","programming"],"image":["https://steemitimages.com/0x0/https://steemitimages.com/DQmYrgnfgXgnYraTmDMLwnJh8YPMJPbko3iwsKuhoFdM7FE/image.png","https://steemitimages.com/DQmPip7g6J4bDLZMCUAEpXJaEDova8BPKFXyFSekaTDgYxZ/image.png"],"links":["https://steemit.com/steemdev/@pilcrow/calculating-a-user-s-steemit-reputation-score-in-javascript-bonus-content-an-angular-pipe-that-does-it-for-you","https://steemit.com/steemit/@driv3n/want-to-become-a-steem-whale-steem-power-and-vests-only-tell-part-of-the-story","https://steemit.com/steem/@dantheman/how-to-calculate-the-market-capitalization-of-steem","https://steemit.com/steem-help/@hisnameisolllie/what-are-vests","https://jsfiddle.net/u9xzzbqd/"],"app":"steemit/0.1","format":"markdown"}
created2017-08-18 11:58:36
last_update2017-08-18 12:07:48
depth0
children11
last_payout2017-08-25 11:58:36
cashout_time1969-12-31 23:59:59
total_payout_value35.015 HBD
curator_payout_value11.531 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,046
author_reputation2,531,070,549,481
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id12,167,984
net_rshares13,302,870,486,709
author_curate_reward""
vote details (35)
@freedomglen ·
Goeie info dit! Dank je wel voor het delen! Upvoted, followed, resteemd :) Thanx! :)
properties (22)
authorfreedomglen
permlinkre-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20170820t075628462z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-08-20 07:56:24
last_update2017-08-20 07:56:24
depth1
children0
last_payout2017-08-27 07:56:24
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_length84
author_reputation3,966,089,349
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,327,641
net_rshares0
@kingyus ·
wow impressive keep it up
properties (22)
authorkingyus
permlinkre-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20170820t004123647z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-08-20 00:45:21
last_update2017-08-20 00:45:21
depth1
children0
last_payout2017-08-27 00:45:21
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_length25
author_reputation6,166,984,828,186
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,306,215
net_rshares0
@lightingmacsteem ·
This is one very useful info, thanks! If you are into good infos, I am blogging daily about offgrid lighting the non-solar, non-wind approach. Please visit my posts it may interest you. Upvoted anf followed you.
properties (22)
authorlightingmacsteem
permlinkre-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20170818t132659710z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-08-18 12:20:12
last_update2017-08-18 12:20:12
depth1
children0
last_payout2017-08-25 12:20:12
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_length211
author_reputation2,150,713,093,459
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,169,270
net_rshares0
@madlila ·
$0.10
The math is a little beyond me, but  the working example was exactly what I needed for it to make some sense.

Thanks.
👍  
properties (23)
authormadlila
permlinkre-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20170824t161304245z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-08-24 16:12:57
last_update2017-08-24 16:12:57
depth1
children1
last_payout2017-08-31 16:12:57
cashout_time1969-12-31 23:59:59
total_payout_value0.072 HBD
curator_payout_value0.023 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length118
author_reputation189,021,660,577
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,753,929
net_rshares23,965,329,250
author_curate_reward""
vote details (1)
@pilcrow ·
You're welcome, glad I could help you make some sense of it!
properties (22)
authorpilcrow
permlinkre-madlila-2017824t194242669z
categorysteemdev
json_metadata{"tags":"steemdev","app":"esteem/1.4.6","format":"markdown+html","community":"esteem"}
created2017-08-24 17:42:45
last_update2017-08-24 17:42:45
depth2
children0
last_payout2017-08-31 17:42:45
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_length60
author_reputation2,531,070,549,481
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries
0.
accountesteemapp
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,762,002
net_rshares0
@rusinho027 ·
$0.09
Very good your help and your tricks, thanks for sharing!
👍  
properties (23)
authorrusinho027
permlinkre-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20170824t145506452z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2017-08-24 14:56:18
last_update2017-08-24 14:56:18
depth1
children1
last_payout2017-08-31 14:56:18
cashout_time1969-12-31 23:59:59
total_payout_value0.070 HBD
curator_payout_value0.023 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation37,286,301,645
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,746,643
net_rshares23,689,865,695
author_curate_reward""
vote details (1)
@pilcrow ·
You're quite welcome!
properties (22)
authorpilcrow
permlinkre-rusinho027-2017824t194441143z
categorysteemdev
json_metadata{"tags":"steemdev","app":"esteem/1.4.6","format":"markdown+html","community":"esteem"}
created2017-08-24 17:44:42
last_update2017-08-24 17:44:42
depth2
children0
last_payout2017-08-31 17:44:42
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_length21
author_reputation2,531,070,549,481
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries
0.
accountesteemapp
weight500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,762,181
net_rshares0
@sn0n ·
Is there an update for https://jsfiddle.net/u9xzzbqd/ ??  Seems to not be working..
👍  
properties (23)
authorsn0n
permlinkre-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20180116t104310237z
categorysteemdev
json_metadata{"tags":["steemdev"],"links":["https://jsfiddle.net/u9xzzbqd/"],"app":"steemit/0.1"}
created2018-01-16 10:43:18
last_update2018-01-16 10:43:18
depth1
children2
last_payout2018-01-23 10:43: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_length83
author_reputation25,801,997,535,483
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id29,911,721
net_rshares0
author_curate_reward""
vote details (1)
@gokulnk ·
$0.10
Looks like the signatures of these functions changed some time. The parameters for ``Promise.all`` should promises themselves. So I tweaked the code a little. I am not sure if this is the best way as I had create a new promise for each API call. 

You can check it out here https://jsfiddle.net/u0wwf829/2/
👍  
properties (23)
authorgokulnk
permlinkre-sn0n-re-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20180131t212954835z
categorysteemdev
json_metadata{"tags":["steemdev"],"links":["https://jsfiddle.net/u0wwf829/2/"],"app":"steemit/0.1"}
created2018-01-31 21:29:54
last_update2018-01-31 21:29:54
depth2
children1
last_payout2018-02-07 21:29:54
cashout_time1969-12-31 23:59:59
total_payout_value0.088 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length306
author_reputation17,871,219,215,380
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id33,954,720
net_rshares16,232,590,730
author_curate_reward""
vote details (1)
@sn0n ·
Thanks for the update. Checking it out now.
properties (22)
authorsn0n
permlinkre-gokulnk-re-sn0n-re-pilcrow-tutorial-calculating-your-steem-power-using-steemjs-bonus-content-an-angular-pipe-that-does-it-for-you-20180131t214500189z
categorysteemdev
json_metadata{"tags":["steemdev"],"app":"steemit/0.1"}
created2018-01-31 21:45:00
last_update2018-01-31 21:45:00
depth3
children0
last_payout2018-02-07 21:45:00
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_length43
author_reputation25,801,997,535,483
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id33,957,518
net_rshares0
@steemitboard ·
Congratulations @pilcrow! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

[![](https://steemitimages.com/70x80/http://steemitboard.com/notifications/payout.png)](http://steemitboard.com/@pilcrow) Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click [here](https://steemit.com/@steemitboard)

If you no longer want to receive notifications, reply to this comment with the word `STOP`

> By upvoting this notification, you can help all Steemit users. Learn how [here](https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png)!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-pilcrow-20170818t143453000z
categorysteemdev
json_metadata{"image":["https://steemitboard.com/img/notifications.png"]}
created2017-08-18 14:34:51
last_update2017-08-18 14:34:51
depth1
children0
last_payout2017-08-25 14:34:51
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_length689
author_reputation38,975,615,169,260
root_title"[Tutorial] Calculating your Steem Power using SteemJS [bonus content: an Angular Pipe that does it for you]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id12,179,495
net_rshares0