Ethers.js: Question: best strategy to get a replacement transaction?

Created on 28 Jun 2020  ·  8Comments  ·  Source: ethers-io/ethers.js

Support transaction T_0 was submitted by user with nonce n, and later they send a new transaction T_1 with nonce n, before T_0 was mined. Regardless whether or not T_1 does get mined (maybe it does, maybe it doesn't, or maybe there is an even newer T_k), how would I go about getting the transaction hash for T_1 or T_k given the transaction hash of T_0 easily?

Etherscan seems to have some kind of way of detecting this, see here for example (image below).

image

discussion

All 8 comments

This information isn't available in general on Ethereum.

Etherscan does this by creating a massive set of additional indices, and keeping a copy of the transaction pool, including stale transactions.

Some services may offer insight into the transaction pool, but it isn't something I've seen. I've written my own transaction pool monitoring scrips, which I'll be releasing soon as examples of who to accomplish this, but it requires running a custom service that is always connected. My scripts are for a gas station, but with slight modification they could be a general transaction pool monitor. I should create that as an example too, since it is something people want...

Makes sense.

Closing this now, but please feel free to re-open if you think there is anything missing, or continue commenting if you find out more. Future people may stumble upon this issue.

Thanks! :)

If I know the nonce of T_0, is there a good way to poll for success or failure of a tx with that nonce without running an indexing service?

Let's say I don't need any info about the mempool - just what eventually happened on-chain for that particular nonce.

I'm guessing the answer may be that I'd need to maintain an index that tracks the mapping of nonce to tx hash?

This is a short library I’m putting together to handle this. Mostly for the case you use MetaMask, which means the TX hash can change (e.g. the TX is cancelled or “speed up”-ed).

The best way I have come up with is to store the block number at the time the TX is submitted. Then poll for the waitForTransaction(hash) and transactionCount("latest"), until one changes. If the receipt is returned, the transaction was in modified and it will contain the block number and hash. Otherwise, if the nonce changes, it was mined but modified. You will need to use getBlockWithTransactions and scan the transactions matching the from and nonce near where the nonce changed to find it.

It’s a bit complex which is why I want to abstract it into a library. If you have archival support, I also have a little script that uses binary search using getTransactionCount that finds all the transactions. I need to get all these added tot he cookbook section of the documentation. :)

I am also look for the best solution to detect the replaced tx, seems there's no easy way to do it

there is an EIP-2831 Transaction Replacement Message for it

I will keep check the progress

Heya!

Sorry, forgot to update this issue sooner. Ethers now supports detecting replacement transactions. For more details, see the May highlights article. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pointtoken picture pointtoken  ·  24Comments

Levino picture Levino  ·  35Comments

pkieltyka picture pkieltyka  ·  36Comments

fmsouza picture fmsouza  ·  44Comments

Tomix3D picture Tomix3D  ·  39Comments