PatrickAlphaC / hardhat-fund-me-fcc

82 stars 184 forks source link

Not Actually? #182

Closed oblakov0372 closed 10 months ago

oblakov0372 commented 10 months ago

yarn hardhat test --grep "withdraw" yarn run v1.22.21 warning package.json: No license field $ D:\Developing_WEB3\HardhatFundMe\node_modules.bin\hardhat test --grep withdraw

Fund me withdraw

  1) withdraws ETH from a single funder

  2) is allows us to withdraw with multiple funders

0 passing (2s) 2 failing

1) Fund me withdraw withdraws ETH from a single funder: TypeError: Cannot read properties of undefined (reading 'getBalance') at Context. (test\unit\FundeMe.test.js:45:59)

2) Fund me withdraw is allows us to withdraw with multiple funders: TypeError: Cannot read properties of undefined (reading 'getBalance') at Context. (test\unit\FundeMe.test.js:80:59) at processTicksAndRejections (node:internal/process/task_queues:95:5)

describe("withdraw", function () { beforeEach(async () => { await fundMe.fund({ value: sendValue }); }); it("withdraws ETH from a single funder", async () => { // Arrange 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();
  const { gasUsed, effectiveGasPrice } = transactionReceipt;
  const gasCost = gasUsed.mul(effectiveGasPrice);

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

  // Assert
  // Maybe clean up to understand the testing
  assert.equal(endingFundMeBalance, 0);
  assert.equal(
    startingFundMeBalance.add(startingDeployerBalance).toString(),
    endingDeployerBalance.add(gasCost).toString()
  );
});
// this test is overloaded. Ideally we'd split it into multiple tests
// but for simplicity we left it as one
it("is allows us to withdraw with multiple funders", async () => {
  // Arrange
  const accounts = await ethers.getSigners();
  for (i = 1; i < 6; i++) {
    const fundMeConnectedContract = await fundMe.connect(accounts[i]);
    await fundMeConnectedContract.fund({ value: sendValue });
  }
  const startingFundMeBalance = await fundMe.provider.getBalance(
    fundMe.address
  );
  const startingDeployerBalance = await fundMe.provider.getBalance(
    deployer
  );

  // Act
  const transactionResponse = await fundMe.cheaperWithdraw();
  // Let's comapre gas costs :)
  // const transactionResponse = await fundMe.withdraw()
  const transactionReceipt = await transactionResponse.wait();
  const { gasUsed, effectiveGasPrice } = transactionReceipt;
  const withdrawGasCost = gasUsed.mul(effectiveGasPrice);
  console.log(`GasCost: ${withdrawGasCost}`);
  console.log(`GasUsed: ${gasUsed}`);
  console.log(`GasPrice: ${effectiveGasPrice}`);
  const endingFundMeBalance = await fundMe.provider.getBalance(
    fundMe.address
  );
  const endingDeployerBalance = await fundMe.provider.getBalance(deployer);
  // Assert
  assert.equal(
    startingFundMeBalance.add(startingDeployerBalance).toString(),
    endingDeployerBalance.add(withdrawGasCost).toString()
  );
  // Make a getter for storage variables
  await expect(fundMe.getFunder(0)).to.be.reverted;

  for (i = 1; i < 6; i++) {
    assert.equal(
      await fundMe.getAddressToAmountFunded(accounts[i].address),
      0
    );
  }
});
PatrickAlphaC commented 10 months ago

Thanks for this!

Can you:

  1. Make this a discusson on the full repo? https://github.com/smartcontractkit/full-blockchain-solidity-course-js/
  2. Follow this section for formatting questions? https://www.youtube.com/watch?t=19846&v=gyMwXuJrbJQ&feature=youtu.be
buoe commented 6 months ago

Bro, have you solved it. My problem is the same as yours, and can't be solved.

Famoz20 commented 4 months ago

i spent hours on this but i fixed it at last

replace the


const transactionReceipt = await transactionResponse.wait();
  const { gasUsed, effectiveGasPrice } = transactionReceipt;
  const withdrawGasCost = gasUsed.mul(effectiveGasPrice);

with

                const gasCost = async (txHash, toNative = true) => {
                const transactionResponse = await fundMe.withdraw()
                const transactionReceipt = await transactionResponse.wait(1)
                const { gasUsed, effectiveGasPrice } = await transactionReceipt(
                    [txHash]
                )

                const gasCost = BigInt(gasUsed) * BigInt(effectiveGasPrice)

it should work . And thanks again PatrickAlphaC for the good work

Famoz20 commented 4 months ago

Bro, have you solved it. My problem is the same as yours, and can't be solved.

i spent hours on this but i fixed it at last

replace the

const transactionReceipt = await transactionResponse.wait();
  const { gasUsed, effectiveGasPrice } = transactionReceipt;
  const withdrawGasCost = gasUsed.mul(effectiveGasPrice);

with

                const gasCost = async (txHash, toNative = true) => {
                const transactionResponse = await fundMe.withdraw()
                const transactionReceipt = await transactionResponse.wait(1)
                const { gasUsed, effectiveGasPrice } = await transactionReceipt(
                    [txHash]
                )

                const gasCost = BigInt(gasUsed) * BigInt(effectiveGasPrice)

it should work . And thanks again PatrickAlphaC for the good work