Using Steem-API with Ruby Part 4 — Convert VESTS ⇔ Steem

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 4th part you learn how VESTS, Steem Power and STEEM are connected and can be converted.

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

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.

You should also have read Part 2 and Part 3 of the tutorial as this part build on them.

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

Difficulty

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

Tutorial Contents

When you take a look at your account wallet you find three values: Steem, Steem Power and Steem Dollar.

Screenshot at Feb 04 142910.png

With both Steem and Steem Power measured in Steem and Steem Dollar in SBD or just a $ sign. However, if you look at the output of Tutorial Part 2 you will see that there is no Steem Power — just a few ridiculous large values called vesting measured in VESTS:

Screenshot at Jan 27 17-44-14.png

This vesting is the Steem Power. However since VESTS values are ridiculous large they are usually not displayed anywhere in the user interface. Instead the amount of Steem needed buy the VESTS (called power up) or you get when you sell your VESTS (called power down) is displayed. The conversion is done by the following formula:

steem = vest \frac{total_vesting_fund_steem}{total_vesting_shares}

Note that total_vesting_fund_steem and total_vesting_shares aren't constant and this is the reason why it seems that your Steem power is slowly increasing for no apparent reason: VESTS are constantly getting more expensive and the user interface is showing the amount of Steem you would get if you sell your VESTS today.

After all this theory let's get to the practical part. I made a VESTS to Steem script using steem-ruby and a Steem to VESTS script using radiator. In example calls I convert the Steem Power of the various user level:

Logo Level Your Steem Power in VESTS Your Steem Power in Steem
Plankton 0 to 999'999 VESTS 0 to ≈500 Steem
Minnow 1'000'000 to 9'999'999 VESTS ≈500 to ≈5'000 Steem
Dolphin 10'000'000 to 99'999'999 VESTS ≈5'000 to ≈50'000 Steem
Ocra 100'000'000 to 999'999'999 VESTS ≈50'000 to ≈500'000 Steem
Whale more than 1'000'000'000 VESTS more than ≈500'000 Steem

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

Implementation using steem-ruby

The first script Steem_From_VEST.rb converts VESTS to Steem:


Shows usage help if the no values are given to convert.

if ARGV.length == 0 then
   puts """
Steem_From_VEST — Print convert list of VESTS value to Steem values

Usage:
   Steem-Print-Balances values …

"""
else

read arguments from command line

   Values = ARGV

Calculate the conversion Rate. We use the Amount class from Part 2 to convert the string values into amounts.

   _total_vesting_fund_steem = Amount.new Global_Properties.total_vesting_fund_steem
   _total_vesting_shares     = Amount.new Global_Properties.total_vesting_shares
   _conversion_rate          = _total_vesting_fund_steem / _total_vesting_shares

iterate over the valued passed in the command line

   Values.each do |value|

convert the value to steem by multiplying with the conversion rate and display the value

      _steem = value.to_f * _conversion_rate
      puts "%1$18.6f VESTS = %2$15.3f STEEM" % [value, _steem]
   end
end

Hint: Follow this link to Github for the complete script with syntax highlighting: Steem_From_VEST.rb.

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

Screenshot at Feb 04 165002.png

As you can see the Steem values of the user levels are slightly below 500, 5000, … . Remember that VESTS get more expensive so one million VESTS will eventually cost more then 500 Steem.

Implementation using radiator

The second script Steem_To_VEST.rb converts VESTS to Steem. Apart from printout there is only one character difference.


Shows usage help if the no values are given to convert.

if ARGV.length == 0 then
   puts """
Steem_To_VEST — Print convert list of Steem value to VESTS values

Usage:
   Steem-Print-Balances values …

"""
else

read arguments from command line

   Values = ARGV

Calculate the conversion Rate. We use the Amount class from Part 2 of the tutorial to convert the string values into amounts.

   _total_vesting_fund_steem = Amount.new Global_Properties.total_vesting_fund_steem
   _total_vesting_shares     = Amount.new Global_Properties.total_vesting_shares
   _conversion_rate          = _total_vesting_fund_steem / _total_vesting_shares

iterate over the valued passed in the command line

   Values.each do |value|

convert the value to steem by dividing with the conversion rate and display the value. Here is the actual difference: A division instead of a multiplication.

      _vest = value.to_f / _conversion_rate
      puts "%1$15.3f STEEM = %2$18.6f VEST" % [value, _vest]
   end
end

Hint: Follow this link to Github for the complete script with syntax highlighting: Steem_To_VEST.rb.

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

Screenshot at Feb 05 141113.png

As you see buying Steem Power for 500, 5000 … Steem will get you slightly more VESTS then 1 million, 10 million … putting you will within the the respective user level. But remember, by the time you read this you will get less VEST then displayed.

Curriculum

First tutorial

Previous tutorial

Next tutorial

Proof of Work

Image Source

Beneficiary

image.png

comment votes posts level payout commented voted

H2
H3
H4
Upload from PC
Video gallery
3 columns
2 columns
1 column
8 Comments