How to Cancel a Transaction on Ethereum
This guide explains how to cancel a pending transaction using MetaMask, Alchemy’s SDK, or Web3
Once your transaction gets confirmed on the Ethereum network, you cannot cancel it. However, you can effectively cancel transactions still in the Mempool by creating a second transaction with the same nonce (number only used once) but a higher gas fee. Since miners are incentivized to include the second transaction (with the higher gas fee) first, the nonce will be “used,” and the original transaction will be dropped from the mempool.
Here are some possible reasons you might cancel a transaction:
- You sent a transaction with a meager gas fee that is now stuck in a pending state.
- You already know the transaction is going to fail.
- You want to create a failsafe if something goes wrong with your transaction.
Cancel Transaction: MetaMask
Metamask has a built-in feature that allows you to cancel pending transactions with one click.
Set the max priority gas fee higher than your original transaction to ensure cancellation.
Cancel Transaction: Alchemy’s SDK
In this section, we will create our own cancellation transaction using Alchemy’s SDK. (Web3.js code examples are also included.)
Prerequisites
Before you begin this tutorial, please ensure you have the following:
- An Alchemy account (Create a free Alchemy account).
- An Ethereum address or MetaMask wallet (Create a MetaMask wallet).
- NodeJS and npm installed (Install NodeJs and NPM).
Connect to Alchemy
- From the Alchemy Dashboard, hover over Apps, then click +Create App.
- Name your app: Cancel-Tx.
- Select Ethereum as your chain and Sepolia as your network.
- Click Create app.
Request ETH from the Alchemy Sepolia faucet
- From Sepolia Faucet, sign in with your Alchemy account.
- Paste your MetaMask address and click Send me ETH.
While you can use the Goerli testnet, we caution against it as the Ethereum Foundation has announced that Goerli will soon be deprecated.
We therefore recommend using Sepolia as Alchemy has full Sepolia support and a free Sepolia faucet also.
Setup Project Environment
Open VS Code (or your preferred IDE) and enter the following in the terminal:
Once inside our project directory, initialize npm (node package manager) with the following command:
Press enter and answer the project prompt as follows:
Press enter again to complete the prompt. If successful, a package.json
file will have been created in your directory.
Install environment tools
The tools you will need to complete this tutorial are:
- Alchemy’s SDK
- dotenv (so that you can store your private key and API key safely)
To install the above tools, ensure you are still inside your root folder and type the following commands in your terminal:
Alchemy SDK:
Dotenv:
Create a Dotenv File
Create a .env
file in your root folder. The file must be named .env
or it will not be recognized.
In the .env
file, we will store all of our sensitive information (i.e., our Alchemy API key and MetaMask private key).
Copy the following into your .env
file:
- Replace
{YOUR_ALCHEMY_API_KEY}
with your Alchemy API key found in your app’s dashboard, under VIEW KEY:
- Replace
{YOUR_PRIVATE_KEY}
with your MetaMask private key.
To retrieve your MetaMask private key:
- Open the extension, click on the three dots menu, and choose Account Details.
- Click Export Private Key and enter your MetaMask password.
- Replace the Private Key in your
.env
file with your MetaMask Private Key.
Create cancelTx.js
file
Create a file named cancelTx.js
and add the following code:
The code above sets up our Alchemy SDK to connect to the blockchain and send transactions.
Next, let’s create two transactions. The first will be the transaction we intend to cancel and the second will be our cancellation transaction. See the code below, noting specifically lines 17-29:
Notice that we set the same nonce for both transactions.
Here’s an overview of the transaction parameters:
gasLimit
: The maximum amount of gas used to execute our transaction. 21000 wei is the minimum amount of gas an operation on Ethereum will use, so to ensure our transaction will be executed we put 53000 wei here.maxPriorityFeePerGas
: The miner’s tip. (Check out Alchemy’s guide to learn more)maxFeePerGas
: Transaction base fee + miner’s tipnonce
: (number only used once) used to keep track of transactions for a specific address.type
: The EIP-2718 type of the transaction envelope.
Finally, let’s send our transactions! For this, add the following try-catch statement, noting specifically lines 31-49:
Above, we call the sendTransaction
function and pass in our transaction.
To run our script, type this command in terminal:
If successful, you should see an output similar to the following:
On Alchemy, head to your app’s dashboard and navigate to the Mempool tab.
After your replacement transaction has been mined, you should see that your first transaction was dropped and replaced:
Nice work! You successfully created then canceled a transaction using Alchemy’s SDK!
Feel free to reach out to us in the Alchemy Discord for help or chat with us @Alchemy on Twitter!