Skip to content

Commit

Permalink
chore: initial foundry deployment/utils/tests
Browse files Browse the repository at this point in the history
Co-authored-by: Sean Casey <[email protected]>
Co-authored-by: Paweł Kędzia <[email protected]>
  • Loading branch information
3 people authored May 12, 2023
1 parent da8ed1e commit 667381b
Show file tree
Hide file tree
Showing 54 changed files with 7,001 additions and 1,484 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/changeset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 7.18.1

- name: Set up node
uses: actions/setup-node@v2
with:
cache: pnpm
node-version: 16
node-version: 18

- name: Install dependencies
run: pnpm install
Expand Down
19 changes: 12 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:
cancel-in-progress: true

env:
CACHE_VERSION: '4'
CACHE_VERSION: '5'
NODE_OPTIONS: '--max-old-space-size=4096'

jobs:
Expand All @@ -35,8 +35,6 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 7.18.1

- name: Set up node
uses: actions/setup-node@v2
Expand All @@ -47,8 +45,14 @@ jobs:
- name: Install dependencies
run: pnpm install

- name: Make interfaces
run: make interfaces

- name: Run tests
run: forge test
env:
ETHEREUM_NODE_MAINNET: ${{ secrets.ETHEREUM_NODE_MAINNET }}
ETHEREUM_NODE_POLYGON: ${{ secrets.ETHEREUM_NODE_POLYGON }}

test:
name: Test
Expand All @@ -65,8 +69,6 @@ jobs:

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 7.18.1

- name: Set up node
uses: actions/setup-node@v2
Expand Down Expand Up @@ -113,10 +115,13 @@ jobs:
- name: Check out repository
uses: actions/checkout@v3

- name: Install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 7.18.1

- name: Set up node
uses: actions/setup-node@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ cache/
artifacts/
deployments/
node_modules/
tests/interfaces/internal/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ deployments/

# Ignore dependencies.
lib/

# Ignore tests directory (uses `forge fmt`).
tests/
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "0.6.12 || 0.7.6"],
"compiler-version": ["error", "0.6.12 || 0.7.6 || 0.8.17 || ^0.8.10"],
"func-visibility": ["error", { "ignoreConstructors": true }],
"no-empty-blocks": "off",
"not-rely-on-time": "off",
Expand Down
135 changes: 133 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,133 @@
clean:
git clean -dfX --exclude !**/.env* --exclude !**/cache/hardhat-network-fork
# See https://tech.davis-hansson.com/p/make
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
.DEFAULT_GOAL := all

MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules

ifndef VERBOSE
MAKEFLAGS += --silent
endif

ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >

CAST := cast
FORGE := forge

TESTS_DIR := tests/
CONTRACTS_DIR := contracts/
ARTIFACTS_DIR := artifacts/
INTERFACES_DIR := tests/interfaces/internal/
INTERFACES_LICENSE_HEADER := // SPDX-License-Identifier: Unlicense

.PHONY: help
help: ## Describe useful make targets
> grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "%-30s %s\n", $$1, $$2}'

.PHONY: all
all: build lint test ## Run build, lint (default)

.PHONY: build
build: artifacts interfaces ## Build all contract artifacts & interfaces

.PHONY: artifacts
artifacts: $(ARTIFACTS_DIR) ## Build all contract artifacts

.PHONY: interfaces
interfaces: $(INTERFACES_DIR) ## ## Generate interfaces for all contracts listed in interfaces.txt

.PHONY: test
test: ## Run the entire test suite
> $(FORGE) test

.PHONY: lint
lint: ## Lint all contract source files
# TODO: Switch to `forge fmt` for the contract source files too.
> $(FORGE) fmt --check $(TESTS_DIR) $(INTERFACES_DIR)

.PHONY: format
format: ## Format all contract source files
# TODO: Switch to `forge fmt` for the contract source files too.
> $(FORGE) fmt $(TESTS_DIR) $(INTERFACES_DIR)

.PHONY: clean
clean: ## Remove all untracked files and directories
> git clean -dfX --exclude !**/.env* --exclude !**/deployments --exclude !**/cache/hardhat-network-fork

$(ARTIFACTS_DIR): Makefile $(shell find $(CONTRACTS_DIR) -type f -name "*.sol")
> mkdir -p $(@D)
> # Remove this once the `forge build` command supports a more capable version of the `--skip` option.
> export FOUNDRY_TEST=this-directory-does-not-exist
> $(FORGE) build --extra-output-files abi
> touch $@

$(INTERFACES_DIR): Makefile $(ARTIFACTS_DIR) interfaces.txt
> mkdir -p $(@D)
>
> # Remove all existing interfaces and abis.
> find $(INTERFACES_DIR) -type f -name "*.sol" -delete
> find $(INTERFACES_DIR) -type f -name "*.abi.json" -delete
>
> # Read interfaces.txt line by line and use `cast interface` to generate the interfaces.
> while read -r line; do
> # Skip empty lines and lines starting with `#`.
> if [[ -z "$$line" || "$$line" == \#* ]]; then
> continue
> fi
>
> # The line format is `output: input`.
> output="$$(echo $$line | cut -d ':' -f1 | xargs)"
> input="$$(echo $$line | cut -d ':' -f2 | xargs)"
> if [[ -z "$$output" || -z "$$input" ]]; then
> echo "Invalid line format n interfaces.txt ($$line)"
> exit 1;
> fi
>
> # Extract the output name of the interface from the output path.
> name="$$(basename $$output | cut -d '.' -f1)"
> if [[ -z "$$name" ]]; then
> echo "Invalid output $$output in interfaces.txt"
> exit 1
> fi
>
> # Prepend the interfaces directory to the output path and check the file extension.
> output="$(INTERFACES_DIR)$$output"
> if [[ ! "$$input" == *.abi.json ]]; then
> echo "Invalid extension for interface source $$input"
> exit 1
> fi
>
> # If the input is a path, use it directly. Otherwise, try to find the file in the artifacts directory.
> if echo "$$input" | grep -q "/"; then
> path="$$input"
> else
> path="$$(find $(ARTIFACTS_DIR) -type f -name $$input | head -n 1)"
> fi
>
> # Check if the source file was found.
> if [[ -z "$$path" || ! -f "$$path" ]]; then
> echo "Failed to locate source file for $$input"
> exit 1
> fi
>
> dir="$$(dirname $$output)"
> abi="$$dir/$$name.abi.json"
>
> # Create the parent directory and copy the abi file over.
> mkdir -p "$$dir"
> cp "$$path" "$$abi"
>
> # Generate the interface using `cast interface`.
> $(CAST) interface "$$abi" -o "$$output" -n "$$name"
>
> # Add a license header to the generated interface.
> echo -e "$(INTERFACES_LICENSE_HEADER)\n$$(cat $$output)" > $$output
> done < "interfaces.txt"
>
> touch $@
64 changes: 53 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,85 @@

Enzyme is an Ethereum-based protocol for decentralized on-chain asset management. It is a protocol for people or entities to manage their wealth & the wealth of others within a customizable and safe environment. Enzyme empowers anyone to set up, manage and invest in customized on-chain investment vehicles.

## Install
## Security Issues and Bug Bounty

If you find a vulnerability that may affect live deployments, you can submit a report via:

A. Immunefi(https://immunefi.com/bounty/enzymefinance/), or

B. Direct email to [[email protected]](mailto:[email protected])

Please **DO NOT** open a public issue.

## Using this Repo

### A Tale of Two Frameworks

:construction:

This repo is currently in-flux for a gradual move from Hardhat to Foundry, so there are mixed dependencies, deployment mechanisms, helpers, and tests. The following rules should hold:

- all production contracts live in `contracts/persistent/` and `contracts/release/` (deployed contracts [here](https://docs.enzyme.finance/developers/contracts))
- the "old" Hardhat-based dependencies / deployment / helpers / tests live in `packages/`
- the "new" Foundry-based dependencies / deployment / helpers / tests live in `tests/`

Test suites are being gradually migrated from the Hardhat setup to Foundry, so check both for test coverage.

### Prerequisites

1. Make sure to have the following installed:

- [node](https://www.nodejs.org)
- [pnpm](https://pnpm.io)
- [foundry](https://github.com/foundry-rs/foundry)
- [make](https://www.gnu.org/software/make/)

```sh
2. Clone this repo:

```
git clone [GIT_REPOSITORY_URL]
cd protocol
```

### Dependencies

1. Install node packages:

```sh
pnpm install
```

## Compile contracts
2. Generate internal interfaces for foundry deployment and tests:

```sh
make build
```

### Compile contracts

```sh
pnpm compile
```

## Test
### Run tests

First, create a `.env` file by copying `.env.example`. Input your Ethereum node endpoint info as-needed (generally, only setting `ETHEREUM_NODE_MAINNET` is fine).
1. Create a `.env` file by copying `.env.example`. Input your Ethereum (and/or other networks) node endpoint info as-needed (generally, only setting `ETHEREUM_NODE_MAINNET`, `ETHEREUM_NODE_POLYGON`, etc is fine).

Then, you can run tests. The full test suite can be run with:
2. Run hardhat tests with (defaults to full test suite):

```sh
pnpm test
```

3. Run foundry tests with (defaults to full test suite):

```sh
forge test
```

Note that tests might fail on the first runs while building a cache for the fork block, due to timeout. Continue to run tests as-needed, which will build the cache.

## Contribute

See [our contributing instructions](CONTRIBUTING.md).

Please note that all repositories hosted under this organization follow our [Code of Conduct](CODE_OF_CONDUCT.md), make sure to review and follow it.

### Security Issues

If you find a vulnerability that may affect live or testnet deployments, please send your report privately to [[email protected]](mailto:[email protected]). Please **DO NOT** file a public issue.
6 changes: 5 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ cache_path = "cache"
bytecode_hash = "none"
verbosity = 3
remappings = [
"@enzyme/=contracts/",
"@uniswap/=node_modules/@uniswap/",
"@openzeppelin/=node_modules/@openzeppelin/",
"@openzeppelin-solc-0.7/=node_modules/@openzeppelin-solc-0.7/",
"forge-std/=lib/forge-std/src/"
]
fs_permissions = [{ access = "read", path = "./artifacts"}]

[profile.default.optimizer_details]
yul = false

[rpc_endpoints]
mainnet = "${ETHEREUM_NODE_MAINNET}"
polygon = "${ETHEREUM_NODE_POLYGON}"
Loading

0 comments on commit 667381b

Please sign in to comment.