mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
First-Upload
This commit is contained in:
parent
b1cec853be
commit
84b49d591a
11 changed files with 7787 additions and 0 deletions
29
.github/workflows/build.yml
vendored
Normal file
29
.github/workflows/build.yml
vendored
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
name: Docker Image Build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
types: [ closed ]
|
||||
|
||||
jobs:
|
||||
Build:
|
||||
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Build')
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Build the Docker image
|
||||
run: docker build . --file Dockerfile
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
push: true
|
||||
tags: vatman/client-go:latest
|
||||
46
.github/workflows/deploy.yml
vendored
Normal file
46
.github/workflows/deploy.yml
vendored
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
name: Docker Image Deploy
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
types: [ closed ]
|
||||
|
||||
jobs:
|
||||
Deploy:
|
||||
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'CI:Deploy')
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout Code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Set Up Node.js
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '20.9'
|
||||
|
||||
- name: Start Local Devnet
|
||||
run: |
|
||||
docker run -d --name ethereum-node -p 8545:8545 -p 30303:30303 vatman/client-go --dev --http --http.api eth,web3,net --http.corsdomain "http://remix.ethereum.org"
|
||||
|
||||
- name: Deploy Hardhat Project
|
||||
working-directory: ./hardhat
|
||||
run: |
|
||||
npm install --save-dev hardhat
|
||||
npx hardhat run scripts/deploy.js --network localhost
|
||||
|
||||
- name: Hardhat test
|
||||
working-directory: ./hardhat
|
||||
run: npx hardhat test
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Export the Docker image
|
||||
run: docker commit ethereum-node vatman/client-go-hardhat:latest
|
||||
|
||||
- name: Export and push
|
||||
run: docker push vatman/client-go-hardhat:latest
|
||||
11
hardhat/.gitignore
vendored
Normal file
11
hardhat/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
node_modules
|
||||
.env
|
||||
coverage
|
||||
coverage.json
|
||||
typechain
|
||||
typechain-types
|
||||
|
||||
# Hardhat files
|
||||
cache
|
||||
artifacts
|
||||
|
||||
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 script 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 run scripts/deploy.js
|
||||
```
|
||||
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.9;
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
15
hardhat/docker/docker-compose.yml
Normal file
15
hardhat/docker/docker-compose.yml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
version: '3'
|
||||
services:
|
||||
app:
|
||||
image: vatman/client-go
|
||||
container_name: ethereum-node
|
||||
entrypoint:
|
||||
- geth
|
||||
- --dev
|
||||
- --http
|
||||
- --http.api
|
||||
- eth,web3,net
|
||||
- --http.corsdomain
|
||||
- "http://remix.ethereum.org"
|
||||
ports:
|
||||
- 8545:8545
|
||||
11
hardhat/hardhat.config.js
Normal file
11
hardhat/hardhat.config.js
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
require("@nomicfoundation/hardhat-toolbox");
|
||||
|
||||
/** @type import('hardhat/config').HardhatUserConfig */
|
||||
module.exports = {
|
||||
solidity: "0.8.19",
|
||||
networks: {
|
||||
localhost: {
|
||||
url: "http://localhost:8545",
|
||||
},
|
||||
}
|
||||
};
|
||||
7462
hardhat/package-lock.json
generated
Normal file
7462
hardhat/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
7
hardhat/package.json
Normal file
7
hardhat/package.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"name": "hardhat-project",
|
||||
"devDependencies": {
|
||||
"@nomicfoundation/hardhat-toolbox": "^3.0.0",
|
||||
"hardhat": "^2.18.3"
|
||||
}
|
||||
}
|
||||
33
hardhat/scripts/deploy.js
Normal file
33
hardhat/scripts/deploy.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// 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}`
|
||||
);
|
||||
}
|
||||
|
||||
// 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
126
hardhat/test/Lock.js
Normal 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]
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue