Learning Python with @felixxx #2: Warm Up Exercises and How Much Does a Witness Earn ?

in #steem-python7 years ago (edited)

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.:

Please also consider voting for my witness @felixxx here:

https://steemit.com/~witnesses

Sort:  

FYI, in the title "Excercises“ should be "Exercises"

Thanks, fixed !

You're welcome

@felixxx is german so some miss spelling is expected but f@elixxx is better then this dude

Sehr interessant! Wenn man in die Eingeweide der Blockchain schauen kann, bekommt man viel mehr Durchblick wie hier der Hase läuft. Du hast dich da sehr tief hineingefuchst. Tolle Leistung! Respekt.

Ich wollte eigentlich nicht nur damit prahlen, was ich jetzt alles kann, sondern andere einweihen, damit sie es nicht ganz so schwer haben wie ich.

Das erste Beispiel hat ganze 4 Zeilen Code ...

Ich würde mich sehr freuen, wenn sich jemand mal daran versuchen würde :)

Als ich mich mit knapp 40 Jahren vom Techi zur Führungskraft verwandelt habe, ist die Technik bei mir aus der Übung geraten. Wahrscheinlich käme ich da wieder rein, bin ja UNIX/LINUX Jünger. Aber andere können diese Sachen viel besser und schneller als ich. Daher begnüge ich mich damit, derartige Artikel mit Interesse und Freude zu lesen und zu voten.

Naja, dann freue ich mich, dass man dem Text auch etwas entnehmen kann, ohne dass man alle Schritte selber nachvollzieht.

Thanks for sharing Python-Steemit tutorials. I'm only voting for 23 witnesses so i will add you later on when I have key with higher rights in my hand. I'm only voting for witnesses that are active and contribute.

I appreciate it.

How much roughly does a top 19 witness earn a day currently?

24 * 60 * 60 / 3 / 21 * 389.527170 VESTS

= 546551.5474285714 VESTS

Roughly 0.5 MVests/ day, which is roughly 250 STEEM Power.

Learn it very well.nice job dia