How to calculate your remaining bandwidth using steem-python
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
The contribution was reviewed again as per your request and I feel you have a point .
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @shreyasgune, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Hey @bloodviolet I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.
As per rule:
End-user focused tutorials must address a minimum of three substantial concepts that are unique to the Open Source project and essential learning requirements for end-users. Preference is given to tutorials that are part of a curriculum (series) of tutorials all of which are sequential and built on previously learned skills and knowledge.
You can contact us on Discord.
[utopian-moderator]
With the current influx of new users and this being a common question I would ask to check it again.
As for the 3 things somebody learns during this tutorial, I think the tutorial teaches at least 3 concepts of how the Steemit Blockchain works.
Congratulations @bloodviolet! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of upvotes
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP