Skip to content

Commit 5886488

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

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

aptos_sdk/async_client.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -568,25 +568,46 @@ 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_transaction(self, txn_hash: str) -> None:
572-
"""
573-
Waits up to the duration specified in client_config for a transaction to move past pending
574-
state.
575-
"""
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"
571+
async def _wait_for_txn(self, txn_hash: str) -> None:
572+
response = await self._get(endpoint=f"transactions/by_hash/{txn_hash}")
573+
if response.status_code >= 400:
574+
raise ApiError(response.text, response.status_code)
575+
is_pending = response.json()["type"] == "pending_transaction"
576+
577+
if is_pending:
578+
response = await self._get(endpoint=f"transactions/wait_by_hash/{txn_hash}")
579+
if response.status_code >= 400:
580+
raise ApiError(response.text, response.status_code)
581+
is_pending = response.json()["type"] == "pending_transaction"
582+
583+
# retry if the txn is still pending
584+
while is_pending:
585+
response = await self._get(endpoint=f"transactions/by_hash/{txn_hash}")
586+
if response.status_code >= 400:
587+
raise ApiError(response.text, response.status_code)
588+
is_pending = response.json()["type"] == "pending_transaction"
589+
if not is_pending:
590+
break
582591
await asyncio.sleep(1)
583-
count += 1
584592

585-
response = await self._get(endpoint=f"transactions/by_hash/{txn_hash}")
586593
assert (
587594
"success" in response.json() and response.json()["success"]
588595
), f"{response.text} - {txn_hash}"
589596

597+
async def wait_for_transaction(self, txn_hash: str) -> None:
598+
"""
599+
Waits up to the duration specified in client_config for a transaction to move past pending
600+
state.
601+
"""
602+
await asyncio.wait_for(
603+
self._wait_for_txn(txn_hash),
604+
timeout=(
605+
self.client_config.transaction_wait_in_seconds
606+
if self.client_config.transaction_wait_in_seconds
607+
else None
608+
),
609+
)
610+
590611
async def account_transaction_sequence_number_status(
591612
self, address: AccountAddress, sequence_number: int
592613
) -> bool:

0 commit comments

Comments
 (0)