<center></center> This tutorial is part of a series where we explain different aspects of programming with `steem-python`. Links to the other tutorials can be found in the curriculum section below. In part 9 of this series we learned how to calculate the total rewards of a post and in part 12 we learned how to calculate curation rewards of a post. In this tutorial we will learn how to put this all together and estimate all rewards of posts made by a specific account in the last `N` days! --- #### What will I learn - How to calculate beneficiary shares - How to calculate author rewards and beneficiary rewards - How to calculate rewards in the last `N` days #### Requirements - Python3.6 - `steem-python` #### Difficulty - Intermediate --- ### Tutorial If you find it easier to follow along this way you can get the entire code for this tutorial from [here](https://github.com/amosbastian/steempy-tutorials/blob/master/part_14/all_rewards.py), where I have split everything up into the relevant sections. #### Calculating beneficiary shares Something most people might not have realised is that Steemit has a thing called beneficiaries, where you can specify others that will get a part of your author rewards. For example, when posting via Utopian.io the utopian.pay account is set as a beneficiary and receives 25% percent of your author rewards. This looks like this ``` "beneficiaries": [ { "account": "utopian.pay", "weight": 2500 } ] ``` We need to use this information to get the percentage of our author rewards that goes to our benificiaries! To do this we can simply sum the weight of each beneficiary and divide by `10000`, like so ``` def beneficiaries_pct(post): weight = sum([beneficiary["weight"] for beneficiary in post["beneficiaries"]]) return weight / 10000.0 ``` which in this case would give `0.25`! #### Calculating author and beneficiary rewards Now that we can calculate the percentage of the author rewards that goes to beneficiaries we can easily calculate the author rewards. In part 9 we already calculated the total reward of a post, so to get the author reward we need to do the following: 1. Subtract the curation reward calculated in part 12 2. Multiply this by (1.0 - beneficiaries' percentage) To do this we can use the code from part 9 and part 12 ``` # Everything we need to calculate the reward reward_fund = steem.get_reward_fund() reward_balance = Amount(reward_fund["reward_balance"]).amount recent_claims = float(reward_fund["recent_claims"]) reward_share = reward_balance / recent_claims base = Amount(steem.get_current_median_history_price()["base"]).amount votes = [vote for vote in post["active_votes"]] ### Curation reward penalty def curation_penalty(post, vote): post_time = post["created"] vote_time = parse(vote["time"]) time_elapsed = vote_time - post_time reward = time_elapsed / timedelta(minutes=30) * 1.0 if reward > 1.0: reward = 1.0 return reward ### Calculating curation reward per vote curation_pct = 0.25 def curation_reward(post, vote): rshares = float(vote["rshares"]) base_share = reward_share * base return (rshares * curation_penalty(post, vote) * curation_pct) * base_share ``` and create a new `estimate_rewards(post)` function that looks like this ``` ### Adding everything together def estimate_rewards(post): votes = [vote for vote in post["active_votes"]] total_share = sum([float(vote["rshares"]) * reward_share * base for vote in votes]) curation_share = sum([curation_reward(post, vote) for vote in votes]) author_share = (total_share - curation_share) * (1.0 - beneficiaries_pct(post)) beneficiary_share = (total_share - curation_share) * beneficiaries_pct(post) print(f"Estimated total reward for this post is ${total_share:.2f}") print(f"Estimated author reward for this post is ${author_share:.2f}") print(f"Estimated beneficiary reward for this post is ${beneficiary_share:.2f}") print(f"Estimated curation reward for this post is ${curation_share:.2f}\n") ``` which outputs the following ``` Estimated total reward for this post is $29.46 Estimated author reward for this post is $17.22 Estimated beneficiary reward for this post is $5.74 Estimated curation reward for this post is $6.51 ``` #### Estimating rewards in last `N` days Alright, so now we can calculate every single reward for a post, we can use this to calculate this for every post in the last `N` days. To do this we need to do a few things; we need to set this time period somehow and iterate over all posts in our account's history. Setting the time period is pretty simple and can be done using `timedelta()`. If we want to set this period to three days, then we could do the following ``` time_period = datetime.timedelta(days=3) ``` Iterating over all posts in an account's history can be done just like streaming the blockchain by using the `history_reverse()`. As an example I will use the `estimate_rewards()` for each post @steempytutorials made in the last three days ``` for post in Account(account).history_reverse(filter_by="comment"): try: post = Post(post) # Check if post isn't too old if post.time_elapsed() < time_period: # Make sure post hasn't already been seen if post.is_main_post() and not post["title"] in posts: posts.add(post["title"]) print(post["title"]) estimate_rewards(post) else: break except Exception as error: continue ``` where we add each post's title to a set (an unordered collection with no duplicate elements), because when someone edits a post it counts as its own separate post, and we don't want to estimate rewards for the same post multiple times. Running this outputs the following at the time of writing this post ``` part 12: Upvote Posts In Batches Based On Current Voting Power With Steem-Python Estimated total reward for this post is $23.18 Estimated author reward for this post is $13.67 Estimated beneficiary reward for this post is $4.56 Estimated curation reward for this post is $4.95 Daily Steem-Python Challenge #12, Win 1 Steem! Estimated total reward for this post is $3.78 Estimated author reward for this post is $3.70 Estimated beneficiary reward for this post is $0.00 Estimated curation reward for this post is $0.08 Part 12: How To Estimate Curation Rewards Using Steem-Python Estimated total reward for this post is $29.43 Estimated author reward for this post is $17.19 Estimated beneficiary reward for this post is $5.73 Estimated curation reward for this post is $6.50 ``` **Congratulations**, you've now learned how to estimate each type of reward of a post using `steem-python`. You have also learned how to iterate over an account's post history and print each post's estimated rewards! #### Curriculum - [Part 9: How To Calculate A Post's Total Rewards Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/how-to-calculate-a-post-s-total-rewards-using-steem-python) - [Part 12: How To Estimate Curation Rewards Using Steem-Python](https://utopian.io/@steempytutorials/part-12-how-to-estimate-curation-rewards) --- The code for this tutorial can be found on [GitHub](https://github.com/amosbastian/steempy-tutorials/blob/master/part_14/all_rewards.py)! This tutorial was written by @amosbastian in conjunction with @juliank. <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@steempytutorials/how-to-estimate-all-rewards-in-last-n-days-using-steem-python">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>
author | steempytutorials | ||||||
---|---|---|---|---|---|---|---|
permlink | how-to-estimate-all-rewards-in-last-n-days-using-steem-python | ||||||
category | utopian-io | ||||||
json_metadata | {"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":84843862,"name":"steem-python","full_name":"steemit/steem-python","html_url":"https://github.com/steemit/steem-python","fork":false,"owner":{"login":"steemit"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","steemdev","programming","python","tutorial"],"users":["steempytutorials","amosbastian","juliank"],"links":["https://github.com/amosbastian/steempy-tutorials/blob/master/part_14/all_rewards.py","https://utopian.io/utopian-io/@steempytutorials/how-to-calculate-a-post-s-total-rewards-using-steem-python","https://utopian.io/@steempytutorials/part-12-how-to-estimate-curation-rewards","https://utopian.io/utopian-io/@steempytutorials/how-to-estimate-all-rewards-in-last-n-days-using-steem-python"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515886103/kmzfcpvtzuwhvqhgpyjp.png"],"moderator":{"account":"jestemkioskiem","time":"2018-01-26T17:45:02.625Z","reviewed":true,"pending":false,"flagged":false}} | ||||||
created | 2018-01-25 17:00:18 | ||||||
last_update | 2018-01-26 17:45:03 | ||||||
depth | 0 | ||||||
children | 11 | ||||||
last_payout | 2018-02-01 17:00:18 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 21.405 HBD | ||||||
curator_payout_value | 5.993 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 7,762 | ||||||
author_reputation | 31,094,047,689,691 | ||||||
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 32,247,858 | ||||||
net_rshares | 3,745,896,797,734 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
teamsteem | 0 | 170,522,902,785 | 1% | ||
abh12345 | 0 | 45,559,973,858 | 5% | ||
remlaps1 | 0 | 28,462,075,554 | 100% | ||
decebal2dac | 0 | 4,721,775,753 | 100% | ||
gutzofter | 0 | 34,522,030,466 | 100% | ||
lisa.palmer | 0 | 1,828,040,344 | 100% | ||
juliank | 0 | 346,116,754,304 | 30% | ||
muliadi | 0 | 355,819,075 | 100% | ||
turymenecier | 0 | 52,354,512 | 30% | ||
dogancankilment | 0 | 2,947,074,575 | 100% | ||
bogaev | 0 | 408,645,017 | 100% | ||
coolguy123 | 0 | 18,545,394,355 | 7% | ||
utopian-io | 0 | 3,059,072,814,426 | 1.7% | ||
amosbastian | 0 | 14,025,748,863 | 100% | ||
eventta | 0 | 614,619,793 | 100% | ||
francian | 0 | 449,183,600 | 100% | ||
samve | 0 | 478,583,504 | 100% | ||
veenox | 0 | 156,156,931 | 100% | ||
oups | 0 | 1,204,109,034 | 100% | ||
gbd | 0 | 7,272,107,942 | 6% | ||
fisherck | 0 | 1,206,557,328 | 100% | ||
theloresiobhan | 0 | 451,358,454 | 100% | ||
steempytutorials | 0 | 3,248,098,726 | 100% | ||
luj1 | 0 | 568,464,247 | 100% | ||
percetakanhca | 0 | 506,940,685 | 100% | ||
rubik66 | 0 | 614,473,123 | 100% | ||
muselew | 0 | 368,683,839 | 100% | ||
findo1 | 0 | 580,676,805 | 100% | ||
aris2404 | 0 | 476,213,481 | 100% | ||
sadiyalover | 0 | 559,166,355 | 100% | ||
kabirlec9 | 0 | 0 | 100% | ||
prasannab | 0 | 0 | 100% |
I shamelessly plug here a little promotion for my [python-based ongoing contest](https://steemit.com/aicontest/@gbd/the-ai-contest-1-public-goods-problem) for you Python lovers. Best bot to play the Public Goods game wins the pot
author | gbd |
---|---|
permlink | re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t094544028z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"links":["https://steemit.com/aicontest/@gbd/the-ai-contest-1-public-goods-problem"],"app":"steemit/0.1"} |
created | 2018-01-26 09:45:45 |
last_update | 2018-01-26 09:45:45 |
depth | 1 |
children | 1 |
last_payout | 2018-02-02 09:45:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.771 HBD |
curator_payout_value | 0.255 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 229 |
author_reputation | 788,699,522,692 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,428,387 |
net_rshares | 113,979,248,554 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
gbd | 0 | 113,979,248,554 | 100% |
I will definitely check it out!
author | amosbastian |
---|---|
permlink | re-gbd-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t104713014z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-26 10:47:12 |
last_update | 2018-01-26 10:47:12 |
depth | 2 |
children | 0 |
last_payout | 2018-02-02 10:47:12 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.176 HBD |
curator_payout_value | 0.058 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 31 |
author_reputation | 174,473,586,900,705 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,440,457 |
net_rshares | 26,675,994,342 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
gbd | 0 | 26,675,994,342 | 23% |
Thank you for the contribution. It has been approved. You can contact us on [Discord](https://discord.gg/uTyJkNm). **[[utopian-moderator]](https://utopian.io/moderators)**
author | jestemkioskiem |
---|---|
permlink | re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t164507699z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-26 17:45:09 |
last_update | 2018-01-26 17:45:09 |
depth | 1 |
children | 0 |
last_payout | 2018-02-02 17:45:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 172 |
author_reputation | 41,292,066,961,817 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,527,852 |
net_rshares | 0 |
good post, I like your post .. I need your support please visit my blog https://steemit.com/@muliadi if you like my post please give upvote, resteem &follow me. thank you, keep on steemit.
author | muliadi |
---|---|
permlink | re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t080531695z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"links":["https://steemit.com/@muliadi"],"app":"steemit/0.1"} |
created | 2018-01-26 08:05:33 |
last_update | 2018-01-26 08:05:33 |
depth | 1 |
children | 0 |
last_payout | 2018-02-02 08:05:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 189 |
author_reputation | 4,383,871,806,504 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,409,871 |
net_rshares | 0 |
What OS are you using for this steem-python library? I tried it on my Windows 10 laptop but it doesn't work. Is steem-python limited to Linux OS? or is there something I'm missing in my installation?
author | sinbad989 |
---|---|
permlink | re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t095136488z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-26 09:51:36 |
last_update | 2018-01-26 09:51:36 |
depth | 1 |
children | 5 |
last_payout | 2018-02-02 09:51:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.078 HBD |
curator_payout_value | 0.024 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 203 |
author_reputation | 1,082,780,746,128 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,429,491 |
net_rshares | 11,938,247,112 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
juliank | 0 | 11,938,247,112 | 1% |
Personally I'm using Ubuntu and I think @juliank uses a Mac. You need Python3.6 to install `steem-python`, but you also need `scrypt`, which is only available on Windows for Python3.5, so that's a really annoying problem... I will see if I can figure something out and get back to you!
author | amosbastian |
---|---|
permlink | re-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t104956674z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"users":["juliank"],"app":"steemit/0.1"} |
created | 2018-01-26 10:49:57 |
last_update | 2018-01-26 10:49:57 |
depth | 2 |
children | 2 |
last_payout | 2018-02-02 10:49:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 285 |
author_reputation | 174,473,586,900,705 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,441,012 |
net_rshares | 0 |
I'm using an Anaconda with python 3.5. This is an example of an error I encounter on using its functionalities.  Have you encountered something like this?
author | sinbad989 |
---|---|
permlink | re-amosbastian-re-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180127t095152881z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"image":["https://steemitimages.com/DQmR6ngL9i8TF1P1VZ3kMsfkynZkY2Sr1Uwb9LiDiz9NFGT/steem_lib_error.PNG"],"app":"steemit/0.1"} |
created | 2018-01-27 09:51:57 |
last_update | 2018-01-27 09:51:57 |
depth | 3 |
children | 1 |
last_payout | 2018-02-03 09:51:57 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 275 |
author_reputation | 1,082,780,746,128 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,697,301 |
net_rshares | 0 |
I use a mac os indeed for testing, but I run all the script from a server. You can get yourself a droplet at digital ocean for $5 a month. You can then mount the droplet drive this to your own pc and basically work on your own pc and run the files via a terminal on the droplet
author | juliank |
---|---|
permlink | re-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t143736232z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-26 14:37:36 |
last_update | 2018-01-26 14:37:36 |
depth | 2 |
children | 1 |
last_payout | 2018-02-02 14:37:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 277 |
author_reputation | 117,823,071,447,502 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,488,925 |
net_rshares | 0 |
$5 is expensive for a beginner like me. My goal for now is to learn how to use steem python library. Thanks for your suggestion. I'll definitely try digital ocean when I have enough knowledge for automated script.
author | sinbad989 |
---|---|
permlink | re-juliank-re-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180127t094012127z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-27 09:40:15 |
last_update | 2018-01-27 09:40:15 |
depth | 3 |
children | 0 |
last_payout | 2018-02-03 09:40:15 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 215 |
author_reputation | 1,082,780,746,128 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,695,240 |
net_rshares | 0 |
### Hey @steempytutorials I am @utopian-io. I have just upvoted you! #### Achievements - You have less than 500 followers. Just gave you a gift to help you succeed! - Seems like you contribute quite often. AMAZING! #### Suggestions - Contribute more often to get higher and higher rewards. I wish to see you often! - Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck! #### Get Noticed! - Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions! #### Community-Driven Witness! I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER! - <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a> - <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a> - Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a> [](https://steemit.com/~witnesses) **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**
author | utopian-io |
---|---|
permlink | re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180127t120206757z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-27 12:02:06 |
last_update | 2018-01-27 12:02:06 |
depth | 1 |
children | 0 |
last_payout | 2018-02-03 12:02:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,514 |
author_reputation | 152,955,367,999,756 |
root_title | "Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 32,721,773 |
net_rshares | 0 |