How to write a JS Script to cast vote on a post. Learning JavaScript--2

in hive-148441 •  5 years ago 

Hello People!

Today we are going to learn How to write a JS Script to cost vote on a post.? It is very easy to cast a vote on steemit blockchain using javascript. To run our script we will need a compiler we can either use the online compiler I discussed in my previous post or we can use the installed compiler. In this tutorial, I am going to use visual studio code. Visual studio code is one of the best compiler/editors for almost every language. I may write a proper post on the features of the visual studio but for now, this much information is enough to continue. So without wasting time lets start writing our script.

*First of all, Install visual studio code and open it.

image.png

  • Click on the open folder and create a folder where you want to store your script and files.

image.png

  • Open the terminal

image.png

And typenpm install steem --save. If you are getting any errors then download the latest version of nodejs and then try again using nodejs.

image.png

  • Ignore the warnings for now.

image.png

  • Now create a new file by click on this sign

image.png

image.png

  • Now copy-paste the whole code
var steem = require('steem');
var username = 'ENTER_USERNAME_HERE';
var password = 'ENTER_PASSWORD_HERE'; 

var wif = steem.auth.toWif(username, password, 'posting');
steem.broadcast.vote(wif, username, 'pakgamer', 'learning-object-oriented-programming-in-c-data-encapsulation-and-inheritance', 10000, function(err, result) {
    console.log(err, result);
});
  • Assign your username to the username variable and your posting key to you password variable.
    The voting weight is 100 by default you can change it by changing the value above.

image.png

  • Now the script is ready ,let's test it by running it. To run your script press the run button on the right side.

image.png

  • If everything went right it should vote and respond that your vote is done. For me, it is giving me some error. That's because I have already voted on my post.

Note:
If you copy-paste the above code and run it. it should vote for my post. So if you don't want to upvote my post and want to upvote another post then simple change the author name and post the link just like the user-name and password. If you have any questions please ask in the comment if you want to add something or I did something wrong please correct me in the comments

I think that much is enough for today. Take care till next post

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!