Skip to content

Commit 7f44597

Browse files
committed
update wait_for_tansaction api to support long poll
1 parent f65bdd5 commit 7f44597

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

aptos_sdk/async_client.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -568,24 +568,31 @@ async def transaction_pending(self, txn_hash: str) -> bool:
568568
raise ApiError(response.text, response.status_code)
569569
return response.json()["type"] == "pending_transaction"
570570

571+
async def _wait_for_txn(self, txn_hash: str) -> None:
572+
while True:
573+
response = await self._get(endpoint=f"transactions/wait_by_hash/{txn_hash}")
574+
if response.status_code >= 400:
575+
raise ApiError(response.text, response.status_code)
576+
is_pending = response.json()["type"] == "pending_transaction"
577+
if not is_pending:
578+
assert (
579+
"success" in response.json() and response.json()["success"]
580+
), f"{response.text} - {txn_hash}"
581+
return
582+
571583
async def wait_for_transaction(self, txn_hash: str) -> None:
572584
"""
573585
Waits up to the duration specified in client_config for a transaction to move past pending
574586
state.
575587
"""
576-
577-
count = 0
578-
while await self.transaction_pending(txn_hash):
579-
assert (
580-
count < self.client_config.transaction_wait_in_seconds
581-
), f"transaction {txn_hash} timed out"
582-
await asyncio.sleep(1)
583-
count += 1
584-
585-
response = await self._get(endpoint=f"transactions/by_hash/{txn_hash}")
586-
assert (
587-
"success" in response.json() and response.json()["success"]
588-
), f"{response.text} - {txn_hash}"
588+
await asyncio.wait_for(
589+
self._wait_for_txn(txn_hash),
590+
timeout=(
591+
self.client_config.transaction_wait_in_seconds
592+
if self.client_config.transaction_wait_in_seconds
593+
else None
594+
),
595+
)
589596

590597
async def account_transaction_sequence_number_status(
591598
self, address: AccountAddress, sequence_number: int

0 commit comments

Comments
 (0)