Hivejs Tutorials--- How to cast a vote.

in GEMS4 years ago

Hello Everyone!

As we have discussed the Hivejs in our previous post. If you don't know what Hivejs is. Hivejs is the Official Javascript library. Using Hivejs we can develop some awesome tools. In the previous post we have installed the Node.js, VS code, and Hivejs. If you want to learn about Hivejs and want to get started from the beginning then, please read the previous Introduction post. In this post we will write a script that will cast vote to the specific post. So let's get started.
image.png

To vote a post using Hivejs this piece of code will help you

var hive= require('@hiveio/hive-js');

var username= "YOUR-USERNAME";
var permlink= 'LINK_OF-THE-POST';
var author=  'THE-WRITER-NAME;
var password= 'POSTING-KEY'



hive.broadcast.vote(password, username, author, permlink, weight, function(err, result) {
  console.log(err, result);
});

Copy-paste the code to your Visual studio code and change the values of the variable.

  • The first argument is password(your private posting key). Never reveal your private posting key keep it somewhere safe.

  • The Second one is username. Assign your username to the username variable. You can also put these values directly but for the tutorial, I used variables. it is easier to explain the code.

  • Third one is the author. Assign the name of the author to the variable or put it directly.

  • The next is permalink. Now here you do not need to put the whole link.

image.png

Just like this the link to my previous post. so If I want to vote I will need this much.

image.png

  • The last one is the weight. If you want to upvote a post 100% then put 10000 there. If 50% then half of it is 5000 and so on.

Now I will run the code and upvote a post using the above script.

image.png

So the code in the above picture will vote my previous post with 100% voting power.
If your the post is voted then it should return something like this.

null {
  id: '058f377088f0775d4a3bd454af220d4aa3426d00',
  block_num: 42967228,
  trx_num: 9,
  expired: false,
  ref_block_num: 41127,
  ref_block_prefix: 2289917910,
  expiration: '2020-04-29T15:28:09',
  operations: [ [ 'vote', [Object] ] ],
  extensions: [],
  signatures: [
    '202881b1b108cab5e95d33005fdfc545c0a1beb1a5904a4e0aeb85bd5ef58aa089292f37f99eefb9fabe5ab4cd5c6c7e2da84dbe0c0e1f7da11abbc609dc048086'
  ]
}

Now let's vote a post which is newly created. To check our code again.

image.png

Ok Let's vote this post. It has no vote yet and also a good post.

image.png

It is the same code I just changed the author name and the post link.

image.png

And our vote is cast.

image.png
As you can see in the above pic.

This much is enough. In the next post, we will learn something new above Hivejs. If you have any question above this topic please ask in the comments.

ntitled-1.png

Sort:  

This is great. One thing you could improve upon is maybe using the ES6 syntax.

I will give it a try for sure.

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!