How to calculate voting power with hf20 in js.

in steem •  2 months ago

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 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

Happy hf20 ! :)

Of and if you have a spot left please consider voting for SteemPress as witness

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you, this is so helpful!

This is just what I need, thanks :)

the variable in the description, account, does not exist.

callback doesn't do anything

thx for the contribution.

·

Good catch ! Copy pasted from my template a bit too fast. thanks :)