create account

Part 9: How To Calculate A Post's Total Rewards Using Steem-Python by steempytutorials

View this thread on: hive.blogpeakd.comecency.com
· @steempytutorials · (edited)
$38.88
Part 9: How To Calculate A Post's Total Rewards 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. Today we will learn how to calculate the rewards of a post!

---

#### What will I learn

- What the reward fund looks like
- How to calculate a post's reward share
- How to calculate a posts actual reward

#### Requirements

- Python3.6
- `steem-python`

#### Difficulty

- Basic

---

### Tutorial
#### The reward fund
The way rewards are distributed on Steemit is by calculating the total reward shares of a post and using this to calculate how much a post should be rewarded. To find out all this information `steem-python` provides a very handy function called `get_reward_fund()` which gives us the following information

```
{
  "id": 0,
  "name": "post",
  "reward_balance": "717950.323 STEEM",
  "recent_claims": "373489696650140999",
  "last_update": "2018-01-20T15:33:27",
  "content_constant": "2000000000000",
  "percent_curation_rewards": 2500,
  "percent_content_rewards": 10000,
  "author_reward_curve": "linear",
  "curation_reward_curve": "square_root"
}
```

were we can see some useful information. Here `reward_balance` is the total reward pool and `recent_claims` is the amount of recent claims. We can use this to calculate how much one share of the reward balance is worth, like so

```
from steem import Steem
from steem.amount import Amount

steem = Steem()
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
print(f"One reward share is currently worth: {reward_share}")
```

where we use `Amount` to automatically convert `707222.264 STEEM` to an actual number. This code outputs the following:

```
One reward share is currently worth: 1.922662039408618e-12
```
This currently doesn't mean that much to us, but in the next section it will become clear what we can use this for.

#### Calculating reward shares of a post
To calculate the total reward shares of a post we need to look at all the total reward shares of the votes on that post. You should recall that in [part 2](https://utopian.io/utopian-io/@steempytutorials/part-2-how-to-stream-and-filter-the-blockchain-using-steem-python) of this tutorial series we looked at what a post actually looks like. A post contains a list called `active_votes` with all the currently active votes on it, which all look something like this

```
{
    "voter": "juliank",
    "weight": 203149,
    "rshares": "130300902876",
    "percent": 1000,
    "reputation": "65384949253746",
    "time": "2018-01-12T17:27:42"
}
```
As you can see each vote in `active_votes` has an attribute called "rshares". We can use this to calculate the total reward shares, or `rshares`, of a post with the following code

```
post = Post("@steempytutorials/part-8-how-to-create-your-own-upvote-bot-using-steem-python")

total_rshares = 0
for vote in post["active_votes"]:
    total_rshares += float(vote["rshares"])

print(f"The total reward shares of this post are: {total_rshares:.2f}")
```

which currently gives the following output

```
The total reward shares of this post are: 1734177616928.00
```

#### Calculating the actual reward of a post
Now we have the total reward shares of a post we should be able to simply multiply it with how much one reward share is worth, right? Let's see what this gives us

```
print(f"The post is worth ${total_rshares * reward_share:.2f}")
```
which outputs

```
The post is worth $3.33
```

Wait a minute... that doesn't seem right. Checking the post on Steemit shows a completely different value, so what's going wrong? The problem is that we still need to multiply this with the average STEEM/SBD price to get the actual value of the post. Thankfully we can find out this price by using the `get_current_median_history_price()` function. This currently returns something like this:

```
{
  "base": "4.323 SBD",
  "quote": "1.000 STEEM"
}
```
The base price is what we need! We can simply take this and then multiply it with our total reward shares to get the actual reward of the post

```
base = Amount(steem.get_current_median_history_price()["base"]).amount
print(f"The post is actually worth ${total_rshares * reward_share * base:.2f}")
```
which outputs

```
The post is actually worth $14.42
```

Indeed, going to the post on Steemit shows that this is (roughly) the correct reward! **Congratulations**, you now know how to calculate the rewards of a post using `steem-python`!

<center>![total_rewards.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516462339/vbh9oswhsb2bir5ytkbt.png)
</center>

#### Curriculum
- [Part 0: How To Install Steem-python, The Official Steem Library For Python](https://utopian.io/utopian-io/@amosbastian/how-to-install-steem-python-the-official-steem-library-for-python)
- [Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python)
- [Part 2: How To Stream And Filter The Blockchain Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-2-how-to-stream-and-filter-the-blockchain-using-steem-python)
- [Part 3: Creating A Dynamic Autovoter That Runs 24/7](https://steemit.com/utopian-io/@steempytutorials/part-3-creating-a-dynamic-upvote-bot-that-runs-24-7-first-weekly-challenge-3-steem-prize-pool)
- [Part 4: How To Follow A Voting Trail Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-4-how-to-follow-a-voting-trail-using-steem-python)
- [Part 5: Post An Article Directly To The Steem Blockchain And Automatically Buy Upvotes From Upvote Bots](https://utopian.io/utopian-io/@steempytutorials/part-5-post-an-article-directly-to-the-steem-blockchain-and-automatically-buy-upvotes-from-upvote-bots)
- [Part 6: How To Automatically Reply To Mentions Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-6-how-to-automatically-reply-to-mentions-using-steem-python)
- [Part 7: How To Schedule Posts And Manually Upvote Posts For A Variable Voting Weight With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-7-how-to-schedule-posts-and-manually-upvote-posts-for-a-variable-voting-weight-with-steem-python)
- [Part 8: How To Create Your Own Upvote Bot Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-8-how-to-create-your-own-upvote-bot-using-steem-python)

---

The code for this tutorial can be found on [GitHub](https://github.com/amosbastian/steempy-tutorials/blob/master/part_10/post_reward.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-calculate-a-post-s-total-rewards-using-steem-python">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorsteempytutorials
permlinkhow-to-calculate-a-post-s-total-rewards-using-steem-python
categoryutopian-io
json_metadata{"community":"utopian","app":"steemit/0.1","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":["amosbastian","juliank"],"links":["https://utopian.io/utopian-io/@steempytutorials/part-2-how-to-stream-and-filter-the-blockchain-using-steem-python","https://utopian.io/utopian-io/@amosbastian/how-to-install-steem-python-the-official-steem-library-for-python","https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python","https://steemit.com/utopian-io/@steempytutorials/part-3-creating-a-dynamic-upvote-bot-that-runs-24-7-first-weekly-challenge-3-steem-prize-pool","https://utopian.io/utopian-io/@steempytutorials/part-4-how-to-follow-a-voting-trail-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-5-post-an-article-directly-to-the-steem-blockchain-and-automatically-buy-upvotes-from-upvote-bots","https://utopian.io/utopian-io/@steempytutorials/part-6-how-to-automatically-reply-to-mentions-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-7-how-to-schedule-posts-and-manually-upvote-posts-for-a-variable-voting-weight-with-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-8-how-to-create-your-own-upvote-bot-using-steem-python","https://github.com/amosbastian/steempy-tutorials/blob/master/part_10/post_reward.py","https://utopian.io/utopian-io/@steempytutorials/how-to-calculate-a-post-s-total-rewards-using-steem-python"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515886103/kmzfcpvtzuwhvqhgpyjp.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516462339/vbh9oswhsb2bir5ytkbt.png"],"moderator":{"account":"deathwing","time":"2018-01-21T01:07:57.185Z","reviewed":true,"pending":false,"flagged":false}}
created2018-01-20 17:00:39
last_update2018-01-23 12:17:48
depth0
children7
last_payout2018-01-27 17:00:39
cashout_time1969-12-31 23:59:59
total_payout_value30.428 HBD
curator_payout_value8.454 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,146
author_reputation31,094,047,689,691
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,906,925
net_rshares4,294,586,905,102
author_curate_reward""
vote details (25)
@deathwing ·
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)
authordeathwing
permlinkre-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180121t010805238z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-21 01:08:03
last_update2018-01-21 01:08:03
depth1
children0
last_payout2018-01-28 01:08:03
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_reputation269,077,595,754,009
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,988,471
net_rshares0
@tatchcapital ·
This is great stuff, so hard to find good explicative content ! Please keep it up !
👍  ,
properties (23)
authortatchcapital
permlinkre-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t210137731z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-20 21:01:36
last_update2018-01-20 21:01:36
depth1
children0
last_payout2018-01-27 21:01: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_length83
author_reputation327,234,531,510
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,951,329
net_rshares674,566,519
author_curate_reward""
vote details (2)
@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-calculate-a-post-s-total-rewards-using-steem-python-20180121t232639318z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-21 23:26:39
last_update2018-01-21 23:26:39
depth1
children0
last_payout2018-01-28 23:26:39
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 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id31,224,973
net_rshares0
@veleje · (edited)
$0.39
Probably silly question, from someone new to Python, how come we can't use the same code for 'recent_claims' that we used for 'reward_balance' - code in second line:

![0019_py_question_reward_fund.png](https://steemitimages.com/DQmTK9z8AL4V4FD3SaDABV9YsFUjRukJwa3oD8FBSy7EAQX/0019_py_question_reward_fund.png)
👍  , ,
properties (23)
authorveleje
permlinkre-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t194505785z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"image":["https://steemitimages.com/DQmTK9z8AL4V4FD3SaDABV9YsFUjRukJwa3oD8FBSy7EAQX/0019_py_question_reward_fund.png"],"app":"steemit/0.1"}
created2018-01-20 19:45:06
last_update2018-01-20 19:46:06
depth1
children3
last_payout2018-01-27 19:45:06
cashout_time1969-12-31 23:59:59
total_payout_value0.342 HBD
curator_payout_value0.048 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length310
author_reputation490,220,361,761
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,938,577
net_rshares34,537,680,620
author_curate_reward""
vote details (3)
@amosbastian · (edited)
$0.18
It's because `Amount` is a class in `steem-python` that splits a string like "10 SBD" into `amount`(10) and `asset` (SBD) as you can see [here](https://github.com/steemit/steem-python/blob/master/steem/amount.py).  The `float()` function just converts a number or a string into a float! If you really wanted you could do `recent_claims = float(reward_fund["reward_balance"].split()[0])` instead, but it's easier to just use `Amount` class.
👍  ,
properties (23)
authoramosbastian
permlinkre-veleje-re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t201201375z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://github.com/steemit/steem-python/blob/master/steem/amount.py"],"app":"steemit/0.1"}
created2018-01-20 20:12:00
last_update2018-01-20 20:13:18
depth2
children1
last_payout2018-01-27 20:12:00
cashout_time1969-12-31 23:59:59
total_payout_value0.144 HBD
curator_payout_value0.036 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length439
author_reputation174,473,586,900,705
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,943,324
net_rshares16,240,485,077
author_curate_reward""
vote details (2)
@veleje ·
I see, thank you!
This example here shows better what is going on, nice, now I have two options!
properties (22)
authorveleje
permlinkre-amosbastian-re-veleje-re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t205125482z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-01-20 20:51:24
last_update2018-01-20 20:51:24
depth3
children0
last_payout2018-01-27 20:51:24
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_length96
author_reputation490,220,361,761
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,949,765
net_rshares0
@juliank ·
$0.10
There are no silly questions! I will let @amosbastian tackle this one though as he wrote this tutorial
👍  
properties (23)
authorjuliank
permlinkre-veleje-re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t195608098z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["amosbastian"],"app":"steemit/0.1"}
created2018-01-20 19:56:09
last_update2018-01-20 19:56:09
depth2
children0
last_payout2018-01-27 19:56:09
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_length102
author_reputation117,823,071,447,502
root_title"Part 9: How To Calculate A Post's Total Rewards Using Steem-Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id30,940,564
net_rshares9,410,934,245
author_curate_reward""
vote details (1)