Skip to content

Commit

Permalink
feat: package revamp (#24)
Browse files Browse the repository at this point in the history
* outdated contracts removed

* hardhat config removed

* dependencies removed

* feat: package revamp part 2 (setup) (#25)

* `hardhat.config.js` added

* `foundry.toml` added

* `fetch_contracts` script added

* system-contracts preprocessing added to `fetch_contracts` script

* Added contracts from https://github.com/matter-labs/era-contracts.git (protocol-defense - 9aab5b864693c24ae3240a4a8370d1a3d855c08a)

* openzeppelin dependencies (`contracts-v4` and `contracts-upgradeable-v4`) added

* 0.8.20 added to hardhat compiler list

* foundry auto solidity version detection enabled

* hardhat ci

* foundry ci

* hardhat ci typo

* ci rework

* ci yml to yaml

* ci fix

* shortened job names in ci

* Revert "Added contracts from https://github.com/matter-labs/era-contracts.git (protocol-defense - 9aab5b864693c24ae3240a4a8370d1a3d855c08a)"

This reverts commit 79798f3.

* unnecessary remappings.txt removed

* forge install: openzeppelin-contracts-v4

v4.9.5

* forge install: openzeppelin-contracts-upgradeable-v4

v4.9.5

* changing to submodule deps for foundry

* yarn removed from foundry ci

* foundry ci syntax fix

* fetch target branch changed to dev

* ref as input for fetch_contracts

* update-contracts workflow

* fetch_contracts usage message extended

* formatting

* default value of ref removed from workflow

* populated package.json

* added logo.svg

* added LICENSE-MIT

* readme updated

* added installation and usage sections to readme

* package.json repository section updated

* publish-package workflow added

* ci: via-ir flag set for l2 build jobs
  • Loading branch information
benceharomi authored Aug 28, 2024
1 parent b8449bf commit c6076fa
Show file tree
Hide file tree
Showing 122 changed files with 769 additions and 11,930 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: CI
on: [pull_request]
jobs:
hardhat-ci:
name: Hardhat
uses: ./.github/workflows/hardhat-ci.yaml
foundry-ci:
name: Foundry
uses: ./.github/workflows/foundry-ci.yaml
22 changes: 22 additions & 0 deletions .github/workflows/foundry-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Foundry CI
on: [workflow_call]
jobs:
build-l1:
name: Build L1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: foundry-rs/foundry-toolchain@v1
- run: forge build --contracts contracts/l1-contracts

build-l2:
name: Build L2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- uses: foundry-rs/foundry-toolchain@v1
- run: forge build --via-ir --contracts contracts/l2-contracts
38 changes: 38 additions & 0 deletions .github/workflows/hardhat-ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Hardhat CI
on: [workflow_call]
jobs:
build-l1:
name: Build L1
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.18.0
cache: yarn
- run: yarn
- run: HARDHAT_CONTRACTS_PATH=contracts/l1-contracts yarn hardhat compile

build-l2:
name: Build L2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.18.0
cache: yarn
- run: yarn
- run: HARDHAT_CONTRACTS_PATH=contracts/l2-contracts HARDHAT_VIA_IR=true yarn hardhat compile

build-sc:
name: Build SC
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.18.0
cache: yarn
- run: yarn
- run: HARDHAT_CONTRACTS_PATH=contracts/system-contracts HARDHAT_VIA_IR=true yarn hardhat compile
18 changes: 18 additions & 0 deletions .github/workflows/publish-package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Publish Package
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.18.0
registry-url: 'https://registry.npmjs.org'
scope: '@matterlabs'
- run: yarn
- run: yarn publish --tag ${{ github.event.release.tag_name }} --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
40 changes: 40 additions & 0 deletions .github/workflows/update-contracts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Update Contracts
on:
workflow_dispatch:
inputs:
ref:
description: "Branch, tag or commit of https://github.com/matter-labs/era-contracts to fetch contracts from"
type: string
required: true

permissions:
contents: write
pull-requests: write

jobs:
update-contracts:
name: Update Contracts
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 18.18.0
cache: yarn

- name: fetch contracts from the given ref
run: .scripts/fetch_contracts --ref ${{ inputs.ref }}

- name: create a new branch
run: git checkout -b contracts-update-${{ inputs.ref }}-$(date +%Y%m%d%H%M%S)

- name: push the new branch to the repository
run: git push origin HEAD

- name: create a pull request
run: |
gh pr create \
--title "$(git log -1 --pretty=%B)" \
--body "Updates contracts from https://github.com/matter-labs/era-contracts/tree/${{ inputs.ref }}" \
--base main \
--head $(git rev-parse --abbrev-ref HEAD)
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
node_modules
cache
artifacts

# Hardhat
/cache
/artifacts

# Foundry
/out
/cache_forge
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "lib/openzeppelin-contracts-v4"]
path = lib/openzeppelin-contracts-v4
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/openzeppelin-contracts-upgradeable-v4"]
path = lib/openzeppelin-contracts-upgradeable-v4
url = https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable
124 changes: 124 additions & 0 deletions .scripts/fetch_contracts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/bash

# Define the repository URL
REPO_URL="https://github.com/matter-labs/era-contracts.git"

# Branch/tag/commit to fetch
BRANCH_TAG_COMMIT=""

while [[ $# -gt 0 ]]; do
case "$1" in
--ref)
BRANCH_TAG_COMMIT="$2"
shift 2
;;
*)
echo "Unknown option: $1" >&2
echo "Usage: $0 --ref branch|tag|commit of $REPO_URL" >&2
exit 1
;;
esac
done

# Check if the ref is set
if [ -z "$BRANCH_TAG_COMMIT" ]; then
echo "Usage: $0 --ref branch|tag|commit of $REPO_URL" >&2
exit 1
fi

# Define the destination directory and a temporary directory for the clone
DEST_DIR="$(pwd)/contracts"
TEMP_DIR=$(mktemp -d)

# Create necessary directories if they don't exist
mkdir -p "$DEST_DIR/l1-contracts" "$DEST_DIR/l2-contracts" "$DEST_DIR/system-contracts"

# Clone the repository without checking out files
git clone --no-checkout "$REPO_URL" "$TEMP_DIR" >/dev/null 2>&1

# Navigate to the temporary directory and checkout the desired branch/tag/commit
cd "$TEMP_DIR"
git checkout "$BRANCH_TAG_COMMIT" >/dev/null 2>&1

# Capture the commit hash
COMMIT_HASH=$(git rev-parse HEAD)

# Check if BRANCH_TAG_COMMIT equals COMMIT_HASH
if [ "$BRANCH_TAG_COMMIT" = "$COMMIT_HASH" ]; then
# If they are the same, only use COMMIT_HASH
COMMIT_HASH_MESSAGE="$COMMIT_HASH"
else
# Otherwise, use BRANCH_TAG_COMMIT-COMMIT_HASH
COMMIT_HASH_MESSAGE_MESSAGE="$BRANCH_TAG_COMMIT - $COMMIT_HASH"
fi

# Set up sparse checkout and fetch only the contracts directories
git sparse-checkout init --cone >/dev/null 2>&1
# For system-contracts we need to fetch the whole directory for preprocessing
git sparse-checkout set l1-contracts/contracts l2-contracts/contracts system-contracts >/dev/null 2>&1
git checkout >/dev/null 2>&1

# Preprocess the system contracts. It needs to be executed in the system-contracts subdirectory of the repository. The
# templates are in the `contracts` directory and preprocessed contracts are in the `contracts-preprocessed` directory.
preprocess_system_contracts() {
# Install dependencies
yarn

# Preprocess the contracts
yarn build

# Remove artifacts
find . -type d -name "artifacts" -exec rm -rf {} +

# Replace `contracts` with `contracts-preprocessed`
rm -rf contracts
mv contracts-preprocessed contracts
}

# Preprocess the system contracts
cd "$TEMP_DIR"/system-contracts
preprocess_system_contracts
cd "$TEMP_DIR"

# Function to compare directories and sync if differences are found
check_and_sync() {
local src_dir="$1"
local dest_dir="$2"

# Use rsync to check for differences (dry run), and fallback to diff if necessary
if ! rsync -acn --delete "$src_dir/" "$dest_dir/" | grep -q . && diff -qr "$src_dir" "$dest_dir" >/dev/null; then
echo "No differences detected between $src_dir and $dest_dir"
return 0
else
echo "Differences detected between $src_dir and $dest_dir"
return 1
fi
}

# Check for differences and sync if needed
DIFF_FOUND=0
for dir in l1-contracts/contracts l2-contracts/contracts system-contracts/contracts; do
check_and_sync "$dir" "$DEST_DIR/${dir%/contracts}"
if [ $? -eq 1 ]; then
DIFF_FOUND=1
fi
done

# If differences were found, clear destination and sync, then commit the changes
if [ $DIFF_FOUND -eq 1 ]; then
for dir in l1-contracts l2-contracts system-contracts; do
rm -rf "$DEST_DIR/$dir/*"
rsync -ac --delete "$dir/contracts/" "$DEST_DIR/$dir/"
done

# Clean up and commit the changes
cd "$(dirname "$DEST_DIR")"
git add contracts
git commit -m "Updated contracts ($COMMIT_HASH_MESSAGE_MESSAGE)" >/dev/null
echo "Contracts fetched, committed, and added to the destination repository from $REPO_URL ($COMMIT_HASH_MESSAGE_MESSAGE)"
else
echo "No changes detected, contracts are up to date. The script will exit without copying or committing."
fi

# Clean up the temporary directory
rm -rf "$TEMP_DIR"
21 changes: 21 additions & 0 deletions LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Matter Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 55 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,56 @@
# zkSync Smart Contracts
# ZKsync Contracts

zkSync smart contracts. More detailed description to be added soon.
[![Logo](logo.svg)](https://zksync.io)

This package contains ZKsync L1, L2 and System Contracts. For more details see the [source repository](https://github.com/matter-labs/era-contracts).

## Installation

### Hardhat

```bash
yarn add @matterlabs/zksync-contracts
```

### Foundry

```bash
forge install matter-labs/v2-testnet-contracts
```

Add the following to the `remappings.txt` file of your project:

```txt
@matterlabs/zksync-contracts/=/lib/v2-testnet-contracts/
```

## Usage

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IPaymaster} from "@matterlabs/zksync-contracts/contracts/system-contracts/interfaces/IPaymaster.sol";
contract MyPaymaster is IPaymaster {
// IMPLEMENTATION
}
```

You can find a lot of useful examples in the [ZKsync docs](https://docs.zksync.io).

## License

ZKsync Contracts are distributed under the terms of the MIT license.

See [LICENSE-MIT](LICENSE-MIT) for details.

## Official Links

- [Website](https://zksync.io)
- [GitHub](https://github.com/matter-labs)
- [ZK Credo](https://github.com/zksync/credo)
- [X](https://x.com/zksync)
- [X for Devs](https://x.com/zksyncdevs)
- [Discord](https://join.zksync.dev)
- [Mirror](https://zksync.mirror.xyz)
9 changes: 9 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[profile.default]
auto_detect_solc = true
evm_version = 'cancun'
optimizer = true
optimizer-runs = 9999999
src = 'contracts'
out = 'out'
cache_path = 'cache_forge'
libs = ["lib"]
Loading

0 comments on commit c6076fa

Please sign in to comment.