Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wait_for_confirmation() to AlgodClient #214

Merged
merged 12 commits into from
Oct 4, 2021
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
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -1816,7 +1816,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