Javascript

在 ethers.js 中如何處理 wait()?

  • October 29, 2021

在 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;
});

wait()在我如何在我的程式碼中寫入等待之後,它將在這裡返回收據?

我用:

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

這行不通。

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);
}

通過這種方式,您可以在程式碼中編寫等待。

要麼像這樣:

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

async或者如果你從一個函式內部呼叫整個東西,就像這樣:

const receipt = await wait();
// do whatever you wanna do with `receipt`

引用自:https://ethereum.stackexchange.com/questions/80619