create account

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

View this thread on: hive.blogpeakd.comecency.com
· @learncode ·
$2.39
Part 5: Coding on Hive With Python - Working with Resource Credits
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](https://peakd.com/@learncode) first.

![image.png](https://files.peakd.com/file/peakd-hive/learncode/23u6X11XGuTsKsvrFZBDpA6McV75Cktey1qgw7R1239yHaFSP6bHNh5reE5qZQaGMXVVC.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](https://files.peakd.com/file/peakd-hive/learncode/23u5ZX2czd4dwvPTAxnpNvXoYYWUL23xvbe8hF6Kts2Qnc6598MEYjZuSEG8bCR159T3N.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](https://vote.hive.uno/@pizza.witness) of approval for our witness (@pizza.witness). Here's a convenient way to [vote](https://vote.hive.uno/@pizza.witness) using HiveKeychain or HiveSigner: https://vote.hive.uno/@pizza.witness. Thanks for your support!

๐Ÿ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 17 others
properties (23)
authorlearncode
permlinkpart-5-coding-on-hive-with-python-working-with-resource-credits
categoryprogramming
json_metadata"{"app":"peakd/2021.07.1","format":"markdown","description":"Part 5 discusses how to query Resource Credits (RC) account balance and how to check RC costs for various Hive ops","tags":["programming","coding","hive","python","beem","ctp","palnet","proofofbrain","stem","broadhive"],"users":["learncode","Hive.Pizza","hivetrending","thebeardflex","pizza.witness","pizza.witness."],"image":["https://files.peakd.com/file/peakd-hive/learncode/23u6X11XGuTsKsvrFZBDpA6McV75Cktey1qgw7R1239yHaFSP6bHNh5reE5qZQaGMXVVC.png","https://files.peakd.com/file/peakd-hive/learncode/23u5ZX2czd4dwvPTAxnpNvXoYYWUL23xvbe8hF6Kts2Qnc6598MEYjZuSEG8bCR159T3N.png"]}"
created2021-07-05 20:38:06
last_update2021-07-05 20:38:06
depth0
children7
last_payout2021-07-12 20:38:06
cashout_time1969-12-31 23:59:59
total_payout_value1.202 HBD
curator_payout_value1.191 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,995
author_reputation682,726,195,093
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,742,033
net_rshares4,756,321,997,270
author_curate_reward""
vote details (81)
@hivebuzz ·
Congratulations @learncode! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@learncode/replies.png?202107052146"></td><td>You got more than 50 replies.<br>Your next target is to reach 100 replies.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@learncode) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out the last post from @hivebuzz:**
<table><tr><td><a href="/hivebuzz/@hivebuzz/pud-202107-feedback"><img src="https://images.hive.blog/64x128/https://i.imgur.com/zHjYI1k.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/pud-202107-feedback">Feedback from the July 1st Hive Power Up Day - ATH Volume record!</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-learncode-20210705t221147000z
categoryprogramming
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2021-07-05 22:11:48
last_update2021-07-05 22:11:48
depth1
children0
last_payout2021-07-12 22:11:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length960
author_reputation369,386,340,709,685
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,743,301
net_rshares0
@pizzabot ·
<div class='pull-right'><center><sup>Connect</sup></center><p><a href="https://discord.gg/Q9bQAKpWGS"><img src="https://files.peakd.com/file/peakd-hive/pizzabot/AKF96fKjnX3wjXERHcKAFHaoHnfTVhXqPjXVz8E1Th9nPiJqmFtaycosVpPBZ7z.png"></a></p></div><div class='pull-right'><center><sup>Trade</sup></center><p><a href='https://hive-engine.com/?p=market&t=PIZZA'><img src="https://files.peakd.com/file/peakd-hive/pizzabot/23sxbi2M4UjELDzQjxPdzubdgfMjHTCtA1xueyxmnhJUrB8136VyK3pqynyWYiZYF9HrC.png"></a></p></div><center><br> <p>@learncode! This post has been manually curated by the <strong><em>$PIZZA</em></strong> Token team!</p><p>Learn more about <strong><em>$PIZZA Token</em></strong> at <a href="https://hive.pizza">hive.pizza</a>. Enjoy a slice of $PIZZA on us!</p> </center><div></div>
properties (22)
authorpizzabot
permlinkre-part-5-coding-on-hive-with-python-working-with-resource-credits-20210705t234640z
categoryprogramming
json_metadata"{"app": "beem/0.24.26"}"
created2021-07-05 23:46:42
last_update2021-07-05 23:46:42
depth1
children0
last_payout2021-07-12 23:46:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length785
author_reputation7,561,320,422,270
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,744,535
net_rshares0
@poshtoken ·
https://twitter.com/HiveTrending/status/1412149263027490822
<sub> The rewards earned on this comment will go directly to the person sharing the post on Twitter as long as they are registered with @poshtoken. </sub>
properties (22)
authorposhtoken
permlinkre-learncode-part-5-coding-on-hive-with-python-working-with-res20875
categoryprogramming
json_metadata"{"app":"Poshtoken 0.0.1"}"
created2021-07-05 20:40:18
last_update2021-07-05 20:40:18
depth1
children0
last_payout2021-07-12 20:40:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length215
author_reputation5,413,052,484,832,097
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries
0.
accountreward.app
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id104,742,068
net_rshares0
@stickupboys ·
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](https://media.tenor.com/images/db1e6cb061c2617820152e436ba2cc4e/tenor.gif)
properties (22)
authorstickupboys
permlinkre-learncode-qvsneo
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2021.07.1"}
created2021-07-05 22:49:36
last_update2021-07-05 22:49:36
depth1
children1
last_payout2021-07-12 22:49:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length227
author_reputation488,834,890,932,513
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,743,745
net_rshares0
@pizzabot ·
<div class='pull-right'><center><sup>Connect</sup></center><p><a href="https://discord.gg/Q9bQAKpWGS"><img src="https://files.peakd.com/file/peakd-hive/pizzabot/AKF96fKjnX3wjXERHcKAFHaoHnfTVhXqPjXVz8E1Th9nPiJqmFtaycosVpPBZ7z.png"></a></p></div><div class='pull-right'><center><sup>Trade</sup></center><p><a href='https://hive-engine.com/?p=market&t=PIZZA'><img src="https://files.peakd.com/file/peakd-hive/pizzabot/23sxbi2M4UjELDzQjxPdzubdgfMjHTCtA1xueyxmnhJUrB8136VyK3pqynyWYiZYF9HrC.png"></a></p></div><center><br> <p>@learncode! I sent you a slice of <strong><em>$PIZZA</em></strong> on behalf of @stickupboys.</p> <p>Learn more about <strong><em>$PIZZA Token</em></strong> at <a href="https://hive.pizza">hive.pizza</a> <sub>(7/20)</sub></p> </center><div></div>
properties (22)
authorpizzabot
permlinkre-re-learncode-qvsneo-20210705t225041z
categoryprogramming
json_metadata"{"app": "beem/0.24.26"}"
created2021-07-05 22:50:42
last_update2021-07-05 22:50:42
depth2
children0
last_payout2021-07-12 22:50:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length766
author_reputation7,561,320,422,270
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,743,756
net_rshares0
@thelogicaldude ·
$0.09
This is great content for any #hivehustlers wanting to mess around with Python! 
๐Ÿ‘  , , , ,
properties (23)
authorthelogicaldude
permlinkre-learncode-qvtnvl
categoryprogramming
json_metadata{"tags":["programming"],"app":"peakd/2021.07.1"}
created2021-07-06 11:57:21
last_update2021-07-06 11:57:21
depth1
children1
last_payout2021-07-13 11:57:21
cashout_time1969-12-31 23:59:59
total_payout_value0.047 HBD
curator_payout_value0.047 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length80
author_reputation360,705,107,758,635
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,752,989
net_rshares187,111,356,172
author_curate_reward""
vote details (5)
@learncode ·
$0.03
Thanks for reading!
๐Ÿ‘  
properties (23)
authorlearncode
permlinkre-thelogicaldude-202176t6325654z
categoryprogramming
json_metadata{"tags":["programming"],"app":"ecency/3.0.18-mobile","format":"markdown+html"}
created2021-07-06 13:32:06
last_update2021-07-06 13:32:06
depth2
children0
last_payout2021-07-13 13:32:06
cashout_time1969-12-31 23:59:59
total_payout_value0.015 HBD
curator_payout_value0.015 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length19
author_reputation682,726,195,093
root_title"Part 5: Coding on Hive With Python - Working with Resource Credits"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id104,754,338
net_rshares61,693,174,701
author_curate_reward""
vote details (1)