Skip to content

Commit

Permalink
feat(contracts): support hardhat and foundry for contracts package
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjei committed Sep 24, 2024
1 parent 97f9c8d commit 79b6c40
Show file tree
Hide file tree
Showing 22 changed files with 264 additions and 178 deletions.
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ artifacts
cache
typechain-types

# foundry
cache_forge
out
docs

# production
dist
build
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
node-version: 20
cache: yarn

- name: "Install Foundry"
uses: "foundry-rs/foundry-toolchain@v1"

- name: Install dependencies
run: yarn

Expand Down Expand Up @@ -69,6 +72,9 @@ jobs:
node-version: 20
cache: yarn

- name: "Install Foundry"
uses: "foundry-rs/foundry-toolchain@v1"

- name: Install dependencies
run: yarn

Expand Down
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ artifacts
cache
typechain-types

# foundry
cache_forge
out
docs

# production
dist
build
Expand Down
3 changes: 2 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"semi": false,
"arrowParens": "always",
"trailingComma": "none"
"trailingComma": "none",
"plugins": ["prettier-plugin-solidity"]
}
2 changes: 1 addition & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"remark-mdx": "^3.0.1",
"remark-preset-lint-consistent": "^6.0.0",
"remark-preset-lint-recommended": "^7.0.0",
"typescript": "~5.5.0"
"typescript": "5.3"
},
"browserslist": {
"production": [
Expand Down
2 changes: 1 addition & 1 deletion apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"preview": "vite preview"
},
"devDependencies": {
"typescript": "^5.5.0",
"typescript": "5.3",
"vite": "^5.4.1"
}
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0",
"@types/node": "^22.5.5",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"changelogithub": "^0.13.10",
"czg": "^1.9.4",
"eslint": "^8.56.0",
Expand All @@ -31,9 +31,10 @@
"husky": "^9.1.6",
"lint-staged": "^15.2.10",
"prettier": "^3.3.3",
"prettier-plugin-solidity": "^1.4.1",
"rimraf": "^6.0.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.0"
"typescript": "5.3"
},
"scripts": {
"build": "yarn build:docs && yarn build:website && yarn build:packages",
Expand All @@ -45,14 +46,14 @@
"test:coverage": "yarn test:contracts-coverage",
"test:contracts": "yarn workspace excubiae-contracts test",
"test:contracts-coverage": "yarn workspace excubiae-contracts test:coverage",
"format": "prettier -c . && yarn workspace excubiae-docs format",
"format:write": "prettier -w . && yarn workspace excubiae-docs format:write",
"format": "prettier -c . && yarn workspace excubiae-docs format && yarn workspace excubiae-contracts format:forge",
"format:write": "prettier -w . && yarn workspace excubiae-docs format:write && yarn workspace excubiae-contracts format:forge && yarn workspace excubiae-docs format",
"lint": "eslint . && yarn workspace excubiae-contracts lint",
"lint:fix": "eslint . --fix && yarn workspace excubiae-contracts lint:fix",
"version:bump": "scripts/version.ts ${0}",
"version:publish": "scripts/publish.ts",
"version:release": "changelogithub",
"clean": "scripts/clean-apps.ts && scripts/clean-packages.ts && rimraf node_modules",
"clean": "scripts/clean-apps.ts && scripts/clean-packages.ts && rimraf node_modules coverage",
"remove:stable-version-field": "scripts/remove-stable-version-field.ts && yarn format:write",
"precommit": "lint-staged",
"postinstall": "husky && git config --local core.editor cat"
Expand Down
5 changes: 5 additions & 0 deletions packages/contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ node_modules
/cache
/artifacts

# Forge files
/out
/cache_forge
/docs

# TypeChain files
/typechain
/typechain-types
Expand Down
6 changes: 0 additions & 6 deletions packages/contracts/.prettierrc.json

This file was deleted.

7 changes: 0 additions & 7 deletions packages/contracts/.solhint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"extends": "solhint:recommended",
"plugins": ["prettier"],
"rules": {
"code-complexity": ["error", 7],
"compiler-version": ["error", ">=0.8.0"],
Expand All @@ -11,12 +10,6 @@
"func-visibility": ["error", { "ignoreConstructors": true }],
"max-line-length": ["error", 120],
"not-rely-on-time": "off",
"prettier/prettier": [
"error",
{
"endOfLine": "auto"
}
],
"reason-string": ["warn", { "maxLength": 80 }]
}
}
1 change: 1 addition & 0 deletions packages/contracts/.solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
contracts/node_modules
5 changes: 4 additions & 1 deletion packages/contracts/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"publishConfig": {
"access": "public"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"dependencies": {
"@openzeppelin/contracts": "^5.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.27;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

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

contract Lock {
contract Lock is Ownable(msg.sender) {
uint256 public unlockTime;
address payable public owner;

event Withdrawal(uint256 amount, uint256 when);

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

unlockTime = _unlockTime;
owner = payable(msg.sender);
}

function withdraw() public {
function withdraw() public onlyOwner {
// 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);
payable(owner()).transfer(address(this).balance);
}
}
22 changes: 22 additions & 0 deletions packages/contracts/contracts/test/Lock.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25 <0.9.0;

import {Test} from "forge-std/src/Test.sol";
import {console2} from "forge-std/src/console2.sol";

import {Lock} from "../src/Lock.sol";

contract LockTest is Test {
Lock internal lock;

function setUp() public virtual {
// Set unlock time to 1 hour from now
lock = new Lock{value: 1 ether}(block.timestamp + 1 hours);
}

function test_CannotWithdrawYet() external {
// Attempt to withdraw before unlock time
vm.expectRevert("You can't withdraw yet");
lock.withdraw();
}
}
6 changes: 6 additions & 0 deletions packages/contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = 'contracts/src'
out = 'out'
libs = ['node_modules']
test = 'contracts/test'
cache_path = 'cache_forge'
9 changes: 8 additions & 1 deletion packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { HardhatUserConfig } from "hardhat/config"
import "@nomicfoundation/hardhat-toolbox"
import "@nomicfoundation/hardhat-foundry"

const config: HardhatUserConfig = {
solidity: "0.8.27"
solidity: "0.8.27",
paths: {
sources: "./contracts/src",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
}
}

export default config
31 changes: 22 additions & 9 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
{
"name": "excubiae-contracts",
"packageManager": "[email protected]",
"private": true,
"installConfig": {
"hoistingLimits": "dependencies"
},
"scripts": {
"start": "hardhat node",
"compile": "hardhat compile",
"start:hardhat": "hardhat node",
"start:anvil": "anvil",
"compile": "yarn compile:hardhat && yarn compile:forge",
"compile:hardhat": "hardhat compile",
"compile:forge": "forge compile",
"deploy": "hardhat deploy",
"test": "hardhat test",
"test:report-gas": "REPORT_GAS=true hardhat test",
"test:coverage": "hardhat coverage",
"test": "yarn test:hardhat && yarn test:forge",
"test:hardhat": "hardhat test",
"test:forge": "forge test",
"test:report-gas": "REPORT_GAS=true yarn test && forge test --gas-report",
"test:coverage": "hardhat coverage && forge coverage",
"typechain": "hardhat typechain",
"format:forge": "forge fmt",
"lint": "solhint 'contracts/**/*.sol'",
"lint:fix": "solhint 'contracts/**/*.sol' --fix --noPrompt"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-foundry": "^1.1.2",
"@nomicfoundation/hardhat-ignition": "^0.15.5",
"@nomicfoundation/hardhat-ignition-ethers": "^0.15.0",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
Expand All @@ -25,16 +35,19 @@
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": "^22.6.1",
"chai": "^4.2.0",
"ethers": "^6.4.0",
"forge-std": "github:foundry-rs/forge-std#v1.9.2",
"hardhat": "^2.22.11",
"hardhat-gas-reporter": "^1.0.8",
"prettier-plugin-solidity": "^1.4.1",
"solhint": "^5.0.3",
"solhint-plugin-prettier": "^0.1.0",
"solidity-coverage": "^0.8.1",
"ts-node": "^10.9.2",
"typechain": "^8.3.0",
"typescript": "^5.5.0"
"typescript": "5.3"
},
"dependencies": {
"@openzeppelin/contracts": "^5.0.2"
}
}
2 changes: 2 additions & 0 deletions packages/contracts/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@openzeppelin/=node_modules/@openzeppelin/
forge-std/=node_modules/forge-std/
5 changes: 4 additions & 1 deletion packages/contracts/test/Lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ describe("Lock", () => {
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")
await expect(lock.connect(otherAccount).withdraw()).to.be.revertedWithCustomError(
lock,
"OwnableUnauthorizedAccount"
)
})

it("Shouldn't fail if the unlockTime has arrived and the owner calls it", async () => {
Expand Down
14 changes: 13 additions & 1 deletion scripts/clean-packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@ import { readdirSync, rmSync } from "fs"

const folderName = "packages"

const gitIgnored = ["node_modules", "dist", "build", "artifacts", "typechain-types", "cache"]
const gitIgnored = [
"node_modules",
"dist",
"build",
"artifacts",
"typechain-types",
"cache",
"cache_forge",
"docs",
"out",
"coverage.json",
"lib"
]

async function main() {
const folders = readdirSync(folderName, { withFileTypes: true })
Expand Down
2 changes: 1 addition & 1 deletion scripts/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function main() {
// Apply the versions
execSync("yarn version apply --all", { stdio: "inherit" })

await import("./remove-stable-version-field")
await import("./remove-stable-version-field.js")

execSync("yarn format:write")
execSync(`NO_HOOK=1 git commit -am 'chore: v${version}'`)
Expand Down
Loading

0 comments on commit 79b6c40

Please sign in to comment.