Skip to content

Commit

Permalink
chore: add Node Test Runner/Viem template
Browse files Browse the repository at this point in the history
This is based on the Mocha/Ethers template and includes:

* the lock contract taken from v2
* the viem tests for lock taken from v2 and adapted
* simple Solidity test contracts from the example project
* a script deploying a simple contract to a local optimism Hardhat
  network
  • Loading branch information
kanej committed Oct 29, 2024
1 parent 896dfea commit a6af747
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 12 deletions.
29 changes: 25 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions v-next/hardhat/templates/mocha-ethers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ This project demonstrates basic Hardhat usecases within a TypeScript project. It

For integration testing it uses the Mocha test runner and the Ethers.js library for interacting with Ethereum nodes.

Try running the following commands to see Hardhat in action:
Try running the following commands to see Hardhat's testing in action:

```shell
# Run the both the mocha integration test suite
# and the Solidity Test suite
npx hardhat3 test
```

Hardhat v3 comes with support for simulating chains other than Ethereum l1. To deploy against an

### Multi-chain support

To deploy a contract to an in-memory Hardhat node simulating Base (an Optimism l2 chain), run:
Expand Down
2 changes: 1 addition & 1 deletion v-next/hardhat/templates/mocha-ethers/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/types/config";
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/config";

import HardhatMochaTestRunner from "@ignored/hardhat-vnext-mocha-test-runner";
import HardhatEthers from "@ignored/hardhat-vnext-ethers";
Expand Down
8 changes: 4 additions & 4 deletions v-next/hardhat/templates/mocha-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"description": "A Typescript Hardhat project using Mocha and Ethers.js",
"type": "module",
"devDependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.5",
"@ignored/hardhat-vnext-ethers": "workspace:^3.0.0-next.1",
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.1",
"@ignored/hardhat-vnext-network-helpers": "workspace:^3.0.0-next.1",
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.6",
"@ignored/hardhat-vnext-ethers": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-mocha-test-runner": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-network-helpers": "workspace:^3.0.0-next.2",
"@types/chai": "^4.2.0",
"@types/chai-as-promised": "^8.0.1",
"@types/mocha": ">=9.1.0",
Expand Down
14 changes: 14 additions & 0 deletions v-next/hardhat/templates/node-test-runner-viem/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Node modules
/node_modules

# Compilation output
/dist

# pnpm deploy output
/bundle

# Hardhat Build Artifacts
/artifacts

# Hardhat compilation (v2) support directory
/cache
34 changes: 34 additions & 0 deletions v-next/hardhat/templates/node-test-runner-viem/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# A TypeScript Hardhat project using Node Test Runner and Viem

> WARNING: This demonstration project is still in development. It is part of the Hardhat v3 upgrade. It is not for production use.
> NOTE: There are several plugins from the Hardhat toolbox that have not been ported to Hardhat v3 yet.
This project demonstrates basic Hardhat usecases within a TypeScript project. It comes with:

* a minimal Hardhat configuration file
* JS/TS integration tests
* Solidity Tests
* A script demonstrating how to deploy a contract to an in-memory Hardhat node simulating Base (an Optimism l2 chain)

## Usage

### Testing

For integration testing it uses the Node Test Runner and the Viem library for interacting with Ethereum nodes.

Try running the following commands to see Hardhat's testing in action:

```shell
# Run the both the Node Test Runner integration test suite
# and the Solidity Test suite
npx hardhat3 test
```

### Multi-chain support

To deploy a contract to an in-memory Hardhat node simulating Base (an Optimism l2 chain), run:

```shell
npx hardhat3 run scripts/deploy-counter-contract.ts
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Counter {
uint public x;

function inc() public {
x++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./Counter.sol";

contract CounterTest {
Counter counter;

function setUp() public {
counter = new Counter();
}

function testInitialValue() public view {
require(counter.x() == 0, "Initial value should be 0");
}

function testFuzzInc(uint8 x) public {
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);
}
}

34 changes: 34 additions & 0 deletions v-next/hardhat/templates/node-test-runner-viem/contracts/Lock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

// Uncomment this line to use console.log
// import "hardhat/console.sol";

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

event Withdrawal(uint amount, uint when);

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

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

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

owner.transfer(address(this).balance);
}
}
23 changes: 23 additions & 0 deletions v-next/hardhat/templates/node-test-runner-viem/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { HardhatUserConfig } from "@ignored/hardhat-vnext/config";

import HardhatNodeTestRunner from "@ignored/hardhat-vnext-node-test-runner";
import HardhatViem from "@ignored/hardhat-vnext-viem";
import HardhatNetworkHelpers from "@ignored/hardhat-vnext-network-helpers";

const config: HardhatUserConfig = {
plugins: [HardhatNodeTestRunner, HardhatViem, HardhatNetworkHelpers],
solidity: "0.8.24",
defaultNetwork: "hardhat",
networks: {
"local-base": {
chainId: 8453,
type: "edr",
chainType: "optimism",
gas: "auto",
gasPrice: "auto",
gasMultiplier: 1,
},
},
};

export default config;
15 changes: 15 additions & 0 deletions v-next/hardhat/templates/node-test-runner-viem/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "template-node-test-runner-viem",
"private": true,
"version": "0.0.1",
"description": "A Typescript Hardhat project using Node Test Runner and Viem",
"type": "module",
"devDependencies": {
"@ignored/hardhat-vnext": "workspace:^3.0.0-next.6",
"@ignored/hardhat-vnext-viem": "workspace:^3.0.0-next.1",
"@ignored/hardhat-vnext-node-test-runner": "workspace:^3.0.0-next.2",
"@ignored/hardhat-vnext-network-helpers": "workspace:^3.0.0-next.2",
"typescript": "~5.5.0",
"viem": "^2.21.17"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import hre from "@ignored/hardhat-vnext";

async function deployCounterContract() {
const optimism = await hre.network.connect("local-base", "optimism");

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

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

deployCounterContract();
Loading

0 comments on commit a6af747

Please sign in to comment.