Canceling lots of Ethereum Transactions using CLI tools (seth, geth)
It’s a turbulent time and Ethereum gas price is more volatile than Bitcoin price. You’re sending txs with tears but yet they still lost the gas price competition and got stuck for too long. You want to cancel them.
If you’re using browser tools like Metamask or MyCrypto, you could do it through its own UI; however, if you prefer to do it in batches or in a more composable way, here’s how to do it in the command line.
You will need two packages installed in your system: geth
& seth
and an Ethereum JSON-RPC endpoint.
The endpoint could be a cloud service like Infura, or it could be your own full-node by running geth
. How to setup Infura or run a full-node is outside the scope of this article, so going forward we will assume you already have one of the said endpoints ready.
geth
, like mentioned above, is one of the most popular Ethereum full-node software. But we don’t necessarily have to run a full-node; we are just using it for wallet management so your private keys can be kept safe locally.
seth
is a CLI wrapper for sending Ethereum JSON-RPC requests. It searches geth
for your wallet and sends the tx out to either Infura or your own geth
full-node.
Install geth and Import Your Wallets
geth
's official wiki has thorough instructions for installation on various OS. Once installed, you can import your wallet through CLI.
Put your private key in a text file (ex. /path/to/your/private-key
). Note this is temporary and we will remove the file later for safety. Also the private key SHOULD NOT have the 0x
prefix.
Next, import it with the following command. You will be asked for password to protect the wallet:
$ geth account import /path/to/your/private-keyINFO [07-25|15:37:07.145] Maximum peer count ETH=50 LES=0 total=50Your new account is locked with a password. Please give a password. Do not forget this password.Password:Repeat password:
Now you can safely delete /path/to/your/private-key
since it is already imported and stored safely inside geth
, and you probably don’t want to continue holding your private key in a plain text file.
Install seth
Before installing seth
you need to install its dependencies nix
, which can be done in a one-liner; however, as of writing, there are some extra settings required to install nix
in newer Mac OS like 10.15 Catalina and beyond, so:
For non-Mac OS or earlier Mac OS before 10.15:
sh <(curl -L https://nixos.org/nix/install)
For Mac OS 10.15 and beyond:
sh <(curl -L https://nixos.org/nix/install) --darwin-use-unencrypted-nix-store-volume
Once nix
is installed, you can install seth
with another one-liner:
curl https://dapp.tools/install | sh
However, for some reasons it did not work for me, so I did it manually. Follow the instructions below if the one-liner above does not work for you either:
$ nix-shell --pure -p cacert cachix curl git jq nix[nix-shell:~/tmp]$ cachix use dappConfigured https://dapp.cachix.org binary cache in /Users/detoo/.config/nix/nix.conf[nix-shell:~/tmp]$ git clone --recursive https://github.com/dapphub/dapptools $HOME/.dapp/dapptools[nix-shell:~/tmp]$ nix-env -f $HOME/.dapp/dapptools -iA dapp seth solc hevm ethsign...created 29 symlinks in user environment
Cancel the Transactions
Now everything is ready and let’s try cancelling a tx.
Assuming that your wallet address is 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
, and the nonce of the tx you want to cancel is 123
and its gas price is 30 gwei
. What we are going to do is actually sending a replacement no-op tx with the same nonce and a higher gas price, which is effectively “cancelling” the original tx by overwriting it with a no-op. A typical example of a no-op tx is to send 0 ether to yourself, this will (1) change nothing and (2) spend very little gas, which is what we want for a cancellation.
To send such tx, run the command:
$ seth --rpc-url=https://url.to.your.endpoint send -F 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef --value=0 --nonce=123 --gas-price=$(seth --to-wei 40 gwei)Ethereum account passphrase (not echoed):
--rpc-url
should be the endpoint you got from Infura, or http://localhost:8545
if you are running geth
full-node with default settings.
Note we are sending the new tx with gas price at least 10% higher than the old ones so the miner is more likely to accept it.
Since we specified the account to send transaction from (-F
), seth
will automatically search geth
keystore for the wallet. If you have imported the private key for 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
in the previous steps, seth
will find it and you will be prompt for the password. Enter the password you set during the import, then it will sign and send the tx and wait for the confirmation. Once confirmed, the cancellation is complete!
Did Somebody Say Composability?
The nice thing about CLI is you can “program” it. Say if you are running a bot and it got stuck with not just one but 10 low gas txs. Instead of running the command 10 times, you could write a simple script to automate the process:
$ for i in {123..132}; do seth --rpc-url=https://url.to.your.endpoint send -F 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef 0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef --value=0 --nonce=$i --gas-price=$(seth --to-wei 40 gwei) --password=/path/to/your/password/file; done
Note the extra parameter --password
. We want the process automated and can do so by writing the password in a separate file /path/to/your/password/file
and point seth
to it, so it won’t prompt for password every single time.
Onwards to productivity… and profit?