Javascript (NodeJS) Function to Check if a Witness Has been Voted by or Proxied By on Steem Blockchain


On Steem Blockchain, we can either proxy a witness vote or cast a witness vote (out of 30 votes) to a steem witness. We can use the steemjs library to get account information which includes the current proxy and the list of witness votes (up to 30):

1
2
3
4
5
6
7
8
9
10
function is_voted_by(witness, id) {
    return new Promise((resolve, reject) => {
        steem.api.getAccounts([id], function(err, response) {
            if (err) reject(err);
            if (typeof response == "undefined") reject("undefined");
            const data = response[0];
            resolve((data.proxy === witness) || data.witness_votes.includes(witness)); 
        });          
    });
}
function is_voted_by(witness, id) {
    return new Promise((resolve, reject) => {
        steem.api.getAccounts([id], function(err, response) {
            if (err) reject(err);
            if (typeof response == "undefined") reject("undefined");
            const data = response[0];
            resolve((data.proxy === witness) || data.witness_votes.includes(witness)); 
        });          
    });
}

Testing to prove that it works:

1
2
3
4
5
6
(async function() {
    const check_witness_vote = await is_voted_by("justyy", "ety001");
    console.log(check_witness_vote); // true as @ety001 votes @justyy
    const check_proxy_vote = await is_voted_by("justyy", "mrspointm");
    console.log(check_proxy_vote ); // true as @mrspointm sets proxy to @justyy
})();
(async function() {
    const check_witness_vote = await is_voted_by("justyy", "ety001");
    console.log(check_witness_vote); // true as @ety001 votes @justyy
    const check_proxy_vote = await is_voted_by("justyy", "mrspointm");
    console.log(check_proxy_vote ); // true as @mrspointm sets proxy to @justyy
})();

The is_voted_by returns a promise so that we can await on it.

The above NodeJs example runs in SteemJs Editor

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
415 words
Last Post: Teaching Kids Programming - Count Number of Pairs With Absolute Difference K
Next Post: Teaching Kids Programming - Final Value of Variable After Performing Operations (via Reduce Function)

The Permanent URL is: Javascript (NodeJS) Function to Check if a Witness Has been Voted by or Proxied By on Steem Blockchain

Leave a Reply