-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Comments
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. |
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. |
This has been added in On the TransactionsResponse wait method, confirmations can be passed à la Thanks! :) |
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
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. |
No, I those 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 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. :) |
Hi Richard, 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! 👍 |
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? |
Obviously, Does this make sense?: contract.on(filter, (from, to, value, event) => {
parseTransaction(event);
});
parseTransaction = async (transaction) => {
const tx = await transaction.getTransaction();
tx.wait(5);
...
} |
Something like that would work fine, or you could use It probably makes sense for me to add |
It works! Happy New Year! 🥳 |
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
The text was updated successfully, but these errors were encountered: