PatrickAlphaC / hardhat-fund-me-fcc

82 stars 183 forks source link

Question - Had to increment the nonce by 1 for withdraw transcation to succeed and provide gasLimit. Why? #75

Open srihari11235 opened 1 year ago

srihari11235 commented 1 year ago

Issue Description

Below error is thrown when running staging.test

Error: replacement fee too low [ See: https://links.ethers.org/v5-errors-REPLACEMENT_UNDERPRICED ] (error={"name":"ProviderError","code":-32000,"_isProviderError":true}, method="sendTransaction", transaction=undefined, code=REPLACEMENT_UNDERPRICED, version=providers/5.7.0)

Code causing the issue

 it("allow people to fund and withdraw", async function () {
        const txResponse = await fundMe.fund({ value: sendValue });
        const txReceipt = txResponse.wait(6);
        const txResponse_Withdraw  = await fundMe.withdraw();
        const txReceipt_withdraw = txResponse_Withdraw.wait(12);

        const endingBalance = await fundMe.provider.getBalance(fundMe.address);
        assert.equal(endingBalance.toString(), "0");
    })

The fund transaction was executed without issue and I could see the transaction detail in the Goerli testnet. But on the execution of the withdraw() transaction, it failed with the error mentioned above.

Fix that resolved the issue

I had to capture the nonce of the fund() transaction and increment it by 1 for withdraw() which resolved the issue.

Fixed code -

    it("allow people to fund and withdraw", async function () {
        const txResponse = await fundMe.fund({ value: sendValue });
        const txReceipt = txResponse.wait(6);

        const nouce = txResponse.nonce;

        const txResponse_Withdraw  = await fundMe.withdraw({ nonce : nouce + 1, gasLimit: 2000000});
        const txReceipt_withdraw = txResponse_Withdraw.wait(12);

        const endingBalance = await fundMe.provider.getBalance(fundMe.address);
        assert.equal(endingBalance.toString(), "0");
    })

Questions

  1. What is nonce of a transaction? Why did I have to increment by 1 to succeed withdraw() ?
  2. Patrick is not doing this as part of the tutorial. Is this something new added as part of new updates?