steem-python for dummies #8 - Calculating post rewards


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

H2
H3
H4
3 columns
2 columns
1 column
11 Comments