steem-python for dummies #5 - Bundling blockchain operations

Hello,

This is the 5. post of "steem-python for dummies" series. If you didn't read the older ones take your time and have a look.

  1. steem-python for dummies #1 - Introduction and Basic Operations
  2. steem-python for dummies #2 - Playing with account data
  3. steem-python for dummies #3 - Coding an upvote bot
  4. steem-python for dummies #4 - Private Memos

In this post, we will learn about bundling operations into one transaction.

Bundling Operations

If you follow the standart prosedure, every operation you broadcast also creates one transaction. However, if you do multiple things at once (for example: comment + upvote), you can broadcast them in one transaction. It will help network efficiency and reduces content in the chain.

Use Case #1 - Payouts

Let's say, you're a bidding bot operator and you send payouts in every 2.4 hours. You don't need to create a transaction for every transfer operation.

We will:

  • use TransactionBuilder class
  • create Transfer operations and append them into TransactionBuilder instance
  • sign it with the corresponding private key
  • broadcast it

Normally, steem-python encapsulates signing and broadcasting so we don't call these steps. But since bundling operations is kinda edge case, we will call them explicitly.

from steem.transactionbuilder import TransactionBuilder
from steembase import operations
s = Steem(nodes=["https://rpc.buildteam.io"],
          keys=["active_private_key"])
bot_account = "emrebeyler"
payouts = [
    operations.Transfer(**{
        "from": bot_account,
        "to": "gokos",
        "amount": "0.001 SBD",
        "memo": "payout",
    }),
    operations.Transfer(**{
        "from": bot_account,
        "to": "turbot",
        "amount": "0.001 SBD",
        "memo": "payout",
    }),
    operations.Transfer(**{
        "from": bot_account,
        "to": "omersurer",
        "amount": "0.001 SBD",
        "memo": "payout",
    }),
]
tb = TransactionBuilder()
tb.appendOps(payouts)
tb.appendSigner(bot_account, "active")
tb.sign()
tb.broadcast()

Tip: If you want to test first, you can pass no_broadcast=True to TransactionBuilder. This way it don't really broadcast transaction and you have your funds in your pocket.

steemd representation of the example transaction

Use Case #2 - Curation bots

In the first example, we have bundled same type operations into one transaction. It is possible that doing the same thing with different type of operations.

Let's say you have a curator bot which upvotes and comments quality posts. (like qurator) You can optimize the process by bundling comment + upvote operations into one transaction.

from steem.transactionbuilder import TransactionBuilder
from steembase import operations
from steem.post import Post
s = Steem(nodes=["https://rpc.buildteam.io"],
          keys=["active_private_key"])
bot_account = "emrebeyler"
post = Post(
    "@omersurer/mozilla-firefox-web-tarayicisi-kullanimi-turkish-tutorial")
comment = operations.Comment(**{
    "parent_author": post["author"],
    "parent_permlink": post["permlink"],
    "author": bot_account,
    "permlink": "re-" + post["permlink"],
    "title": None,
    "body": "Upvoted!.",
    "json_metadata": None,
})
upvote = operations.Vote(**{
    "voter": bot_account,
    "author": post["author"],
    "permlink": post["permlink"],
    "weight": +50,
})
tb = TransactionBuilder()
tb.appendOps([upvote,  comment])
tb.appendSigner(bot_account, "active")
tb.sign()
tb.broadcast()

steemd representation of the example transaction

Since steem blockchain stores and handles a lot of data, it's our duty to use it optimized. We should bundle related operations into one transaction where it is possible.


That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts.



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now