Unit test done
This commit is contained in:
parent
074260c945
commit
aefe632675
@ -9,6 +9,7 @@ pragma solidity ^0.8.0;
|
||||
|
||||
/***** 2. Import *****/
|
||||
import "./PriceConverter.sol";
|
||||
import "hardhat/console.sol";
|
||||
// Save Gas by using constant, immutable for variables
|
||||
|
||||
/***** 3. Error Codes *****/
|
||||
@ -54,6 +55,8 @@ contract FundMe {
|
||||
revert FundMe__NotOwner();
|
||||
}
|
||||
_; // doing the rest of the code
|
||||
|
||||
console.log("Log available via 'yarn hardhat test'");
|
||||
}
|
||||
|
||||
/***** Function Orders:
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -126,24 +126,89 @@ describe("FundMe", 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);
|
||||
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 endingFundMeBalance = await fundMe.provider.getBalance(fundMe.address);
|
||||
const endingDeployerBalance = await fundMe.provider.getBalance(deployer);
|
||||
const endingFundMeBalance = await fundMe.provider.getBalance(
|
||||
fundMe.address,
|
||||
);
|
||||
const endingDeployerBalance =
|
||||
await fundMe.provider.getBalance(deployer);
|
||||
|
||||
const {gasUsed, effectiveGasPrice } = transactionReceipt; //gasUsed = "0x8b14"; effectiveGasPrice = "0x4e4814ec";
|
||||
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())
|
||||
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.withdraw();
|
||||
// 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.funders(0)).to.be.reverted;
|
||||
|
||||
for (i = 1; i < 6; i++) {
|
||||
assert.equal(
|
||||
await fundMe.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.withdraw()).to.be.reverted;
|
||||
await expect(
|
||||
attacherConnectedContract.withdraw(),
|
||||
).to.be.revertedWith("FundMe__NotOwner");
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user