![upvote.png](https://files.peakd.com/file/peakd-hive/lonelywolf/MwKfr7ni-upvote.png) (Want to use the **logo** yourself? no problem, check out the post I made about [My Art Design - Hive Logo](https://peakd.com/hive-174578/@lonelywolf/art-design-hive-logo-by-lonelywolf-stav-art-inside-free-usage)) --- #### Repository: https://www.npmjs.com/package/steem-js-patched - ## Helpful tutorials: [[HIVE Patched] SteemJS Full Tutorial - All The Functions - All The Abilities](https://hive.blog/hive-139531/@lonelywolf/hive-patched-steemjs-full-tutorial-all-the-functions-all-the-abilities) [[Hive Patched Tutorial] SteemJS - Vote, Comment and Follow functions - All In One](https://hive.blog/hive-139531/@lonelywolf/hive-patched-tutorial-steemjs-vote-comment-and-follow-functions-all-in-one) # All of this tutorial is patched for Hive, so it works for steem and **Hive**! #### All of the examples and results is from the old tutorial but still same for hive! Hello! Today you're going to learn how to create an upvoting bot with that calculate simply the voting power by the payment, `note` the calculation is a really simple calculation. Load the steem/HIVE package: ``` const steem = require('steem-js-patched') ``` To install the package `npm install steem-js-patched --save` - Repository above --- First we will add the user guest123 (or your account) to the code, this(guest123) is a global account for steemjs developers ``` const ACC_NAME = 'guest123', // Account Name ACC_KEY = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg'; // Account Private WIF Key ``` you want to add the Hive API to work with **Hive** blockchain ``` steem.api.setOptions({ url: 'https://api.hive.blog' }); ``` So, First things first, we will get from the easiest to the hardest, firstly we're going to get the transactions from the blockchain ``` steem.api.streamTransactions('head', function(err, result) { let type = result.operations[0][0]; // getting the type of the transaction let data = result.operations[0][1]; // getting the data of the transaction } ``` this function gets the newest transactions that go through the hive blockchain, such as new blogs, transfers etc. as I already explained in the code, the type is getting the transaction type (new blogs/new transfer etc.) and data getting the data from the transaction, for the blog it will be an author, permlink, content etc. and for transfer, it will be a memo, sender, receiver etc. now we need to check if the transaction type is a transfer and if it is, we check if the receiver is our account. ``` if(type == 'transfer' && data.to == ACC_NAME) { // checking if the transaction type is a transfer and the reciever is our account } ``` simple enough, we're checking if type(transaction type) is `transfer` and if the receiver is our account. if it is we need to check the memo and check if the memo is url if it is we're going to send the vote, if it's not we're getting it as normal transfers. ``` var memo = data.memo.split('/'); if(memo[0] == "https:"){ // checking if the memo is a url console.log("Incoming request for vote from: " + data.from +", value: " + data.amount + "\n\n"); // sending a comment to the console and telling us that new request for vote is come. streamVote(data.memo, data.amount); }else{ console.log("Incoming transaction from: " + data.from + ", this is not a vote requst."); console.log("MEMO: " + data.memo); } ``` everything explained above! ### Now, create the functions - StreamVote & CalcVoteWeight firstly, streamvote, create a function called StreamVote with the variables URL, Amount ``` function streamVote(url, amount) { } ``` now we need to get the weight and the author ``` const memo = url.split('/'); // spliting the URL to array const author = memo[4].split('@')[1]; // getting the author of the post const weight = calcVoteWeight(amount); // getting the weight value by the amount ``` memo = url author is the author of the post that got from the url, example: ~~https://steemit.com/utopian-io~~/**@lonelywolf**/~~steem-bots---auto-follower-bot-steemjs--nodejs----begginer-tutorial~~ weight is the calculated weight from the calculate function that we will create after. now we just need to stream the vote to the blockchain ``` steem.broadcast.vote(ACC_KEY, ACC_NAME, author, memo[5], weight, function(err, result) { // starting the voting process if(!!err) throw err; // close the program and send comment to the console with the error details if there is an error console.log('Voted Succesfully, permalink: ' + memo[5] + ', author: ' + author + ', weight: ' + weight / 1000 + '%.', err); //if the vote Succesfully sent it will send all of the information to the console }); ``` so we have the broadcast. vote function, as it says it sends a vote, it uses the wif(private key), the account name, the author name, the permlink(memo[5]) and the voting weight. if there is an error we're sending it to the console and crash the application if everything has done successfully we're sending the info to the console. - The calculate function create a new function with the variable `amountPaid` ``` //this function will calculate the voting weight for the paid upvote as simple as it can be function calcVoteWeight(amountPaid){ } ``` now get the token type and value ``` const token = amountPaid.split(' '), tokenType = token.pop(), //taking the token(coin) type [HIVE/HBD] tokenValue = token.shift(); // taking only the token(coin) value ``` as easy as it can be, we're splitting the string by spaces and getting the token type & value now create a new variable called `weight` and check for values to set the weight to it ``` let weight; if (tokenValue >= 0.6){ // checking if the token(coin) value is higher than 0.6 (example: >0.6$ HBD/ >0.6 HIVE) weight = 100; } else if (tokenValue >= 0.25) { // same weight = 40; } else if (tokenValue >= 0.1) { // same weight = 20; }else{ // just like the else but if it's lower than 0.1 weight = 10; } ``` first, we check if the value is higher or equal to `0.6` if it is we're sending a full vote if the amount is higher than `0.25` we're sending 40% vote if the amount is higher than `0.1` we're sending 20% vote if the amount is lower than `0.1`(else) we're sending 10% vote now we need to get the ratio between hive and hbd, so because it's simple bot we're doing it manually ``` const HbdHiveRatio = 0.5; // the ratio between HBD and HIVE, example: HIVE = 0.8 and HBD 1.2, the ratio is around the 0.5 because you need 1.5 HIVE to get 1 HBD ``` this is the ratio between hbd and hive, example: hive = 0.8 and hbd 1.2, the ratio is around 0.5 because you need 1.5 hive to get 1 HBD ``` if( tokenType == 'HIVE') { // checking if the token(coin) type is HIVE return (weight * HbdHiveRatio) * 100; //if it is the value weight will calculate with the Hbd to Hive Ratio } else { return weight * 100; // if it's not Hive it will be a regular vote } ``` now we're checking if the coin type is hive if it is we're calculating it with the `hbd to hive` ratio, if the token is HBD we'll send a normal vote. ### Conclusion simply, you're done, just run the script and check it, send 0.01 HBD to your account (the account that on the script) with the post URL and get the vote. if there are any problems with the code, comment down below and I'll help you! **if you have any suggestions for next tutorials I will appreciate it if you can comment on your suggestions, thanks!** ### Have a great day!

See: [Hive Patched] Upvoting By Payment In 4 Easy Steps - Basic Bot by @lonelywolf