Cheaper Gas

This commit is contained in:
2024-08-06 18:49:09 +08:00
parent aefe632675
commit 28ac29b8ba
4 changed files with 228 additions and 25 deletions
+109 -13
View File
@@ -25,18 +25,18 @@ describe("FundMe", function () {
eth1 = ethers.utils.parseEther("1"); // Same 1 ETH
describe("constructor", async function () {
it("Set the aggregator priceFeedAddress correctly", async function () {
const response = await fundMe.priceFeed(); // AggregatorV3Interface
const response = await fundMe.s_priceFeed(); // AggregatorV3Interface
//console.log("AggregatorV3Interface" + response);
assert.equal(response, mockV3Aggregator.address);
});
it("Updated the amount funded data structure", async function () {
await fundMe.fund({ value: eth1 });
const response = await fundMe.addressToAmountFunded(deployer);
const response = await fundMe.s_addressToAmountFunded(deployer);
assert.equal(response.toString(), eth1.toString()); // Send 1 ETH
});
it("Adds funder to array of funders", async function () {
await fundMe.fund({ value: eth1 });
const funder = await fundMe.funders(0);
const funder = await fundMe.s_funders(0);
assert.equal(funder, deployer);
});
});
@@ -80,11 +80,11 @@ describe("FundMe", function () {
});
// Check if addr1 is added to funders
expect(await fundMe.funders(0)).to.equal(addr1.address);
expect(await fundMe.s_funders(0)).to.equal(addr1.address);
// Check if the amount funded is recorded
expect(await fundMe.addressToAmountFunded(addr1.address)).to.equal(
ethers.utils.parseEther("1.0"),
);
expect(
await fundMe.s_addressToAmountFunded(addr1.address),
).to.equal(ethers.utils.parseEther("1.0"));
});
it("should revert if the amount sent is below the minimum funding value", async function () {
@@ -110,10 +110,10 @@ describe("FundMe", function () {
}),
).not.to.be.reverted;
expect(await fundMe.funders(0)).to.equal(addr1.address);
expect(await fundMe.addressToAmountFunded(addr1.address)).to.equal(
minFunding,
);
expect(await fundMe.s_funders(0)).to.equal(addr1.address);
expect(
await fundMe.s_addressToAmountFunded(addr1.address),
).to.equal(minFunding);
});
});
@@ -191,11 +191,11 @@ describe("FundMe", function () {
endingDeployerBalance.add(withdrawGasCost).toString(),
);
// Make a getter for storage variables
await expect(fundMe.funders(0)).to.be.reverted;
await expect(fundMe.s_funders(0)).to.be.reverted;
for (i = 1; i < 6; i++) {
assert.equal(
await fundMe.addressToAmountFunded(accounts[i].address),
await fundMe.s_addressToAmountFunded(accounts[i].address),
0,
);
}
@@ -211,4 +211,100 @@ describe("FundMe", function () {
).to.be.revertedWith("FundMe__NotOwner");
});
});
describe("cheaperWithdraw", async function () {
beforeEach(async function () {
await fundMe.fund({ value: eth1 }); // Add 1 ETH first
});
it("Withdraw ETH from a single funder", async function () {
/*
* Deployer = funder
* fundMe and replace with ethers, important to getBalance
*/
// Arrange
const startingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address,
);
const startingDeployerBalance =
await fundMe.provider.getBalance(deployer);
// Act
const transactionResponse = await fundMe.cheaperWithdraw();
const transactionReceipt = await transactionResponse.wait(1);
const endingFundMeBalance = await fundMe.provider.getBalance(
fundMe.address,
);
const endingDeployerBalance =
await fundMe.provider.getBalance(deployer);
const { gasUsed, effectiveGasPrice } = transactionReceipt; //gasUsed = "0x8b14"; effectiveGasPrice = "0x4e4814ec";
const gasCost = gasUsed.mul(effectiveGasPrice);
// Assert
assert.equal(endingFundMeBalance, 0);
assert.equal(
startingFundMeBalance.add(startingDeployerBalance),
endingDeployerBalance.add(gasCost).toString(),
);
});
it("Allow us to withdraw with multiple funders", async function () {
const accounts = await ethers.getSigners();
for (let a = 0; a < 6; a++) {
const fundMeConnectedContract = await fundMe.connect(
accounts[a],
);
await fundMeConnectedContract.fund({ value: eth1 });
}
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.cheaperWithdraw()
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.s_funders(0)).to.be.reverted;
for (i = 1; i < 6; i++) {
assert.equal(
await fundMe.s_addressToAmountFunded(accounts[i].address),
0,
);
}
});
it("Only allow the owner to withdraw", async function () {
const accounts = await ethers.getSigners();
const attacker = accounts[1];
const attacherConnectedContract = await fundMe.connect(attacker);
await expect(attacherConnectedContract.cheaperWithdraw()).to.be
.reverted;
await expect(
attacherConnectedContract.cheaperWithdraw(),
).to.be.revertedWith("FundMe__NotOwner");
});
});
});