Existing stateless contract works on top of our geth fork

This commit is contained in:
evgeniy-scherbina 2024-01-03 10:41:39 -05:00
parent 18f414276c
commit bbd08b4716
2 changed files with 127 additions and 119 deletions

View file

@ -2,6 +2,14 @@ import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox"; import "@nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = { const config: HardhatUserConfig = {
defaultNetwork: "geth",
networks: {
hardhat: {},
geth: {
url: "http://localhost:8545",
// accounts: [privateKey1, privateKey2, ...]
}
},
solidity: "0.8.19", solidity: "0.8.19",
}; };

View file

@ -6,122 +6,122 @@ import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai"; import { expect } from "chai";
import { ethers } from "hardhat"; import { ethers } from "hardhat";
describe("Lock", function () { // describe("Lock", function () {
// We define a fixture to reuse the same setup in every test. // // We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state, // // We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test. // // and reset Hardhat Network to that snapshot in every test.
async function deployOneYearLockFixture() { // async function deployOneYearLockFixture() {
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60; // const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
const ONE_GWEI = 1_000_000_000; // const ONE_GWEI = 1_000_000_000;
//
const lockedAmount = ONE_GWEI; // const lockedAmount = ONE_GWEI;
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS; // const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
//
// Contracts are deployed using the first signer/account by default // // Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners(); // const [owner, otherAccount] = await ethers.getSigners();
//
const Lock = await ethers.getContractFactory("Lock"); // const Lock = await ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount }); // const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
//
return { lock, unlockTime, lockedAmount, owner, otherAccount }; // return { lock, unlockTime, lockedAmount, owner, otherAccount };
} // }
//
describe("Deployment", function () { // describe("Deployment", function () {
it("Should set the right unlockTime", async function () { // it("Should set the right unlockTime", async function () {
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture); // const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
//
expect(await lock.unlockTime()).to.equal(unlockTime); // expect(await lock.unlockTime()).to.equal(unlockTime);
}); // });
//
it("Should set the right owner", async function () { // it("Should set the right owner", async function () {
const { lock, owner } = await loadFixture(deployOneYearLockFixture); // const { lock, owner } = await loadFixture(deployOneYearLockFixture);
//
expect(await lock.owner()).to.equal(owner.address); // expect(await lock.owner()).to.equal(owner.address);
}); // });
//
it("Should receive and store the funds to lock", async function () { // it("Should receive and store the funds to lock", async function () {
const { lock, lockedAmount } = await loadFixture( // const { lock, lockedAmount } = await loadFixture(
deployOneYearLockFixture // deployOneYearLockFixture
); // );
//
expect(await ethers.provider.getBalance(lock.target)).to.equal( // expect(await ethers.provider.getBalance(lock.target)).to.equal(
lockedAmount // lockedAmount
); // );
}); // });
//
it("Should fail if the unlockTime is not in the future", async function () { // 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 // // We don't use the fixture here because we want a different deployment
const latestTime = await time.latest(); // const latestTime = await time.latest();
const Lock = await ethers.getContractFactory("Lock"); // const Lock = await ethers.getContractFactory("Lock");
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith( // await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
"Unlock time should be in the future" // "Unlock time should be in the future"
); // );
}); // });
}); // });
//
describe("Withdrawals", function () { // describe("Withdrawals", function () {
describe("Validations", function () { // describe("Validations", function () {
it("Should revert with the right error if called too soon", async function () { // it("Should revert with the right error if called too soon", async function () {
const { lock } = await loadFixture(deployOneYearLockFixture); // const { lock } = await loadFixture(deployOneYearLockFixture);
//
await expect(lock.withdraw()).to.be.revertedWith( // await expect(lock.withdraw()).to.be.revertedWith(
"You can't withdraw yet" // "You can't withdraw yet"
); // );
}); // });
//
it("Should revert with the right error if called from another account", async function () { // it("Should revert with the right error if called from another account", async function () {
const { lock, unlockTime, otherAccount } = await loadFixture( // const { lock, unlockTime, otherAccount } = await loadFixture(
deployOneYearLockFixture // deployOneYearLockFixture
); // );
//
// We can increase the time in Hardhat Network // // We can increase the time in Hardhat Network
await time.increaseTo(unlockTime); // await time.increaseTo(unlockTime);
//
// We use lock.connect() to send a transaction from another account // // We use lock.connect() to send a transaction from another account
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith( // await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
"You aren't the owner" // "You aren't the owner"
); // );
}); // });
//
it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () { // it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
const { lock, unlockTime } = await loadFixture( // const { lock, unlockTime } = await loadFixture(
deployOneYearLockFixture // deployOneYearLockFixture
); // );
//
// Transactions are sent using the first signer by default // // Transactions are sent using the first signer by default
await time.increaseTo(unlockTime); // await time.increaseTo(unlockTime);
//
await expect(lock.withdraw()).not.to.be.reverted; // await expect(lock.withdraw()).not.to.be.reverted;
}); // });
}); // });
//
describe("Events", function () { // describe("Events", function () {
it("Should emit an event on withdrawals", async function () { // it("Should emit an event on withdrawals", async function () {
const { lock, unlockTime, lockedAmount } = await loadFixture( // const { lock, unlockTime, lockedAmount } = await loadFixture(
deployOneYearLockFixture // deployOneYearLockFixture
); // );
//
await time.increaseTo(unlockTime); // await time.increaseTo(unlockTime);
//
await expect(lock.withdraw()) // await expect(lock.withdraw())
.to.emit(lock, "Withdrawal") // .to.emit(lock, "Withdrawal")
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg // .withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
}); // });
}); // });
//
describe("Transfers", function () { // describe("Transfers", function () {
it("Should transfer the funds to the owner", async function () { // it("Should transfer the funds to the owner", async function () {
const { lock, unlockTime, lockedAmount, owner } = await loadFixture( // const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
deployOneYearLockFixture // deployOneYearLockFixture
); // );
//
await time.increaseTo(unlockTime); // await time.increaseTo(unlockTime);
//
await expect(lock.withdraw()).to.changeEtherBalances( // await expect(lock.withdraw()).to.changeEtherBalances(
[owner, lock], // [owner, lock],
[lockedAmount, -lockedAmount] // [lockedAmount, -lockedAmount]
); // );
}); // });
}); // });
}); // });
}); // });