This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
Tutorial 8: Async Send and Monitor #312
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from iota import AsyncIota, ProposedTransaction, Address, TryteString | ||
from typing import List | ||
import asyncio | ||
|
||
# Asynchronous API instance. | ||
api = AsyncIota( | ||
adapter='https://nodes.devnet.iota.org:443', | ||
devnet=True, | ||
) | ||
|
||
# An arbitrary address to send zero-value transactions to. | ||
addy = Address('PZITJTHCIIANKQWEBWXUPHWPWVNBKW9GMNALMGGSIAUOYCKNWDLUUIGAVMJYCHZXHUBRIVPLFZHUVDLME') | ||
|
||
# Timeout after which confirmation monitoring stops (seconds). | ||
timeout = 120 | ||
# How often should we poll for confirmation? (seconds) | ||
polling_interval = 5 | ||
|
||
|
||
async def send_and_monitor( | ||
transactions: List[ProposedTransaction] | ||
) -> bool: | ||
""" | ||
Send a list of transactions as a bundle and monitor their confirmation | ||
by the network. | ||
""" | ||
print('Sending bundle...') | ||
st_response = await api.send_transfer(transactions) | ||
|
||
sent_tx_hashes = [tx.hash for tx in st_response['bundle']] | ||
|
||
print('Sent bundle with transactions: ') | ||
print(*sent_tx_hashes, sep='\n') | ||
|
||
# Measure elapsed time | ||
elapsed = 0 | ||
|
||
print('Checking confirmation...') | ||
while len(sent_tx_hashes) > 0: | ||
# Determine if transactions are confirmed | ||
git_response = await api.get_inclusion_states(sent_tx_hashes, None) | ||
lzpap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
for i, (tx, is_confirmed) in enumerate(zip(sent_tx_hashes, git_response['states'])): | ||
if is_confirmed: | ||
print('Transaction %s is confirmed.' % tx) | ||
# No need to check for this any more | ||
del sent_tx_hashes[i] | ||
del git_response['states'][i] | ||
|
||
if len(sent_tx_hashes) > 0: | ||
if timeout <= elapsed: | ||
# timeout reached, terminate checking | ||
return False | ||
# Show some progress on the screen | ||
print('.') | ||
# Put on hold for polling_interval. Non-blocking, so you can | ||
# do other stuff in the meantime. | ||
await asyncio.sleep(polling_interval) | ||
elapsed = elapsed + polling_interval | ||
|
||
# All transactions in the bundle are confirmed | ||
return True | ||
|
||
|
||
async def do_something() -> None: | ||
""" | ||
While waiting for confirmation, you can execute arbitrary code here. | ||
""" | ||
for _ in range(5): | ||
print('Doing something in the meantime...') | ||
await asyncio.sleep(2) | ||
|
||
|
||
async def main() -> None: | ||
""" | ||
A simple application that sends zero-value transactions to the Tangle and | ||
monitors the confirmation by the network. While waiting for the | ||
confirmation, we schedule a task (`do_something()`) to be executed concurrently. | ||
""" | ||
# Transactions to be sent. | ||
transactions = [ | ||
ProposedTransaction( | ||
address=addy, | ||
value=0, | ||
message=TryteString.from_unicode('First'), | ||
), | ||
ProposedTransaction( | ||
address=addy, | ||
value=0, | ||
message=TryteString.from_unicode('Second'), | ||
), | ||
ProposedTransaction( | ||
address=addy, | ||
value=0, | ||
message=TryteString.from_unicode('Third'), | ||
), | ||
] | ||
|
||
# Schedule coroutines as tasks, wait for them to finish and gather their | ||
# results. | ||
result = await asyncio.gather( | ||
send_and_monitor(transactions), | ||
# Send the same content. Bundle will be different! | ||
send_and_monitor(transactions), | ||
do_something(), | ||
) | ||
|
||
if not (result[0] and result[1]): | ||
print('Transactions did not confirm after %s seconds!' % timeout) | ||
else: | ||
print('All transactions are confirmed!') | ||
|
||
if __name__ == '__main__': | ||
# Execute main() inside an event loop if the file is ran | ||
asyncio.run(main()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.