Skip to content

Instantly share code, notes, and snippets.

@bruce-forks
Created February 6, 2018 03:35
Show Gist options
  • Save bruce-forks/6bbf9279b9d1e4bb1a03e493f853338f to your computer and use it in GitHub Desktop.
Save bruce-forks/6bbf9279b9d1e4bb1a03e493f853338f to your computer and use it in GitHub Desktop.
My solution to the steem-python challenge #20
from steem import Steem
from steem.account import Account
from steem.post import Post
from steem.amount import Amount
from dateutil.parser import parse
from datetime import datetime, timedelta
steem = Steem()
account = "steempytutorials"
def beneficiaries_pct(post):
weight = sum([beneficiary["weight"] for beneficiary in post["beneficiaries"]])
return weight / 10000.0
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 round(reward, 2)
curation_pct = 0.25
def curation_reward(post, vote, rshare, base):
rshares = float(vote["rshares"])
base_share = rshare * base
return (rshares * curation_penalty(post, vote) * curation_pct) * base_share
def get_author_rewards(post):
total_payout = Amount(post["total_payout_value"]).amount
# We are still in the voting period here-use estimation code from #14
if total_payout == 0:
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"]]
total_share = sum([float(vote["rshares"]) * reward_share * base for vote in votes])
curation_share = sum([curation_reward(post, vote, reward_share, base) for vote in votes])
total_payout = (total_share - curation_share) * (1.0 - beneficiaries_pct(post))
return total_payout
# Get days since account creation
created = parse(Account(account)["created"]).date()
today = datetime.today().date()
days = today - created
# Create dictionary
dates = {}
for day in range(days.days + 1):
dates[str(created + timedelta(days=day))] = 0
# Iterate over all blog posts
post_limit = 0
while post_limit < Account(account)["post_count"]:
for post in steem.get_blog(account, post_limit, 500):
post = Post(post["comment"])
if post.is_main_post() and post["author"] == account:
post_date = str(post["created"].date())
payout = get_author_rewards(post)
dates[post_date] += payout
post_limit += 500
# Getting x and y values
x = [datetime.strptime(date, "%Y-%m-%d").date() for date in dates.keys()]
y = [sum(list(dates.values())[0:i]) for i in range(len(dates.values()))]
def first_reward():
for i, reward in enumerate(y):
if reward > 0:
return i
# Plotting the graph
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.xticks([x[0], x[first_reward() - 1], x[-1]], visible=True,
rotation="horizontal")
plt.plot(x, y)
plt.xlabel("{} to {} for @{}".format(created, today, account))
plt.ylabel("Sum of post rewards generated")
plt.grid()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment