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!)
- steem-python for dummies #1 - Introduction and Basic Operations
- steem-python for dummies #2 - Playing with account data
- steem-python for dummies #3 - Coding an upvote bot
- steem-python for dummies #4 - Private Memos
- steem-python for dummies #5 - Bundling Blockchain Operations
- steem-python for dummies #6 - Delegating
- steem-python for dummies #7 - Creating Accounts
- steem-python for dummies #8 - Calculating Post Rewards
- steem-python for dummies #9 - Creating orders in the market
- steem-python for dummies #10 - Listening accounts
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.
1. Planning
parse_block function
- Get a block
- Split the block into transactions
- Split the transactions into operations
- Do something with that (store maybe?)
- Sleep with block interval
- Go to step1
This is a basic flow of a block parser. To listen all future blocks, let's expand the logic a little bit.
listen_blocks function
- Start an infinite loop (while True)
- Get a starting block ID
- Get the last irreversible block ID
- Get the difference and send each block id to parse_block function
- Sleep for block interval in seconds. (Currently 3)
That sounds good, right? That's the main flow of block explorers in the crypto-currency world. Let's implement that flow.
2. Implementation
parse_block function
Let's start with implementing parse_block function. It should get an integer which indicates the block height.
- We will use steem.get_block() to get block details.
- We will loop block["transactions"] to get related transaction
- We will iterate transaction["operations"] to get all operations in transaction.
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.
listen_blocks function
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.
- Get a starting block height. If it's not set, get the latest one.
- Parse the blocks between starting point and the last irreversible block.
- Sleep 3 seconds and go to step 2.
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
.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
python is <3
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
indeed.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hey @emrebeyler I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Birthdaypost !BEER
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
View or trade
BEER
.Hey @emrebeyler, here is a little bit of
BEER
from @isnochys for you. Enjoy it!Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit