How to calculate your remaining bandwidth using steem-python

Thumbnail

What Will I Learn?

  • Calculate bandwidth allowance inside the Steem network
  • Calculate bandwidth used
  • Calculate remaining percentage of your bandwidth

Requirements

  • Python > 3.5
  • steem-python library
  • General knowledge about the steem-python library

Difficulty

I will not explain every step, so:

  • Intermediate

Tutorial Contents

I recently joined Steem and ever since then the limiting factor of me interacting with the community and curating posts was not the voting power but the bandwidth limitation. While you could just check steemd, you could also write a short script and bypass the (currently) extremely long loading times.
How does bandwidth allocation work exactly?
Well, inside the dynamic global properties we find max_virtual_bandwidth, which is shared across all users and is divided according to their vesting shares (vesting shares translate to Steem Power).
To get the share of the bandwidth for a specific account, we simply calculate:

(max_virtual_bandwidth * (vesting_shares + received_vesting_shares) / total_vesting_shares) / 1000000

Note that because max_virtual_bandwidth is originally multiplied by 1000000 we have to divide by 1000000.

So let's start with adding our imports and defining some stuff that we'll need later on:

from steem import Steem
from datetime import datetime
s = Steem(nodes=['https://api.steemit.com'])
account = "ADD YOUR ACCOUNT NAME"
account_data = s.get_account(account)
global_data = s.get_dynamic_global_properties()

Now we get all the necessary variables list we got as a response from the API:

received_vesting_shares =float(account_data["received_vesting_shares"].replace(" VESTS", ""))
vesting_shares = float(account_data["vesting_shares"].replace(" VESTS", ""))
max_virtual_bandwidth = float(global_data["max_virtual_bandwidth"])
total_vesting_shares = float(global_data["total_vesting_shares"].replace(" VESTS", ""))

Because the information is delivered inside a string, we have to get rid of " VESTS" to convert it to a float.
Now we just have to apply the above-mentioned formula and we have our allocated bandwidth.

allocated_bandwidth = (max_virtual_bandwidth * (vesting_shares + received_vesting_shares) / total_vesting_shares)
allocated_bandwidth = round(allocated_bandwidth / 1000000)

As you can see I used round() to round the number to the nearest integer. You don't have to do it but it looks better this way IMO.
Please note that this will break the script if you have less then 0.5 steem power. If that's the case I want to remind that this can break your steam account. A bandwidth allowance of zero means you can't do anything!

Next step is to calculate the used bandwidth. Since average_bandwidth is only updated if you commit something into the blockchain, we have to calculate our used bandwidth as well.
To do this we need to get the current UTC time as well as last_bandwidth_update. last_bandwidth_update has to be parsed into the DateTime format, so we can get how much time passed since the last bandwidth update. An important piece of information is also that it takes 1 week for the bandwidth to go from 0 to 100.

total_seconds = 604800
date_bandwidth = account_data["last_bandwidth_update"]
date_bandwidth = datetime.strptime(date_bandwidth, "%Y-%m-%dT%H:%M:%S")
seconds_since_last_update = datetime.utcnow() - date_bandwidth
seconds_since_last_update = seconds_since_last_update.total_seconds()
average_bandwidth = float(account_data["average_bandwidth"])

total_seconds is the length of a week in seconds.
In the third line, we parse the string with the last bandwidth update into the DateTime format for easy calculations.
In line five, we convert the time into seconds.

Now we can do the actual calculation:

used_bandwidth = 0
if seconds_since_last_update < total_seconds:
    used_bandwidth = (((total_seconds - seconds_since_last_update) * average_bandwidth) / total_seconds)
used_bandwidth = round(new_bandwidth / 1000000)

Now we add some print statements to get everything displayed:

print("current bandwidth used: " + str(new_bandwidth))
print("current bandwidth allocated: " + str(allocated_bandwidth))
print("bandwidth percent used: " + str(100 * new_bandwidth / allocated_bandwidth))
print("bandwidth percent remaining: " + str(100 - (100 * new_bandwidth / allocated_bandwidth)))

Finished! Now we have a simple script that gives us information on the bandwidth on any account. Thanks for reading :)

Here is the link to the code snippet

Special thanks to @jfollas for his amazing article that explains how bandwidth works.



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center