feat: add devnet and Dockerfile for it

This commit is contained in:
Moreti Georgiev 2025-03-07 00:29:21 +02:00
parent af36384ffe
commit caae2eb3f8
No known key found for this signature in database
GPG key ID: 08C2C7BF6630F4CE
12 changed files with 8241 additions and 0 deletions

24
Dockerfile.devnet Normal file
View file

@ -0,0 +1,24 @@
# Base image without redundant platform flag
FROM 967763122477.dkr.ecr.eu-central-1.amazonaws.com/limechain/geth AS devnet
RUN apk add --no-cache nodejs npm curl jq
WORKDIR /app
COPY hardhat/ ./hardhat/
COPY genesis.json /app/genesis.json
WORKDIR /app/hardhat
RUN npm install
# Create startup script
COPY scripts/start-with-deploy.sh /usr/local/bin/start-with-deploy.sh
RUN chmod +x /usr/local/bin/start-with-deploy.sh
# Set entry point
ENTRYPOINT ["/usr/local/bin/start-with-deploy.sh"]
# Expose necessary ports
EXPOSE 8545 8546 8547 30303

25
genesis.json Normal file
View file

@ -0,0 +1,25 @@
{
"config": {
"chainId": 1337,
"homesteadBlock": 0,
"eip150Block": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
"petersburgBlock": 0,
"istanbulBlock": 0,
"berlinBlock": 0,
"londonBlock": 0,
"terminalTotalDifficulty": 0,
"shanghaiTime": 0
},
"terminalTotalDifficulty": "-1",
"difficulty": "0",
"gasLimit": "8000000",
"alloc": {
"f39fd6e51aad88f6f4ce6ab8827279cfffb92266": {
"balance": "1000000000000000000000"
}
}
}

17
hardhat/.gitignore vendored Normal file
View file

@ -0,0 +1,17 @@
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
View 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.js
```

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

18
hardhat/hardhat.config.js Normal file
View file

@ -0,0 +1,18 @@
require("@nomicfoundation/hardhat-toolbox");
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
networks: {
localhost: {
url: "http://127.0.0.1:8545",
accounts: {
mnemonic: "test test test test test test test test test test test junk",
},
chainId: 1337,
gasPrice: 8000000000
},
},
solidity: {
version: "0.8.28",
}
};

View file

@ -0,0 +1,18 @@
// This setup uses Hardhat Ignition to manage smart contract deployments.
// Learn more about it at https://hardhat.org/ignition
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");
const JAN_1ST_2030 = 1893456000;
const ONE_GWEI = 1_000_000_000n;
module.exports = 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 };
});

7873
hardhat/package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

20
hardhat/package.json Normal file
View file

@ -0,0 +1,20 @@
{
"name": "hardhat",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "hardhat ignition deploy ./ignition/modules/Lock.js --network localhost"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"hardhat": "^2.22.19"
},
"dependencies": {
"dotenv": "^16.4.7"
}
}

48
hardhat/scripts/deploy.js Normal file
View file

@ -0,0 +1,48 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");
async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 60;
const lockedAmount = hre.ethers.parseEther("0.001");
const lock = await hre.ethers.deployContract("Lock", [unlockTime], {
value: lockedAmount,
});
await lock.waitForDeployment();
console.log(
`Lock with ${ethers.formatEther(
lockedAmount
)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.target}`
);
let tx = lock.deploymentTransaction()
const data = {
"Transaction Hash": tx.hash,
"From": tx.from,
"To": tx.to,
"Gas Limit": Number(tx.gasLimit),
"Gas Price": Number(tx.gasPrice),
"Block Number": tx.blockNumber
} // Get the transaction hash
console.log("Transaction Data:\n", data);
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

126
hardhat/test/Lock.js Normal file
View file

@ -0,0 +1,126 @@
const {
time,
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");
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 ethers.getSigners();
const Lock = await 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 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 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]
);
});
});
});
});

View file

@ -0,0 +1,25 @@
#!/bin/sh
# Start geth in the background with the provided arguments
# First initialize with genesis block
geth --datadir /app/data init /app/genesis.json
# Then start geth with API options
geth --dev --http --http.addr=0.0.0.0 --http.api=eth,net,web3,txpool,debug,admin --dev.period 1 --datadir /app/data &
GETH_PID=$!
echo "Waiting for Geth RPC to be ready..."
until curl --silent --fail http://localhost:8545 > /dev/null; do
sleep 1
done
echo "Geth RPC is available"
# Deploy contracts if needed
cd /app/hardhat
# yes "y" | npx hardhat run scripts/deploy.js --network localhost
# sleep 10
yes "y" | npx hardhat ignition deploy ignition/modules/Lock.js --network localhost
# Wait for the geth process to finish
wait $GETH_PID