Added Test
This commit is contained in:
11
README.md
11
README.md
@@ -11,10 +11,15 @@ REPORT_GAS=true npx hardhat test
|
||||
npx hardhat node
|
||||
npx hardhat ignition deploy ./ignition/modules/Lock.js
|
||||
```
|
||||
|
||||
yarn hardhat run scripts/deploy.js --network sepolia
|
||||
yarn hardhat run scripts/deploy.js --network hardhat
|
||||
|
||||
yarn hardhat verify --network sepolia DEPLOYED_CONTRACT_ADDRESS "0x2f86Ad03BE88664cB3d73219fF6dDdDBa598449F"
|
||||
0xE5EA2da0357072607cE5bA85Cd0b73935c14CAF7
|
||||
0xc2022b56eBC140B5FebCf9FBaB14c17db4C315C4 - Verified
|
||||
0xc2022b56eBC140B5FebCf9FBaB14c17db4C315C4 - Verified
|
||||
0x433F69590c504285b40C559d7370745eF795889b
|
||||
0x2D4055E580436058f1fC295Fd7C15Bc46DbF1218
|
||||
|
||||
Previous Hacked Code
|
||||
https://rekt.news/leaderboard/
|
||||
|
||||
|
||||
|
||||
@@ -28,6 +28,12 @@ module.exports = {
|
||||
accounts: [PRIVATE_KEY_GANACHE],
|
||||
chainId: 5777,
|
||||
},
|
||||
localhost: {
|
||||
// Hardhost similar like ganache, can debug the transaction
|
||||
// Start in terminal - yarn hardhat node
|
||||
url: "http://127.0.0.1:8545/",
|
||||
chainId: 31337,
|
||||
},
|
||||
},
|
||||
solidity: "0.8.24",
|
||||
etherscan: {
|
||||
|
||||
126
test/Lock.js
126
test/Lock.js
@@ -1,126 +0,0 @@
|
||||
const {
|
||||
time,
|
||||
loadFixture,
|
||||
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
|
||||
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
|
||||
const { expect } = require("chai");
|
||||
|
||||
describe("Lock", function () {
|
||||
// We define a fixture to reuse the same setup in every test.
|
||||
// We use loadFixture to run this setup once, snapshot that state,
|
||||
// and reset Hardhat Network to that snapshot in every test.
|
||||
async function deployOneYearLockFixture() {
|
||||
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
|
||||
const ONE_GWEI = 1_000_000_000;
|
||||
|
||||
const lockedAmount = ONE_GWEI;
|
||||
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
|
||||
|
||||
// Contracts are deployed using the first signer/account by default
|
||||
const [owner, otherAccount] = await ethers.getSigners();
|
||||
|
||||
const Lock = await ethers.getContractFactory("Lock");
|
||||
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
|
||||
|
||||
return { lock, unlockTime, lockedAmount, owner, otherAccount };
|
||||
}
|
||||
|
||||
describe("Deployment", function () {
|
||||
it("Should set the right unlockTime", async function () {
|
||||
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
|
||||
|
||||
expect(await lock.unlockTime()).to.equal(unlockTime);
|
||||
});
|
||||
|
||||
it("Should set the right owner", async function () {
|
||||
const { lock, owner } = await loadFixture(deployOneYearLockFixture);
|
||||
|
||||
expect(await lock.owner()).to.equal(owner.address);
|
||||
});
|
||||
|
||||
it("Should receive and store the funds to lock", async function () {
|
||||
const { lock, lockedAmount } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
expect(await ethers.provider.getBalance(lock.target)).to.equal(
|
||||
lockedAmount
|
||||
);
|
||||
});
|
||||
|
||||
it("Should fail if the unlockTime is not in the future", async function () {
|
||||
// We don't use the fixture here because we want a different deployment
|
||||
const latestTime = await time.latest();
|
||||
const Lock = await ethers.getContractFactory("Lock");
|
||||
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
|
||||
"Unlock time should be in the future"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Withdrawals", function () {
|
||||
describe("Validations", function () {
|
||||
it("Should revert with the right error if called too soon", async function () {
|
||||
const { lock } = await loadFixture(deployOneYearLockFixture);
|
||||
|
||||
await expect(lock.withdraw()).to.be.revertedWith(
|
||||
"You can't withdraw yet"
|
||||
);
|
||||
});
|
||||
|
||||
it("Should revert with the right error if called from another account", async function () {
|
||||
const { lock, unlockTime, otherAccount } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
// We can increase the time in Hardhat Network
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
// We use lock.connect() to send a transaction from another account
|
||||
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
|
||||
"You aren't the owner"
|
||||
);
|
||||
});
|
||||
|
||||
it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
|
||||
const { lock, unlockTime } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
// Transactions are sent using the first signer by default
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
await expect(lock.withdraw()).not.to.be.reverted;
|
||||
});
|
||||
});
|
||||
|
||||
describe("Events", function () {
|
||||
it("Should emit an event on withdrawals", async function () {
|
||||
const { lock, unlockTime, lockedAmount } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
await expect(lock.withdraw())
|
||||
.to.emit(lock, "Withdrawal")
|
||||
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
|
||||
});
|
||||
});
|
||||
|
||||
describe("Transfers", function () {
|
||||
it("Should transfer the funds to the owner", async function () {
|
||||
const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
|
||||
deployOneYearLockFixture
|
||||
);
|
||||
|
||||
await time.increaseTo(unlockTime);
|
||||
|
||||
await expect(lock.withdraw()).to.changeEtherBalances(
|
||||
[owner, lock],
|
||||
[lockedAmount, -lockedAmount]
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
33
test/test-deploy.js
Normal file
33
test/test-deploy.js
Normal file
@@ -0,0 +1,33 @@
|
||||
const { ethers } = require("hardhat");
|
||||
const { expect, assert } = require("chai");
|
||||
|
||||
describe("SimpleStorage", function () {
|
||||
let simpleStorageFactory;
|
||||
let simpleStorage;
|
||||
beforeEach(async function () {
|
||||
simpleStorageFactory = await ethers.getContractFactory("SimpleStorage");
|
||||
simpleStorage = await simpleStorageFactory.deploy();
|
||||
});
|
||||
|
||||
it("Should start with a favorite number of 123", async function () {
|
||||
const currentValue = await simpleStorage.viewMyNumber();
|
||||
const expectedValue = "123";
|
||||
// assert
|
||||
assert.equal(currentValue.toString(), expectedValue);
|
||||
|
||||
// expect
|
||||
//expect(currentValue.toString()).to.equal(expectedValue);
|
||||
});
|
||||
|
||||
// Run this specific test only!
|
||||
//it.only("Should update when we call storeMyNumber", async function () {
|
||||
it("Should update when we call storeMyNumber", async function () {
|
||||
const expectedValue = "7296";
|
||||
const transactionResponse =
|
||||
await simpleStorage.storeMyNumber(expectedValue);
|
||||
await transactionResponse.wait(1);
|
||||
|
||||
const currentValue = await simpleStorage.viewMyNumber();
|
||||
assert.equal(currentValue.toString(), expectedValue);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user