Javascript (NodeJS) Function to Get the Downvote Power of Account on Steem Blockchain

in #witness-category3 years ago

Accounts on Steem Blockchain have daily "Free" Downvote Power - which has 20% of the upvoting Mana. We can use the following Javascript/NodeJs function to calculate the downvote power.

We can access the "last" seen downvote mana and the timestamp in the getAccounts API - then we need to calculate the mana restored since the timestamp.

And by using async/await, you can await getting the downvote power in a function that is marked async.

function getDownvotePower(id) {
    return new Promise((resolve, reject) => {
        steem.api.getAccounts([id], function(err, response) {
            if (err) reject(err);
            const current_mana = response[0].voting_manabar.current_mana;
            const downvote_mana = response[0].downvote_manabar.current_mana;
            const downvote_last_update_time = response[0].downvote_manabar.last_update_time;
            const downvote_per = downvote_mana / (current_mana / (response[0].voting_power / 100) / 4);          
            const secondsago = (new Date - new Date(downvote_last_update_time * 1000)) / 1000;
            let vpow = downvote_per * 100 + (10000 * secondsago / 432000);
            resolve(Math.min(vpow / 100, 100).toFixed(2));
        });          
    });
}

// Sample Usage
(async function() {
    const val = await getDownvotePower('justyy');
    log(val);
})();

Run the code at SteemJS Editor.

Reposted to Blog

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^

NEW! Following my Trail (Upvote or/and Downvote)

Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com

My contributions

Delegation Service

  1. Voting Algorithm Updated to Favor those High Delegations!
  • Delegate 1000 to justyy: Link
  • Delegate 5000 to justyy: Link
  • Delegate 10000 to justyy: Link

Support me

If you like my work, please:

  1. Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
  2. Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
  3. Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
    Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses