Skip to content

Commit

Permalink
Merge pull request #5892 from NomicFoundation/improve-mocha-template
Browse files Browse the repository at this point in the history
Template improvements
  • Loading branch information
alcuadrado authored Oct 30, 2024
2 parents 16ab415 + 0edd1a1 commit 62b26ef
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 184 deletions.
3 changes: 2 additions & 1 deletion v-next/hardhat-viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"type": "module",
"types": "dist/src/index.d.ts",
"exports": {
".": "./dist/src/index.js"
".": "./dist/src/index.js",
"./types": "./dist/src/types.js"
},
"keywords": [
"ethereum",
Expand Down
1 change: 1 addition & 0 deletions v-next/hardhat/.prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/node_modules
/dist
CHANGELOG.md
/templates/*/artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ const runScriptWithHardhat: NewTaskActionFunction<RunActionArguments> = async (
}

if (!noCompile) {
await hre.tasks.getTask("compile").run({ quiet: true });
await hre.tasks.getTask("compile").run({});
console.log();
}

await import(pathToFileURL(normalizedPath).href);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ contract CounterTest {
for (uint8 i = 0; i < x; i++) {
counter.inc();
}
require(counter.x() == x, "Value after calling inc x times should be x");
}

function invariant() public pure {
assert(true);
require(counter.x() == x, "Value after calling inc x times should be x");
}
}

38 changes: 19 additions & 19 deletions v-next/hardhat/templates/mocha-ethers/contracts/Lock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ pragma solidity ^0.8.24;
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);
event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);
constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}
unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");
require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);
emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
owner.transfer(address(this).balance);
}
}
15 changes: 9 additions & 6 deletions v-next/hardhat/templates/mocha-ethers/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/config";
import {
HardhatUserConfig,
configVariable,
} from "@ignored/hardhat-vnext/config";

import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";
import HardhatEthers from "@ignored/hardhat-vnext-ethers";
Expand All @@ -15,13 +18,13 @@ const config: HardhatUserConfig = {
],
},
networks: {
"local-base": {
chainId: 8453,
edrOp: {
type: "edr",
chainId: 10,
chainType: "optimism",
gas: "auto",
gasPrice: "auto",
gasMultiplier: 1,
forkConfig: {
jsonRpcUrl: "https://mainnet.optimism.io",
},
},
},
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import hre from "@ignored/hardhat-vnext";
import { network } from "@ignored/hardhat-vnext";

async function deployCounterContract() {
const optimism = await hre.network.connect("local-base", "optimism");
console.log("Deploying a contract into a local fork of Optimism");
const { ethers } = await network.connect("edrOp", "optimism");

const contract = await optimism.ethers.deployContract("Counter");
const counter = await ethers.deployContract("Counter");

console.log("Counter contract address:", await contract.getAddress());
}

deployCounterContract();
console.log("Counter contract address:", await counter.getAddress());
101 changes: 45 additions & 56 deletions v-next/hardhat/templates/mocha-ethers/test/Lock.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import hre from "@ignored/hardhat-vnext";
import { NetworkConnection } from "@ignored/hardhat-vnext/types/network";
import { describe, it, before } from "mocha";
import { network } from "@ignored/hardhat-vnext";
import { expect } from "chai";

// TODO: Chai as promised support needs to be added either directly
// or through the updated `chai-matchers` package.
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
const { expect } = chai;
import "./setup.js";

import { HardhatEthers } from "@ignored/hardhat-vnext-ethers/types";

describe("Lock", function () {
let networkConnection: NetworkConnection<"l1">;
let networkHelpers: any; // TODO: We need to export this type in @ignored/hardhat-vnext-network-helpers
let ethers: HardhatEthers;

before(async function () {
const connection = await network.connect();
ethers = connection.ethers;
networkHelpers = connection.networkHelpers;
});

// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
Expand All @@ -21,56 +24,49 @@ describe("Lock", function () {

const lockedAmount = ONE_GWEI;

const unlockTime =
(await networkConnection.networkHelpers.time.latest()) + ONE_YEAR_IN_SECS;
const latestTime = await networkHelpers.time.latest();
const unlockTime = latestTime + ONE_YEAR_IN_SECS;

// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await networkConnection.ethers.getSigners();
const [owner, otherAccount] = await ethers.getSigners();

const Lock = await networkConnection.ethers.getContractFactory("Lock");
const Lock = await ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });

return { lock, unlockTime, lockedAmount, owner, otherAccount };
}

before(async function () {
networkConnection = await hre.network.connect();
});

describe("Deployment", function () {
it("Should set the right unlockTime", async function () {
const { lock, unlockTime } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
const { lock, unlockTime } = await networkHelpers.loadFixture(
deployOneYearLockFixture,
);

expect(await lock.unlockTime()).to.equal(BigInt(unlockTime));
});

it("Should set the right owner", async function () {
const { lock, owner } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
const { lock, owner } = await networkHelpers.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 networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
const { lock, lockedAmount } = await networkHelpers.loadFixture(
deployOneYearLockFixture,
);

expect(
await networkConnection.ethers.provider.getBalance(lock.target),
).to.equal(BigInt(lockedAmount));
expect(await ethers.provider.getBalance(lock.target)).to.equal(
BigInt(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 networkConnection.networkHelpers.time.latest();
const Lock = await networkConnection.ethers.getContractFactory("Lock");
const latestTime = await networkHelpers.time.latest();
const Lock = await ethers.getContractFactory("Lock");

// TODO: bring back the original test assertion `hardhat-chai-matchers`
// is available with `revertedWith`.
Expand All @@ -83,7 +79,7 @@ describe("Lock", function () {
describe("Withdrawals", function () {
describe("Validations", function () {
it("Should revert with the right error if called too soon", async function () {
const { lock } = await networkConnection.networkHelpers.loadFixture(
const { lock } = await networkHelpers.loadFixture(
deployOneYearLockFixture,
);

Expand All @@ -94,12 +90,10 @@ describe("Lock", function () {

it("Should revert with the right error if called from another account", async function () {
const { lock, unlockTime, otherAccount } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
await networkHelpers.loadFixture(deployOneYearLockFixture);

// We can increase the time in Hardhat Network
await networkConnection.networkHelpers.time.increaseTo(unlockTime);
await networkHelpers.time.increaseTo(unlockTime);

// We use lock.connect() to send a transaction from another account
await expect(
Expand All @@ -108,13 +102,12 @@ describe("Lock", function () {
});

it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
const { lock, unlockTime } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
const { lock, unlockTime } = await networkHelpers.loadFixture(
deployOneYearLockFixture,
);

// Transactions are sent using the first signer by default
await networkConnection.networkHelpers.time.increaseTo(unlockTime);
await networkHelpers.time.increaseTo(unlockTime);

await expect(lock.withdraw()).to.eventually.be.fulfilled;
});
Expand All @@ -125,11 +118,9 @@ describe("Lock", function () {
// is available for asserting on events.
it("Should emit an event on withdrawals", async function () {
const { lock, unlockTime, lockedAmount } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
await networkHelpers.loadFixture(deployOneYearLockFixture);

await networkConnection.networkHelpers.time.increaseTo(unlockTime);
await networkHelpers.time.increaseTo(unlockTime);

const withdrawResult = await lock.withdraw();

Expand All @@ -147,17 +138,15 @@ describe("Lock", function () {
// TODO: bring back the original Transfers test once
// `hardhat-chai-matchers` has been ported.
it("Should transfer the funds out of the timelock", async function () {
const { lock, unlockTime } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
const { lock, unlockTime } = await networkHelpers.loadFixture(
deployOneYearLockFixture,
);

await networkConnection.networkHelpers.time.increaseTo(unlockTime);
await networkHelpers.time.increaseTo(unlockTime);

await lock.withdraw();

const afterLockedBalance =
await networkConnection.ethers.provider.getBalance(lock);
const afterLockedBalance = await ethers.provider.getBalance(lock);

expect(afterLockedBalance).to.equal(0n);
});
Expand Down
4 changes: 4 additions & 0 deletions v-next/hardhat/templates/mocha-ethers/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file contains some setup code that won't be required in the future.
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,4 @@ contract CounterTest {
}
require(counter.x() == x, "Value after calling inc x times should be x");
}

function invariant() public pure {
assert(true);
}
}

38 changes: 19 additions & 19 deletions v-next/hardhat/templates/node-test-runner-viem/contracts/Lock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ pragma solidity ^0.8.24;
// import "hardhat/console.sol";

contract Lock {
uint public unlockTime;
address payable public owner;
uint public unlockTime;
address payable public owner;

event Withdrawal(uint amount, uint when);
event Withdrawal(uint amount, uint when);

constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);
constructor(uint _unlockTime) payable {
require(
block.timestamp < _unlockTime,
"Unlock time should be in the future"
);

unlockTime = _unlockTime;
owner = payable(msg.sender);
}
unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);
function withdraw() public {
// Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal
// console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp);

require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");
require(block.timestamp >= unlockTime, "You can't withdraw yet");
require(msg.sender == owner, "You aren't the owner");

emit Withdrawal(address(this).balance, block.timestamp);
emit Withdrawal(address(this).balance, block.timestamp);

owner.transfer(address(this).balance);
}
owner.transfer(address(this).balance);
}
}
Loading

0 comments on commit 62b26ef

Please sign in to comment.