steem-python for dummies #5 - Bundling blockchain operations

in #utopian-io7 years ago

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

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

thanks.

Dostum buradan artık bir 50 S gelmesi lazım diye düşünüyurum.
Tebrikler..

tutorial kategorisinde olduğu için oldukça zor o rakamın gelmesi. :) teşekkürler.

ne kadar geldiği de çok önemli değil, bu yazı dizisini olabildiğince sürdürüp güzel bir kaynak oluşturmak istiyorum.

Fikirlerini beğeniyorum ve ufak bir tavsiye lazım.
Rekor upvote için hangi kategoride nasıl birşey paylaşmalıyım.
(Kod hariç verebilirsen bir fikir)

This series is priceless.

@emrebeyler I am part of the MSP-Waves Broadcast leadership team. We are a radio station for Steemians, founded by @aggroed and @ausbitbank.

We are looking for help with development and I wonder if you'd be interested in joining our team of MSP-Waves volunteers?

cc: @r0nd0n

thanks!

What kind of help do you need?

I am pretty occupied with my current schedule, but I want to hear about it.

You can contact me at discord.

🎵 Woooot!!! 🎶
This post has been manually curated by @msp-waves - the amazing radio station for Steemians, courtesy of @globocop.

Listen to MSP-Waves Radio on MSPWaves.com

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by Emrebeyler from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

Ütopian Fatihi Tr olduk . Umarım seçilir 😊

Congratulations! Your post has been manually selected by @flauwy for the daily @utopian-1up curation trail. 11 Steemians/Utopians are following my trail and have upvoted your post for $1.41 in total. Join the discussion for how to make Utopian 1UP a community effort to select the best Utopian contributions of the day. You can follow the @utopian-1up trail on SteemAuto (tutorial).

Disclaimer: 1UP is in not affiliated or organized by the official Utopian.io project!

Hey @emrebeyler I am @utopian-io. I have just upvoted you at 7% Power!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • I introduced a competition factor. My vote is based also on how competitive the category used is.

Human Curation

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Birthdaypost !BEER



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