create account

Digging into Hive ruby library by tipy

View this thread on: hive.blogpeakd.comecency.com
· @tipy ·
$2.14
Digging into Hive ruby library
I wanted to create a little script to automate the reward claiming for my account.

I know there is the amazing hive.autoclaim service, but I wanted to learn a bit more how Hive was working and thought it would be an easy task to begin with.

I couldn’t be more wrong.

First thing first, I visited the hive developer [documentation](https://developers.hive.io/tutorials).
It looks like there is 3 supported languages, javascript, python and ruby so I decided to pick Ruby which is the one I most familiar with.

![Picture by Joshua Fuller](https://images.unsplash.com/photo-1522776851755-3914469f0ca2?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80)

There is no examples in the tutorial section for claiming rewards with Ruby but the Javascript library has an [example](https://developers.hive.io/tutorials-javascript/claim_rewards.html).

So I need two operations, the first one `get_accounts` to have the actual rewards and the second one is `claim_reward_balance`, to make the claim itself.

Let’s look at Radiator source code now!

```bash
EDITOR=code bundle open radiator
```

Ok, I have a `find_account` method and a `claim_reward_balance` method, perfect.

I thought I found everything I needed at this point and the Radiator code is readable and heavily commented, so let’s go.

At this point my implementation looked like this:

```ruby
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'

Bundler.require

ACCOUNT_NAME = 'tipy'
OPTIONS = {
  chain: :hive,
  wif: '5J*****************'
}.freeze

def pending_rewards
  chain = Radiator::Chain.new(OPTIONS)
  account_data = chain.find_account(ACCOUNT_NAME)

  account_data.slice(
    :reward_hive_balance,
    :reward_hbd_balance,
    :reward_vesting_balance
  )
end

def claim_rewards(rewards)
  chain = Radiator::Chain.new(OPTIONS.merge(account_name: ACCOUNT_NAME))
  chain.claim_reward_balance!(
    reward_hive: rewards.reward_hive_balance,
    reward_hbd: rewards.reward_hbd_balance,
    reward_vests: rewards.reward_vesting_balance
  )
end

def empty_reward?(rewards)
  rewards.reward_hive_balance == '0.000 HIVE' && rewards.reward_hbd_balance == '0.000 HBD' && rewards.reward_vesting_balance == '0.000000 VESTS'
end

pr = pending_rewards
if empty_reward?(pr)
  puts 'Nothing to claim'
  return
end

puts "Rewards: #{pr.to_h}"
output = claim_rewards(pr)
puts output
```

and it was not working… :(

The code for making the claim_reward operation was throwing this exception:

`{“error":"condenser_api.broadcast_transaction_synchronous: Assert Exception:is_asset_type( reward_hbd, HBD_SYMBOL ): Reward HBD must be expressed in HBD"} (Hive::UnexpectedAssetError)`

Let’s look at the Radiator code:

```ruby
# Create a claim_reward_balance operation.
#
# Examples:
#
#     steem = Radiator::Chain.new(chain: :steem, account_name: 'your account name', wif: 'your wif')
#     steem.claim_reward_balance(reward_sbd: '100.000 SBD')
#     steem.broadcast!
#
# @param options [::Hash] options
# @option options [String] :reward_steem The amount of STEEM to claim, like: `100.000 STEEM`
# @option options [String] :reward_sbd The amount of SBD to claim, like: `100.000 SBD`
# @option options [String] :reward_vests The amount of VESTS to claim, like: `100.000000 VESTS`
def claim_reward_balance(options)
    reward_steem = options[:reward_steem] || '0.000 STEEM'
    reward_sbd = options[:reward_sbd] || '0.000 SBD'
    reward_vests = options[:reward_vests] || '0.000000 VESTS'

    @operations << {
        type: :claim_reward_balance,
        account: account_name,
        reward_steem: reward_steem,
        reward_sbd: reward_sbd,
        reward_vests: reward_vests
    }

    self
end
```

Obviously, it cannot work, this piece of code only works for Steem (Radiator is an “high level” library which works for both Hive and Steem).

But this method is just a kind of wrapper to create a Transaction, so I switched to transaction

```ruby
def claim_rewards(rewards)
  tx = Radiator::Transaction.new(OPTIONS)
  tx.operations << {
    type: :claim_reward_balance,
    account: ACCOUNT_NAME,
    reward_hive: rewards.reward_hive_balance,
    reward_hbd: rewards.reward_hbd_balance,
    reward_vests: rewards.reward_vesting_balance
  }

  tx.process(true)
end
```

and it’s still not working, but I made some progress, I’ve got an new error now:

`{"error":"condenser_api.broadcast_transaction_synchronous: missing required posting authority:Missing Posting Authority tipy"} (Hive::MissingPostingAuthorityError)`

WTF is this error? I have to admit I was a bit lost at this point so I asked for help on Discord.

I thought my WIF key was wrong and I tried every Hive keys without any success, but the good point is I now have a clear understanding of the WIF format.

I tried the code from the Javascript tutorial and my WIF key was the right one.

So I tried to see if there were an issue with the Radiator code. I’m pretty sure there is one but I don’t know why.

To be sure, the problem was in the Radiator code, I switched to hive-ruby (the library underneath Radiator):

```ruby
def claim_rewards(rewards)
  options = OPTIONS.merge(params: {
                            account: ACCOUNT_NAME,
                            reward_hive: rewards.reward_hive_balance,
                            reward_hbd: rewards.reward_hbd_balance,
                            reward_vests: rewards.reward_vesting_balance
                          })

  Hive::Broadcast.claim_reward_balance(options)
end
```

and it’s now working!



---



At some points, I was really close to give up and move to Javascript, but instead I decided to explore the Radiator library, and I’m really glad I did.

I learnt a lot about the library itself, and Hive internals.

I don’t want to blame anyone with this post, just share my experience.
Thanks inertia for maintaining the Radiator and hive-ruby librairies, I browsed the code enough to say it’s a great job.

Maybe I’ll try to understand the Radiator code more and try to do some pull requests (but hey opensource depends on good will and it’s finally sunny here ;p).

I’ve seen some vcr tests in the code and this is something I wanted to try myself for a long time, so this is maybe the right time to do it.

The ruby documentation on Hive developer portal also needs some love.

Hope you enjoyed my little adventure writing this piece of code.

Happy coding!
👍  , , , , , , , , , , , , , , , , , , , ,
properties (23)
authortipy
permlinkdigging-into-hive-ruby-library
categoryhive-169321
json_metadata{"links":["https://developers.hive.io/tutorials","https://images.unsplash.com/photo-1522776851755-3914469f0ca2?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1500&q=80","https://developers.hive.io/tutorials-javascript/claim_rewards.html"],"users":["param","option","option","option","operations"],"tags":["hive-169321","programming","ruby","radiator","proofofbrain"],"app":"ecency/3.0.18-vision","format":"markdown+html"}
created2021-08-11 06:24:21
last_update2021-08-11 06:24:21
depth0
children5
last_payout2021-08-18 06:24:21
cashout_time1969-12-31 23:59:59
total_payout_value1.074 HBD
curator_payout_value1.070 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,416
author_reputation2,588,336,936,599
root_title"Digging into Hive ruby library"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,455,317
net_rshares2,946,477,311,018
author_curate_reward""
vote details (21)
@biggestloser ·
I know java, python, R also,    but I don't know about Ruby.
Feeling stupid now.....
Great post by the way 👍
👍  
properties (23)
authorbiggestloser
permlinkqxvydw
categoryhive-169321
json_metadata{"app":"hiveblog/0.1"}
created2021-08-15 14:46:45
last_update2021-08-15 14:46:45
depth1
children0
last_payout2021-08-22 14:46:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length108
author_reputation705,599,888,230
root_title"Digging into Hive ruby library"
beneficiaries
0.
accountdemotruk
weight300
1.
accounthiveonboard
weight100
2.
accounttipu
weight100
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,558,060
net_rshares2,180,969,039
author_curate_reward""
vote details (1)
@cyboule ·
Bonjour et merci de votre vote
properties (22)
authorcyboule
permlinkre-tipy-2021819t20246654z
categoryhive-169321
json_metadata{"tags":["hive-169321","programming","ruby","radiator","proofofbrain"],"app":"ecency/3.0.19-vision","format":"markdown+html"}
created2021-08-19 18:02:42
last_update2021-08-19 18:02:42
depth1
children0
last_payout2021-08-26 18:02:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation47,399,155,158,214
root_title"Digging into Hive ruby library"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,650,692
net_rshares0
@ecency ·
**Yay!** 🤗<br>Your content has been **boosted with Ecency Points**, by @tipy. <br>Use Ecency daily to boost your growth on platform! <br><br><b>Support Ecency</b><br>[Vote for Proposal](https://hivesigner.com/sign/update-proposal-votes?proposal_ids=%5B141%5D&approve=true)<br>[Delegate HP and earn more](https://ecency.com/hive-125125/@ecency/daily-100-curation-rewards)
properties (22)
authorecency
permlinkre-2021811t91944930z
categoryhive-169321
json_metadata{"tags":["ecency"],"app":"ecency/3.0.16-welcome","format":"markdown+html"}
created2021-08-11 09:19:45
last_update2021-08-11 09:19:45
depth1
children0
last_payout2021-08-18 09:19:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length370
author_reputation618,466,050,514,436
root_title"Digging into Hive ruby library"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,457,355
net_rshares0
@hivebuzz ·
Congratulations @tipy! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@tipy/upvoted.png?202108110836"></td><td>You received more than 50 upvotes.<br>Your next target is to reach 100 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@tipy) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinkhivebuzz-notify-tipy-20210811t090640
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-08-11 09:06:39
last_update2021-08-11 09:06:39
depth1
children0
last_payout2021-08-18 09:06:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length614
author_reputation370,313,823,790,458
root_title"Digging into Hive ruby library"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,457,160
net_rshares0
@inertia ·
I’m not sure why you had trouble with Radiator.  But maybe there was a bug 3 years ago.  I went back to my original tutorial app to see if it still worked.  I also updated it to use Hive examples:

https://peakd.com/@inertia/stinkypete-rb-reward-claim-script-for-steem

Yes, the devportal should also have a simplified tutorial for this.  I should work on that.
properties (22)
authorinertia
permlinkre-tipy-sbns1s
categoryhive-169321
json_metadata{"tags":["hive-169321"],"app":"peakd/2024.4.1"}
created2024-04-09 04:49:03
last_update2024-04-09 04:49:03
depth1
children0
last_payout2024-04-16 04:49:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length361
author_reputation346,568,901,399,561
root_title"Digging into Hive ruby library"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id132,717,349
net_rshares0