Python for STEEM #3-1: Homework

in python •  5 years ago 

This is part of a series of tutorials.
The goal is to produce a hands-on guide for people who want to jump straight into developing tools for STEEM.

Yesterday's Homework

First, I have to apologize.
The homework was a bit hard, I thought it would be easier.
I will try to go through this quickly, just to not leave anybody hanging.

This is how I solved it:

from beem.account import Account
from beem.blockchain import Blockchain

blockchain = Blockchain()
head_block = blockchain.get_current_block_num()

test_account = Account("holger80")

rewards = []
reward_sum = 0

for curation_reward in test_account.history_reverse(start = head_block, stop = head_block - 30000, use_block_num = True, only_ops = "curation_reward"):
    rewards.append(curation_reward['reward']['amount'])

for reward_num in range(10): # starts at 0
    reward_sum += int(rewards[reward_num])
    print('\n', reward_num + 1, ':',  rewards[reward_num])

print('\naverage:', reward_sum/ 10, '\n... this look like its in VESTS ...')    

Explanation

For this example, I am using the head-block as orientation (start).
I then use history_reverse to crawl through the account's history (30000 blocks backwards / stop = start - 30000) and append all curation rewards during that time to my list rewards.
I then use a counting loop to iterate through the first 10 rewards (using reward_num for indexing).
Sum them up and divide by 10 and the result is a number, that I hope is VESTS.

This is not very elegant, but works ok, I guess.

image.png

Remember to always put this in front:

from beem import Steem
from beem.instance import set_shared_steem_instance

steemit_api = Steem(node=["https://api.steemit.com"])
set_shared_steem_instance(steemit_api)

Notes

  • date_time

history supports date_time objects - the cleaner way to code this, would make use of this instead of the head-block. I stuck to integers for now, because I did not feel like explaining date_time.

  • 30000 blocks

STEEM's block-time is 3 seconds. You can trust STEEM's timestamps. Part of the witnesses' job is to maintain a well synchronized clock.
There are exactly 28800 blocks in a day, but 30,000 is a good enough approximation for me. There might not be 10 curation rewards in this short time-frame for all accounts. Change this number, if you need to.

  • for i in range(k):

I think this is a very neat method for a counting loop.
I can read it almost like an English sentence.

  • +=

This is just beautiful.
a += b replaces a = a + b

  • VESTS

In this example, the history method returned the reward-amount as a number only. They are VESTS, I assume.
That is not congruent with how this normally works. I expected a STEEM asset as return. Without use of beem's asset class, those would have been a bit complicated to calculate with, though.
I shall be more careful, should I give out another homework :)


As always

If you do not understand something of the above, just ask here or in chat.

btw

Where is my STEM vote ? I was promised rewards :D

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

i tried yesterday the "steem" module in python but some of the functions i had to manually update as some contributions that did some bug fixes on github were not approved yet :/ but over all i think its an amazing module for making ur steem bot :DDD

how about you do the tutorial step by step, instead ?
steem-python is deprecated, that's why I use beem.

  ·  5 years ago (edited)

yes and the way beem is used seems almost identical to steem-python, but i wanted to play with the dex function which is not available in beem

edit: actually nvm i just see it uses market instead of dex... they just renamed it

All the dex functions are in the markets module now.

You will recieve STEM rewards on two of your posts, check
https://stemgeeks.net/@felixxx

I did use the above code as the basis of my post today. Yes, it was mostly copy and paste, but I did a few changes. See: https://stemgeeks.net/ulog/@mytechtrail/teaching-myself-python-day-7