Skip to content

Commit 65f4d47

Browse files
feat: Add solc-based contract compilation and Go binding generation (Phase 1) (#1827)
Signed-off-by: Jonathan Oppenheimer <147infiniti@gmail.com> Co-authored-by: Arran Schlosberg <519948+ARR4N@users.noreply.github.com>
1 parent be576a0 commit 65f4d47

File tree

15 files changed

+5158
-5
lines changed

15 files changed

+5158
-5
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ jobs:
5959
uses: actions/checkout@v4
6060
with:
6161
fetch-depth: 0
62+
submodules: recursive
6263
- name: Set up Go
6364
uses: actions/setup-go@v5
6465
with:
6566
go-version-file: "go.mod"
67+
- name: Set up solc
68+
uses: ARR4N/setup-solc@v0.2.0
69+
with:
70+
versions: '0.8.30'
6671
- name: Use Node.js
6772
uses: actions/setup-node@v4
6873
with:
@@ -87,6 +92,11 @@ jobs:
8792
uses: actions/checkout@v4
8893
with:
8994
fetch-depth: 0
95+
submodules: recursive
96+
- name: Set up solc
97+
uses: ARR4N/setup-solc@v0.2.0
98+
with:
99+
versions: '0.8.30'
90100
- name: Use Node.js
91101
uses: actions/setup-node@v4
92102
with:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ diffs/
6161
avalanchego/
6262

6363
.direnv
64+
65+
# Contract compilation artifacts (binary files are not committed)
66+
contracts/artifacts/

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "contracts/lib/openzeppelin-contracts"]
2+
path = contracts/lib/openzeppelin-contracts
3+
url = https://github.com/OpenZeppelin/openzeppelin-contracts.git
4+
branch = v5.4.0

Taskfile.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,15 @@ tasks:
123123
- task: check-generate-rlp
124124

125125
setup-contracts:
126-
desc: Set up contracts by installing NPM dependencies, cleaning Hardhat cache, and compiling contracts
126+
desc: Set up contracts by compiling Solidity contracts and generating Go bindings
127127
dir: ./contracts
128128
cmds:
129+
# Keep npm/Hardhat compilation for existing TypeScript tests (Phase 2-3 migration)
129130
- cmd: npm ci
130131
- cmd: npx hardhat clean
131132
- cmd: npx hardhat compile
133+
# New: Compile contracts with solc and generate Go bindings
134+
- cmd: go generate ./...
132135

133136
shellcheck:
134137
desc: Run shellcheck static analysis on all shell scripts with version management

contracts/README.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,76 @@ The goal of this guide is to lay out best practices regarding writing, testing a
1212

1313
## Prerequisites
1414

15-
### NodeJS and NPM
15+
### Go
1616

17-
First, install the LTS (long-term support) version of [nodejs](https://nodejs.org/en). This is `18.16.0` at the time of writing. NodeJS bundles `npm`.
17+
This project requires Go 1.21 or later. Install from [golang.org](https://golang.org/dl/).
18+
19+
### Solidity Compiler (solc)
20+
21+
The Solidity compiler version 0.8.30 is required to compile contracts. In CI, this is installed automatically via the [setup-solc](https://github.com/ARR4N/setup-solc) GitHub Action.
22+
23+
For local development, install solc 0.8.30:
24+
- **macOS**: `brew install solidity`
25+
- **Linux**: Follow instructions at [solidity docs](https://docs.soliditylang.org/en/latest/installing-solidity.html)
26+
- **CI**: Automatically installed via GitHub Actions
27+
28+
After installation, create a version-specific alias or symlink:
29+
```bash
30+
# Option 1: Symlink (works in all contexts including go generate)
31+
sudo ln -sf $(which solc) /usr/local/bin/solc-v0.8.30 # Linux
32+
sudo ln -sf $(which solc) /opt/homebrew/bin/solc-v0.8.30 # macOS (Homebrew)
33+
34+
# Option 2: Shell alias (interactive shells only)
35+
echo "alias solc-v0.8.30='solc'" >> ~/.bashrc # or ~/.zshrc
36+
```
1837

1938
### Solidity and Avalanche
2039

2140
It is also helpful to have a basic understanding of [Solidity](https://docs.soliditylang.org) and [Avalanche](https://docs.avax.network).
2241

2342
## Dependencies
2443

25-
Clone the repo and install the necessary packages via `yarn`.
44+
Clone the repo and install dependencies:
2645

2746
```bash
2847
git clone https://github.com/ava-labs/subnet-evm.git
48+
cd subnet-evm/contracts
49+
npm ci # Installs OpenZeppelin and other Node.js dependencies
50+
```
51+
52+
## Compiling Contracts
53+
54+
Contracts are compiled using `solc` directly, and Go bindings are generated using `abigen` from [libevm](https://github.com/ava-labs/libevm).
55+
56+
OpenZeppelin contracts are included as a git submodule at `contracts/lib/openzeppelin-contracts/` (pinned to v5.4.0).
57+
58+
From the repository root, run:
59+
60+
```bash
61+
./scripts/run_task.sh setup-contracts
62+
```
63+
64+
This will:
65+
1. Compile all Solidity contracts in `contracts/contracts/` to ABIs and bytecode
66+
2. Generate Go bindings in `contracts/bindings/`
67+
68+
The compilation artifacts (`.abi` and `.bin` files) are stored in `contracts/artifacts/` (gitignored).
69+
The generated Go bindings in `contracts/bindings/` are committed to the repository.
70+
71+
### Manual Compilation
72+
73+
To manually compile contracts and generate bindings:
74+
75+
```bash
2976
cd contracts
30-
npm install
77+
npm ci # Install dependencies if not already done
78+
go generate ./... # Compile contracts and generate bindings
3179
```
3280

81+
All compilation and code generation is configured in `contracts/contracts/compile.go` using `go:generate` directives. The directives execute in order:
82+
1. First, `solc` compiles `.sol` files to `.abi` and `.bin` files in `artifacts/`
83+
2. Then, `abigen` generates Go bindings from the artifacts to `bindings/*.go`
84+
3385
## Write Contracts
3486

3587
`AllowList.sol` is the base contract which provided AllowList precompile capabilities to inheriting contracts.

0 commit comments

Comments
 (0)