steem-python tips #1 - Calculating power-ups!

in #python6 years ago (edited)

Powering up


Powering up is a process on steem blockchain where you convert your liquid STEEMs to Steem Power. Every time an account powers up, it broadcast a "transfer_to_vesting" operation to the network.

@sndbox and some other organizations have weekly power-up challenges. And I was lucky to win a couple of them. Today I was curious how much I have powered up in the last month. (I did powered-up a lot.)

Filtering "transfer_to_vesting" ops with steem-python


Out script's main flow should be like this:

  • Get an input with username, start_date, and end_date
  • Filter all "transfer_to_vesting" operations between start_date and end_date
  • Sum all powered-up amount in corresponding transactions.

And we have the number.

Basic example to filter specific operations:

from steem import Steem
from steem.account import Account
s = Steem(nodes=["https://api.steemit.com"])
acc = Account('emrebeyler', steemd_instance=s)
for op in acc.history_reverse(filter_by=["transfer_to_vesting"]):
    print(op)

This script will filter all "transfer_to_vesting" operations in your entire account history and prints them.

The Calculator script


from steem import Steem
from steem.account import Account
from steem.amount import Amount
from dateutil.parser import parse
from datetime import datetime
import tableprint as tp
def calculate_powerup(username, start_date, end_date):
    s = Steem(nodes=["https://api.steemit.com"])
    account = Account(username, steemd_instance=s)
    data = []
    total = 0
    for op in account.history_reverse(filter_by=["transfer_to_vesting"]):
        ts = parse(op["timestamp"])
        if ts > end_date:
            continue
        if ts < start_date:
            break
        total += Amount(op["amount"]).amount
        data.append((op["amount"], op["timestamp"]))
    data.append(("Total: %s STEEM" % round(total, 2), "-"))
    return data, total
if __name__ == '__main__':
    data, total = calculate_powerup(
        'emrebeyler',
        datetime(2018, 2, 1),
        datetime(2018, 3, 1),
    )
    tp.table(data, ['Amount', 'Timestamp'], width=32)

Example output


Screen Shot 2018-03-01 at 14.29.50.png

Seems like I was good powering-up on February!


You can play with other accounts and timeframes by changing start_date and end_date. Make sure you have steem-python and tableprint libraries installed on your local environment.

Have fun.

Sort:  

This is awesome! Do you know if there's something similar in Steem.js? I couldn't find a way to target the power up transactions specifically, so when I made the power up calcuator for sndbox I ended up having to just make an API call for a big slice of a user's transactions and then filter out the ones that were power ups.

Actually, this is the same w/ python. It's a syntactic sugar that just filters while browsing all type of transactions.

I remember a feature request at Github asking if filtering can be done on the steem daemon itself but it's not there yet.

Ah ok, yeah I found a feature request for Steem.js about that feature too. Super cool what you did here though! I keep getting more curious about Python :P

That's my pythonman 😎

Grazie, Zorto.

woah nice one

did you start learning python?

Yeah I started you are my idol thanks for sharing

you put out some awesome stuff man. thanks for everything you do! :)

Whoa, creativity.

Yeah nice guys

This post has received gratitude of 5.80% from @appreciator courtesy of @emrebeyler!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.029
BTC 67584.80
ETH 3438.61
USDT 1.00
SBD 2.70