Martin Krischik avatar

Using Steem-API with Ruby Part 13 — Print Data from Steem Engine Database Table

krischik

Published: 06 Jun 2019 › Updated: 06 Jun 2019Using Steem-API with Ruby Part 13 — Print Data from Steem Engine Database Table

Using Steem-API with Ruby Part 13 — Print Data from Steem Engine Database Table

Steemit_Ruby_Engine.png

Repositories

SteemRubyTutorial

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

radiator

Steem Engine

steem-engine_logo-horizontal-dark.png

What Will I Learn?

This tutorial shows how to interact with the Steem blockchain, Steem database and Steem Engine using Ruby. When accessing Steem Engine using Ruby their only one APIs available to chose: radiator.

img_train.png

In this particular chapter you learn how to read a single row from the Steem Engine database tables.

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 radiator

Difficulty

For reader with programming experience this tutorial is basic level.

Tutorial Contents

Steem Engine allows users to add token and contacts to the steem block chain. Currently only three predefined contracts are know: "tokens", "market", and "steempegged". Each contract has one or more database table to store their data.

img_steem-engine_overview.png

In order to read the content of a table it is necessary to know the name of database tables to query. The Steem-Print-Print-SSC-Contract.rb from the previous part of the tutorial prints all data of a Steem Engine contract — including the database tables used by the contract.

The table names are found in the aptly named attribute “tables”. Each table name is prefixed with their contract name. Presumably to make them unique throughout the system. Here a list of the currently known tables names:

Unique nameContactTable
market_buyBookmarketbuyBook
market_metricsmarketmetrics
market_sellBookmarketsellBook
market_tradesHistorymarkettradesHistory
steempegged_withdrawalssteempeggedwithdrawals
tokens_balancestokensbalances
tokens_contractsBalancestokenscontractsBalances
tokens_paramstokensparams
tokens_pendingUnstakestokenspendingUnstakes
tokens_tokenstokenstokens

Implementation using radiator

As mentioned only radiator offers an API to access Steem Engine. For this radiator offerers a name space called Radiator::SSC. To access the database tables their are two methods: Contracts.find_one and Contracts.find. The latter will be described in the next part of the tutorial.

Im this part of the tutorialContracts.find_one is used to access the row of any table. The method has three mandatory parameters: contract, table and query:

contract: The name of the contract. As mentione there are currently three known contracts: "tokens", "market", and "steempegged".
table: The name of the tables to query. See below.
query: A list of column names and values.


In order to just access the first row the query attribute is left empty and the result is just printed. There is no explicit error message, if the query fails no data is returned.

if ARGV.length == 0 then
   puts "
Steem-Print-SSC-Table-Sample — Print first row of a steem engine table.

Usage:
   Steem-Print-SSC-Table-Sample contract_name table_name

"
else
   # read arguments from command line

   _contract = ARGV[0]
   _table = ARGV[1]

   # the query attribute is mandantory, supply an empty query
   # to receive the first row.

   _row = Contracts.find_one(
      contract: _contract,
      table: _table,
      query: {
      }
   )

   if _row == nil then
      puts "No data found, possible reasons:

⑴ The contract does not exist
⑵ The table does not exist
⑶ The table is empty
"
   else
      pp _row
   end
end

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

The output of the command for the table “tokens” of the contract “tokens” is:

Screenshot at Jun 06 20-03-31.png

The output of the command for the table “balances” of the contract “tokens” is:

Screenshot at Jun 06 20-06-39.png

From those output you can learn the names of the columns of the database and both tables will be used to update the Steem-Print-Balances.rb on GitHub script to print the Steem Engine Token in addition to Steem, Steem Dollar and VESTS.

Curriculum

First tutorial

Previous tutorial

Next tutorial

Proof of Work

Image Source

Beneficiary

Beneficiary.png

comment votes posts level payout commented voted

Leave Using Steem-API with Ruby Part 13 — Print Data from Steem Engine Database Table to:

Written by

For each right there is an equal and opposite obligation. — me

Read more #utopian-io posts


Best Posts From Martin Krischik

We have not curated any of krischik's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Martin Krischik