PatrickAlphaC / hardhat-fund-me-fcc

82 stars 183 forks source link

TransactionExecutionError: Transaction ran out of gas #116

Closed Rampalamp closed 1 year ago

Rampalamp commented 1 year ago

Hello, I am using the default hardhat network and when running my unit tests for withdraw I am getting the following error : TransactionExecutionError: Transaction ran out of gas

Here is the test - it fails on the line that reads : const transactionResponse = await fundMe.withdraw();


describe("withdraw", async function() {
        //contract needs funding before we can run withdraw tests, setup a beforeEach
        beforeEach(async function() {
            await fundMe.fund({ value: sendValue });
        });

        it("withdraw ETH from a single funder", async function() {
            //Arrange the test
            const startingFundMeBalance = await fundMe.provider.getBalance(
                fundMe.address
            );

            const startingDeployerBalance = await fundMe.provider.getBalance(
                deployer
            );
            //Act

            const transactionResponse = await fundMe.withdraw();

            const transactionReceipt = await transactionResponse.wait(1);

            const { gasUsed, effectiveGasPrice } = transactionReceipt;

            const gasCost = gasUsed.mul(effectiveGasPrice);

            const endingFundMeBalance = await fundMe.provider.getBalance(
                fundMe.address
            );
            const endingDeployerBalance = await fundMe.provider.getBalance(
                deployer
            );

            //gasCost

            //Assert
            assert.equal(endingFundMeBalance, 0);
            assert.equal(
                startingFundMeBalance.add(startingDeployerBalance).toString(),
                endingDeployerBalance.add(gasCost).toString()
            );
        });
    });

Here is the withdraw function

 function withdraw() public onlyOwner {
        //require(msg.sender == owner, "Sender is not owner");

        for (
            uint256 funderIndex = 0;
            funderIndex < funders.length;
            funderIndex = funderIndex++
        ) {
            address funder = funders[funderIndex];
            //resetting balance of amount to 0
            addressToAmountFunded[funder] = 0;
        }
        //reset the array

        //0 specifies the new array starts with 0 elements.
        funders = new address[](0);

        (bool callSuccess, ) = i_owner.call{value: address(this).balance}("");
        require(callSuccess, "Call failed");

    }

I have tried upping the blockGasLimit in the hardhat config for the hardhat network, but this did not seem to help. I have not ran into this Transaction ran out of gas error when using the default hardhat until now. Besides perhaps some variable name differences, I have been following the youtube tutorial exactly.

Attempted to google variations of "hardhat test transaction ran out of gas" but did not receive many good hits and scanned older issues in this repot. Any help is appreciated, thanks in advance.

Rampalamp commented 1 year ago

I think I found the issue... After increasing the gasBlockLimit substantially I received the following error: FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

This led me to believe the for loop in the withdraw function was not working as intended. I checked and noticed one difference.

BAD LOOP CAUSING ERROR

for (
            uint256 funderIndex = 0;
            funderIndex < funders.length;
            funderIndex = funderIndex++
        ) 

GOOD LOOP

for (
            uint256 funderIndex = 0;
            funderIndex < funders.length;
            funderIndex++
        )

So it looks like the funderIndex = funderIndex++ increment operation was not working as intended. I think in the initial Remix FundMe section of the video tutorial, that the code was written in the incorrect format, but it looks like the code has been fixed in the repos.

Just confirmed, at the 4:39:03 time stamp of the Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course