14

In ethers.js,

provider.sendTransaction(rawTransaction).then((transaction) => {
    // A full Transaction Response is returned
    // - from
    // - to
    // - gasLimit, gasPrice
    // - nonce
    // - r, s, v
    // - wait() => Promise that resolves the Transaction Receipt once mined
    //             and rejects with an error is the stats is 0; the error
    //             will have a transactionHash property as well as a
    //             transaction property.

    let hash = transaction.hash;
});

Here it will return the receipt after wait() how can I write the wait in my code?

I use:

.then(function(tx){
wait()=>{}
})

This will not work.

Louis
  • 1,155
  • 5
  • 17
  • 29

3 Answers3

8
const sendTransaction = async() => {
    const transaction = await provider.sendTransaction(rawTransaction);
    // wait() has the logic to return receipt once the transaction is mined
    const receipt = await wait(transaction);
}

In this way you can write a wait in your code.

balajipachai
  • 937
  • 5
  • 12
7

For ethers.js v5:

let receipt = await tx.wait()
Travis Reeder
  • 471
  • 5
  • 8
6

Either like this:

wait().then(function(receipt) {
    // do whatever you wanna do with `receipt`
});

Or like this if you call the whole thing from inside an async function:

const receipt = await wait();
// do whatever you wanna do with `receipt`
goodvibration
  • 26,003
  • 5
  • 46
  • 86