Part 11: How To Build A List Of Transfers And Broadcast These In One Transaction With Steem-Python
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,.
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
- Part 0: How To Install Steem-python, The Official Steem Library For Python
- Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python
- Part 2: How To Stream And Filter The Blockchain Using Steem-Python
- Part 3: Creating A Dynamic Autovoter That Runs 24/7
- Part 4: How To Follow A Voting Trail Using Steem-Python
- Part 5: Post An Article Directly To The Steem Blockchain And Automatically Buy Upvotes From Upvote Bots
- Part 6: How To Automatically Reply To Mentions Using Steem-Python
- Part 7: How To Schedule Posts And Manually Upvote Posts For A Variable Voting Weight With Steem-Python
- Part 8: How To Create Your Own Upvote Bot Using Steem-Python
- Part 9: How To Calculate A Post's Total Rewards Using Steem-Python
- Part 10: Use Urls To Retrieve Post Data And Construct A Dynamic Post With Steem-Python
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
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
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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