Skip to content

Commit

Permalink
First iteration of improvements to the viem template
Browse files Browse the repository at this point in the history
  • Loading branch information
alcuadrado committed Oct 29, 2024
1 parent 49bc12f commit db7e95d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 62 deletions.
10 changes: 5 additions & 5 deletions v-next/hardhat/templates/node-test-runner-viem/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,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 { viem } = await network.connect("edrOp", "optimism");

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

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

deployCounterContract();
console.log("Counter contract address:", await counter.address);
87 changes: 38 additions & 49 deletions v-next/hardhat/templates/node-test-runner-viem/test/Lock.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { describe, it, before } from "node:test";
import assert from "node:assert/strict";
import hre from "@ignored/hardhat-vnext";
import { network } from "@ignored/hardhat-vnext";
import { getAddress, parseGwei } from "viem";
import { NetworkConnection } from "@ignored/hardhat-vnext/types/network";
import { DefaultChainType } from "@ignored/hardhat-vnext/types/network";
import { HardhatViemHelpers } from "@ignored/hardhat-vnext-viem/types";

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

before(async function () {
const connection = await network.connect();
viem = connection.viem;
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,
// and reset Hardhat Network to that snapshot in every test.
Expand All @@ -14,22 +23,17 @@ describe("Lock", function () {

const lockedAmount = parseGwei("1");
const unlockTime = BigInt(
(await networkConnection.networkHelpers.time.latest()) + ONE_YEAR_IN_SECS,
(await networkHelpers.time.latest()) + ONE_YEAR_IN_SECS,
);

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

const lock = await networkConnection.viem.deployContract(
"Lock",
[unlockTime],
{
value: lockedAmount,
},
);
const [owner, otherAccount] = await viem.getWalletClients();

const publicClient = await networkConnection.viem.getPublicClient();
const lock = await viem.deployContract("Lock", [unlockTime], {
value: lockedAmount,
});

const publicClient = await viem.getPublicClient();

return {
lock,
Expand All @@ -41,34 +45,26 @@ describe("Lock", function () {
};
}

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,
);

assert.equal(await lock.read.unlockTime(), unlockTime);
});

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

assert.equal(await lock.read.owner(), getAddress(owner.account.address));
});

it("Should receive and store the funds to lock", async function () {
const { lock, lockedAmount, publicClient } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
await networkHelpers.loadFixture(deployOneYearLockFixture);

assert.equal(
await publicClient.getBalance({
Expand All @@ -80,11 +76,9 @@ describe("Lock", 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
const latestTime = BigInt(
await networkConnection.networkHelpers.time.latest(),
);
const latestTime = BigInt(await networkHelpers.time.latest());
await assert.rejects(
networkConnection.viem.deployContract("Lock", [latestTime], {
viem.deployContract("Lock", [latestTime], {
value: 1n,
}),
(error: Error) =>
Expand All @@ -96,7 +90,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 @@ -107,15 +101,13 @@ 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 retrieve the contract with a different account to send a transaction
const lockAsOtherAccount = await networkConnection.viem.getContractAt(
const lockAsOtherAccount = await viem.getContractAt(
"Lock",
lock.address,
{ client: { wallet: otherAccount } },
Expand All @@ -127,13 +119,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 assert.doesNotReject(lock.write.withdraw());
});
Expand All @@ -142,11 +133,9 @@ describe("Lock", function () {
describe("Events", function () {
it("Should emit an event on withdrawals", async function () {
const { lock, unlockTime, lockedAmount, publicClient } =
await networkConnection.networkHelpers.loadFixture(
deployOneYearLockFixture,
);
await networkHelpers.loadFixture(deployOneYearLockFixture);

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

const hash = await lock.write.withdraw();
await publicClient.waitForTransactionReceipt({ hash });
Expand Down

0 comments on commit db7e95d

Please sign in to comment.