confirmation
Documentation for eth_defi.confirmation Python module.
Transaction broadcasting, block confirmation and completion monitoring.
Wait for multiple transactions to be confirmed and read back the results from the blockchain
The safest way to get transactions out is to use
wait_and_broadcast_multiple_nodes()
Some notes
Functions
Broadcast and wait a bunch of signed transactions to confirm. |
|
|
Broadcast and wait a bunch of signed transactions to confirm. |
|
Check for nonce re-use issues. |
|
from address missing in the tx payload |
|
|
|
Try to broadcast transactions through multiple nodes. |
Broadcast transactions through a MEV blocker enabled endpoint. |
|
|
Watch multiple transactions executed at parallel. |
Exceptions
Out of gas funds for an executor. |
|
Could not broadcast a transaction for some reason. |
|
We exceeded the transaction confirmation timeout. |
|
Don't try to rebroadcast these. |
|
Chain has a different nonce than we expect. |
|
Out of gas funds for an executor. |
|
Out of gas funds for an executor. |
|
Transaction reverted on-chain. |
- exception BroadcastFailure
Bases:
ExceptionCould not broadcast a transaction for some reason.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception ConfirmationTimedOut
Bases:
ExceptionWe exceeded the transaction confirmation timeout.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception NonRetryableBroadcastException
Bases:
ExceptionDon’t try to rebroadcast these.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception NonceMismatch
Bases:
ExceptionChain has a different nonce than we expect.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception OutOfGasFunds
Bases:
eth_defi.confirmation.NonRetryableBroadcastExceptionOut of gas funds for an executor.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception NonceTooLow
Bases:
eth_defi.confirmation.NonRetryableBroadcastExceptionOut of gas funds for an executor.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception BadChainId
Bases:
eth_defi.confirmation.NonRetryableBroadcastExceptionOut of gas funds for an executor.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception Reverted
Bases:
ExceptionTransaction reverted on-chain.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- is_invalid_sender(eth_rpc_error_messag)
from address missing in the tx payload
- wait_transactions_to_complete(web3, txs, confirmation_block_count=0, max_timeout=datetime.timedelta(seconds=300), poll_delay=datetime.timedelta(seconds=1), node_switch_timeout=datetime.timedelta(seconds=60))
Watch multiple transactions executed at parallel.
Use simple poll loop to wait all transactions to complete.
If
web3is configured to useeth_defi.provider.fallback.FallbackProvider, try to switch between alternative node providers when confirming the transactions, because sometimes low quality nodes (Ankr, LlamaNodes) do not see transactions for several minutes.Example:
raw_bytes1 = get_tx_broadcast_data(signed1) tx_hash1 = web3.eth.send_raw_transaction(raw_bytes) raw_bytes2 = get_tx_broadcast_data(signed2) tx_hash2 = web3.eth.send_raw_transaction(raw_bytes2) complete = wait_transactions_to_complete(web3, [tx_hash1, tx_hash2]) # Check both transaction succeeded for receipt in complete.values(): assert receipt.status == 1 # tx success
- Parameters
txs (List[Union[hexbytes.main.HexBytes, str]]) – List of transaction hashes
confirmation_block_count (int) – How many blocks wait for the transaction receipt to settle. Set to zero to return as soon as we see the first transaction receipt.
node_switch_timeout –
Switch to alternative fallback node provider every time we reach this limit.
Sometimes our node is malfunctioning (LlamaNodes, Ankr) and does not report transactions timely. Try with another node.
See
eth_defi.provider.fallback.FallbackProviderfor details.web3 (web3.main.Web3) –
- Returns
Map of transaction hashes -> receipt
- Return type
- broadcast_transactions(web3, txs, confirmation_block_count=0, work_around_bad_nodes=True, bad_node_sleep=0.5)
Broadcast and wait a bunch of signed transactions to confirm.
Multiple transactions can be broadcasted and confirmed in a single go, to ensure fast confirmation batches.
- Parameters
web3 (web3.main.Web3) – Web3
txs (List[eth_account.datastructures.SignedTransaction]) – List of Signed transactions
work_around_bad_nodes – If true try to work around issues with low quality JSON-RPC APIs like Ganache by checking if the transaction broadcast succeeded
confirmation_block_count – How many blocks wait for the transaction receipt to settle. Set to zero to return as soon as we see the first transaction receipt or when using insta-mining tester RPC.
- Returns
List of tx hashes
- Raises
If the JSON-RPC node rejects the transaction.
Anvil will reject some transactions immediately: if there is not enough gas money
Ethereum Tester reject some transactions immediately on any error in automining mode
- Return type
List[hexbytes.main.HexBytes]
- broadcast_and_wait_transactions_to_complete(web3, txs, confirm_ok=True, work_around_bad_nodes=True, confirmation_block_count=0, max_timeout=datetime.timedelta(seconds=300), poll_delay=datetime.timedelta(seconds=1))
Broadcast and wait a bunch of signed transactions to confirm.
Multiple transactions can be broadcasted and confirmed in a single go, to ensure fast confirmation batches.
- Parameters
web3 (web3.main.Web3) – Web3
txs (List[eth_account.datastructures.SignedTransaction]) – List of Signed transactions
confirm_ok – Raise an error if any of the transaction reverts
max_timeout – How long we wait until we give up waiting transactions to complete
poll_delay – Poll timeout between the tx check loops
work_around_bad_nodes – If true try to work around issues with low quality JSON-RPC APIs like Ganache by checking if the transaction broadcast succeeded
confirmation_block_count (int) – How many blocks wait for the transaction receipt to settle. Set to zero to return as soon as we see the first transaction receipt.
- Returns
Map transaction hash -> receipt
- Raises
Reverted – If the transaction did not go through and confirm_ok is set.
- Return type
- wait_and_broadcast_multiple_nodes(web3, txs, confirmation_block_count=0, max_timeout=datetime.timedelta(seconds=300), poll_delay=datetime.timedelta(seconds=1), node_switch_timeout=datetime.timedelta(seconds=180), check_nonce_validity=True, mine_blocks=False, inter_node_delay=datetime.timedelta(seconds=60))
Try to broadcast transactions through multiple nodes.
Broadcast transaction through all nodes
Wait to confirm
If
node_switch_timeoutis reached, try to confirm using an alternative node
- Parameters
web3 (web3.main.Web3) – Web3 instance with
eth_defi.provider.fallback.FallbackProviderconfigured as its RPC provider.txs (Collection[Union[eth_account.datastructures.SignedTransaction, eth_defi.hotwallet.SignedTransactionWithNonce]]) –
List of transaction to broadcast.
Most be pre-ordered by
(address, nonce).confirmation_block_count (int) – How many blocks wait for the transaction receipt to settle. Set to zero to return as soon as we see the first transaction receipt.
node_switch_timeout –
Switch to alternative fallback node provider every time we reach this limit.
Sometimes our node is malfunctioning (LlamaNodes, Ankr) and does not report transactions timely. Try with another node.
See
eth_defi.provider.fallback.FallbackProviderfor details.check_nonce_validity – Check if signed nonces match on-chain data before attempting to broadcat.
mine_blocks –
For forked mainnet RPCs (Anvil) make sure the blockchain is making blocks.
Only use with Anvil.
inter_node_delay –
Work around bad JSON-RPC SaaS providers.
Sleep this time between multiple tx broadcasts.
See https://github.com/ethereum/go-ethereum/issues/26890
Problematic providers: Alchemy.
Reset for Anvil to make unit tests faster.
- Returns
Map of transaction hashes -> receipt
- Raises
ConfirmationTimedOut – If we cannot get transactions out
Starting nonce does not match what we see on chain.
When
check_nonce_validityis set.If all nodes fail to broadcast the transaction, then raise an exception.
It’s likely that there is a problem with a transaction.
The exception is raised after we try multiple nodes multiple times, based on
node_switch_timeoutand other arguments.A reverted transaction is not an exception, but will be returned in the receipts.
In the case of multiple exceptions, the last one is raised. The exception is whatever lower stack is giving us.
OutOfGasFunds – The hot wallet account does not have enough native token to cover the tx fees.
- Return type
- check_nonce_mismatch(web3, txs)
Check for nonce re-use issues.
Compare pre-signed transactions with on-chain addresses’ nonce states.
- Raises
NonceMismatch – If your transaction broadcast is going to fail because nonce too low.
- Parameters
web3 (web3.main.Web3) –
txs (Collection[Union[eth_account.datastructures.SignedTransaction, eth_defi.hotwallet.SignedTransactionWithNonce]]) –
- wait_and_broadcast_multiple_nodes_mev_blocker(provider, txs, max_timeout=datetime.timedelta(seconds=600), poll_delay=datetime.timedelta(seconds=10), broadcast_and_read_delay=datetime.timedelta(seconds=6), try_other_provider_delay=datetime.timedelta(seconds=45))
Broadcast transactions through a MEV blocker enabled endpoint.
Cannot transact multiple transactions simultaneously, need to broadacst and confirm one by one
For all transactions
Broadcast transaction
- Wait until it is confirmed
To avoid nonce errors
- Parameters
web3 – Web3 instance with
eth_defi.provider.fallback.FallbackProviderconfigured as its RPC provider.txs (Collection[Union[eth_account.datastructures.SignedTransaction, eth_defi.hotwallet.SignedTransactionWithNonce]]) –
List of transaction to broadcast.
Most be pre-ordered by
(address, nonce).check_nonce_validity – Check if signed nonces match on-chain data before attempting to broadcat.
provider (eth_defi.provider.mev_blocker.MEVBlockerProvider) –
- Returns
Map of transaction hashes -> receipt
- Raises
ConfirmationTimedOut – If we cannot get transactions out
Starting nonce does not match what we see on chain.
When
check_nonce_validityis set.If all nodes fail to broadcast the transaction, then raise an exception.
It’s likely that there is a problem with a transaction.
The exception is raised after we try multiple nodes multiple times, based on
node_switch_timeoutand other arguments.A reverted transaction is not an exception, but will be returned in the receipts.
In the case of multiple exceptions, the last one is raised. The exception is whatever lower stack is giving us.
OutOfGasFunds – The hot wallet account does not have enough native token to cover the tx fees.
- Return type