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 instalment of the tutorial you learn how to access the conversion rate of STEEM and Steem Backed Dollars (SBD).
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
Note: Both steem-ruby and radiator provide a file called steem.rb. This means that:
If there is anything not clear you can ask in the comments.
Provided you have some programming experience this tutorial is basic level.
In the last part of the tutorial we took a look at the account wallet where we noted that steem power is shown on STEEM and not VESTS. If you look at the screen shot again you will notice that in the last row the estimated account value is quoted in '$':
It is important to know that this is not the account value in US$. Instead it's the account value in Steem Backed Dollars (SBD). One SBD is supposed to be about one US$ but this can vary. Which leads to the question: What is the exchange rate between SBD and STEEM?
This isn't documented in the official API documentation or the official tutorials. Unlike VESTS to STEEM conversion which is well documented.
What is documented is get_current_median_history_price. This value is a rolling median price for SBD and is provided by the Witnesses. Being a rolling median it's smother but also a little behind the actual price.
The get_current_median_history_price can be read using call to the Condenser_Api API. As you can see it returns not one but two values called base and quote and they are both ridiculously large:
How they are used isn't documented but it's possible to deduct the usage from the types: base is in SDB and quote in STEEM so a division is the way to go:
Reading the get_current_median_history_price is pretty straight forward and similar to what you have learned Part 3. The actual code samples are rather short after this much theory.
begin
Create instance to the steem condenser API which will give us, among other things, access to the median history price.
Condenser_Api = Steem::CondenserApi.new
Read the median history. Yes, it's as simple as this.
Median_History_Price = Condenser_Api.get_current_median_history_price
Calculate the conversion Rate for STEEM to SBD. We use a trick here: to_f ignores the text after the space which simplifies the code.
_base = Median_History_Price.result.base
_quote = Median_History_Price.result.quote
Conversion_Rate_Steem = _base.to_f / _quote.to_f
rescue => error
I am using Kernel::abort so the code snipped including error handler can be copy pasted into other scripts.
Kernel::abort("Error reading global properties:
".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 a constant for the rest of the script. Do note that using a constant is only suitable for short running script. Long running scripts would need to re-read the value on a regular basis.
pp Median_History_Price
Show some actual sample conversion rates:
puts ("1.000 STEEM = %1$15.3f SBD") % (1.0 * Conversion_Rate_Steem)
puts ("1.000 SBD = %1$15.3f STEEM") % (1.0 / Conversion_Rate_Steem)
Hint: Follow this link to Github for the complete script with syntax highlighting: Steem-Dump-Median-History-Price.rb.
The output of the command (for the steem account) looks like this:
It's pretty much indentical to the radiator implementaiton.
begin
Create instance to the steem condenser API which will give us access to the median history price.
Condenser_Api = Radiator::CondenserApi.new
Read the median history. Yes, it's as simple as this.
Median_History_Price = Condenser_Api.get_current_median_history_price
Calculate the conversion Rate for STEEM to SBD. We use a trick here: to_f ignores the text after the space which simplifies the code.
_base = Median_History_Price.result.base
_quote = Median_History_Price.result.quote
Conversion_Rate_Steem = _base.to_f / _quote.to_f
rescue => error
I am using Kernel::abort so the code snipped including error handler can be copy pasted into other scripts.
Kernel::abort("Error reading global properties:
".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 a 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 Median_History_Price
Show some actual sample conversion rates:
puts ("1.000 STEEM = %1$15.3f SBD") % (1.0 * Conversion_Rate_Steem)
puts ("1.000 SBD = %1$15.3f STEEM") % (1.0 / Conversion_Rate_Steem)
Hint: Follow this link to Github for the complete script with syntax highlighting: Steem-Print-Median-History-Price.rb.
The output of the command (for the steem account) looks identical to the previous output:
