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

provider.once returns text instead of numbers #30

Open
Owenchan007 opened this issue Jan 20, 2024 · 2 comments
Open

provider.once returns text instead of numbers #30

Owenchan007 opened this issue Jan 20, 2024 · 2 comments

Comments

@Owenchan007
Copy link

In this section, following the tutorial should return the number 1, but I got the wrong text instead

CODE

function listenForTransactionMine(transactionResponse, provider) {
    console.log(`Mining ${transactionResponse.hash}...`)
    // create a listener for the blockchain
    return new Promise((resolve, reject) => {
        provider.once(transactionResponse.hash, (transactionReceipt) => {
            console.log(
                `Completed with ${transactionReceipt.confirmations} confirmations..`
            )
            resolve()
        })
    })
}

ERROR

Completed with async confirmations() {
        return (await this.provider.getBlockNumber()) - this.blockNumber + 1
    } confirmations..
@Owenchan007
Copy link
Author

Does this have anything to do with my use of ether.js 6?

@Bijan-F
Copy link

Bijan-F commented Mar 27, 2024

i had the same issue and found the answer here:
ethers-io/ethers.js#4256

my final listen for tx mine function:

function listenForTransactionToBeMined(txResponse, provider) {
    console.log(`Mining ${txResponse.hash}...`);
    return new Promise((resolve, reject) => {
        provider.once(txResponse.hash, async (txReceipt) => {
            await txResponse.wait(1);
            console.log(`Completed with ${await txReceipt.confirmations()} confirmations!`);
            resolve();
        });
    })
}

So basically transactionReceipt.confirmations is a method which means it should be written like this:
transactionReceipt.confirmations()
and it also returns a promise so you should use await and since the listenForTransactionToBeMined isn't async and we want it to stay like this(for some reason idk why), i added the async keyword before the anonymous function in the second argument of provider.once:
provider.once(txResponse.hash, async (txReceipt) => {

and i also figured out if i don't add the await txResponse.wait(1); line, it logs with 0 confirmations.

hope this helps

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

No branches or pull requests

2 participants