Ruby Steem tutorial 4 - Claim rewards automatically

In the previous tutorial, we discussed how to get Steem account and post information via radiator, today we will learn how to claim rewards automatically by writing a Ruby program.

The problem

Everytime when the rewards arrive, user need to login and click the “Redeem Rewards (Transfer To Balance)” button to claim. Repeating doing this is very tedious. In this tutorial, we will find a solution to do this job automatically for us.

Write Ruby code to do the job

In the following code, your private posting key will be needed.

First we need to use Radiator Api to find if there are any rewards available to claim. If yes, Transaction Api can be used to claim the rewards.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require 'radiator'
user = 'yuxi'
pkey = 'YOUR_PRIVATE_POSTING_KEY'
api = Radiator::Api.new()
account = api.find_account(user)
reward_sbd = account.reward_sbd_balance
reward_steem = account.reward_steem_balance
reward_vesting = account.reward_vesting_balance
if reward_sbd!='0.000 SBD' or reward_steem!='0.000 STEEM' or reward_vesting!='0.000 VESTS'
tx = Radiator::Transaction.new(wif: pkey)
claim = {
type: :claim_reward_balance,
account: user
}
claim[:reward_steem] = reward_steem
claim[:reward_sbd] = reward_sbd
claim[:reward_vests] = reward_vesting
tx.operations << claim
tx.process(true)
print 'Claimed: '
print reward_steem + ' ' if reward_steem != '0.000 STEEM'
print reward_sbd + ' ' if reward_sbd != '0.000 SBD'
print reward_vesting if reward_vesting !='0.000 VESTS'
puts
else
puts 'Nothing to claim.'
end

Now we can test the above Ruby script by running:

1
ruby auto_claim.rb

The output looks like:

1
Nothing to claim.

If there are any rewards being claimed, the output looks like:

1
Claimed: 47.236998 VESTS

Setup a cron job

To make the scripts automatically run, we can config a cron job, e.g. in Ubuntu OS, just type:

1
nano /home/yuxi/claim.sh

Enter the following content:

1
2
3
#!/bin/bash
ruby /home/yuxi/auto_claim.rb

Then add the bash script into the cron job list by typing:

1
2
3
4
5
6
7
crontab -e
```
Add the following link in the editor:
```bash
00 * * * * /home/yuxi/claim.sh >> /home/yuxi/logs/claims.log 2>&1

Then your script will run every hour automatically to claim all rewards for you.

Share