create account

How to calculate voting power with hf20 in js. by howo

View this thread on: hive.blogpeakd.comecency.com
· @howo ·
$1.16
How to calculate voting power with hf20 in js.
Hello, 

Among all the chaos of the rc system, it's time to update your apps ! The new mana system means that the old way of calculating voting power is no longer correct. 

I've updated [SteemSnippets](https://steemit.com/programming/@howo/introducting-steemsnippets)  with a new code snippet for mana calculation.

Huge thanks to @asgarth who provided most of the code, I only improved it slightly following @roadscape remarks and bundled it into a function.

So anyways, here's the function : 

```
var steem = require('steem');
steem.api.setOptions({url: 'https://api.steemit.com'});
/**
 * Gets the voting power of an account. code mostly by @asgarth
 * @param {String} account - account of whom we want to check the steem power
 * @param {String} callback - callback which will have the voting power as parameter
 */
function getvotingpower(account_name, callback) {
    return new Promise(resolve => {
        steem.api.getAccounts([account_name], function (err, account) {

            account = account[0];

            const totalShares = parseFloat(account.vesting_shares) + parseFloat(account.received_vesting_shares) - parseFloat(account.delegated_vesting_shares) - parseFloat(account.vesting_withdraw_rate);

            const elapsed = Math.floor(Date.now() / 1000) - account.voting_manabar.last_update_time;
            const maxMana = totalShares * 1000000;
            // 432000 sec = 5 days
            let currentMana = parseFloat(account.voting_manabar.current_mana) + elapsed * maxMana / 432000;

            if (currentMana > maxMana) {
                currentMana = maxMana;
            }

            const currentManaPerc = currentMana * 100 / maxMana;

            return resolve(currentManaPerc);
        });
    });
}


async function example()
{
    const vp = await getvotingpower("howo");
    console.log(vp);
}

example();
```

Code is also on [github](https://github.com/drov0/steemsnippets/blob/master/steemjs/voting_power/voting_power.js)

Happy hf20 ! :) 

Of and if you have a spot left please consider voting for [SteemPress as witness](https://steemit.com/steempress/@steempress/steempress-vision-2018-roadmap-going-full-time-and-launching-witness-steempress)
👍  , , , , , , , , , , , , , ,
properties (23)
authorhowo
permlinkhow-to-calculate-voting-power-with-hf20-in-js
categorysteem
json_metadata{"tags":["steem","development","steemsnippets","hf20","vp"],"users":["asgarth","roadscape"],"links":["https://steemit.com/programming/@howo/introducting-steemsnippets","https://github.com/drov0/steemsnippets/blob/master/steemjs/voting_power/voting_power.js","https://steemit.com/steempress/@steempress/steempress-vision-2018-roadmap-going-full-time-and-launching-witness-steempress"],"app":"steemit/0.1","format":"markdown"}
created2018-09-27 20:04:24
last_update2018-09-27 20:04:24
depth0
children4
last_payout2018-10-04 20:04:24
cashout_time1969-12-31 23:59:59
total_payout_value0.871 HBD
curator_payout_value0.284 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,199
author_reputation515,737,853,740,869
root_title"How to calculate voting power with hf20 in js."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id72,269,969
net_rshares662,770,515,459
author_curate_reward""
vote details (15)
@kjnk ·
$0.16
Thank you, this is so helpful!
👍  ,
properties (23)
authorkjnk
permlinkre-howo-how-to-calculate-voting-power-with-hf20-in-js-20180930t100212255z
categorysteem
json_metadata{"tags":["steem"],"app":"steemit/0.1"}
created2018-09-30 10:02:12
last_update2018-09-30 10:02:12
depth1
children0
last_payout2018-10-07 10:02:12
cashout_time1969-12-31 23:59:59
total_payout_value0.122 HBD
curator_payout_value0.039 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation670,294,526,884
root_title"How to calculate voting power with hf20 in js."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id72,348,311
net_rshares80,319,732,429
author_curate_reward""
vote details (2)
@lanmower ·
$0.10
the variable in the description, account, does not exist.

callback doesn't do anything

thx for the contribution.
👍  
properties (23)
authorlanmower
permlinkre-howo-how-to-calculate-voting-power-with-hf20-in-js-20180929t134100805z
categorysteem
json_metadata{"tags":["steem"],"app":"steemit/0.1"}
created2018-09-29 13:41:03
last_update2018-09-29 13:41:03
depth1
children1
last_payout2018-10-06 13:41:03
cashout_time1969-12-31 23:59:59
total_payout_value0.075 HBD
curator_payout_value0.024 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length114
author_reputation-4,369,177,824,692
root_title"How to calculate voting power with hf20 in js."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id72,315,614
net_rshares50,772,478,835
author_curate_reward""
vote details (1)
@howo ·
Good catch ! Copy pasted from my template a bit too fast. thanks :)
properties (22)
authorhowo
permlinkre-lanmower-re-howo-how-to-calculate-voting-power-with-hf20-in-js-20180929t144612198z
categorysteem
json_metadata{"tags":["steem"],"app":"steemit/0.1"}
created2018-09-29 14:46:12
last_update2018-09-29 14:46:12
depth2
children0
last_payout2018-10-06 14:46: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_length67
author_reputation515,737,853,740,869
root_title"How to calculate voting power with hf20 in js."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id72,317,924
net_rshares0
@programmingvalue ·
$0.15
This is just what I need, thanks :)
👍  ,
properties (23)
authorprogrammingvalue
permlinkre-howo-how-to-calculate-voting-power-with-hf20-in-js-20180930t100248293z
categorysteem
json_metadata{"tags":["steem"],"app":"steemit/0.1"}
created2018-09-30 10:02:48
last_update2018-09-30 10:02:48
depth1
children0
last_payout2018-10-07 10:02:48
cashout_time1969-12-31 23:59:59
total_payout_value0.110 HBD
curator_payout_value0.035 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length35
author_reputation2,087,259,996,059
root_title"How to calculate voting power with hf20 in js."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id72,348,327
net_rshares72,886,498,779
author_curate_reward""
vote details (2)