Python for STEEM #3: Modules

in #busy5 years ago

This is the third part to a series of tutorials.
The goal is to produce a hands-on guide for people who want to jump straight into developing tools for STEEM.


API

Whenever I query data 'from the blockchain', I have to connect to a node that runs the corresponding API plugin to my request. I just got reminded of how little I understand about this.
I will stick to what works for me and explain as best as I can.
For this guide I will treat the API like a black box and will look at beem's modules only. Most of beem's methods closely reflect the underlying API calls, though.

Modules

These are beem's modules
https://beem.readthedocs.io/en/latest/modules.html

image.png

In the last part, I used the blockchain module.
https://beem.readthedocs.io/en/latest/beem.blockchain.html

from beem.blockchain import Blockchain
blockchain = Blockchain()

This instantiates blockchain as of the class Blockchain like defined in:
https://github.com/holgern/beem/blob/master/beem/blockchain.py

In the documentation you can look up, which parameters the different methods accept and what data type they will return.

head_block = blockchain.get_current_block_num()

The method get_current_block_num, returns an int.

account.py

As an example, I will go through some methods of the account module;
https://beem.readthedocs.io/en/latest/beem.account.html

method: balances

from beem.account import Account

test_account = Account(account = 'holger80')
print(test_account.balances)

This returns a dictionary, but the print() function will print it as a string automagically.

method: get_balances()

from beem.account import Account

test_account = Account(account = "holger80")
print(test_account.get_balances())

This returns the same dictionary as the balances method does.

method: get_voting_power()

beem has some higher level functions, which are abstracted from the raw API data:

from beem.account import Account

test_account = Account(account = "holger80")
print(test_account.get_voting_power())
print(test_account.get_voting_power(with_regeneration = False))

By default, the parameter with_regeneration is set to True.
The regeneration however, is being calculated for you by beem.
The STEEM API will always only return the voting power at the time of the last vote.

Account.history()

The history() method deserves its own chapter.
Sadly, the default node set in beem does not support it at the moment.
I have to set the node by hand like this:

from beem import Steem
from beem.instance import set_shared_steem_instance

steemit_api = Steem(node=["https://api.steemit.com"])
set_shared_steem_instance(steemit_api)

This makes our black box API work again.
Leave this in front of all examples from now on, for good measure.


For most purposes, history_reverse is the more useful function, but is derived from history anyways - It is just the other way around starting at the latest block, going backwards.

from beem.account import Account
from beem.blockchain import Blockchain

blockchain = Blockchain()
head_block = blockchain.get_current_block_num()

test_account = Account("holger80")

for operation in test_account.history_reverse(start = head_block, stop = head_block - 10000, use_block_num = True):
    print(operation)

This returns all operations during the last 10000 blocks for the specified account.
There are more examples in the documentation.

Congratulations

This should enable you to explore all of beem's modules and methods.
If you want me to explain a specific method more closely, let me know in the comments or in chat.

Homework

Can you calculate the average of your last 10 curation rewards ?

Sort:  

Great series, have a !BEER.

Will work on the homework :)

You can get some bonus points for figuring out how I can configure beem to work with the testnet :)

Congratulations @goldcoin, you are successfuly trended the post that shared by @felixxx!
@felixxx will receive 3.59609625 TRDO & @goldcoin will get 2.39739750 TRDO curation in 3 Days from Post Created Date!

"Call TRDO, Your Comment Worth Something!"

To view or trade TRDO go to steem-engine.com
Join TRDO Discord Channel or Join TRDO Web Site

thats amazing! thanks for sharing =)

yes i agree i would love to use python on steem (tho i have to still learn json)

I find your tutorials to be useful. But I don’t like homework :)

I kind of got ahead of myself there XD
As I am doing it myself, I realize it is not trivial.



Hey @felixxx, here is a little bit of BEER for you. Enjoy it!

Well I have some fun with testing various beem modules, but I have yet to find out how to convert VESTS to SP. I need to learn how to extract items from a dictionary to be able to do the homework.

PS. I will do a post of some of my testing shortly.

If you can do it in VESTS, that's fine.
I will introduce the asset class later ...

Congratulations @felixxx, your post successfully recieved 3.59609625 TRDO from below listed TRENDO callers:

@goldcoin earned : 2.3973975 TRDO curation


To view or trade TRDO go to steem-engine.com
Join TRDO Discord Channel or Join TRDO Web Site

Useful guide. thanks.