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

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

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