[Hive Patched Tutorial] SteemJS - Vote, Comment and Follow functions - All In One

in OCD4 years ago

aio.png


Repository:

https://www.npmjs.com/package/steem-js-patched

All of this tutorial is patched for Hive, so it works for Hive and steem!

All of the examples and results is from the old tutorial but still same for hive!

Hello guys, in this tutorial you will learn how to upvote posts,
comment on a post and follow an account.

so let's start with opening the new steemjs application,
create a new project

npm init

image.png

can change:

  • entry point to app.js(you can leave it index.js, I like to use the main file as app.js)
  • author to your name
  • you can add a description, doesn't really matter
    and now you have the JSON file.

install steem package,

npm I steem-js-patched --save
image.png
(Updated to steem-js-patched!!!)

it will update all the packages and install the steem package.

now let's create the application file,
create a file with the entry point name, I used app.js
image.png

now add the steem package -

let steem = require('steem-js-patched');

Add the Hive API:

steem.api.setOptions({  url: 'https://api.hive.blog' })

this line will make sure you're using the API of Hive and not STEEM

now create 2 variables,
account - the account name,
key - the account private posting key

let account = "lonelywolf",
    key = "5Kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

nice, we're all set!


Follow Function

first, create the function,

//Follow Account
function streamFollow(acc, key, following){

}

acc - the account
key - the posting key
following - the account we're going to follow

now we need to create a json variable because follow account is a custom json broadcast

  var json = JSON.stringify(
  ['follow', {
  follower: acc,
  following: following,
  what: ['blog']
  }]);

follow - the action
follower - our account
following - the account we're going to follow
what - this is the type of the account (which is a blog)

now we can send the customJson

  steem.broadcast.customJson(key, [], [acc], 'follow', json, function(err, result) {
      console.log(err, result);
  });

here we're sending the custom json with the posting key, the account name, and the action type.

usage:

streamFollow(account, key, "appreciator");

results:

image.png

null - says that there are no errors, the rest is the result we're getting from the blockchain!


Voting function

so I hope you saved the account and key variables cause we need them again 😉

create the function :

function streamVote(acc, key, author, permlink, weight){

}

to make your life easier create a variable at the top called weight

let weight = 10*100; //first number = the vote percentage, 0.1-100%

the maximum weight is 10,000 which is 100%, so we're getting the weight and multiply it by 100 so it will be easier to know the percentage.

now we need to send the vote with broadcast function through steemjs

steem.broadcast.vote(key, acc, author, permlink, weight, function(err, result) {
    if(err) return console.log(err);
});

now we're going to calculate the voting power and send a console comment

so first we need the account data

steem.api.getAccounts([acc], function(err, res){

});

now we need to get the last vote time and calculate it with the voting power data to get the voting power of our account

var secondsago = (new Date - new Date(res[0].last_vote_time + "Z")) / 1000,
      vpow = res[0].voting_power + (10000 * secondsago / 432000);
vpow = Math.min(vpow / 100, 100).toFixed(2);

now we have the voting power and we can send the console log

console.log('@' + acc+ ', Voted Succesfully, permalink: ' + permlink+ ', author: ' + author + ', weight: ' + weight / 100 + '%. Current Voting Power: ' + vpow + '%');

great, now the function is working perfectly!

usage:

streamVote(acc, key, "lonelywolf", "ex-coinbase-cto-bitcoin-will-eventually-conquer-wall-street", weight);

results:
image.png
Fullscreen

steemd:
image.png

(old results, but working same on Hive!)


Comment Function

now for the "complex" one, the comment function.

so before we're going for the comment function we need a function called "makeid" which will create new ID for the comment permlink.

function makeid(number) {
    var text = "";
    var possible = "abcdefghijklmnopqrstuvwxyz";
    for (var i = 0; i < number; i++)
      text += possible.charAt(Math.floor(Math.random() * possible.length));
  
    return text;
  }

text - at the end will be the ID result
possible - the possible digits for the ID

at the loop, we're adding a digit with random mathematics!

example output of 13 digits: jxjcazzkktznt

now we're all set to create the comment function

function streamComment(acc, key, author, permlink, permalink, body){

}

permlink - the permlink of the post
permalink - the permlink of the new comment
body - the content of the comment

now we can send the broadcast function

    steem.broadcast.comment(key, author, permlink, acc,
    permalink, '', body,
    JSON.stringify({
    tags: "steemit",
    app: 'steemit/0.1'
    }),
    function(err, result) {
        if(!!err)
            console.log("Comment Failed!", err);
        else
            console.log("Comment Posted Succesfully, Author: " + author);
    });

to make it easier to read,
we're using the posting key, the author name, the permlink of the post,
our account name, the new permalink for the comment and the body to post a new comment.
if you want to use this for a platform like PALnet you should change "tags" to
tags: "palnet"

great, we can now use the function!

usage:

streamComment(acc, key, "lonelywolf", 'ex-coinbase-cto-bitcoin-will-eventually-conquer-wall-street', makeid(12), "this is a test comment");

results:

image.png
image.png

I hope I teach you something new, if you have any suggestions for the next tutorials, please let me know :)

This is a repost of my old post, I patched some sections to make it work with Hive!

I hope you found the tutorial useful, Have a great day!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 53955.17
ETH 2265.61
USDT 1.00
SBD 2.34