Hello,
This is the 11. post of "steem-python for dummies" series. If you didn't read the older ones take your time and have a look. (Just realizing we have already completed 10 posts!)
In this post, we will learn how to listen new blocks and parse them into transactions and operations. Basically, what you need for a simple block explorer.
This is a basic flow of a block parser. To listen all future blocks, let's expand the logic a little bit.
That sounds good, right? That's the main flow of block explorers in the crypto-currency world. Let's implement that flow.
Let's start with implementing parse_block function. It should get an integer which indicates the block height.
I will skip storing them because it's irrelavant to the subject. However, if you want to store the data somewhere, I suggest using mongodb since it will be easier to get things working in a fast way.
from steem import Steem
from pprint import pprint
s = Steem()
def parse_block(block_height):
block = s.get_block(block_height)
for transaction in block["transactions"]:
for operation in transaction['operations']:
operation_type, operation_data = operation[0:2]
print("Operation: %s" % operation_type)
pprint(operation_data)
parse_block(18260154)
This snippet will print each operation with it's name and it's raw data on block 18260154.
Note that, this is the simplest way of parsing a block. But normally:
You need to check get_block returns OK. Sometimes, nodes become unstable and you have timeouts. My practice for that to have a retry mechanism to get the data.
Blocks may have zero transactions. Especially if you parse old blocks, you will encounter them. So, checking a block actually has transactions is a must.
Every operation type has a different type of data inside it. Read operations.py to see a good list and how they're implemented as objects.
We need to have a stream of new blocks produced and pass them into get_block() to see the latest operations in the network.
def get_last_block_height():
props = s.get_dynamic_global_properties()
return props['last_irreversible_block_num']
def listen_blocks(starting_point=None):
if not starting_point:
starting_point = get_last_block_height()
while True:
while(get_last_block_height() - starting_point) > 0:
starting_point += 1
parse_block(starting_point)
print("Sleeping for 3 seconds...")
time.sleep(3)
This snippet has 3 steps.
Enjoy your matrix themed console with STEEM transactions:
You can see the whole script here.
That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts
.