Skip to content

Commit b88a471

Browse files
Added wait_for_confirmation() to AlgodClient (#214)
* 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
1 parent 3581434 commit b88a471

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

algosdk/error.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,7 @@ def __init__(self, msg):
203203

204204
class IndexerHTTPError(Exception):
205205
pass
206+
207+
208+
class ConfirmationTimeoutError(Exception):
209+
pass

algosdk/future/transaction.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ def __init__(
10481048
metadata_hash=None,
10491049
note=None,
10501050
lease=None,
1051-
rekey_to=None
1051+
rekey_to=None,
10521052
):
10531053
super().__init__(
10541054
sender=sender,
@@ -1137,7 +1137,7 @@ def __init__(
11371137
clawback,
11381138
note=None,
11391139
lease=None,
1140-
rekey_to=None
1140+
rekey_to=None,
11411141
):
11421142
super().__init__(
11431143
sender=sender,
@@ -3008,3 +3008,36 @@ def assign_group_id(txns, address=None):
30083008
tx.group = gid
30093009
result.append(tx)
30103010
return result
3011+
3012+
3013+
def wait_for_confirmation(algod_client, txid, wait_rounds=0, **kwargs):
3014+
"""
3015+
Block until a pending transaction is confirmed by the network.
3016+
3017+
Args:
3018+
algod_client (algod.AlgodClient): Instance of the `algod` client
3019+
txid (str): transaction ID
3020+
wait_rounds (int, optional): The number of rounds to block for before
3021+
exiting with an Exception. If not supplied, there is no timeout.
3022+
"""
3023+
last_round = algod_client.status()["last-round"]
3024+
current_round = last_round + 1
3025+
3026+
while True:
3027+
# Check that the `wait_rounds` has not passed
3028+
if wait_rounds > 0 and current_round > last_round + wait_rounds:
3029+
raise error.ConfirmationTimeoutError(
3030+
f"Wait for transaction id {txid} timed out"
3031+
)
3032+
3033+
tx_info = algod_client.pending_transaction_info(txid, **kwargs)
3034+
3035+
# The transaction has been confirmed
3036+
if "confirmed-round" in tx_info:
3037+
return tx_info
3038+
3039+
# Wait until the block for the `current_round` is confirmed
3040+
algod_client.status_after_block(current_round)
3041+
3042+
# Incremenent the `current_round`
3043+
current_round += 1

test/steps/steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ def fund_contract(context):
997997
)
998998
context.txn = context.wallet.sign_transaction(context.txn)
999999
context.acl.send_transaction(context.txn)
1000-
context.acl.status_after_block(context.acl.status()["lastRound"] + 3)
1000+
transaction.wait_for_confirmation(context.acl, context.txn.get_txid(), 10)
10011001

10021002

10031003
@when("I claim the algos")

test/steps/v2_steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ def create_transient_and_fund(context, transient_fund_amount):
18331833
)
18341834
signed_payment = context.wallet.sign_transaction(payment)
18351835
context.app_acl.send_transaction(signed_payment)
1836-
context.app_acl.status_after_block(sp.first + 2)
1836+
transaction.wait_for_confirmation(context.app_acl, payment.get_txid(), 10)
18371837

18381838

18391839
@step(

test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def test_transaction(self):
187187
self.assertEqual(self.acl.pending_transaction_info(txid)["tx"], txid)
188188

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

192192
# get transaction info two different ways
193193
info_1 = self.acl.transactions_by_address(

0 commit comments

Comments
 (0)