def wait_for_txs( client_or_web3: Union[Web3, JSONRPCClient], txhashes: Set[TransactionHash], timeout: int = 360 ): if isinstance(client_or_web3, Web3): web3 = client_or_web3 else: web3 = client_or_web3.web3 start = time.monotonic() outstanding = False txhashes = set(txhashes) while txhashes and time.monotonic() - start < timeout: remaining_timeout = timeout - (time.monotonic() - start) if outstanding != len(txhashes) or int(remaining_timeout) % 10 == 0: outstanding = len(txhashes) log.debug( "Waiting for tx confirmations", outstanding=outstanding, timeout_remaining=int(remaining_timeout), ) for txhash in txhashes.copy(): tx = web3.eth.getTransactionReceipt(txhash) if tx and tx["blockNumber"] is not None: status = tx.get("status") if status is not None and status == 0: raise ScenarioTxError(f"Transaction {encode_hex(txhash)} failed.") txhashes.remove(txhash) time.sleep(0.1) time.sleep(1) if len(txhashes): txhashes_str = ", ".join(encode_hex(txhash) for txhash in txhashes) raise ScenarioTxError(f"Timeout waiting for txhashes: {txhashes_str}")
def wait_for_txs(client_or_web3, txhashes, timeout=360): if isinstance(client_or_web3, Web3): web3 = client_or_web3 else: web3 = client_or_web3.web3 start = time.monotonic() outstanding = False txhashes = txhashes[:] while txhashes and time.monotonic() - start < timeout: remaining_timeout = timeout - (time.monotonic() - start) if outstanding != len(txhashes) or int(remaining_timeout) % 10 == 0: outstanding = len(txhashes) log.debug( "Waiting for tx confirmations", outstanding=outstanding, timeout_remaining=int(remaining_timeout), ) for txhash in txhashes[:]: tx = web3.eth.getTransaction(txhash) if tx and tx['blockNumber'] is not None: txhashes.remove(txhash) time.sleep(.1) time.sleep(1) if len(txhashes): txhashes_str = ', '.join(encode_hex(txhash) for txhash in txhashes) raise ScenarioTxError( f"Timeout waiting for txhashes: {txhashes_str}", )
def wait_for_txs(web3: Web3, transactions: Iterable[TransactionSent], timeout: int = 360): start = time.monotonic() outstanding = None txhashes = { transaction_sent.transaction_hash for transaction_sent in transactions } while txhashes and time.monotonic() - start < timeout: remaining_timeout = timeout - (time.monotonic() - start) if outstanding != len(txhashes) or int(remaining_timeout) % 10 == 0: outstanding = len(txhashes) log.debug( "Waiting for tx confirmations", outstanding=outstanding, timeout_remaining=int(remaining_timeout), ) for txhash in txhashes.copy(): # TODO: use `JsonRpcClient.poll_transaction` here? try: # FIXME: remove `encode_hex` when TxHash type is fixed tx: Optional[TxReceipt] = web3.eth.getTransactionReceipt( encode_hex(txhash)) except TransactionNotFound: tx = None if tx and tx["blockNumber"] is not None: status = tx.get("status") if status is not None and status == 0: raise ScenarioTxError( f"Transaction {encode_hex(txhash)} failed.") # we want to add 2 blocks as confirmation if tx["blockNumber"] + 2 < web3.eth.getBlock( "latest")["number"]: txhashes.remove(txhash) time.sleep(0.1) time.sleep(1) if len(txhashes): txhashes_str = ", ".join(encode_hex(txhash) for txhash in txhashes) raise ScenarioTxError(f"Timeout waiting for txhashes: {txhashes_str}")
def wait_for_txs(client, txhashes, timeout=180): start = time.monotonic() outstanding = False txhashes = txhashes[:] while txhashes and time.monotonic() - start < timeout: remaining_timeout = timeout - (time.monotonic() - start) if outstanding != len(txhashes) or int(remaining_timeout) % 10 == 0: outstanding = len(txhashes) log.debug( "Waiting for tx confirmations", outstanding=outstanding, timeout_remaining=int(remaining_timeout), ) for txhash in txhashes[:]: tx = client.web3.eth.getTransaction(txhash) if tx and tx['blockNumber'] is not None: txhashes.remove(txhash) time.sleep(.1) time.sleep(.5) if len(txhashes): raise ScenarioTxError( f"Timeout waiting for txhashes: {', '.join(txhashes)}")