steem-python tips #5 - Calculating witness rewards

in #steem-python8 years ago (edited)

Calculating witness rewards


Every time a witness produces a block, steem blockchain rewards the witness with a producer_reward operation in the blockchain. This is a virtul operation - don't exactly bounded to a transaction, therefore it doesn't have a transaction ID.

It is possible to go through the account history and calculate how much a witness made by being a witness. :)

Some of the properties of a producer_reward operation:

{
    'producer': 'emrebeyler',
    'vesting_shares': '1980.464769 VESTS',
    'block': 20633599,
    'timestamp': '2018-03-13T07:48:27',
}

Main Flow of the script


  • Go through the account history
  • Filter "producer_reward" operations
  • Sum all rewarded VESTS and convert to Steam Power


My witness earnings so far. I am rich!

Script


from steem import Steem
from steem.account import Account
from steem.amount import Amount
from steem.converter import Converter
import logging
logger = logging.getLogger('producer rewards')
logger.setLevel(logging.INFO)
logging.basicConfig()
def calculate_producer_rewards(steemd_instance, witness_account):
    account = Account(witness_account,steemd_instance=steemd_instance)
    total_vests = 0
    for producer_reward in account.history_reverse(filter_by=["producer_reward"]):
        total_vests += Amount(producer_reward["vesting_shares"]).amount
    converter = Converter(steemd_instance=s)
    total_sp = converter.vests_to_sp(total_vests)
    return total_vests, total_sp
if __name__ == '__main__':
    s = Steem(nodes=["https://api.steemit.com"])
    witness_account = 'emrebeyler'
    vests, sp = calculate_producer_rewards(s, witness_account)
    print("Account: %s, Total Vests: %s, Total SP: %s" % (
        witness_account, round(vests, 2), round(sp, 2)))

See the script nicely formatted at Github Gists.

Notes


  • This script scans all operation history. Execution time may take forever for old witnesses.

  • Conversion to the SP is done with the current VESTS to SP rate. This is a dynamic variable and may differ from time to time.

Requirements


You need to have steem-python library installed in your local environment.

Have fun.

Sort:  

This is a very useful script for witnesses!

Cool little script. Might be useful for a dashboard or site or something.

That's actually a good idea - I will do a leaderboard for the top100 witnesses :)

İyiymiş bu sistem teşekkürler 😊

Great informative update news...thank you so much...i like it...

I don't think you want to have steam python tips in your title ;)

fixed. thanks!

You're welcome. I'm glad I could help.

informasi sangat beemanfaat, kamu telah melakukan pekerjaan yang mulia, terimakasih telah berbagi..good luck