SteemBots - Upvoting By Payment - [SteemJS&Node.JS] - Basic Bot

in #utopian-io6 years ago

image.png


Repository:

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

What Will I Learn?

  • You will learn how to make a bot that upvoting by payment
  • You will learn how to get broadcast transactions (transactions that goessteemjs now on the blockchain)
  • You will learn how to stream an upvote to the blockchain

Requirements

  • Node.JS
  • SteemJS

Difficulty

  • Basic

Tutorial Contents

You're going to learn how to create upvoting bot with that calculate simply the voting power by the payment, note the calculation is a really simple calculation.

Curriculum

The Tutorial

  • As the latest tutorials, don't forget to create a basic server! (if you don't know how go to the nodejs #1 tutorial, it's fully explained there).

Add the user guest123 to the code, this is a global account for steemjs developers

const ACC_NAME = 'guest123', // Account Name
    ACC_KEY = '5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg'; // Account Private WIF Key

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 steem 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 [steem/sbd]
    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$ SBD / >0.6 STEEM)
    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 steem and sbd, so because it's simple bot we're doing it manually

  const SbdSteemRatio = 0.5; // the ratio between sbd and steem, example: steem = 0.8 and sbd 1.2, the ratio is around the 0.5 because you need 1.5 steem to get 1 sbd

this is the ratio between sbd and steem, example: steem = 0.8 and sbd 1.2, the ratio is around 0.5 because you need 1.5 steem to get 1 sbd

  if( tokenType == 'STEEM') { // checking if the token(coin) type is steem
      return (weight * steemToSbdRatio) * 1000; //if it is the value weight will calculate with the Sbd to Steem Ratio
  } else {
      return weight * 1000; // if it's not steem it will be a regular vote
  }

now we're checking if the coin type is steem if it is we're calculating it with the sbd to steem ratio,
if the token is sbd we'll send a normal vote.

Finale

and simply, you're done, just run the script and check it, send 0.01 sbd 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!

Proof of Work Done

have a good day, we're done for this tutorial!
this series will continue at the next week with more tutorials and with more content!

Sort:  
Loading...

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

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!

Coin Marketplace

STEEM 0.25
TRX 0.11
JST 0.032
BTC 62432.37
ETH 3003.22
USDT 1.00
SBD 3.78