Steem Bots - Curation Trail - [NodeJS & SteemJS] - Basic

in utopian-io •  6 years ago 

image.png


Repository:

https://github.com/steemit/steem-js

What Will I Learn?

  • You will learn how to see new transactions that goes through the blockchain in real-time
  • You will learn how to "follow" specific user votes and vote after him
  • You will learn how

Requirements

  • Node.JS
  • SteemJS

Difficulty

  • Basic

Tutorial Contents

You're going to learn how to make automatic curation trail that follows after a target account and sending votes after him.

Curriculum

The Tutorial

first create few variables -

const ACC_NAME = 'guest123',
    ACC_KEY = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg';

var min_pw = 98, // minimum power to make a vote
    weight = 10*100, // weight for vote (10 = percentage)
    TARGET = "lonelywolf"; // the target of the curation trail (who we're following on the trail)

the account name and key is the public steem.js account (only private posting key!)


so first make 2 functions, one for calculating the voting power and second to stream the vote

  • vote stream
function streamVote(author, permalink, weight) {
    steem.broadcast.vote(ACC_KEY, ACC_NAME, author, permalink, weight, function(err, result) { //"broadcast" the vote to the blockchain
        if(err)
            throw err;
        
        console.log('Voted Succesfully, permalink: ' + permalink + ', author: ' + author + ', weight: ' + weight / 1000 + '%.');
    });
}

so easy enough, we're taking the author, permlink and weight of the vote
sending a broadcast to the blockchain and make the vote.
than sending a comment to the console.


function getVoteWeight(acc_name){
    steem.api.getAccounts([acc_name], function(err, res){
        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);
        return vpow;
    });
}

at this function we're checking the vote weight,
we're checking it by the account name, simply we're taking the last_voted_time, the vote power and calculate it.
you can check the full code to see it full detailed.


steem.api.streamTransactions('head', function(err, result){
    const type = result.operations[0][0];
    const data = result.operations[0][1];
    if (type == 'vote' && data.voter == TARGET) {
            console.log('@' + TARGET + ' Just voted now!');
            if(getVoteWeight('lonelywolf') < min_pw){
                console.log("Your voting weight is less than the minimum: " + min_pw +", so the vote have not been sent!");
            }else{
                streamVote(data.author, data.permlink, weight);
            }
    }
});

now we need to listen for new transactions that goes through the blockchain, so we get the type and the data from the result operations.

then we check if the type of transaction is vote and we check if the voter(the account that made the vote) is our TARGET (the account we're following.

if it is we're checking if the weight is higher then the minimum power (so we're checking for example if the vote weight is higher than 84)
if it is, we're sending the vote, if not we're sending comment to the console.

now we can just run the script and let it run forever.

results:

=> [Function]
@lonelywolf Just voted now!
Voted Succesfully, permalink: liquid-steem-report-nov-7-2018, author: socky, weight: 10%.

Have a great day!

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!
Sort Order:  

Thank you for your contribution @lonelywolf.
After reviewing your tutorial we suggest the following points for improvement in your next tutorial:

  • In your tutorial it is very important to explain more in detail about what you are developing.

  • Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.

  • We suggest you improve the structure of your tutorial and the language as explained in the code.

  • Put some images in your tutorial to demonstrate results of what you are developing.

  • Include proof of work under the shape of a gist or your own github repository containing your code.

Thanks for the development of your tutorial. We look forward to your next contribution.

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? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks, I'll fix my issues next time!

Thank you for your review, @portugalcoin! Keep up the good work!

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by TheLonelyWolF🐺 from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

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

Hi, @lonelywolf!

You just got a 0.72% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

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!