Part 11: How To Build A List Of Transfers And Broadcast These In One Transaction With Steem-Python

in #utopian-io7 years ago (edited)

steem-python.png

This tutorial is part of a series where different aspects of programming with steem-python are explained. Links to the other tutorials can be found in the curriculum section below. This part is a direct continuation on Part 10: Use Urls To Retrieve Post Data And Construct A Dynamic Post With Steem-Python. Where part 10 focused on constructing the post, this tutorial will post it to blockchain, upvote it for a variable amount and transfer SBD to each author in one transaction.


What will I learn

  • How to build a list of transfers
  • Broadcast a list of transfers in one transaction

Requirements

  • Python3.6
  • steem-python

Difficulty

  • Basic

Tutorial

Setup

Start by going over the previous tutorial if you have not done so. After this download the files from Gitub. Again there are 2 files. A text file in which the urls and post meta data is stored and the python script itself. In this example a post containing winners from a photography contest is constructed from urls, to each winners entry. This post is then submitted to the blockchain, and upvoted for a variable weight. In addition prize money is payed out to all winners in one grouped transaction on the blockchain.

In order to submit the post additional data is required. This will be stored in winners.txt before the urls.

winners.txt

#1 account
#2 post title
#3 tags
#4 upvote weight
#5 memo for transaction
#6 total amount of SBD
#7-7+n urls

Run the script by with the filename as the first argument
> python main.py winners.txt

How to build a list of transactions

The Steem blockchain allows for transfers to be grouped together in one transaction. To make use of this, a list of each transfer has to be made. After which this list can be broadcasted at once. Each transfer consists of a sender from, receiver to, amount in x.xxx SBD or x.xxx STEEM and a memo which can contain a message.

transfers = []

for each winner:
  transfers.append(    {
          'from': account,
          'to': author,
          'amount': '{0:.3f} SBD'.format(amount),
          'memo': memo
      })

Broadcast a transaction

The next block of code takes the list of transfers and constructs a single transaction. This is then broadcasted to the blockchain. no_broadcast=False can be set to True in case of testing. This allows you to test if everything is correct before sending SBD/STEEM.

tb = TransactionBuilder(no_broadcast=False)
operation = [operations.Transfer(**x) for x in transfers]
tb.appendOps(operation)
tb.appendSigner(account, 'active')
tb.sign()
tx = tb.broadcast()

Submitting and upvoting the post

The part which submits the post to the blockchain and upvotes it for a variable upvote weight has been taken from a previous tutorial

steem.post(title, body, account, permlink, None, None, None, None, tags, None, False)
steem.vote('@{}/{}'.format(account, permlink), upvote_weight, account)

Running the code

Running the code will now construct a post from urls and metadata. Submit it to the blockchain, upvote the post and transfer SBD to each winner. As can be seen in the photo below. Thetransaction_id for each SBD transfer is the same. Grouping transaction increases efficiency and reduces stress on the blockchain,.

Screenshot 2018-01-22 18.30.54.png

To combine knowledge from the other tutorials. To schedule this post to be submitted at a certain time the following command can be used.

> echo 'python main.py winners.txt' | at 6:00 PM

Curriculum


The code for this tutorial can be found on GitHub!

This tutorial was written by @juliank in conjunction with @amosbastian.



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]

Mucho gracias!

very informative and educative post, I will try this tutorial

Have fun! If you have any questions feel free to ask

Good tutorial!

Very interesting.
It would be good to learn all this.

Hey @steempytutorials I am @utopian-io. I have just upvoted you!

Achievements

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

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

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

Coin Marketplace

STEEM 0.21
TRX 0.26
JST 0.040
BTC 101165.24
ETH 3668.48
USDT 1.00
SBD 3.16