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

Sending txs from same account concurrently could result in nonce mismatch error #292

Open
adu-web3 opened this issue Jan 22, 2025 · 0 comments

Comments

@adu-web3
Copy link
Contributor

adu-web3 commented Jan 22, 2025

Description

// get current nonce for faucet account
        const startNonce = await ethers.provider.getTransactionCount(faucet.address);
        
        // transfer 1 ether gas tokens to all accounts concurrently with explicit nonces
        const transferPromises = initialAccounts.map((account, index) => 
            faucet.sendTransaction({
                to: account.address,
                value: ethers.parseEther("1"),
                nonce: startNonce + index // increment nonce for each transaction
            })
        );
        
        // wait for all transactions to be mined
        const txs = await Promise.all(transferPromises);
        await Promise.all(txs.map(tx => tx.wait()));

        // verify balances
        await Promise.all(
            initialAccounts.map(async account => {
                expect(await ethers.provider.getBalance(account.address))
                    .to.be.greaterThanOrEqual(ethers.parseEther("1"));
            })
        );

with this test code, concurrently sending txs from the same account would result in nonce-mismatch error: ProviderError: invalid nonce; got 12, expected 13: invalid sequence: invalid sequence, even if you assign the correct nonce for each tx, because the tx with higher nonce may reach the mempool earlier and it would be rejected instead of staying in mempool and wait for txs with lower nonces to be processed. This behavior may need to be optimized to improve user experience and to align with other EVM-comaptible chains

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

1 participant