flim.eth
flim.eth

Reputation: 1

How to catch TimeExhausted exception in web3

wondering if anyone can help me figure out why I am not catching the TimeExhausted exception when using

wait_for_transaction_receipt()

from web3.py

I want the error to be thrown after waiting 45 seconds, I want to catch the error, then go do some other stuff, and wait again. I want to repeat this cycle until the transaction is mined & then exit / break. I think it's a simple syntax issue but not sure -- last time the exception was raised, my code didn't catch it, and instead it lead to an AttributeError

here's my code:

from web3 import Web3

while True:
        try:
            tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=45)
            print(f"{str(dt.utcnow())[:-7]} | Transaction receipt: {tx_receipt}")
            break
        except Web3.TimeExhausted as e:
            print(f"{str(dt.utcnow())[:-7]} | A Inner exception caught: {e}.")
            print(
                f"{str(dt.utcnow())[:-7]} | Timed out waiting for transaction. "
                + f"Will update SMA & wait again..."
            )
            update_sma_data()
            pass

Finally here is my error text:

Traceback (most recent call last):
  File "/home/flim/.local/lib/python3.8/site-packages/web3/eth.py", line 783, in wait_for_transaction_receipt
    _timeout.sleep(poll_latency)
  File "/home/flim/.local/lib/python3.8/site-packages/web3/_utils/threads.py", line 89, in sleep
    self.check()
  File "/home/flim/.local/lib/python3.8/site-packages/web3/_utils/threads.py", line 82, in check
    raise self
web3._utils.threads.Timeout: 45 seconds

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "money-maker-mike.py", line 301, in sign_and_send_tx
    tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=45)
  File "/home/flim/.local/lib/python3.8/site-packages/web3/eth.py", line 787, in wait_for_transaction_receipt
    raise TimeExhausted(
web3.exceptions.TimeExhausted: Transaction HexBytes('0xd60bc63a96b5b81b98ca39308fc9ab709d2f2ccfd1b03c064db285f954633d34') is not in the chain after 45 seconds

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "money-maker-mike.py", line 529, in <module>
    main()
  File "money-maker-mike.py", line 522, in main
    trade()
  File "money-maker-mike.py", line 373, in trade
    zero_ex_trade(sell_token, buy_token, sell_amount)
  File "money-maker-mike.py", line 278, in zero_ex_trade
    sign_and_send_tx(tx)
  File "money-maker-mike.py", line 306, in sign_and_send_tx
    except Web3.TimeExhausted as e:
AttributeError: type object 'Web3' has no attribute 'TimeExhausted'

Thanks in advance :D

Upvotes: 0

Views: 823

Answers (1)

Nikolaj Š.
Nikolaj Š.

Reputation: 1996

from web3.exceptions import TimeExhausted

try:
    ...
except TimeExhausted as e:
    ...

Source: web3.py/test_transactions.py at master · eric-s321/web3.py

Upvotes: 1

Related Questions