Using Steem-API with Ruby Part 3 — Print Dynamic Global PropertiessteemCreated with Sketch.

in #utopian-io6 years ago (edited)

Steemit_Ruby.png

Repositories

SteemRubyTutorial

You can find all examples from this tutorial as fully functional scripts on GitHub:

steem-ruby

radiator

What Will I Learn?

This tutorial shows how to interact with the Steem blockchain and Steem database using Ruby. When using Ruby you have two APIs available to chose: steem-api 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.
  • radiator uses classic function return values and provides high level human readable data.

Since both APIs have advantages and disadvantages I have provided sample code for both APIs so you can decide which is more suitable for you.

In this 3nd part you learn the request the get the dynamic global properties. This properties can, for example, be used to convert VESTS balances into the more common STEEM balances. You use that with this formula:

steem = vest \frac{total_vesting_fund_steem}{total_vesting_shares}

In the next tutorial we will upgrade Steem-Dump-Balances.rb and Steem-Print-Balances.rb to show steem and vest balances side by side.

Requirements

You should have basic knowledge of Ruby programming you need to install at least Ruby 2.5 as well as the following ruby gems:

gem install bundler
gem install colorize
gem install steem-ruby
gem install radiator

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

Note: Both steem-ruby and radiator provide a file called steem.rb. This means that:

  1. When you install both APIs you need to tell ruby which one to use.
  2. You can't use both APIs in the same script.

Difficulty

Provided you have some programming experience this tutorial is basic level.

Tutorial Contents

The easiest way to get the dynamic global properties is the use of Steem::CondenserApi or Radiator::CondenserApi. On the surface both work the same. Underneath there is a slight difference:

  • steem-ruby only checks https://api.steemit.com for the data.
  • Radiator has a list of 12 fall back server to check.

This makes radiator calls slightly more reliable.

Note: Starting with this tutorial I won't copy paste the whole script any more as this would just be repetitive. Just the part needed to understand the lesson. The fully commented and fully functional scripts are still available on Github.

Implementation using steem-ruby

Steem-Dump-Global-Properties.rb:


Errors are handled via exceptions.

begin

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

   Condenser_Api = Steem::CondenserApi.new

Read the global properties. Yes, it's as simple as this.

   Global_Properties = Condenser_Api.get_dynamic_global_properties
rescue => error

I am using Kernel::abort so the code snipped including error handler can be copy pasted into other scripts. You certainly don't want to continue the script when the global properties are not available.

   Kernel::abort("Error reading global properties:\n".red + error.to_s)
end

Pretty print the result. It might look strange to do so outside the begin / rescue but the value is now available in constant for the rest of the script. Do note that using constant is only suitable for short running script. Long running scripts would need to re-read the value on a regular basis.

pp Global_Properties

Hint: Follow this link to Github for the complete script with syntax highlighting: Steem-Dump-Global-Properties.rb.

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

Screenshot at Jan 30 11-34-40.png

Implementation using radiator

Steem-Print-Global-Properties.rb


Errors are handled via exceptions.

begin

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

   Condenser_Api = Radiator::CondenserApi.new

Read the global properties. Yes, it's as simple as this.

   Global_Properties = Condenser_Api.get_dynamic_global_properties
rescue => error

I am using Kernel::abort so the code snipped including error handler can be copy pasted into other scripts. You certainly don't want to continue the script when the global properties are not available.

   Kernel::abort("Error reading global properties:\n".red + error.to_s)
end

Pretty print the result. It might look strange to do so outside the begin / rescue but the value is now available in constant for the rest of the script. Do note that using constant is only suitable for short running script. Long running scripts would need to re-read the value on a regular basis.

pp Global_Properties

Hint: Follow this link to Github for the complete script with syntax highlighting: Steem-Print-Global-Properties.rb.

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

Screenshot at Jan 30 11-35-37.png

Curriculum

First tutorial

Previous tutorial

Next tutorial

Proof of Work

image.png

comment votes posts level payout commented voted

Sort:  

Thank you for your contribution @krischik.
After analyzing your tutorial we suggest the following points below:

  • In the next tutorial try to further detail the concepts you are explaining. The essence of tutorials is mainly to teach, so try also to have some theory on the subject that you will present.

  • We suggest you put some gifs to show the results on the console. It gets more interesting in visual terms.

Your tutorial is interesting, thanks for your work in developing this contribution.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

Hi @krischik!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @krischik!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hi, @krischik!

You just got a 0.17% upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in here to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 59634.64
ETH 2590.21
USDT 1.00
SBD 2.47