Is it possible to cancel a transaction after it has been broadcasted, but before it has been mined?
This could be useful on sending an erroneous transaction containing invalid data.
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.Sign up to join this community
It can't be canceled, but it can be "out-gassed". You can grab the nonce (can be found on the pending tx on etherscan) and send another transaction with a higher gas price with the same nonce. If you use a different nonce, then they can both be mined, but if you use the same nonce, only one can be mined. If miners see both, they should be smart enough to choose to mine the tx with the higher gas price.
While it's not possible to explicitly cancel a transaction, you can invalidate the transaction before it's mined. The way this works is by broadcasting the same transaction using the same nonce, but with a higher gas fee. The miners are incentivized to process transactions with higher fees. If they process the newer transaction, then the old transaction becomes invalid and therefore rejected.
It's also possible to have set the gas fee too low in the original transaction and they reject the transaction after some time, but it's pretty unlikely.
A pending transaction can be cancelled by using a wallet that supports it.
For example, here are instructions from the MetaMask wallet with diagrams.
Click the Cancel button. The other answers explain more about what happens behind the scenes.
The team at Blocknative wrote an excellent post detailing Canceled Transactions that answers your question: https://blog.blocknative.com/blog/canceled-transactions
Here is what they say about Canceled TX:
What are Canceled transactions?
On Ethereum, a Cancel transaction is an attempt to overwrite a currently pending transaction with a new transaction. It's important to note that Canceled transactions are a convention and not a standard. Typically, a Cancel transaction will have:
- An identical nonce,
- From the same Wallet address,
- Sent to an external account (not a smart contract), with
- At least a 10% higher gas fee,
- but Zero value, and
- Gets signed-and-successfully-submitted before the original transaction is confirmed.
The core of the idea is that, since miners are incentivized to prioritize transactions with more gas, the cancel transaction should be confirmed before the original transaction – even though it entered the mempool later. In other words, Cancel transactions are kind of like probabilistic mulligans.
So yes, you can submit a Cancel TX to the network to try and invalidate a pending TX. But nothing is guaranteed. Once you submit a Cancel you must hope that it is mined before your original transaction.