Steem-Python is the official Python library for STEEM.
https://github.com/steemit/steem-python
This is the second part of a series of tutorials.
To find the first part, click here.
Note:
The library is under constant development, while I will not be able to update this post.
If anything changes in the library, I will have to write a new post, or comment on this one.
In this part of the tutorial, I will look at some STEEM transaction types and some new modules from the library.
Since I got no feedback from the first part, I will just go ahead and write what I think is helpful. If you want me to write a certain tool, please let me know in the comments.
Stream all operations
To warm up, I prepared these 4 lines of code:
from steem.blockchain import Blockchain
chain = Blockchain()
for op in chain.stream():
print(op)
4 lines of code !
This will life stream and print all operations, that are happening on the blockchain.
It should look something like this:
Have a look at those transactions and let me know, if you want me to do something with a certain one next part.
I randomly noticed this one and had an idea ...
{'producer': 'netuoso', 'vesting_shares': '389.527170 VESTS', '_id': '5425fd8ecfb05eb2906db211470387277815b4ba', 'type': 'producer_reward', 'timestamp': datetime.datetime(2017, 12, 22, 12, 20, 48), 'block_num': 18308533, 'trx_id': '0000000000000000000000000000000000000000'}
Apparently, the type of operation is a 'producer_reward'.
The producer is netuoso.
This type of transaction should be part of every block and happen every 3 seconds, since every block is signed by a witness.
I was wondering, how many blocks I have signed so far, since I also run a witness node.
To single out the producer_reward - type transactions, I would need to add a condition:
if op['type'] == 'producer_reward':
To go back in history, I could use blockchain.history() instead of the stream function.
Since I only want to look at one account, I will use account module instead of the blockchain module, though.
Note:
You can always have a look at these modules and their functions here:
https://github.com/steemit/steem-python/blob/master/steem/blockchain.py
https://github.com/steemit/steem-python/blob/master/steem/account.py
Also, if I was to use this script regularly, I would query Steemdata or any other DB instead of getting my data from a steemd-node, as it is much faster. This is just for testing purposes.
The most fitting function seemed to be history_reverse.
Since I did not sign any blocks in the beginning, the normal history function would waste unnecessary time. I only started the witness node 6 months ago - so we do not have to go all the way back in time.
Filter for a certain operation
from steem.account import Account
account = Account('felixxx')
for op in account.history_reverse():
if op['type'] == 'producer_reward':
print(op)
It works:
I do not only want to print the transaction on screen, but I want to add up the amounts, too.
Witnesses earn STEEM Power (SP), which represent vesting shares;
'vesting_shares': '1935.370771 VESTS'
To handle this, I will import the amount module from the library, too.
To convert the VESTS to STEEM Power, I will also import Converter.
How much does a witness earn ?
from steem.account import Account
from steem.amount import Amount
from steem.converter import Converter
account = Account('felixxx')
total_amount = Amount('0.000000 VESTS')
vest = Converter()
for op in account.history_reverse():
if op['type'] == 'producer_reward':
amount = Amount(op['vesting_shares'])
block = op['block']
total_amount = total_amount + amount
total_sp = vest.vests_to_sp(total_amount)
print('Block: ', block, ': +', str(amount), ', total: ', str(total_amount), ' = ', str(total_sp))
Converting the amount every time seems to slow the script down and is kind of unnecessary, but this way I can keep the script short and simple.
Also, there is a little flaw with the conversion.
Instead of VESTS, the last column is STEEM Power (SP).
The numbers are right, just the suffix is wrong.
I could maybe fix this, but it would make this example too complicated.
For testing purposes, this is good enough.
As you can see, the script goes back through the account's history ( in this case mine ) and sums up all producer rewards.
Once it reaches block 0, the loop will end and the script too.
To print just the end result, you only need to tweak the script a little.
I leave this up to you ;)
If you have any questions or suggestions, what scripts I should work on next, please let me know in the comments.
Peace
P.S.: