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

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 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

Sort:  

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

  • We suggest that you further detail your tutorial.

  • In images that are not yours, always place the image source.

  • In code sections it is very important to have comments, for the less experienced reader it is good to reinforce the explanation in the code.

Looking forward to your upcoming tutorials.

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]

In code sections it is very important to have comments

This is a litte back and forth. Some moderators wish for explanations in comments others for explanations as plain text - which is mutually exclusive unless you repeat the information.

Right now i compromise by supplying a fully commented version of the script on Github. I also have all the boiler plate code on Github so not to repeat them in every part of the tutorial.

Posted using Partiko Android

In images that are not yours, always place the image source.

Done.

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

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!

Congratulations @krischik! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You received more than 100 as payout for your posts. Your next target is to reach a total payout of 250

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

You can upvote this notification to help all Steemit users. Learn why here!

Hi, @krischik!

You just got a 0.94% 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.

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

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.026
BTC 59515.78
ETH 2505.02
USDT 1.00
SBD 2.47