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

Await on Transaction Submission Waits for Confirmation #4722

Open
IttsKK opened this issue May 3, 2024 · 10 comments
Open

Await on Transaction Submission Waits for Confirmation #4722

IttsKK opened this issue May 3, 2024 · 10 comments
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@IttsKK
Copy link

IttsKK commented May 3, 2024

Ethers Version

6.11.1

Search Terms

transaction submission, await, transaction confirmation, tx.wait, transaction hash

Describe the Problem

Expected Behavior:
Using await on a transaction method (contract.function) should resolve as soon as the transaction is sent to the network and a transaction hash is received, allowing subsequent code to run immediately after the transaction submission.

Actual Behavior:
Instead, await on contract.function only resolves after the transaction has been confirmed. This behavior is identical to what happens when using tx.wait(), which should only occur at the transaction confirmation stage. Removing tx.wait() from the workflow has no effect, as the initial await on the transaction already waits for confirmation, causing significant delays in UI updates that depend on the transaction submission.

Code Snippet

const tx = await contract.function();
console.log(`Transaction hash: ${tx.hash}`);  // This line is supposed to execute right after transaction submission
await tx.wait();  // This line should wait for the confirmation
console.log("Transaction confirmed.");

Contract ABI

No response

Errors

No response

Environment

node.js (v12 or newer), Browser (Chrome, Safari, etc), Hardhat

Environment (Other)

No response

@IttsKK IttsKK added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels May 3, 2024
@IttsKK IttsKK changed the title Add Bug Title Here Await on Transaction Submission Waits for Confirmation May 3, 2024
@ricmoo
Copy link
Member

ricmoo commented May 3, 2024

That is how tx = await contract.func() works; returning once the transaction is in the mempool. Calling await tx.wait() will then wait for it to be mined, resolving to the receipt. But the initial call certainly doesn't wait for the tx to be mined.

Can you provide an example where you do not believe this is happening?

@IttsKK
Copy link
Author

IttsKK commented May 3, 2024

I understand that await contract.func() is expected to resolve as soon as the transaction reaches the mempool, with await tx.wait() then waiting for the transaction's confirmation. However, I am experiencing behavior that suggests the initial await on the transaction function is waiting until the transaction is confirmed before resolving.

  • Environment: I am running this within a React application, interacting with the network via MetaMask and ethers.
  • Code Snippet:
    const amountToApprove = ethers.parseEther(tokenAmount);
    console.log("Approving Tokens");
    const approve = await paymentTokenContract.approve(tokenSaleAddress, amountToApprove);
    console.log("Tokens Approved, Awaiting Confirmation");
    await approve.wait();
    console.log("Approval Confirmed, Initiating Purchase");
    const tx = await tokenSaleContract.buyTokens(paymentTokenAddress, amountToApprove);
    console.log("Purchase Transaction Submitted, Awaiting Confirmation");
    await tx.wait();
    console.log("Purchase Confirmed");

Observed Behavior:

  • The logs "Tokens Approved, Awaiting Confirmation" and "Purchase Transaction Submitted, Awaiting Confirmation" are expected to print immediately after the respective transactions (approve and buyTokens) are sent to the network.
  • However, these logs only appear after each transaction has been confirmed on the blockchain. This indicates that the await on both approve and buyTokens does not resolve upon submission to the mempool but rather waits until the transaction is confirmed.

@ricmoo
Copy link
Member

ricmoo commented May 3, 2024

What network are you on? Since ethers needs to lookup the tx from the mempool, this can happen on networks (or backends) which do not return transactions in the mempool.

@IttsKK
Copy link
Author

IttsKK commented May 3, 2024

I've tested this on BNB Smart Chain and locally with Hardhat. Everything was working fine up until about a week ago, and I haven't made any changes to the relevant parts of my codebase since then. I suspect this may be an issue with Metamask, but I thought it would be good to ask here as well.

@cosullivan
Copy link

I've tested this on BNB Smart Chain and locally with Hardhat. Everything was working fine up until about a week ago, and I haven't made any changes to the relevant parts of my codebase since then. I suspect this may be an issue with Metamask, but I thought it would be good to ask here as well.

Did you figure this out? I've now just noticed the same same thing on BNB & Arbitrum.

@ArnasAlex
Copy link

ArnasAlex commented May 6, 2024

Started to get the same issue after MetaMask browser extension was updated on chrome. Using ETH Sepolia network.
MetaMask Version
11.14.5

Using Firefox with older MetaMask version works correctly (same as before promise is resolved when tx was submitted and not on tx confirmation).
MetaMask Version
11.12.4

@usame-algan
Copy link

usame-algan commented May 8, 2024

Can confirm that I have the same issues with Metamask since one of their last updates. Calling signer.sendTransaction or contract.connect(signer).<any method> gets stuck until the transaction is executed.

One workaround is to use sendUncheckedTransaction. I think it doesn't officially exist in v6 anymore but we copied it from v5:

export class UncheckedJsonRpcSigner extends JsonRpcSigner {
  async sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
    return this.sendUncheckedTransaction(transaction).then((hash) => {
      return <TransactionResponse>(<unknown>{
        hash,
        nonce: null,
        gasLimit: null,
        gasPrice: null,
        data: null,
        value: null,
        chainId: null,
        confirmations: 0,
        from: null,
        wait: (confirmations?: number) => {
          return this.provider.waitForTransaction(hash, confirmations)
        },
      })
    })
  }
}

// How to use it:
const uncheckedJsonRpcSigner = new UncheckedJsonRpcSigner(signer.provider, await signer.getAddress())
const result = await uncheckedJsonRpcSigner.sendTransaction(...)
// will resolve immediately

@ip10x
Copy link

ip10x commented May 10, 2024

Just chiming in to say this is happening to me as well. Using ethers 6.10.0 and MetaMask on Sepolia or my local node

Code looks like

const tx = await contract.function(<function args>)

const txHash = tx.hash

...

My app is sorta breaking cause it's expecting to get the txHash back immediately and redirect to an intermediate page while the transaction is waiting to be confirmed (it contains the await tx.wait()). Now it skips the intermediate page.

@kaptn3
Copy link

kaptn3 commented May 13, 2024

Same problem

@ricmoo
Copy link
Member

ricmoo commented May 16, 2024

I've contacted MetaMask to find out more about any recent changes. :)

gmxer added a commit to gmx-io/gmx-interface that referenced this issue May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

7 participants