Create Vote-Follow/Trail Script With Stem.js JavaScript Library (Steem.js #4)

in utopian-io •  20 days ago

Repository

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

What Will I Learn?

In this tutorial you will learn how to create and use script that will follow all votes from wanted user.

  • How to get data from head block in Steem blockchain.
  • How to follow votes from wanted user(s).

Requirements

State the requirements the user needs in order to follow this tutorial.

  • Linux or Windows terminal
  • Node.js: sudo apt-get install nodejs for linux
    https://nodejs.org/en/download/ for windows
  • Node.js package manager npm: sudo apt-get install npm for linux
    For windows it's already included in Nodejs installer.
  • Steem.js: npm install steem --save

Difficulty

  • Basic

Create Vote-Follow / Trail Script With Steem.js

In this tutorial I will be using Windows 10 and Atom.io text editor.

First let's create file vote-follow.js and open in in your favorite text editor.

Add first line of code: var steem = require('steem'); This will 'connect' variable steem to Steem.js.

Than add steemj.js function streamTransactions . This will get data from head (current) block.

        steem.api.streamTransactions('head', function(err, result) {
          let txType = result.operations[0][0]
          let txData = result.operations[0][1]
          let includesVotes = checkBlockForVote(txType,txData)
          if (includesVotes) {
            console.log('POST FOUND: ', txData)
          }
        });

This part for code will check data from block. If voter is fbslo it will run function "upvote".

function checkBlockForVote(txType,txData) {
          //check if voter is fbslo. If voter = fbslo, your account will vote same post.
          if( txData.voter == 'fbslo') {
            console.log('POST: ', txData)
            upvote(txData)
          }
        }

Now we need to set function "upvote". It will contain steem.broadcast.vote from steem.js. Parts of code are explained in comments (//comment)

function upvote(txData){
          console.log('Upvoting...',  )
          //You account name
          var voter = 'your-account';
          //Your account private posting key
          var wif = 'private-posting-key';
          var author = txData.author
          var permlink = txData.permlink
          //Change weight 100% vote = 10000; if you use txData.weight it will upvote with same voting weight as "followed" voter.
          var weight = txData.weight
          //Broadcast vote to blockchain
          steem.broadcast.vote(wif,voter,author,permlink,weight,console.log)

        }

Full Code

//get steem.js -> npm install steem --save
var steem = require('steem');

//get transactions from head block
        steem.api.streamTransactions('head', function(err, result) {
          let txType = result.operations[0][0]
          let txData = result.operations[0][1]
          let includesVotes = checkBlockForVote(txType,txData)
          if (includesVotes) {
            console.log('POST FOUND: ', txData)
          }
        });
        function checkBlockForVote(txType,txData) {
          //check if voter is fbslo. If voter = fbslo, your account will vote same post.
          if( txData.voter == 'fbslo') {
            console.log('POST: ', txData)
            upvote(txData)
          }
        }
        function upvote(txData){
          console.log('Upvoting...',  )
          //You account name
          var voter = 'your-account';
          //Your account private posting key
          var wif = 'private-posting-key';
          var author = txData.author
          var permlink = txData.permlink
          //Change weight 100% vote = 10000; if you use txData.weight it will upvote with same voting weight as "followed" voter.
          var weight = txData.weight
          //Broadcast vote to blockchain
          steem.broadcast.vote(wif,voter,author,permlink,weight,console.log)
 }

Video:

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.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • There are parts of the code that have little explanation, try to explain as much as possible.
  • Improve the structure of the 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? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

That's really helpful video for those who have knowledge of programming. I have also extract some website data by using php script and i am really happy to see your work and the algo you use here is helpful

Hey @fbslo
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

You got a 19.35% upvote from @postpromoter courtesy of @fbslo!

Want to promote your posts too? Check out the Steem Bot Tracker website for more info. If you would like to support the development of @postpromoter and the bot tracker please vote for @yabapmatt for witness!

Aún estoy tratando de entender como le doy uso. Tu tutorial está bien, soy yo comenzando en esta tecnología. gracias. aliriera