You can find all examples from this tutorial as fully functional scripts on GitHub:
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:
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:
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.
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:
Provided you have some programming experience this tutorial is basic level.
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:
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.
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:
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: