Over the past couple weeks the hype around decentralized finance (DeFi) applications on Ethereum has grown exponentially. From yield farming to decentralized lending, a lot of innovative products have been released, but few have experienced the growth and usage that Uniswap has.
With a rise in both the number of users trading on Uniswap and the amount of transactions being submitted, the Ethereum network has tended to face an abnormal amount of congestion lately and with that has come higher transaction fees. Not only that, as many of the assets on Uniswap are small or micro cap assets, the volatility is extremely high and transactions frequently end up reverting due to the configurable slippage not being high enough or gas prices becoming elevated mid-transaction which causes the transaction to get stuck. While MetaMask does provide built-in functionality for speeding up a pending transaction, in my experience this normally doesn’t tend to help and just results in having two pending transactions instead of one. In this guide I’m going to walk you through how to cancel your transaction by leveraging MetaMask’s advanced gas controls feature and custom nonce feature.
Enabling Advanced Settings for Gas and Nonce
While in the MetaMask UI, click the account logo at the top right and click on Settings. Then go into the Advanced menu — this is where we’ll enable the features that will allow for replacing a pending transaction.
To enable Advanced Gas Controls and Custom Nonces, enable the two features below:
As a final step in preparing to replace the transaction, we’ll need to get our pending transaction nonce. This can be done by visiting your address on Etherscan and clicking the <# pending> Pending link above your transaction list.
Then we’ll need the nonce for the pending transaction.
Now we’re ready to start replacing and canceling transactions!
Option 1: Submitting a new Swap transaction as a replacement
Let’s say the Swap you submitted was to buy Tether (USDT) with your Ether (ETH) and you don’t only want to cancel the transaction, you want to replace it. What you can do is attempt to submit another Swap like you normally would, but we’ll adjust the transaction attributes using the advanced features we enabled. First, we’ll adjust the transaction speed to the fastest possible.
Then we’ll bump up the gas price in gwei. Normally if the gas price defaulted to something like 100 gwei when using the fastest MetaMask option I’d try bumping it up to 125–150, but that’s completely up to you — just ensure that the gas price is higher than the pending transaction. Then, in the custom nonce field at the bottom of the transaction confirmation page enter the nonce of the pending transaction. This will allow this transaction to be queued up for the same “transaction slot” in your address’s history, and with a higher transaction fee being paid hopefully it gets picked up in a block relatively quickly and cancels out the older pending transaction. For more information on what a nonce is, refer to MyCrypto’s guide. Understanding this attribute of Ethereum transactions is useful in helping grasp why this works.
If the new transaction has been picked up you should see the two pending transactions for the nonce dropped out of your pending transaction list and the new Swap transaction confirmed on the main Etherscan transaction list.
Option 2: Submitting an empty transaction (0 ETH) to cancel the transaction
The second option is for a scenario in which you want to flat out cancel the transaction and not replace it. Let’s say you attempted to buy Token A at a price of 0.001 ETH and the price has shot up to 0.01 ETH and your transaction is still pending. Maybe that price change is outside of the range that you’d be willing to pay so instead of submitting another Swap we just want to clear out that pending transaction. To do this, we’ll follow a similar process as in Option 1, but instead of submitting a new Swap transaction we’ll send a 0 ETH transaction to our own address. This is done to queue up a new transaction with a higher gas fee at the same nonce without actually having to do any transfer of Token A for Token B.
To do this, copy your address in MetaMask by clicking on it at the top of the UI and hit the Send button. Then paste this same address in the recipient field.
Then click next on that first page to get to the transaction confirmation page that you would normally see. Follow the same process as documented in Option 1 — choose the fastest MetaMask transaction speed to get a reasonable default then bump the gas price up slightly. Don’t forget to enter your custom nonce to match the transaction you’re canceling!
If everything worked as expected and the transaction was included in a confirmed block you should see a SELF transaction in your Etherscan transaction history vs. an IN or OUT transaction.
Congratulations, now you know how to replace or cancel pending transactions using MetaMask!
A big shout out to MyCrypto for the guide they wrote that I used as a basis for this post. I suggest reading their guide just to learn about this process from a different perspective:
How To Cancel or Replace a Stuck Transaction
In certain cases you may want to cancel or replace a transaction, for example, when you used a gas price that was too…
Have fun on Uniswap and be careful!