Skip to content

Commit

Permalink
Added wait_for_confirmation() to AlgodClient (#214)
Browse files Browse the repository at this point in the history
* Incorporated wait_for_confirmation into the AlgodClient

* Replaced parts of code that manually wait for transaction to confirm with dedicated wait_for_confirmation()

* Fixed small bug

* Moved locatoin of wait_for_confirmation code to transaction.future

* Moved locatoin of wait_for_confirmation code to transaction.future

* Integrated comments from PR

* Small change in how wait_rounds branches or not

* Reformatted files

* Added doc comment for algod_client
  • Loading branch information
DamianB-BitFlipper authored Oct 4, 2021
1 parent 3581434 commit b88a471
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions algosdk/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,7 @@ def __init__(self, msg):

class IndexerHTTPError(Exception):
pass


class ConfirmationTimeoutError(Exception):
pass
37 changes: 35 additions & 2 deletions algosdk/future/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ def __init__(
metadata_hash=None,
note=None,
lease=None,
rekey_to=None
rekey_to=None,
):
super().__init__(
sender=sender,
Expand Down Expand Up @@ -1137,7 +1137,7 @@ def __init__(
clawback,
note=None,
lease=None,
rekey_to=None
rekey_to=None,
):
super().__init__(
sender=sender,
Expand Down Expand Up @@ -3008,3 +3008,36 @@ def assign_group_id(txns, address=None):
tx.group = gid
result.append(tx)
return result


def wait_for_confirmation(algod_client, txid, wait_rounds=0, **kwargs):
"""
Block until a pending transaction is confirmed by the network.
Args:
algod_client (algod.AlgodClient): Instance of the `algod` client
txid (str): transaction ID
wait_rounds (int, optional): The number of rounds to block for before
exiting with an Exception. If not supplied, there is no timeout.
"""
last_round = algod_client.status()["last-round"]
current_round = last_round + 1

while True:
# Check that the `wait_rounds` has not passed
if wait_rounds > 0 and current_round > last_round + wait_rounds:
raise error.ConfirmationTimeoutError(
f"Wait for transaction id {txid} timed out"
)

tx_info = algod_client.pending_transaction_info(txid, **kwargs)

# The transaction has been confirmed
if "confirmed-round" in tx_info:
return tx_info

# Wait until the block for the `current_round` is confirmed
algod_client.status_after_block(current_round)

# Incremenent the `current_round`
current_round += 1
2 changes: 1 addition & 1 deletion test/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def fund_contract(context):
)
context.txn = context.wallet.sign_transaction(context.txn)
context.acl.send_transaction(context.txn)
context.acl.status_after_block(context.acl.status()["lastRound"] + 3)
transaction.wait_for_confirmation(context.acl, context.txn.get_txid(), 10)


@when("I claim the algos")
Expand Down
2 changes: 1 addition & 1 deletion test/steps/v2_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ def create_transient_and_fund(context, transient_fund_amount):
)
signed_payment = context.wallet.sign_transaction(payment)
context.app_acl.send_transaction(signed_payment)
context.app_acl.status_after_block(sp.first + 2)
transaction.wait_for_confirmation(context.app_acl, payment.get_txid(), 10)


@step(
Expand Down
2 changes: 1 addition & 1 deletion test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_transaction(self):
self.assertEqual(self.acl.pending_transaction_info(txid)["tx"], txid)

# wait for transaction to send
self.acl.status_after_block(sp.first + 2)
transaction.wait_for_confirmation(self.acl, txid, 10)

# get transaction info two different ways
info_1 = self.acl.transactions_by_address(
Expand Down

0 comments on commit b88a471

Please sign in to comment.