steem-python for dummies #8 - Calculating post rewards

in utopian-io •  7 years ago  (edited)


Hello,

This is the 8. post of "steem-python for dummies" series. If you didn't read the older ones take your time and have a look.

  1. steem-python for dummies #1 - Introduction and Basic Operations
  2. steem-python for dummies #2 - Playing with account data
  3. steem-python for dummies #3 - Coding an upvote bot
  4. steem-python for dummies #4 - Private Memos
  5. steem-python for dummies #5 - Bundling Blockchain Operations
  6. steem-python for dummies #6 - Delegating
  7. steem-python for dummies #7 - Creating Accounts

In this post, we will learn about the "rewards". How they're calculated and shared between authors, beneficiaries (co-authors) and curators.

Post Rewards


A post's payout is calculated by the votes given. Each vote on a post has something called rshares which does actually means "Reward Shares".

This stands for determining how much percentage of steem blockchain reward fund is will be paid to the author and curators at the end of the payout period. (7 days)

To convert rshares to payout values, we have that formula.

Fund Per RewardShare = Reward Fund / Recent Claims
Payout = Reward Shares * Fund Per RewardShare / BasePrice

When you multiply Fund Per RewardShare with the vote's rshares property, you know that how much this vote will increments post's payout.

Reward Fund

Steem-Python has a call that gives information about the reward fund and recent claims.

from steem.post import Post
s = Steem()
reward_fund = s.get_reward_fund()
print(reward_fund)

This currently returns with that information.

{
    'id': 0,
    'name': 'post',
    'reward_balance': '721431.557 STEEM',
    'recent_claims': '328380833602342900',
    'last_update': '2017-12-10T18:31:00',
    'content_constant': '2000000000000',
    'percent_curation_rewards': 2500,
    'percent_content_rewards': 10000,
    'author_reward_curve': 'linear',
    'curation_reward_curve': 'square_root'
}

There is still one value is missing in the formula. That is base price. And of course, there is a method for that in steem-python.

s.get_current_median_history_price()["base"]

Let's implement this formula and get one of my post's expected payout.

s = Steem()
reward_fund = s.get_reward_fund()
reward_balance, recent_claims = reward_fund["reward_balance"], \
                                reward_fund["recent_claims"]
base_price = s.get_current_median_history_price()["base"]
def get_payout_from_rshares(rshares):
    fund_per_share = Amount(reward_balance).amount / float(recent_claims)
    payout = float(rshares) * fund_per_share * Amount(base_price).amount
    return payout
p = Post("@emrebeyler/steem-blockchain-i-anlamak-1")
total_payout = 0
for vote in p["active_votes"]:
    total_payout += get_payout_from_rshares(vote["rshares"])
print("Estimated payout for this post: $%.2f" % total_payout)

Output is:

Estimated payout for this post: $21.33

Let's double check with the steemit.

Voila!

Splitting Author, Curator rewards

However, that value is a total and includes total payout for a post. Let's find out how I share the post rewards with the curators.

In order to calculate curation rewards, I need to know when the post is created and when the curated voted for the post.

Let's implement a simple function based on these information which returns the percent of the rshares will be included in the curation rewards.

def curation_reward_pct(post_created_at, vote_created_at):
    reward = ((vote_created_at - post_created_at).seconds / 1800) * 100
    if reward > 100:
        reward = 100
    return reward

Let's put together everything and create a working function which calculating estimations on post rewards.

def get_payout_from_rshares(rshares):
    fund_per_share = Amount(reward_balance).amount / float(recent_claims)
    payout = float(rshares) * fund_per_share * Amount(base_price).amount
    return payout
def curation_reward_pct(post_created_at, vote_created_at):
    reward = ((vote_created_at - post_created_at).seconds / 1800) * 100
    if reward > 100:
        reward = 100
    return reward
def get_payouts(p):
    total_payout = 0
    curation_payout = 0
    for vote in p["active_votes"]:
        total_payout += get_payout_from_rshares(vote["rshares"])
        curation_reward_percent = curation_reward_pct(
                    p["created"], parse(vote["time"]))
        curation_payout += get_payout_from_rshares(
            float(vote["rshares"]) * curation_reward_percent / 400)
    author_payout = total_payout - curation_payout
    if p.get("beneficiaries"):
        beneficiaries_payout = sum(
            b["weight"] for b in p["beneficiaries"]) / 100
        author_payout = author_payout * (
            100 - beneficiaries_payout) / 100
    total = round(total_payout, 2)
    curation = round(curation_payout, 2)
    author = round(author_payout, 2)
    beneficiaries = round((total - curation - author), 2)
    return total, curation, author, beneficiaries
payouts = get_payouts(
    Post("@emrebeyler/steem-python-for-dummies-6-delegating"))
print("Total payout: $%s" % payouts[0])
print("Curator payout: $%s" % payouts[1])
print("Author payout: $%s" % payouts[2])
print("Beneficiaries payout: $%s" % payouts[3])

Which gives the output of:


That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts.



Posted on Utopian.io - Rewarding Open Source Contributors

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:  

Hey @emrebeyler 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. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

okay this is real helpful.. thanks my guy

Very helpful..Thanks for the share.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

WoW :0 Its gread!!! Thanks!!! :D

👍

Hi
I'm porting "Post Reward" algorithm to android. I'm using SteemJ.
My problem is that the results were 1 000 000 times bigger than showing by Steemit page.

val steemJ = SteemJ()
val blog = steemJ.getBlog(AccountName("..."), 0, 10.toShort() )
val rewardFund = steemJ.getRewardFund(RewardFundType.POST)
val rewardBalance = rewardFund.rewardBalance.amount;
val recentClaims = rewardFund.recentClaims;
val base = steemJ.currentMedianHistoryPrice.base.amount
val foundPerShare = rewardBalance.toFloat() / recentClaims.toFloat()
for (i in blog) {
    val payout = i.comment.voteRshares * foundPerShare * base

}

Thanks for the informative post @emrebeyler, but can you help me how to find payout for older posts. Using this technique reward can be calculated for newer posts(within 1 week) with good accuracy but for older posts(say 6 months ago) reward calculated is quite different from original payout reward.

Look at that birthday pos. !BEER



Hey @emrebeyler, here is a little bit of BEER from @isnochys for you. Enjoy it!