Python for STEEM #3: Modules

felixxx(72)
Published in
#busy
Words
426
Reading
2 min
Listen
Play
7y

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 ?

Python for STEEM #3: Modules | Ecency