Using Steem-API with Ruby Part 8 — Print Account Balances with vote powersteemCreated with Sketch.

in #utopian-io5 years ago (edited)

Steemit_Ruby.png

Repositories

SteemRubyTutorial

All examples from this tutorial can be found as fully functional scripts on GitHub:

radiator

What Will I Learn?

This tutorial shows how to interact with the Steem blockchain and Steem database using Ruby. This instalment teacher how to estimate a users vote value.

Requirements

Basic knowledge of Ruby programming is needed. It is necessary to install at least Ruby 2.5 as well as the following ruby gems:

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

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

  1. When both APIs are installed ruby must be told which one to use.
  2. Both APIs can't be used in the same script.

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

Difficulty

For reader with programming experience this tutorial is basic level.

Tutorial Contents

Estimating the vote value is a rather complex process involving all of the following variables:

NameAPIDescription
weightchosen by the userWeight of vote in %. ¹
current voting_power²calculated from the voting power of the last voteCurrent voting power in %. ¹
last_vote_timeDatabaseApi.get_accountsLast time the user voted in UTC.
last voting_power²DatabaseApi.get_accountsVoting power at last vote in %. ¹
vesting_sharesDatabaseApi.get_accountsVESTS owned by account
received_vesting_sharesDatabaseApi.get_accountsVESTS delegated from other accounts
delegated_vesting_sharesDatabaseApi.get_accountsVESTS delegated to other accounts
recent_claimsCondenserApi.get_reward_fundRecently made claims on reward pool
reward_balanceCondenserApi.get_reward_fundReward pool
sbd_median_pricecalulated from base and quoteConversion rate steem ⇔ SBD
baseCondenserApi.get_current_median_history_priceConversion rate steem ⇔ SBD
quoteCondenserApi.get_current_median_history_priceConversion rate steem ⇔ SBD

Notes:

  • ¹ : Stored as fixed point with 4 decimal places and need to be divided by 10⁴ before usage.
  • ² : Both the current and the last voting_power is called voting_power in the official documentation which maybe confusing.
  • ³ : The UTC marker is missing and must be added before converting the string into a date/time.

Step 1 Calculate SBD Median Price

To improve precision of any calculation the Steem database only stores integer numbers. In case of the SDB median price is stored as quotient or fraction which we just divide them as floating point number are good enough for an estimate.

sbd_median_price = \frac{base}{quote}

Step 2 Calculate the users voting power

The database only stores the users voting power at the time of last vote. This makes it necessary to add the voting power regained since the last vote. This is done with following formula:

current_voting_power = voting_power + \frac{seconds_ago}{five_days}

Note that the vote power is bounded between 0% (0.0) and 100% (1.0).

Step 3 Calculate the users VESTS

The users VESTS is calculated by adding and subtracting the in and delegates from the accounts VESTS and your multiply the result with 10⁶ (aka one million).

total_vests = vesting_shares + received_vesting_shares - delegated_vesting_shares

Step 4 Calculate Reward Share

The share of the rewards pool is then calculated by multiplying the power and the VESTS.

rshares = power \times final_vest

Step 5 Calculate the actual estimate

The estimate is calculated from the data of the most recent distribution. For this the reward share is divided by the amount of claims done last time and multiplied by the available reward pool. The result is the reward in Steem which is then then converted to SBD by multiplying it with the SBD median price.

estimate = \frac{rshares}{recent_claims \times reward_balance \times sbd_median_price}

Those who opt for a 100% power up might prefer a printout in Steem. For this leave out the multiplication with the sbd median price. But remember that the dust value is calculated in SBD and only postings with a payout of least 0.020 SBD will actually be payed out.

Implementation using steem-ruby

Since DatabaseApi.get_accounts of steem-api doesn't return the voting_power only the radiator implementation is included.

Implementation using radiator

Steem-Print-Balances.rb has been explained before in Part 2 and Part 6 of the tutorial and to avoid redundancy only the new functionality is described.

Follow this link to Github for the complete script with comments and syntax highlighting : Steem-Print-Balances.rb.


Conversion_Rate_Steem was renamed SBD_Median_Price because that's the name used in the official documentation.

   SBD_Median_Price      = _base.to_f / _quote.to_f

Read the reward funds. get_reward_fund takes one parameter is always "post".

   _reward_fund =  _condenser_api.get_reward_fund("post").result

Extract variables needed for the vote estimate. This is done just once here to reduce the amount of string parsing needed.

   Recent_Claims  = _reward_fund.recent_claims.to_i
   Reward_Balance = Amount.new _reward_fund.reward_balance

Calculate the real voting power of an account. The voting power in the database is only updated when the user makes an up vote and needs to calculated from there.

def real_voting_power (account)
   _last_vote_time = Time.strptime(account.last_vote_time + ":Z" , "%Y-%m-%dT%H:%M:%S:%Z")
   _current_time = Time.now
   _seconds_ago = _current_time - _last_vote_time;
   _voting_power = account.voting_power.to_f / 10000.0
   _retval = _voting_power + (_seconds_ago / Five_Days)

   if _retval > 1.0 then
      _retval = 1.0
   end

   return _retval.round(4);
end

…

_voting_power             = real_voting_power account

Calculate actual vesting by adding and subtracting delegation as well at the final vest for vote estimate.

      _total_vests = _vesting_shares - _delegated_vesting_shares + _received_vesting_shares
      _final_vest  = _total_vests.to_f * 1e6

Calculate the vote value for 100% up votes.

      _weight = 1.0

Calculate the account's current vote value for a 100% up vote.

      _current_power = (_voting_power * _weight) / 50.0
      _current_rshares = _current_power * _final_vest
      _current_vote_value = (_current_rshares / Recent_Claims) * Reward_Balance.to_f * SBD_Median_Price

Calculate the account's maximum vote value for a 100% up vote.

      _max_voting_power = 1.0
      _max_power = (_max_voting_power * _weight) / 50.0
      _max_rshares = _max_power * _final_vest
      _max_vote_value = (_max_rshares / Recent_Claims) * Reward_Balance.to_f * SBD_Median_Price

Print the current and maximum vote value. Colourise the current vote value depending on the voting power. If the vote value is 1.0 then colourise green and otherwise colourise red.

      puts ("  Voting Power    = " +
         "%1$15.3f SBD".colorize(
            if _voting_power == 1.0 then
               :green
            else
               :red
            end
         ) + " of " + "%2$1.3f SBD".blue) % [
         _current_vote_value,
         _max_vote_value]

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

Screenshot at Mar 06 15-52-11.png

The current vote value is printed in red as a vote power is not at it's maximum. However the difference between the current vote value und the maximum vote value is smaller then 0.0005 SDB so the difference isn't shown.

Curriculum

First tutorial

Previous tutorial

Next tutorial

Proof of Work

Image Source

comment votes posts level payout commented voted

Sort:  

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

  • Your tutorial is well structured and explained, did an excellent job in writing the contribution.

  • In code sections it's important to have comments with a brief description, it helps less experienced users understand what you are developing.

  • If any image is not yours, please put the source.

Thank you for your work in developing this tutorial.
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's important to have comments with a brief description

But the code is fully commented. See:

https://github.com/krischik/SteemRubyTutorial/blob/master/Scripts/Steem-Print-Balances.rb

Hi @krischik

I was talking about commenting code on the contribution, not just github.
Thank you for your contribution.


Need help? Write a ticket on https://support.utopian.io/.
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

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

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

You received more than 2000 upvotes. Your next target is to reach 3000 upvotes.

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 Steem users. Learn how here!

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 3.22% 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.35
TRX 0.12
JST 0.040
BTC 70887.21
ETH 3581.98
USDT 1.00
SBD 4.75