mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
test hardhat
This commit is contained in:
parent
0e75f88c34
commit
2f019064e4
13 changed files with 7493 additions and 2 deletions
27
.github/workflows/ci-build.yaml
vendored
Normal file
27
.github/workflows/ci-build.yaml
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
name: CI-Build
|
||||||
|
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
types: [ closed ]
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
devnet:
|
||||||
|
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Build')
|
||||||
|
runs-on: ubuntu-24.04
|
||||||
|
|
||||||
|
services:
|
||||||
|
geth:
|
||||||
|
image: dmanov/go-ethereum:dev-latest
|
||||||
|
ports:
|
||||||
|
- 8545:8545
|
||||||
|
- 30303:30303
|
||||||
|
options: >-
|
||||||
|
--health-cmd="curl --fail http://localhost:8545 || exit 1"
|
||||||
|
--health-interval=10s
|
||||||
|
--health-timeout=5s
|
||||||
|
--health-retries=10
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
name: Task Linux Tests
|
name: CI-Deploy
|
||||||
|
|
||||||
|
|
||||||
on:
|
on:
|
||||||
|
|
@ -11,7 +11,7 @@ permissions:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-push:
|
build-push:
|
||||||
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Build')
|
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Deploy ')
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
environment: dev
|
environment: dev
|
||||||
13
docker-compose.yaml
Normal file
13
docker-compose.yaml
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
version: "3.8"
|
||||||
|
services:
|
||||||
|
geth:
|
||||||
|
container_name: geth
|
||||||
|
image: dmanov/go-ethereum:dev-latest
|
||||||
|
volumes:
|
||||||
|
- ./devnet:/root/.ethereum
|
||||||
|
ports:
|
||||||
|
- "8545:8545" # HTTP based JSON RPC API
|
||||||
|
- "30303:30303" # P2P
|
||||||
|
command: >
|
||||||
|
--http --http.addr 0.0.0.0 --http.api eth,net,web3
|
||||||
|
--dev
|
||||||
35
hardhat/.gitignore
vendored
Normal file
35
hardhat/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
node_modules
|
||||||
|
.env
|
||||||
|
|
||||||
|
# Hardhat files
|
||||||
|
/cache
|
||||||
|
/artifacts
|
||||||
|
|
||||||
|
# TypeChain files
|
||||||
|
/typechain
|
||||||
|
/typechain-types
|
||||||
|
|
||||||
|
# solidity-coverage files
|
||||||
|
/coverage
|
||||||
|
/coverage.json
|
||||||
|
|
||||||
|
# Hardhat Ignition default folder for deployments against a local node
|
||||||
|
ignition/deployments/chain-31337
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
.env
|
||||||
|
|
||||||
|
# Hardhat files
|
||||||
|
/cache
|
||||||
|
/artifacts
|
||||||
|
|
||||||
|
# TypeChain files
|
||||||
|
/typechain
|
||||||
|
/typechain-types
|
||||||
|
|
||||||
|
# solidity-coverage files
|
||||||
|
/coverage
|
||||||
|
/coverage.json
|
||||||
|
|
||||||
|
# Hardhat Ignition default folder for deployments against a local node
|
||||||
|
ignition/deployments/chain-31337
|
||||||
13
hardhat/README.md
Normal file
13
hardhat/README.md
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
# Sample Hardhat Project
|
||||||
|
|
||||||
|
This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a Hardhat Ignition module that deploys that contract.
|
||||||
|
|
||||||
|
Try running some of the following tasks:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
npx hardhat help
|
||||||
|
npx hardhat test
|
||||||
|
REPORT_GAS=true npx hardhat test
|
||||||
|
npx hardhat node
|
||||||
|
npx hardhat ignition deploy ./ignition/modules/Lock.ts
|
||||||
|
```
|
||||||
34
hardhat/contracts/Lock.sol
Normal file
34
hardhat/contracts/Lock.sol
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
// SPDX-License-Identifier: UNLICENSED
|
||||||
|
pragma solidity ^0.8.28;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
hardhat/deploy.ts
Normal file
14
hardhat/deploy.ts
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
import { ethers } from "hardhat";
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const ContractFactory = await ethers.getContractFactory("YourContractName");
|
||||||
|
const contract = await ContractFactory.deploy();
|
||||||
|
await contract.deployed();
|
||||||
|
|
||||||
|
console.log(`Deployed to: ${contract.address}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
main().catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
process.exitCode = 1;
|
||||||
|
});
|
||||||
13
hardhat/hardhat.config.ts
Normal file
13
hardhat/hardhat.config.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { HardhatUserConfig } from "hardhat/config";
|
||||||
|
import "@nomicfoundation/hardhat-toolbox";
|
||||||
|
|
||||||
|
const config: HardhatUserConfig = {
|
||||||
|
solidity: "0.8.28",
|
||||||
|
networks: {
|
||||||
|
localhost: {
|
||||||
|
url: "http://127.0.0.1:8545"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default config;
|
||||||
20
hardhat/ignition/modules/Lock.ts
Normal file
20
hardhat/ignition/modules/Lock.ts
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
// This setup uses Hardhat Ignition to manage smart contract deployments.
|
||||||
|
// Learn more about it at https://hardhat.org/ignition
|
||||||
|
|
||||||
|
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
|
||||||
|
|
||||||
|
const JAN_1ST_2030 = 1893456000;
|
||||||
|
const ONE_GWEI: bigint = 1_000_000_000n;
|
||||||
|
|
||||||
|
const LockModule = buildModule("LockModule", (m) => {
|
||||||
|
const unlockTime = m.getParameter("unlockTime", JAN_1ST_2030);
|
||||||
|
const lockedAmount = m.getParameter("lockedAmount", ONE_GWEI);
|
||||||
|
|
||||||
|
const lock = m.contract("Lock", [unlockTime], {
|
||||||
|
value: lockedAmount,
|
||||||
|
});
|
||||||
|
|
||||||
|
return { lock };
|
||||||
|
});
|
||||||
|
|
||||||
|
export default LockModule;
|
||||||
7169
hardhat/package-lock.json
generated
Normal file
7169
hardhat/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
15
hardhat/package.json
Normal file
15
hardhat/package.json
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
{
|
||||||
|
"name": "hardhat",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"devDependencies": {
|
||||||
|
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
|
||||||
|
"hardhat": "^2.24.2"
|
||||||
|
}
|
||||||
|
}
|
||||||
127
hardhat/test/Lock.ts
Normal file
127
hardhat/test/Lock.ts
Normal file
|
|
@ -0,0 +1,127 @@
|
||||||
|
import {
|
||||||
|
time,
|
||||||
|
loadFixture,
|
||||||
|
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
|
||||||
|
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
|
||||||
|
import { expect } from "chai";
|
||||||
|
import hre from "hardhat";
|
||||||
|
|
||||||
|
describe("Lock", function () {
|
||||||
|
// 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.
|
||||||
|
async function deployOneYearLockFixture() {
|
||||||
|
const ONE_YEAR_IN_SECS = 365 * 24 * 60 * 60;
|
||||||
|
const ONE_GWEI = 1_000_000_000;
|
||||||
|
|
||||||
|
const lockedAmount = ONE_GWEI;
|
||||||
|
const unlockTime = (await time.latest()) + ONE_YEAR_IN_SECS;
|
||||||
|
|
||||||
|
// Contracts are deployed using the first signer/account by default
|
||||||
|
const [owner, otherAccount] = await hre.ethers.getSigners();
|
||||||
|
|
||||||
|
const Lock = await hre.ethers.getContractFactory("Lock");
|
||||||
|
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
|
||||||
|
|
||||||
|
return { lock, unlockTime, lockedAmount, owner, otherAccount };
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Deployment", function () {
|
||||||
|
it("Should set the right unlockTime", async function () {
|
||||||
|
const { lock, unlockTime } = await loadFixture(deployOneYearLockFixture);
|
||||||
|
|
||||||
|
expect(await lock.unlockTime()).to.equal(unlockTime);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should set the right owner", async function () {
|
||||||
|
const { lock, owner } = await 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 loadFixture(
|
||||||
|
deployOneYearLockFixture
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(await hre.ethers.provider.getBalance(lock.target)).to.equal(
|
||||||
|
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 time.latest();
|
||||||
|
const Lock = await hre.ethers.getContractFactory("Lock");
|
||||||
|
await expect(Lock.deploy(latestTime, { value: 1 })).to.be.revertedWith(
|
||||||
|
"Unlock time should be in the future"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Withdrawals", function () {
|
||||||
|
describe("Validations", function () {
|
||||||
|
it("Should revert with the right error if called too soon", async function () {
|
||||||
|
const { lock } = await loadFixture(deployOneYearLockFixture);
|
||||||
|
|
||||||
|
await expect(lock.withdraw()).to.be.revertedWith(
|
||||||
|
"You can't withdraw yet"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Should revert with the right error if called from another account", async function () {
|
||||||
|
const { lock, unlockTime, otherAccount } = await loadFixture(
|
||||||
|
deployOneYearLockFixture
|
||||||
|
);
|
||||||
|
|
||||||
|
// We can increase the time in Hardhat Network
|
||||||
|
await time.increaseTo(unlockTime);
|
||||||
|
|
||||||
|
// We use lock.connect() to send a transaction from another account
|
||||||
|
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWith(
|
||||||
|
"You aren't the owner"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async function () {
|
||||||
|
const { lock, unlockTime } = await loadFixture(
|
||||||
|
deployOneYearLockFixture
|
||||||
|
);
|
||||||
|
|
||||||
|
// Transactions are sent using the first signer by default
|
||||||
|
await time.increaseTo(unlockTime);
|
||||||
|
|
||||||
|
await expect(lock.withdraw()).not.to.be.reverted;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Events", function () {
|
||||||
|
it("Should emit an event on withdrawals", async function () {
|
||||||
|
const { lock, unlockTime, lockedAmount } = await loadFixture(
|
||||||
|
deployOneYearLockFixture
|
||||||
|
);
|
||||||
|
|
||||||
|
await time.increaseTo(unlockTime);
|
||||||
|
|
||||||
|
await expect(lock.withdraw())
|
||||||
|
.to.emit(lock, "Withdrawal")
|
||||||
|
.withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Transfers", function () {
|
||||||
|
it("Should transfer the funds to the owner", async function () {
|
||||||
|
const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
|
||||||
|
deployOneYearLockFixture
|
||||||
|
);
|
||||||
|
|
||||||
|
await time.increaseTo(unlockTime);
|
||||||
|
|
||||||
|
await expect(lock.withdraw()).to.changeEtherBalances(
|
||||||
|
[owner, lock],
|
||||||
|
[lockedAmount, -lockedAmount]
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
11
hardhat/tsconfig.json
Normal file
11
hardhat/tsconfig.json
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2020",
|
||||||
|
"module": "commonjs",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"resolveJsonModule": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue