-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add Node Test Runner/Viem template
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
Showing
14 changed files
with
375 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
10 changes: 10 additions & 0 deletions
10
v-next/hardhat/templates/node-test-runner-viem/contracts/Counter.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
v-next/hardhat/templates/node-test-runner-viem/contracts/Counter.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
v-next/hardhat/templates/node-test-runner-viem/contracts/Lock.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
v-next/hardhat/templates/node-test-runner-viem/hardhat.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
v-next/hardhat/templates/node-test-runner-viem/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
v-next/hardhat/templates/node-test-runner-viem/scripts/deploy-counter-contract.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.