How to check Voting Power And Resource Credit of Hive Account Through dhive

in GEMS4 years ago

Hello Everyone

As you know in the previous posts we have discussed Hivejs and a little bit of dhive. We tried to perform some tasks with Hivej in the previous posts like voting, posting getting account history, etc. In this post, we are going to write a code that will show the voting MANA and the RC( Resource Credit) of any account. This is very useful like when you are performing a task like an account claiming , voting through JavaScript and you want to check the RC/MANA level before perform the specific task so you can check the MANA percentage and then perform the task. So without wasting more time let's start the tutorial.
image.png

  • First of all, We need the latest version of Node.js. If you don't have the Nodejs installed please download it from the official site of Node.js.

image.png

  • After installing the node.js. Open the VS code.

image.png

  • Create a folder and open it in the VS code and create a js file.(index.js)

image.png

  • Now go to terminal and type npm init. This will ask for some info( optional you can just press enter, enter to skip with default)

image.png

  • Now we need to install the Hive Library ( dhive). In order to install dhive paste the command in the terminal. npm install @hivechain/dhive. This will download the necessary file from the Node Package Module.

image.png

  • To check the files are downloaded or not you can check the dependencies in the pakage.json and also the node_Modules folder.

image.png

  • Now we are good to write our script.

image.png

As you can see this is returning a promise. So we can use Async/Await to get our RC.

image.png

  • you can see three properties there. As we need the percentage so hiverc.percentage will return the percentage but still, we need to divide it by 100 in order to obtain an accurate and understandable percentage. So hiverc.percentage/100

image.png

Now to check the Voting Mana we just need a little change.

image.png

Script

const dhive = require('@hivechain/dhive');
var hClient = new dhive.Client('https://api.hive.blog');


let account='YOUR_ACCOUNT_NAME'
async function check(){
let hiverc =  await hClient.rc.getRCMana(account);
let hiveMana= await hClient.rc.getVPMana(account);
console.log(`Voting percentage of ${account} is ${hiveMana.percentage/100}% and RC = ${hiverc.percentage/100}%`);

}
check();


I think this much is enough for today. Now if you like the post please upvote and comment if you want to give me some advise
ntitled-1.png