Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wait for certain number of confirmations ? #229

Closed
subramanianv opened this issue Jul 13, 2018 · 10 comments
Closed

How to wait for certain number of confirmations ? #229

subramanianv opened this issue Jul 13, 2018 · 10 comments
Labels
enhancement New feature or improvement.

Comments

@subramanianv
Copy link

subramanianv commented Jul 13, 2018

Sometimes geth node returns nonce too low when sending transactions in succession. so if we can wait for certain number of confirmations, this problem can be solved

@ricmoo ricmoo added enhancement New feature or improvement. on-deck This Enhancement or Bug is currently being worked on. labels Jul 13, 2018
@ricmoo
Copy link
Member

ricmoo commented Jul 13, 2018

This is something I've been thinking of adding for a while, so I will add it next week to the v4 branch.

I have a few ideas on how to add it to the API, but will experiment this weekend with a few options.

@ricmoo
Copy link
Member

ricmoo commented Jul 16, 2018

This weekend was spent entirely just refactoring and getting the TypeScript definitions all cozy, but I think things are starting to settle, so I will be able to get to this soon. :)

aside: There was also a crazy bug in nodejs that causes it to sort Japanese strings in a different order than browsers, but that is fixed now too. And not all BIP39 wordlists do a final sanity check after loading, and fail loud if the checksum doesn't match the derived wordlist.

@ricmoo
Copy link
Member

ricmoo commented Oct 5, 2018

This has been added in 4.0.3.

On the TransactionsResponse wait method, confirmations can be passed à la tx.wait(confirmations), which will return the TransactionReceipt, similarly provider.waitForTransaction(confirmations) will do the same.

Thanks! :)

@ricmoo ricmoo closed this as completed Oct 5, 2018
@ricmoo ricmoo removed the on-deck This Enhancement or Bug is currently being worked on. label Oct 5, 2018
asnov added a commit to asnov/ethers.js that referenced this issue Oct 14, 2018
Merge commit '3736a1571480a0f69d632d6fc3bde549cbe46162' into fix/tslib

* commit '3736a1571480a0f69d632d6fc3bde549cbe46162': (41 commits)
  Updated dist files.
  Added automatic event parsing for contract transaction receipts from tx.wait.
  Added ability to wait for a specific number of confirmations (ethers-io#229).
  Fix for geth-etc (official geth is fine), which returns Receipts before the blockHash is synced to the database.
  Fixed confirmations tests and bootstrap fast blockNumber.
  Added confirmations to TransactionResponse (ethers-io#156, ethers-io#238).
  Fixed nested errors for providers that were masking true error (ethers-io#292).
  Updated dist files.
  Added version to errors.
  Fixed French and Spanish for browsers without Uint8Array.forEach.
  Added French and Spanish includes to phantomjs test page.
  Increased timeout for querying npm registry.
  Updated dist files.
  Added French and Spanish wordlist dist files.
  Added French and Spanish BIP-39 wordlists (ethers-io#191).
  Added support for JSON serialized BigNumbers in the constructor (ethers-io#288).
  Fixed scrypt for long passwords (ethers-io#223).
  Updated dist files.
  Added chainId as supported override for contract transactions.
  Fixed wildcard events and made nested events more robust (ethers-io#289).
  ...

Conflicts:
	dist/ethers.min.js
	dist/ethers.min.js.map
	package-lock.json
@pstuermlinger
Copy link

Is there any chance this will be rewritten to return something like web3's promievent (https://www.npmjs.com/package/web3-core-promievent)? I'd need to be notified about each new confirmation as well as about chain re-organisations, which might lower the # of confirmations or even set it to 0.
Currently, I don't see a clear path how I could accomplish this. Should I just do a provider.getTransactionReceipt(hash) periodically and watch for the confirmations property - and if that returns null assume the tx being erased from the chain due to a reorg?

@ricmoo
Copy link
Member

ricmoo commented Mar 27, 2019

No, I those Prominevent things seem crazy to me. :p

You could exactly do as you describe:

Example:

let tx = contract.someFunction(params);

let lastConfirms = -1;
async function handler(blockNumber) {
    let receipt = await provider.getTransactionReceipt(tx.hash);
    if (receipt == null || receipt.confirmation < lastConfirms) {
        // A re-org; do what you need here
    }
}
provider.on("block", handler);

tx.wait(3).then((receipt) => {
    // This gets called once there are 3 confirmations
    provider.removeListener("block", handler);
});

In v5, this will be much easier, since you can just use provider.on("fork") or use a more complex event:

let forkEvent = new ForkEvent();

// Trigger if the block is orphaned
forkEvent.watchBlock(hash);

// Trigger if the confirmations ever lower
forkEvent.watchTransaction(hash);

// Trigger if the block hash (once set) changes
forkEvent.watchTransactionBlock(hash);

// Trigger if the transaction does not happen after other
formEvent.watchTransactionAfter(hash, otherHash);

provider.on(forkEvent, (event) => {
})

This API is still experimental, and I'm working on it now. :)

@pstuermlinger
Copy link

Hi Richard,
could receipt == null not also mean "hasn't been mined yet"? So probably the condition should be if (lastConfirms > 0 && (receipt == null || receipt.confirmations < lastConfirms)), shouldn't it?

The capabilities of the v5 API are looking promising. I'm very delighted at it :)

Thanks for your constant support and development on this project! 👍

@EvilJordan
Copy link

Just found this closed issue and am thrilled there's already some thinking around it!

What would you recommend is the best way these days, given this is a year-and-a-half old, to wait to parse a transaction once a certain number of confirmations are hit, without blocking the rest of the program?

@EvilJordan
Copy link

Obviously, tx.wait(n) is the right way to do this, but how to combine with a contract listener with filter?

Does this make sense?:

contract.on(filter, (from, to, value, event) => {
	parseTransaction(event);
});

parseTransaction = async (transaction) => {
	const tx = await transaction.getTransaction();
	tx.wait(5);
	...
}

@ricmoo
Copy link
Member

ricmoo commented Jan 5, 2021

@EvilJordan

Something like that would work fine, or you could use await provider.waitForTransaction(event.transactionHash, 5);

It probably makes sense for me to add confimarions to the event.getTransactionReceipt in v6... I’ll add it to my todo list. :)

@EvilJordan
Copy link

It works! Happy New Year! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement.
Projects
None yet
Development

No branches or pull requests

4 participants