🤖 TUTORIAL - Auto Reward Claimer - Building bots With steem-js #6

in #utopian-io7 years ago (edited)

bot-tut-6.png

This tutorial is part of a series on ‘How To Build Bot’s Using Steem-JS Open Source Library’ it aims to demonstrate practical applications for bots and demystify the code used to create them.

The completed bot code is here - I know some people find it easier to see everything in one block and follow along that way. ⬅️

Outline 📝

In this tutorial, we’ll be using Steem.js to build a bot that checks if there are any pending rewards for a Steem account and then claims them if there are. Once our claiming system works we’ll set up a timer for checking and claiming ever few minutes automatically. Claiming rewards is usually a manual task, logging in, clicking over to the wallet and accepting the notification but this can easily be automated with Steem.js.

Requirements

  • A plain text editor (I use atom.io )
  • Your posting private key - This is found in your wallet on the permissions tab on steemit.com, click ‘show private key’ (remember to keep this safe). This is what allows the bot to claim rewards for you.

Learning Goals

  • Learn more about the steem.js library by looking at the getAccounts() function and the information it returns and using the claimRewardBalance() to interact with the reward system.
  • Create a timer in Javascript to run our code every few minutes.
  • Practise making and using functions

Step 1 - Setup 💻

This is our last tutorial that runs in the browser, the next tutorial will use Node.js to run bots from the command-line.

Use this template to get started - This is a base file for today's project and includes the link to the steem.js library, we’ll do everything else from scratch this time. Open the file in your test editor and in your browser.

We’ll need an ACCOUNT NAME & ACCOUNT_KEY, these allow us to specify the account to check and make sure we have the credentials to initialise a reward claim.

        // Tutorial 06 - Auto Claim Bot
        const ACCOUNT_NAME = ''
        const ACCOUNT_KEY = ''

Step 2 - Get Account Information

First up we’ll use the getAccounts function to revive the information from the blockchain about a specific account. This function returns a lot of useful information about an account. reputation, vote power, balances, account age, profile image etc. All the information you would find by looks at someone's page on Steemit.com and more.

steem.api.getAccounts([ACCOUNT_NAME], function(err, result){
    console.log(result[0]);
})

For example, see this screenshot and much more in the console.

Screen Shot 2018-01-21 at 23.05.19.png

There’s a couple things to note. First notice that the first parameter is an Array, in this case, we have a single name (our account name), but there could be multiple names. This is reflected in the result which is an Array of results, where we want to look at the first item in position zero that corresponds to the first user requested.

Specifically for this bot we’re looking for three values.

reward_sbd_balance
reward_steem_balance
reward_vesting_balance

Steem power can be represented in Steem power or vests, because the blockchain works in vests and uses that to claim rewards we’ll look for that in the account information. These data points will show how much rewards an account has pending.

We can save these values to make it easier to use in out next code block.

steem.api.getAccounts([ACCOUNT_NAME], function(err, result){
    console.log(err, result)
    let sbdReward = result[0].reward_sbd_balance
    let steemReward = result[0].reward_steem_balance
    let steemPowerInVests = result[0].reward_vesting_balance
}); 

Step 3 Broadcast a Claim Transaction

In previous Bot tutorials we looked at reading incoming transactions, now we’re going to broadcast one. The broadcast claim reward is fairly straightforward, it takes a username and key along with the value amounts to claim. We can add this into our code above so right after we retrieve the information we’ll try to claim any rewards.

steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, steemPowerInVests, function(err, result) {
    console.log(err, result);
});

Make sure to console.log the result so we can see if we’ve been successful. This one is a little hard to test unless you have some pending rewards. Finally, let’s put all of our code into a function so we can re-use it easily in future.

p.s you'll get an error instead of a result if you don't have any rewards to claim but don't worry it won't cause an issue.

function claimBalance(){
    steem.api.getAccounts([ACCOUNT_NAME], function(err, result){
        console.log(err, result)
        let sbdReward = result[0].reward_sbd_balance
        let steemReward = result[0].reward_steem_balance
        let steemPowerInVests = result[0].reward_vesting_balance
        steem.broadcast.claimRewardBalance(ACCOUNT_KEY, ACCOUNT_NAME, steemReward, sbdReward, steemPowerInVests, function(err, result) {
             console.log(err, result);
         });
     })
}

Step 4 Create A Timing Interval

The bot we created will auto claim rewards when it runs but what if we want to do it automatically every minute?

In Javascript, we have setTimeout() which will run code after a certain period of time and setInterval() which will run code at a set interval rate. We’ll use setInterval which has two parts to it, the function to run and how often to run it in milliseconds.

The simplest way to write it. 1000 milliseconds is equal to one second so we’ll check and claim every second. Notice the lack of parenthesis when specifying the function.

setInterval(claimBalance, 1000)

You may also have seen setInterval written with an anonymous function. here is an example running every 60seconds or 60,000 milliseconds. The interval is stored in a variable as a calculation to make it a little easier to understand. 60 x 1 second.

const INTERVAL = 60 * 1000
setInterval(function(){
    claimBalance()
}, INTERVAL)

Once the interval is set up we’re good to go, we have an auto reward claiming bot that runs every 60 seconds! One less thing we need to do manually on our Steem accounts.

Here’s the full code #6 🤖

There’s still a lot more we can do with bots and the Steem API but I hope you find this interesting and useful. If you’re looking into building bots let me know or ask any questions below ✌️

Other Posts in This series



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Thanks @deathwing appreciate you reviewing my tutorial 👊

Hey @sambillingham I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

You've already been paid out, but this post is really helpful so thanks!

You are very welcome. Let me know if I can help with anything.

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.028
BTC 69086.63
ETH 2471.20
USDT 1.00
SBD 2.39