40

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.

3
  • 3
    Note that you have on average less than 30 seconds before the transaction is mined, so better catch the error pretty quick Nov 21, 2017 at 22:12
  • How could I change the nonce on new transaction ?
    – Anisniakan
    Oct 8, 2018 at 5:27
  • If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review Oct 8, 2018 at 9:03

4 Answers 4

35

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.

3
  • How do you do this? could you provide code examples?
    – JZ.
    Dec 14, 2017 at 7:21
  • How do you find an existing transaction and update the nonce?
    – JZ.
    Dec 14, 2017 at 7:28
  • Okay but then how do I cancel the transaction with the lower gas price? It seems it remained stuck in the mempool. Oct 25, 2018 at 16:55
14

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.

Related

3
  • 1
    Conceptually what is the difference between cancelling and invalidating?
    – Sentinel
    Nov 21, 2017 at 20:31
  • I see cancelling as an explicit request to the network to dump the transaction, and invalidating as a workaround that limitation. Nov 21, 2017 at 20:51
  • Cancellation (atomicity) vs compensation (workflow). In accounting, a cancellation would be a compensation.
    – Sentinel
    Nov 21, 2017 at 21:45
3

A pending transaction can be cancelled by using a wallet that supports it.

For example, here are instructions from the MetaMask wallet with diagrams.

https://metamask.zendesk.com/hc/en-us/articles/360015489251-How-to-Speed-Up-or-Cancel-a-Pending-Transaction

Click the Cancel button. The other answers explain more about what happens behind the scenes.

enter image description here

1

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:

  1. An identical nonce,
  2. From the same Wallet address,
  3. Sent to an external account (not a smart contract), with
  4. At least a 10% higher gas fee,
  5. but Zero value, and
  6. 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.