How to calculate voting power with hf20 in js.

in #steem7 years 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

Sort:  

Thank you, this is so helpful!

This is just what I need, thanks :)

 7 years ago  Reveal Comment

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