Ethers.js: Differing `wait` and `waitForTransaction` behaviors

Created on 6 Apr 2021  路  8Comments  路  Source: ethers-io/ethers.js

This is a bit of an interesting one!

We have a TransactionResponse that we register a wait() callback on a la:

const response = await signer.sendTransaction(...);
response.wait(r => console.log(r)).catch(e => console.log(e))

The response.hash is also passed to a helper that calls provider.waitForTransaction.

The transaction in question (0xd63407185e358f82bb87ec31e91e841fc2d34e8d23c36e7b44ca03573997a7b3 on xdai) hard-errored in wait(), but hung in waitForTransaction. Is there a better way to get the same behavior in both callbacks?

Here are some logs that may be useful for you from the instance:

  • Repl check of transaction
> const eth = require("ethers");

> const p = new eth.providers.JsonRpcProvider("https://rpc.xdaichain.com/");

> p.getTransaction("0xd63407185e358f82bb87ec31e91e841fc2d34e8d23c36e7b44ca03573997a7b3").then(r => console.log(r)).catch(e => console.log("error", e))
> null

> p.getTransactionReceipt("0xd63407185e358f82bb87ec31e91e841fc2d34e8d23c36e7b44ca03573997a7b3").then(r => console.log(r)).catch(e => console.log("error", e))
> null
  • Error from wait callback:
    ```json
    {
    "time":1617720629536,
    "method":"sendTxAndParseResponse",
    "error":{
    "message":"processing response error (body=\"{\\"jsonrpc\\":\\"2.0\\",\\"error\\":{\\"code\\":-32000,\\"message\\":\\"Block information is incomplete while ancient block sync is still in progress, before it's finished we can't determine the existence of requested item.\\"},\\"id\\":4691}\n\", error={\"code\":-32000}, requestBody=\"{\\"method\\":\\"eth_getTransactionReceipt\\",\\"params\\":[\\"0xd63407185e358f82bb87ec31e91e841fc2d34e8d23c36e7b44ca03573997a7b3\\"],\\"id\\":4691,\\"jsonrpc\\":\\"2.0\\"}\", requestMethod=\"POST\", url=\"https://rpc.xdaichain.com/\", code=SERVER_ERROR, version=web/5.1.0)",
    "type":"Error",
    "context":{

    },
    "stack":"Error: processing response error (body=\"{\\"jsonrpc\\":\\"2.0\\",\\"error\\":{\\"code\\":-32000,\\"message\\":\\"Block information is incomplete while ancient block sync is still in progress, before it's finished we can't determine the existence of requested item.\\"},\\"id\\":4691}\n\", error={\"code\":-32000}, requestBody=\"{\\"method\\":\\"eth_getTransactionReceipt\\",\\"params\\":[\\"0xd63407185e358f82bb87ec31e91e841fc2d34e8d23c36e7b44ca03573997a7b3\\"],\\"id\\":4691,\\"jsonrpc\\":\\"2.0\\"}\", requestMethod=\"POST\", url=\"https://rpc.xdaichain.com/\", code=SERVER_ERROR, version=web/5.1.0)\n at Logger.makeError (webpack:////app/node_modules/@ethersproject/logger/lib/index.js?:180:21)\n at Logger.throwError (webpack:////app/node_modules/@ethersproject/logger/lib/index.js?:189:20)\n at eval (webpack:////app/node_modules/@ethersproject/web/lib/index.js?:266:32)\n at step (webpack:////app/node_modules/@ethersproject/web/lib/index.js?:33:23)\n at Object.eval [as next] (webpack:////app/node_modules/@ethersproject/web/lib/index.js?:14:53)\n at fulfilled (webpack:////app/node_modules/@ethersproject/web/lib/index.js?:5:58)\n at runMicrotasks ()\n at processTicksAndRejections (internal/process/task_queues.js:93:5)"
    },
    "msg":"Transaction reverted"
    }```

Most helpful comment

There is a difference in provider.waitForTransaction and tx.wait() that:

  • waitForTransaction will resolve once transaction is mined even if the actual transaction was success or reverted.
  • tx.wait will be pending until transaction is mined (using waitForTransaction under the hood) and will resolve or reject based on tx was success or reverted.

All 8 comments

Extracting from the error: "Block information is incomplete while ancient block sync is still in progress, before it's finished we can't determine the existence of the requested item.".

If feasible, can you try after the node is done with syncing?

There is a difference in provider.waitForTransaction and tx.wait() that:

  • waitForTransaction will resolve once transaction is mined even if the actual transaction was success or reverted.
  • tx.wait will be pending until transaction is mined (using waitForTransaction under the hood) and will resolve or reject based on tx was success or reverted.

Right, I think the difference I'm more concerned about is tx.wait will throw when this error is encountered while waitForTransaction does not

my tx.wait() function just never returns, any thoughts?

@AhrenFullStop Is your transaction mined?

Hey all, no the transaction is never mined, but I also figured out the issue and its very noob of me 馃檲 the account that I was doing the transaction on had no currency, so it couldn't pay the gas fees.

I still think this is somewhat of an issue because without a manual timeout catch, you would have no way of picking up this error. So a good suggestion for a PR is to check the accounts balance first and return an error, or to use a timeout.

the account that I was doing the transaction on had no currency, so it couldn't pay the gas fees.

Hey, what backend were you using? Because this could be a bug in that code base, I think the node should give a JSON RPC ERROR when someone is submitting such a tx (unless there is a pending tx in mempool that is funding this account).

I think I'm going to close this.

The original issue I believe was the node was syncing, which is in general very difficult to do anything with since every backend does wildly different things during this period. I have monthly meetings with the API steering committee to help address this by determining canonical results that should be returned, so in the future we may have a common way to handle this situation, and throw an UNSUPPORTED_OPERATION error, along with an explanation that the node is syncing.

I would be curious in hearing more about the issue involving insufficient funds, as that should have ben thrown by the backend. But it sounds like it was probably an issue with the backend. But please feel free to re-open if I'm mistaken, especially if you have more info to work from.

Thanks! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ruzpuz picture ruzpuz  路  26Comments

elenadimitrova picture elenadimitrova  路  28Comments

ricmoo picture ricmoo  路  24Comments

fjrojasgarcia picture fjrojasgarcia  路  28Comments

subramanianv picture subramanianv  路  34Comments