[NodeJS] SteemJS - Vote, Comment and Follow functions - All In One
Repository:
https://github.com/steemit/steem-js
What Will I Learn?
- You will learn how to upvote, comment and follow through SteemJS
Requirements
- Node.JS
- SteemJS
Difficulty
- Basic
Curriculum
The Tutorial
Hello guys, in this tutorial you will learn how to upvote posts,
comment on a post and follow the account.
this tutorial will be at any other steemjs tutorial because we're almost always using these functions!
so let's start with opening the new steemjs application,
create a new project
npm init
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 --save
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
now add the steem package -
let steem = require('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, "masoom");
results:
null
- says that there are no errors, the rest is the result we're getting from the blockchain!
steemd:
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
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:
Fullscreen
steemd:
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:
I hope I teach you something new, if you have any suggestions for the next tutorials, please let me know :)
Full code: https://github.com/upmeboost-utopian/utopian-io-tutorials/blob/master/SteemJS/functions-tutorial.js
Proof of Work Done
https://github.com/upmeboost-utopian
Thank you for your contribution @lonelywolf.
After reviewing your tutorial we suggest the following points listed below:
Using the first person in the tutorials makes it difficult to understand the tutorials. We suggest using the third person in your text.
Your tutorial is interesting and well explained, however it's necessary to explain more in detail so that the reader can fully understand what you are teaching.
Improve the structure of the tutorial and your texts. It gets a little disorganized the way you write your tutorial.
Thank you for your work in developing this tutorial.
Looking forward to your upcoming tutorials.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @portugalcoin! Keep up the good work!
Great post, but again not weedcash related.
Hi @lonelywolf!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server
Hey, @lonelywolf!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!