create account

Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python by steempytutorials

View this thread on: hive.blogpeakd.comecency.com
· @steempytutorials · (edited)
$27.40
Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python
<center>![steem-python.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515886103/kmzfcpvtzuwhvqhgpyjp.png)</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/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorsteempytutorials
permlinkhow-to-estimate-all-rewards-in-last-n-days-using-steem-python
categoryutopian-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}}
created2018-01-25 17:00:18
last_update2018-01-26 17:45:03
depth0
children11
last_payout2018-02-01 17:00:18
cashout_time1969-12-31 23:59:59
total_payout_value21.405 HBD
curator_payout_value5.993 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,762
author_reputation31,094,047,689,691
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,247,858
net_rshares3,745,896,797,734
author_curate_reward""
vote details (32)
@gbd ·
$1.03
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
👍  
properties (23)
authorgbd
permlinkre-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t094544028z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://steemit.com/aicontest/@gbd/the-ai-contest-1-public-goods-problem"],"app":"steemit/0.1"}
created2018-01-26 09:45:45
last_update2018-01-26 09:45:45
depth1
children1
last_payout2018-02-02 09:45:45
cashout_time1969-12-31 23:59:59
total_payout_value0.771 HBD
curator_payout_value0.255 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length229
author_reputation788,699,522,692
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,428,387
net_rshares113,979,248,554
author_curate_reward""
vote details (1)
@amosbastian ·
$0.23
I will definitely check it out!
👍  
properties (23)
authoramosbastian
permlinkre-gbd-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t104713014z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-26 10:47:12
last_update2018-01-26 10:47:12
depth2
children0
last_payout2018-02-02 10:47:12
cashout_time1969-12-31 23:59:59
total_payout_value0.176 HBD
curator_payout_value0.058 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length31
author_reputation174,473,586,900,705
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,440,457
net_rshares26,675,994,342
author_curate_reward""
vote details (1)
@jestemkioskiem ·
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)**
properties (22)
authorjestemkioskiem
permlinkre-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t164507699z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-26 17:45:09
last_update2018-01-26 17:45:09
depth1
children0
last_payout2018-02-02 17:45:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation41,292,066,961,817
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,527,852
net_rshares0
@muliadi ·
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.
properties (22)
authormuliadi
permlinkre-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t080531695z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://steemit.com/@muliadi"],"app":"steemit/0.1"}
created2018-01-26 08:05:33
last_update2018-01-26 08:05:33
depth1
children0
last_payout2018-02-02 08:05:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length189
author_reputation4,383,871,806,504
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,409,871
net_rshares0
@sinbad989 ·
$0.10
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?
👍  
properties (23)
authorsinbad989
permlinkre-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t095136488z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-26 09:51:36
last_update2018-01-26 09:51:36
depth1
children5
last_payout2018-02-02 09:51:36
cashout_time1969-12-31 23:59:59
total_payout_value0.078 HBD
curator_payout_value0.024 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length203
author_reputation1,082,780,746,128
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,429,491
net_rshares11,938,247,112
author_curate_reward""
vote details (1)
@amosbastian ·
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!
properties (22)
authoramosbastian
permlinkre-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t104956674z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["juliank"],"app":"steemit/0.1"}
created2018-01-26 10:49:57
last_update2018-01-26 10:49:57
depth2
children2
last_payout2018-02-02 10:49:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length285
author_reputation174,473,586,900,705
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,441,012
net_rshares0
@sinbad989 ·
I'm using an Anaconda with python  3.5. This is an example of an error I encounter on using its functionalities. 

![steem_lib_error.PNG](https://steemitimages.com/DQmR6ngL9i8TF1P1VZ3kMsfkynZkY2Sr1Uwb9LiDiz9NFGT/steem_lib_error.PNG)

Have you encountered something like this?
properties (22)
authorsinbad989
permlinkre-amosbastian-re-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180127t095152881z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"image":["https://steemitimages.com/DQmR6ngL9i8TF1P1VZ3kMsfkynZkY2Sr1Uwb9LiDiz9NFGT/steem_lib_error.PNG"],"app":"steemit/0.1"}
created2018-01-27 09:51:57
last_update2018-01-27 09:51:57
depth3
children1
last_payout2018-02-03 09:51:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length275
author_reputation1,082,780,746,128
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,697,301
net_rshares0
@juliank ·
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
properties (22)
authorjuliank
permlinkre-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180126t143736232z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-26 14:37:36
last_update2018-01-26 14:37:36
depth2
children1
last_payout2018-02-02 14:37:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length277
author_reputation117,823,071,447,502
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,488,925
net_rshares0
@sinbad989 ·
$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.
properties (22)
authorsinbad989
permlinkre-juliank-re-sinbad989-re-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180127t094012127z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-27 09:40:15
last_update2018-01-27 09:40:15
depth3
children0
last_payout2018-02-03 09:40:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length215
author_reputation1,082,780,746,128
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,695,240
net_rshares0
@utopian-io ·
### 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>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](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**
properties (22)
authorutopian-io
permlinkre-steempytutorials-how-to-estimate-all-rewards-in-last-n-days-using-steem-python-20180127t120206757z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-27 12:02:06
last_update2018-01-27 12:02:06
depth1
children0
last_payout2018-02-03 12:02:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,514
author_reputation152,955,367,999,756
root_title"Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,721,773
net_rshares0