Part 5: Coding on Hive With Python - Working with Resource Credits

avatar

Part 5 of this series discusses how to query Resource Credits (RC) account balance and how to check current RC costs for various Hive blockchain operations.

The examples here require some knowledge from parts 1 - 3. If you’re new to coding on Hive with Python, it’s strongly recommended to review the earlier posts first.

image.png


Background - How does the Resource Credits (RC) System Work

Hive has a really interesting system called Resource Credits. Different operations have a cost, and the costs adjust dynamically based on network utilization. Every wallet has a pool of resource credits that can be spent on operations. The maximum number of RCs in the pool is determined by the amount of Hive Power. And the RC pool gradually replenishes over time.

Voting on a post uses a tiny amount of RCs. Claiming an account creation token costs a huge amount of RCs. Wallets with around 5,000 HP can claim one account creation token roughly every 3 days. The cost is variable! Larger stakeholders benefit from waiting for lower RC costs for opportunities to claim lower-cost tokens.

Using Python and the beem library, it’s easy to check for the RC cost for various Hive operations. For this purpose, beem has a convenient RC() object. The code snippets below demonstrate how.

Step 1 - Checking RC Costs of Various Hive Operations

In step 1, beem is called upon to check RC costs of 4 types of operations. First, we need to import beem and set up a Hive blockchain object. Then, feed it in to instantiate beem's RC object beem.rc.RC(). The RC object has convenient methods for requesting the current RC costs of various operation types: custom_json, vote, comment (same as post), and account creation token claims (claim_account). Calling those returns the RC cost as an integer. Last, print() out the results.

Code snippet:

import beem
hive = beem.Hive()
rc = beem.rc.RC(hive_instance=hive)

comment_cost = rc.comment()
vote_cost = rc.vote()
json_cost = rc.custom_json()
account_cost = rc.claim_account()
print('Current RC costs => Custom_json: %d | Vote: %d | Comment/Post: %d | Account Creation Token: %d' % (json_cost, vote_cost, comment_cost, account_cost))

Expected output:

Current RC costs => Custom_json: 157471719 | Vote: 103030502 | Comment/Post: 1129593147 | Account Creation Token: 6391759705103

Step 2 - Checking the Current RC Balance of a Hive Account

In step 2, beem is called upon to check the current RC balance for a Hive wallet. Beem refers to RC balance as mana. Like in LearnCode#3, the input() function prompts the user for keyboard input, asking the Hive wallet name. The specified name is passed to beem's Account() object instantiation. Then, the Account object's get_rc_manabar() method is called to get the current RC balance. The response is a dictionary with several keys including the maximum RC count and the time to fully recharge, but for now all that's needed is the 'current_mana' value. Last, print out a nice message including wallet name and the current RC count.

Code snippet:

import beem

ACCOUNT_NAME = input('Enter account name: ')

acc = beem.account.Account(ACCOUNT_NAME)
mana = acc.get_rc_manabar()['current_mana']
print('Current mana for @%s is %d RCs' % (ACCOUNT_NAME, mana))

Expected output:

Enter account name: learncode
Current mana for @learncode is 9314719867 RCs

Putting it All Together - Determining How Many Ops Are Possible for the Account

Combining snippets from step 1 and step 2, we can calculate, for a given account, how many votes, comments, etc can be broadcast with the current RC balance. First, use code from step 1 to get the current RC costs of the four operation types. Then, query the account's current RC balance. Then compute the number of possible operations of each type. Finally, print out the numbers for each operation.

Code snippet:

import beem
hive = beem.Hive()
rc = beem.rc.RC(hive_instance=hive)
ACCOUNT_NAME = input('Enter account name: ')

comment_cost = rc.comment()
vote_cost = rc.vote()
json_cost = rc.custom_json()
account_cost = rc.claim_account()
print('Current RC costs => Custom_json: %d | Vote: %d | Comment/Post: %d | Account Creation Token: %d' % (json_cost, vote_cost, comment_cost, account_cost))

acc = beem.account.Account(ACCOUNT_NAME)
mana = acc.get_rc_manabar()['current_mana']

possible_jsons = int(mana / json_cost)
possible_votes = int(mana / vote_cost)
possible_comments = int(mana / comment_cost)
possible_accounts = int(mana / account_cost)

print('Account %s can make %d jsons, %d votes, %d comments, %d accounts' % (ACCOUNT_NAME, possible_jsons, possible_votes, possible_comments, possible_accounts))


Expected Output:

Enter account name: learncode
Current RC costs => Custom_json: 157474527 | Vote: 103032095 | Comment/Post: 1129608754 | Account Creation Token: 6391034357264
Account learncode can make 59 jsons, 90 votes, 8 comments, 0 accounts

image.png

p.s. Thanks for reading. Please let me know if anything is unclear or incorrect. This is intended to be a living document to be improved and maintained over time.


This blog is produced by the @Hive.Pizza team (@hivetrending and @thebeardflex). PIZZA Crew is a Hive-powered social group empowering content creators, gamers, and YOU. We host game servers, a DLUX node, a Hive-Engine node, and now a Hive witness node! Please help us out and give your vote of approval for our witness (@pizza.witness). Here's a convenient way to vote using HiveKeychain or HiveSigner: https://vote.hive.uno/@pizza.witness. Thanks for your support!



0
0
0.000
7 comments
avatar

Congratulations @learncode! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You got more than 50 replies.
Your next target is to reach 100 replies.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Feedback from the July 1st Hive Power Up Day - ATH Volume record!
0
0
0.000
avatar

lol i always look at these posts.....then check my glasses....then realise I dont speak spanish... can you google translate this? !PIZZA untitled.gif

0
0
0.000
avatar
Connect

Trade


@learncode! This post has been manually curated by the $PIZZA Token team!

Learn more about $PIZZA Token at hive.pizza. Enjoy a slice of $PIZZA on us!

0
0
0.000