Bot Build #6 - !Simple! Paid Upvoting Bot

in #utopian-io7 years ago (edited)


Repository

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

What Will I Learn?

  • Create Simple Paid Voting Bot

Requirements

  • Node.JS, Here
  • Steem Package (Install: npm install steem --save)

Difficulty

  • Intermediate

Tutorial Contents

You will learn how to create paid voting bot.

Curriculum

The Tutorial

Step 1 - Setup Functions & Variables

First of all, we need 2 variables for the user & wif.

Note: I will use guest123 user, this is a guest user that everybody can use [this user is steemjs official guest user]

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

ACC_KEY - the wif, private posting key.
ACC_NAME - Account Name.

we need two functions, one to calculate the voting weight and one to send the vote.

function calculateVotingWeight(amountPaid){

}

function streamVote(url, amount) {

}

we need those 2 functions to send the vote.
one to calculate the voting weight and one to send the actual vote.

Step 2 - Create The Functions

first, we need to calculate the weight of the vote, because it's a simple bot we will check for sbd value, so for example, if the account has voting power that equals to 1$, 0.5$ or greater will equal to 100% vote.

    const token = amountPaid.split(' '),
    tokenType = token.pop(),
    tokenValue = token.shift();
    let weight;
    if (tokenValue >= 0.5){
        weight = 100;
    } else if (tokenValue >= 0.1) {
        weight = 40;
    } else {
        weight = 20;
    }
    const steemToSbdRatio = 1.1;
    if( tokenType == 'STEEM') {
        return (weight * steemToSbdRatio) * 500;
    } else {
        return weight * 500;
    }

first, we need to split 0.001 SBD(amount) to an only number 0.001, then we use the shift function to delete the SBD/STEEM (the token type).

then we check if the value of the sbd we received is greater than 0.5 if it is we send 100% vote, etc.

and we create the variable steemToSbdRatio,

const steemToSbdRatio = 1.1;

this variable will give us the way to check how much value will steem paid vote equal to.

for nowadays it's equal to around 1.1.

now we need to check if the token type is STEEM or SBD,
if it is STEEM we will multiply weight by steemToSbdRatio and then we will get the actual voting weight by multiply this by 500

Note: multiply by 500 will equal to around 50% vote, if you multiply it by 1000 it will be 100% but when someone using STEEM it will give more than 100% so the vote will not send.

Now we need to create the function that stream/send the vote.
first of all, we want to get the permlink and author from the memo (URL) and calculate the weight.

function streamVote(url, amount) {
    const memo = url.split('/');
    const author = memo[4].split('@')[1];
    const weight = calculateVotingWeight(amount);
}

at first we split the memo(url) whenever there is slash(/), result:

[ 'https:',
  '',
  'steemit.com',
  'utopian-io',
  '@lonelywolf',
  'bot-build-5-curation-trail' ]

we will get json variable with 5 variables, but we need only the name and the permlink.

so, memo[4] is @lonelywolf, but for the vote function, we need only the name so we need to split it.
so, memo[4].split('@')[1];
we need [1] to get the second "split" and this is the name.

so author is memo[4].split('@')[1];

and then we need to get the voting weight so we're using the function we made before calculateVotingWeight.

now we just need to send the vote,

steem.broadcast.vote(ACC_KEY, ACC_NAME, author, memo[5], weight, function(err, result) {
        console.log('Voted Succesfully, permalink: ' + memo[5] + ', author: ' + author + ', weight: ' + weight / 1000 + '%.', err);
    });

ACC_KEY - Account posting private key
ACC_NAME - Account name
author - The post author
memo[5] - The permlink
weight - The voting weight

Step 3 - Read Transfers And Send The Vote

first of all, as the last Bot Build tutorial, we need to read the transfers, now we need to read the sbd/steem transfers.

steem.api.streamTransactions('head', function(err, result) {
    let type = result.operations[0][0];
    let data = result.operations[0][1];
});

this function gets every transaction that goes through the blockchain.

type is the type of the transaction
data is the data we get on the transaction, for our, it will be the sender, memo and amount.

now we need to check if the type is transfer and if the receiver is our account.

     if(type == 'transfer' && data.to == ACC_NAME) {
             console.log("Incoming request for vote from: " + data.from +", value: " + data.amount + "\n\n");
    }

data.to is the receiver
data.from is the sender
data.amount is the amount

now just stream the vote

        streamVote(data.memo, data.amount);

data.memo is the memo the user send at the transfer (transaction)

and here we're done!

if you want to check if all works just use the streamvote function with your parameters, example:

streamVote("https://steemit.com/utopian-io/@lonelywolf/bot-build-5-curation-trail", "0.1 SBD");

results:

Voted Successfully, permalink: bot-build-5-curation-trail, author: lonelywolf, weight: 10%. null

null - if you get this message it says that there are no errors if you don't want to get it just delete the err after the message.

console.log('Voted Succesfully, permalink: ' + memo[5] + ', author: ' + author + ', weight: ' + weight / 1000 + '%.');

You can check the full work at my repl.it account and other bots that I create!

Proof of Work Done

the work made in Repl.it, https://repl.it/@lonelywolf22/Steem-Bots-Paid-Upvoting-Bot
user: https://repl.it/@lonelywolf22

GitHub: https://github.com/lonelywolf1
Utopian Tutorials GitHub Repository: https://github.com/lonelywolf1/Bot-Projects-SteemJS

Sort:  

You got a 3.94% upvote from @brupvoter courtesy of @lonelywolf!

Very interesting this tutorial.
Put comments in the code to make it easier to understand.
Good job!


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you!
My next contribution on the clock, I'll do that :)

Hey @lonelywolf
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!

Super tutorial ! Thanks !

Coin Marketplace

STEEM 0.19
TRX 0.19
JST 0.034
BTC 91243.00
ETH 3101.74
USDT 1.00
SBD 2.86