Skip to content

Instantly share code, notes, and snippets.

@karlb
Created December 2, 2021 13:17
Show Gist options
  • Select an option

  • Save karlb/4ac39436f634c8d2429a5ef8acb94fc7 to your computer and use it in GitHub Desktop.

Select an option

Save karlb/4ac39436f634c8d2429a5ef8acb94fc7 to your computer and use it in GitHub Desktop.
Cancel pending ethereum transactions
import eth_keyfile
from web3 import Account, Web3
from web3.middleware import geth_poa_middleware
w3 = Web3(Web3.HTTPProvider("http://geth.goerli.ethnodes.brainbot.com:8545"))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
addr = "8f5ea3bb3e99c317b8745918f6973f7afbf659c3"
first_stuck = 226164
stuck_nonces = range(first_stuck, first_stuck + 20)
gas_price = w3.toWei(10, "gwei")
keyfile = eth_keyfile.load_keyfile(addr + ".wallet")
pk = Account.decrypt(keyfile, open(addr + ".key").read())
account = Account.from_key(pk)
for nonce in sorted(stuck_nonces):
signed = w3.eth.account.sign_transaction(
dict(
to=account.address,
value=0,
gas=210000,
gasPrice=gas_price,
nonce=nonce,
chainId=w3.eth.chain_id,
),
pk,
)
txhash = w3.eth.send_raw_transaction(signed.rawTransaction)
print(nonce, "->", txhash.hex())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment