Search code examples
ethereumethers.jsether

Flushing pending ethereum transactions with ethers


I'm trying to work out how to clear a pending transaction with the ethers library.

It's ERC20 transfer function that's been called, several times, and quite a few stuck pending.

So, I thought I'd just be able to use ethers library to sendTransaction with the same nonce to flush the transactions.

What I've tried, assuming 100gwei;

const tx = {
  gasPrice: ethers.BigNumber.from(100000000000),
  nonce: 1,
}

wallet.sendTransaction(tx)

Which results in "err: max fee per gas less than block base fee: address xxx, maxFeePerGas: 100000000000 baseFee: 110470877095 (supplied gas 10440696)"}}'.

Uncertain if I'm supposed to have a to in the transaction, and also... supplied gas, should I be adding a gasLimit to the transaction? Uncertain if data or value should contain anything, as cancelling a transaction is done with a 0 value right?


I accidentally cancelled the two first transactions instead by removing the gasPrice and just including the nonce. Damnit, now I have to figure out if I have to cancel all those after or if I can send in transactions with the same nonce.


Solution

  • SOLVED

    I'm so annoyed at how search engine ranking has gone down the drain, all the articles I tried to look at were written by the creators of some wallet or other... I digress.

    I seemed to read that adding nonce and to should be enough several times. Not enough. Just adding the nonce without anything cancels the transaction. nonce, to, gasPrice will complain about gasLimit as it can't work out how much gas a contract method needs (my case). Doing these will just result in a reverted transaction anyway as no data is sent.

    So,

    const tx = {
      to: {CONTRACT_ADDR},
      nonce: x,
      gasLimit: 58000 (just check what its consumed before and add some),
      gasPrice: ethers.utils.parsUnits('100', 'gwei')
      data: {TRANSACTION DATA IN HEX}
    

    Celebrate.