How to Tail the Golos Blockchain in Ruby
Ever want to just watch the blockchain from a terminal, as if you're tailing a log file? Here's how to do that in ruby.
As always, we use Radiator with bundler
. You can get bundler
with this command:
$ gem install bundler
I've tested it on various versions of ruby. The oldest one I got it to work was:
ruby 2.0.0p645 (2015-04-13 revision 50299) [x86_64-darwin14.4.0]
First, make a project folder:
$ mkdir radiator
$ cd radiator
Create a file named Gemfile
containing:
source 'https://rubygems.org'
gem 'radiator', github: 'inertia186/radiator'
gem 'awesome_print'
Then run the command:
$ bundle install
Create a file named tail_blockchain.rb
containing:
require 'rubygems'
require 'bundler/setup'
Bundler.require
stream = Radiator::Stream.new(url: 'http://golos.steem.ws/')
last_op = nil
pattern = ARGV.first
stream.operations do |operation|
if !!pattern
next unless operation.inspect =~ /#{pattern}/i
end
op_key = operation.keys.first
op_value = operation.values.first
log = []
case op_key
when :comment
(v = op_value.title).empty? or log << "title: #{v}"
(v = op_value.parent_author).empty? or log << "parent_author: #{v}"
(v = op_value.parent_permlink).empty? or log << "parent_permlink: #{v}"
log << "author: #{op_value.author}"
log << "permlink: #{op_value.permlink}"
when :vote
log << "weight: #{op_value.weight / 100} %"
log << "voter: #{op_value.voter}"
log << "author: #{op_value.author}, permlink: #{op_value.permlink}"
when :custom_json
log << "id: #{op_value.id}"
log << "json: #{op_value.json}"
when :transfer
log << "from: #{op_value.from}"
log << "to: #{op_value.to}"
log << "amount: #{op_value.amount}"
(v = op_value.memo).empty? or log << "memo: #{v}"
else log << op_value.to_json
end
ap op_key if last_op != op_key
ap log.join(', ')
last_op = op_key
end
Then run it:
$ ruby tail_blockchain.rb
You can also pass along a regex pattern to match on:
$ ruby tail_blockchain.rb ned
Note, the comment
operation (which also represents posts) does not output the body. If the operation, including the body, contains the optional regex, it will display that line without the body so it doesn't flood the display.