Create Your Steem App - Guide Part 3 - Let's Dig Out Some Steem Blocks :)

Hi there!

In two previous parts of our Make Your Steem App guide we've created a virtual machine and connected with steem blockchain. We can now download full Steem Blocks and display them but... what are they made of and are they any useful for a normal, sane person?:) Let's find out!


Very accurate visualization of a just-mined Steem Block


Let's start where we left last time - this is our code to dig out new steem block every 3 seconds and display it:

from steem import Steem
from steem.blockchain import Blockchain
print ("Libaries imported successfully")

steem = Steem(keys=['private_posting_key', 'private_active_key'])
print ("Connection with steem established")

b = Blockchain()
stream = b.stream_from(full_blocks = True)
print ("Streem of new steem blocks initiated")

for block in stream:
 print ("\nNew Block!")
 print ("%s" % block)

Let's review each line so you have better understanding what's going on:

from steem import Steem
from steem.blockchain import Blockchain

Those 2 lines import necessary libraries.

print ("Libaries imported successfully")
The print function displays the text that is contained between symbols (" and "). You can type there anything you want, we just added it to confirm that the 2 lines of code above worked as intended (hopefully ;)

steem = Steem(keys=['private_posting_key', 'private_active_key'])
This is where you connect with the Steem using your account. If you don't know where to find your private keys, just google it - there are many tutorials on steemit :)

b = Blockchain()
stream = b.stream_from(full_blocks = True)
print ("Streem of new steem blocks initiated")

Once we are connected with steem, we can initiate downloading steem blocks.

for block in stream:
Hold on, we are now entering an endless loop - for every new block created in steem blockchain, do the code below:

 print ("\nNew Block!")
 print ("%s" % block)

The \n means start a new line. Then display text "New Block!".
Below that the %s means display variable - and the variable is the steem block itself... This is how it looks like:


Now, when you print (display) steem block, it look like a wall of text, where you can find numbers, weird chars and quite normal sentences. This block contains everything steemians did for the last 3 seconds. So you will find here votes, comments, posts, transfers, flags etc. Think about it as a small database that contains only last 3 seconds of steem. Because it's well structured, it's really easy to navigate and you can quickly find useful data in it. Let's see how.


Let's start with listing the main components of a steem block. We'll do this by adding another for loop. Here's how the updated code looks like:

from steem import Steem
from steem.blockchain import Blockchain
print ("Libaries imported successfully")

steem = Steem(keys=['private_posting_key', 'private_active_key'])
print ("Connection with steem established")

b = Blockchain()
stream = b.stream_from(full_blocks = True)
print ("Streem of new steem blocks initiated")

for block in stream:
 print ("\nNew Block!")
 for component in block: # this is our new loop
  print ("%s" % component) # by the way, the # char is used for comments :)

This will display the following results:

previous
timestamp
witness
transaction_merkle_root
extensions
witness_signature
transactions
block_id
signing_key
transaction_ids

What we're really looking for are the transactions - because posting, making comments, voting etc. are all transactions in the steem block. The rest (timestamp, witness, block_id etc.) are all additional data needed to allow steem blockchain work properly and securely.

OK than, let's display those transactions. This is how we modify our code to do it:

for block in stream:
 print ("\nTransactions in new Steem Block:")
 print ("%s" % block['transactions'])

Instead of wall of text, we can also display them one by one with our fellow loop:

for block in stream:
 print ("\nTransactions in new Steem Block:")
 for transaction in block['transactions']:
  print ("\n%s" % transaction)

This is an example of such a transaction:

{'ref_block_num': 48039, 'ref_block_prefix': 1830916191, 'expiration': '2017-12-11T23:26:30', 'operations': [['custom_json', {'required_auths': [], 'required_posting_auths': ['beekart'], 'id': 'follow', 'json': '["follow",{"follower":"beekart","following":"mitchhunter","what":["blog"]}]'}]], 'extensions': [], 'signatures': ['2013e984919e37ac10ad82cf116e9ff94ecc8d739c24a86e6aace1d3d5aa76fb8b59bf7c9de746ac6f8b65c77d2c4b0a6e9512a35b9bb231693af10867210e48a3']

Still a lot of unnecessary data for us. But each transaction is a small database itself - well structured so we can choose what data to display. Now, what we're looking for is something called operations. Let's modify our code like this:

for block in stream:
 print ("\nOperations in new Steem Block:")
 for transactions in block['transactions']:
  operation = transactions['operations']
  print ("\n%s" % operation)

This will give us result that looks like this:
[['vote', {'voter': 'originalworks', 'author': 'techmojo', 'permlink': '5-minecraft-recreations-that-are-truly-mind-blowing-gaming-top-5-part-20', 'weight': 150}]]

Or like this:
[['comment', {'parent_author': 'steempowerpics', 'parent_permlink': 'my-cryptokitties-experience-an-in-depth-look-at-the-not-so-funny-side', 'author': 'cristianv', 'permlink': 're-steempowerpics-my-cryptokitties-experience-an-in-depth-look-at-the-not-so-funny-side-20171211t235840860z', 'title': '', 'body': 'These “kitties” are clogging up the ethereum Blockchain.', 'json_metadata': '{"tags":["cryptocurrency"],"app":"steemit/0.1"}'}]]

Or this:
[['transfer', {'from': 'banjo', 'to': 'drotto', 'amount': '0.001 SBD', 'memo': 'https://steemit.com/writing/@moonkitty/the-beginning-of-moonkitty'}]]

Now we're talking. This looks like normal database records of steemians actions - and there were just performed because we're downloading only the newest steem blocks.

So what we're basically doing here is digging out fresh, new steem blocks and then picking out useful data about just-performed steemians actions. In other words we're monitoring steem blockchain in real time and checking who's doing what.

In the next part of the guide we're gonna use this data to do something neat ;)

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center