Using Steem-API with Ruby Part 21 — Access Hive Blockchain 3

in hive-175254 •  5 years ago  (edited)

Hive_Steem_Ruby_Engine.png

Repositories

SteemRubyTutorial

All examples from this tutorial can be found as fully functional scripts on GitHub:

steem-ruby

steem-mechanize

radiator

What Will I Learn?

This tutorial shows how to interact with the Steem and Hive blockchain as well as the Steem and Hive database using Ruby. When using Ruby you have three APIs available to chose: steem-api, steem-mechaniz and radiator which differentiates in how return values and errors are handled:

  • steem-api uses closures and exceptions and provides low level computer readable data.
  • steem-mechanize drop in replacement for steem-api with more performance network I/O
  • radiator uses classic function return values and provides high level human readable data.

Since all APIs have advantages and disadvantages sample code for both APIs will be provided so the reader ca decide which is more suitable.

In this chapter of the tutorial you learn how to write scripts which will run both on the Steem and Hive blockchain using the VEST to STEEM and HIVE conversion as example.

Requirements

Basic knowledge of Ruby programming is needed. It is necessary to install at least Ruby 2.5 as well as the following standard ruby gems:

gem install bundler
gem install colorize
gem install contracts

For Hive compatibility you need the hive patched versions of the steem-ruby and radiator ruby gems. These are not yet available in the standard ruby gem repository and you need to install them from source. For this you change into the directory you keep Git clones and use the following commands:

git clone "https://github.com/krischik/steem-ruby"

pushd "steem-ruby"
    git checkout feature/Enhancement22
    gem build "steem-ruby.gemspec"
    gem install "steem-ruby"
popd

git clone "https://github.com/krischik/steem-mechanize"

pushd "steem-mechanize"
    git checkout feature/Enhancement22
    gem build "steem-mechanize.gemspec"
    gem install "steem-mechanize"
popd

git clone "https://github.com/krischik/radiator"

pushd "radiator"
    git checkout feature/Enhancement22
    gem build "radiator.gemspec"
    gem install "radiator"
popd

If you already checked out the steem-ruby and radiator you will need to update them to the newest version.

Note: All APIs steem-ruby, steem-mechanize and radiator provide a file called steem.rb. This means that:

  1. When more then one APIs is installed ruby must be told which one to use.
  2. The three APIs can't be used in the same script.

If there is anything not clear you can ask in the comments.

Difficulty

For reader with programming experience this tutorial is basic level.

Tutorial Contents

In the previous parts we took a first look at accessing the hive blockchain and an initial view at using coins. In this tutorial we will look a deeper view at the use of the various coins.

When writing code a for just one chain one often uses the string literals “STEEM”, “SBD” and “VEST”. This of course won't work for code designed to run with multiple chain.

To support this the Amount class was extended to return the symbol names for three coin types of a Steem style blockchain:

   DEBT_ASSET = Steem::Type::Amount.debt_asset Chain
   CORE_ASSET = Steem::Type::Amount.core_asset Chain
   VEST_ASSET = Steem::Type::Amount.vest_asset Chain

The actual values are:

Chain Asset String
Steem Core "STEEM"
Dept "SBD"
Vest "VESTS"
Hive Core "HIVE"
Dept "HBD"
Vest "VESTS"

Implementation using steem-ruby

The Steem-From-VEST.rb script was previously described in Part 4 of the tutorial. Here we only look at the changed needed to make the script multi chain compatible.

Store the chain name and symbols for convenience.

Chain      = Chain_Options[:chain]
DEBT_ASSET = Steem::Type::Amount.debt_asset Chain
CORE_ASSET = Steem::Type::Amount.core_asset Chain
VEST_ASSET = Steem::Type::Amount.vest_asset Chain

Create instance to the steem condenser API which will give us access to the requested chain

   Condenser_Api = Steem::CondenserApi.new Chain_Options

Calculate the conversion rate. We use the Amount class from Part 2 to convert the sting values into amounts. The chain need to be passed as different chains have different symbols

   _total_vesting_fund_steem = Steem::Type::Amount.new(Global_Properties.total_vesting_fund_steem, Chain)
   _total_vesting_shares     = Steem::Type::Amount.new(Global_Properties.total_vesting_shares, Chain)

Read the median history value and Calculate the conversion Rate for Vests to steem backed dollar. We use the Amount class from Part 2 to convert the string values into amounts.

   _median_history_price = Steem::Type::Price.get Chain
   SBD_Median_Price      = _median_history_price.to_f

Read the reward funds.

   _reward_fund = Steem::Type::Reward_Fund.get Chain

Print the result using the the correct symbols for the requested chain

   puts "%1$18.6f %2$-5s = %3$15.3f %4$-5s,   100%% Upvote: %5$6.3f %6$-3s" % [
       value,
       VEST_ASSET,
       _steem,
       CORE_ASSET,
       _max_vote_value,
       DEBT_ASSET]

Hint: Follow this link to Github for the complete script with comments and syntax highlighting: Steem-From-VEST.rb.

The output of the command (for the steem account) looks like this:

Screenshot at Apr 30 154031.png

Implementation using radiator

The Steem-To-VEST.rb script was previously described in Part 4 of the tutorial. Here we only look at the changed needed to make the script multi chain compatible.

Store the chain name and symbols for convenience.

Chain      = Chain_Options[:chain]
DEBT_ASSET = Radiator::Type::Amount.debt_asset Chain
CORE_ASSET = Radiator::Type::Amount.core_asset Chain
VEST_ASSET = Radiator::Type::Amount.vest_asset Chain

Create instance to the steem condenser API which will give us access to the requested chain

   Condenser_Api = Radiator::CondenserApi.new Chain_Options

Calculate the conversion rate. We use the Amount class from Part 2 to convert the sting values into amounts. The chain need to be passed as different chains have different symbols

   _total_vesting_fund_steem = Radiator::Type::Amount.new(Global_Properties.total_vesting_fund_steem, Chain)
   _total_vesting_shares     = Radiator::Type::Amount.new(Global_Properties.total_vesting_shares, Chain)

Read the median history value and Calculate the conversion Rate for Vests to steem backed dollar. We use the Amount class from Part 2 to convert the string values into amounts.

   _median_history_price = Radiator::Type::Price.get Chain

Read the reward funds.

   _reward_fund = Radiator::Type::Reward_Fund.get Chain

Print the result using the the correct symbols for the requested chain

   puts "%1$18.6f %2$-5s = %3$15.3f %4$-5s,   100%% Upvote: %5$6.3f %6$-3s" % [
       value,
       CORE_ASSET,
       _vest,
       VEST_ASSET,
       _max_vote_value,
       DEBT_ASSET]

Hint: Follow this link to Github for the complete script with comments and syntax highlighting : Steem-To-VEST.rb.

The output of the command (for the steem account) looks identical to the previous output:

Screenshot at Apr 30 154229.png

Curriculum

First tutorial

Previous tutorial

Next tutorial

Proof of Work

Image Source


posts5060.pngcomments5870.pngvotes6680.pnglevel9090.pngpayout6680.pngcommented5870.pngvoted5060.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Not a single comment? Apparently they're not very fond of programming here. I, in particular, have never programmed in ruby.

One question: Do I need to install a lot of things if I want to install ruby on my Windows PC?

Can I create a discord robot with ruby?

Thanks for your time.

Maybe neither site is fond of Hive / Steem cross chain programming.


Ruby has its own package manager. So you install ruby and then use the gem package manager to install the rest.

https://rubyinstaller.org

And it seems you can use ruby for a discord bot:

https://github.com/discordrb/discordrb

Well, I really like to build my own discord bot. So when I have the time I will check which one is the best for the task

  ·  5 years ago (edited)

Maybe people are not fond of steem / hive cross chain development.


Ruby has its own package manager. So you install ruby and then use the gem package manager to install the rest.

https://rubyinstaller.org

And it seems you can use ruby for a discord bot:

https://github.com/discordrb/discordrb

Right now, I'm so busy but when I have the time, I would like to know which one is the best to develop a discord bot