<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. 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> </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/>
author | steempytutorials | ||||||
---|---|---|---|---|---|---|---|
permlink | how-to-calculate-a-post-s-total-rewards-using-steem-python | ||||||
category | utopian-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}} | ||||||
created | 2018-01-20 17:00:39 | ||||||
last_update | 2018-01-23 12:17:48 | ||||||
depth | 0 | ||||||
children | 7 | ||||||
last_payout | 2018-01-27 17:00:39 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 30.428 HBD | ||||||
curator_payout_value | 8.454 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 7,146 | ||||||
author_reputation | 31,094,047,689,691 | ||||||
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 30,906,925 | ||||||
net_rshares | 4,294,586,905,102 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
tatchcapital | 0 | 426,498,786 | 100% | ||
nicnas | 0 | 12,519,841,143 | 30% | ||
juliank | 0 | 392,772,262,986 | 30% | ||
sammie85 | 0 | 401,549,428 | 100% | ||
manoloeldelbombo | 0 | 18,357,218,846 | 100% | ||
veleje | 0 | 9,743,084,866 | 100% | ||
alesyacorni | 0 | 50,469,497 | 100% | ||
belyacki | 0 | 52,361,314 | 100% | ||
bobdos | 0 | 3,412,264,884 | 5% | ||
utopian-io | 0 | 3,829,107,260,152 | 2.36% | ||
travoved | 0 | 6,141,864,410 | 100% | ||
needavote | 0 | 61,421,583 | 20% | ||
steemitstats | 0 | 2,054,834,622 | 5% | ||
pinay | 0 | 1,887,418,797 | 100% | ||
maxg | 0 | 813,871,189 | 100% | ||
amosbastian | 0 | 6,783,864,879 | 100% | ||
bestsmiles | 0 | 120,409,820 | 100% | ||
steempytutorials | 0 | 2,659,485,632 | 100% | ||
chelsea.bear | 0 | 2,078,848,971 | 5% | ||
johangericke | 0 | 1,367,265,507 | 100% | ||
astur | 0 | 301,105,000 | 100% | ||
claraquarius | 0 | 2,078,846,657 | 5% | ||
denispenyok | 0 | 439,349,003 | 100% | ||
luj1 | 0 | 510,013,478 | 100% | ||
yz0rgc0rpb | 0 | 445,493,652 | 100% |
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 | deathwing |
---|---|
permlink | re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180121t010805238z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-21 01:08:03 |
last_update | 2018-01-21 01:08:03 |
depth | 1 |
children | 0 |
last_payout | 2018-01-28 01:08:03 |
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 | 269,077,595,754,009 |
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 30,988,471 |
net_rshares | 0 |
This is great stuff, so hard to find good explicative content ! Please keep it up !
author | tatchcapital |
---|---|
permlink | re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t210137731z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-20 21:01:36 |
last_update | 2018-01-20 21:01:36 |
depth | 1 |
children | 0 |
last_payout | 2018-01-27 21:01: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 | 83 |
author_reputation | 327,234,531,510 |
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 30,951,329 |
net_rshares | 674,566,519 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
iseecure | 0 | 254,160,572 | 100% | ||
tatchcapital | 0 | 420,405,947 | 100% |
### 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-calculate-a-post-s-total-rewards-using-steem-python-20180121t232639318z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-01-21 23:26:39 |
last_update | 2018-01-21 23:26:39 |
depth | 1 |
children | 0 |
last_payout | 2018-01-28 23:26:39 |
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 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 31,224,973 |
net_rshares | 0 |
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: 
author | veleje |
---|---|
permlink | re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t194505785z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"image":["https://steemitimages.com/DQmTK9z8AL4V4FD3SaDABV9YsFUjRukJwa3oD8FBSy7EAQX/0019_py_question_reward_fund.png"],"app":"steemit/0.1"} |
created | 2018-01-20 19:45:06 |
last_update | 2018-01-20 19:46:06 |
depth | 1 |
children | 3 |
last_payout | 2018-01-27 19:45:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.342 HBD |
curator_payout_value | 0.048 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 310 |
author_reputation | 490,220,361,761 |
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 30,938,577 |
net_rshares | 34,537,680,620 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
juliank | 0 | 13,781,728,984 | 1% | ||
veleje | 0 | 10,800,567,088 | 100% | ||
amikphoto | 0 | 9,955,384,548 | 100% |
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.
author | amosbastian |
---|---|
permlink | re-veleje-re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t201201375z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"links":["https://github.com/steemit/steem-python/blob/master/steem/amount.py"],"app":"steemit/0.1"} |
created | 2018-01-20 20:12:00 |
last_update | 2018-01-20 20:13:18 |
depth | 2 |
children | 1 |
last_payout | 2018-01-27 20:12:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.144 HBD |
curator_payout_value | 0.036 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 439 |
author_reputation | 174,473,586,900,705 |
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 30,943,324 |
net_rshares | 16,240,485,077 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
veleje | 0 | 9,632,367,992 | 100% | ||
amosbastian | 0 | 6,608,117,085 | 100% |
I see, thank you! This example here shows better what is going on, nice, now I have two options!
author | veleje |
---|---|
permlink | re-amosbastian-re-veleje-re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t205125482z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"app":"steemit/0.1"} |
created | 2018-01-20 20:51:24 |
last_update | 2018-01-20 20:51:24 |
depth | 3 |
children | 0 |
last_payout | 2018-01-27 20:51:24 |
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 | 96 |
author_reputation | 490,220,361,761 |
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 30,949,765 |
net_rshares | 0 |
There are no silly questions! I will let @amosbastian tackle this one though as he wrote this tutorial
author | juliank |
---|---|
permlink | re-veleje-re-steempytutorials-how-to-calculate-a-post-s-total-rewards-using-steem-python-20180120t195608098z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"users":["amosbastian"],"app":"steemit/0.1"} |
created | 2018-01-20 19:56:09 |
last_update | 2018-01-20 19:56:09 |
depth | 2 |
children | 0 |
last_payout | 2018-01-27 19:56:09 |
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 | 102 |
author_reputation | 117,823,071,447,502 |
root_title | "Part 9: How To Calculate A Post's Total Rewards Using Steem-Python" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 30,940,564 |
net_rshares | 9,410,934,245 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
veleje | 0 | 9,410,934,245 | 100% |