Skip to content

Refactor contracts crate out of the main folder for faster compilation 🚀#4217

Merged
jmg-duarte merged 11 commits intomainfrom
jmgd/contracts
Apr 8, 2026
Merged

Refactor contracts crate out of the main folder for faster compilation 🚀#4217
jmg-duarte merged 11 commits intomainfrom
jmgd/contracts

Conversation

@jmg-duarte
Copy link
Copy Markdown
Contributor

@jmg-duarte jmg-duarte commented Feb 27, 2026

Description

Replace the crates/contracts build.rs-based compile-time code generation with a standalone code-generation tool that produces pre-generated per-contract crates. This eliminates the slow build.rs step from the main workspace build and makes contract bindings explicit, version-controllable, and independently compilable.

Changes

  • Move crates/contracts to top-level contracts/ directory as a standalone binary
  • Replace build.rs code generation with pre-generated Alloy contract binding crates under contracts/generated/
  • Add contracts-facade crate that re-exports all generated bindings for easy consumption
  • Update all workspace crates to import from the new generated crates
  • Add missing contract artifacts (Balancer factory V3-V6, weighted pool factory V4, NoProtocolFee LBP factory, DEX router variants)

How to test

  1. cargo check --workspace --all-targets — workspace compiles against the new generated bindings
  2. cargo nextest run — unit tests pass
  3. cd contracts && cargo run -- --help — codegen tool runs independently
  4. Verify contracts/generated/ output matches checked-in code by re-running the codegen

Charts

Problematic time blocker:
Screenshot 2026-04-08 at 11 32 04

CPU Graph (notice the 60s):
Screenshot 2026-04-08 at 11 32 16

No more time blocker (doesn't fully fit vertically in my screen):
Screenshot 2026-04-08 at 11 38 15

New CPU Graph (notice the 45s and much better (higher) CPU usage):
Screenshot 2026-04-08 at 11 35 33

github-merge-queue bot pushed a commit that referenced this pull request Mar 2, 2026
# Description
Based on @MartinquaXD's comment on Slack, I figured that there would be
places where we can import the specific alloy crate, unlocking earlier
and more parallel compilation. These changes alone seem to save ~5s on a
clean build in my laptop; going down from ~1m to 55s.

There's more gains to take from this, but for that we need to separate
shared further and then split those dependencies.
#4217 would also help a
bunch.

# Changes

* app-data
  * replace alloy with alloy-primitives
* since alloy uses tiny-keccak, replace the tiny-keccak with alloy's
keccak
* chain
  * manually implement thiserror (less 1 dep)
  * replace alloy with alloy-primitives
* model
* replace alloy with alloy-primives, alloy-signer, alloy-signer-local,
alloy-sol-types
* number
  * replace alloy with alloy-primitives
* order-validation
  * replace alloy with alloy-primitives and alloy-contract
  * remove thiserror wrapper (less 1 dep)
* serde-ext
  * replace alloy with alloy-primitives
* testlib
  * replace alloy with alloy-primitives
* winner-selection
  * replace alloy with alloy-primitives


## How to test
Compiler + existing tests
@jmg-duarte jmg-duarte force-pushed the jmgd/contracts branch 2 times, most recently from cb3108b to 612834d Compare March 2, 2026 20:31
MartinquaXD pushed a commit that referenced this pull request Mar 4, 2026
# Description
Based on @MartinquaXD's comment on Slack, I figured that there would be
places where we can import the specific alloy crate, unlocking earlier
and more parallel compilation. These changes alone seem to save ~5s on a
clean build in my laptop; going down from ~1m to 55s.

There's more gains to take from this, but for that we need to separate
shared further and then split those dependencies.
#4217 would also help a
bunch.

# Changes

* app-data
  * replace alloy with alloy-primitives
* since alloy uses tiny-keccak, replace the tiny-keccak with alloy's
keccak
* chain
  * manually implement thiserror (less 1 dep)
  * replace alloy with alloy-primitives
* model
* replace alloy with alloy-primives, alloy-signer, alloy-signer-local,
alloy-sol-types
* number
  * replace alloy with alloy-primitives
* order-validation
  * replace alloy with alloy-primitives and alloy-contract
  * remove thiserror wrapper (less 1 dep)
* serde-ext
  * replace alloy with alloy-primitives
* testlib
  * replace alloy with alloy-primitives
* winner-selection
  * replace alloy with alloy-primitives


## How to test
Compiler + existing tests
@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed.

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed.

@jmg-duarte jmg-duarte removed the stale label Mar 24, 2026
@jmg-duarte jmg-duarte force-pushed the jmgd/contracts branch 2 times, most recently from f61837a to 76644af Compare March 24, 2026 17:28
Move `crates/contracts` to a top-level `contracts/` directory as a
standalone binary that generates Alloy contract bindings from ABI
artifacts. Replace the build.rs-based compile-time code generation
with pre-generated per-contract crates under `contracts/generated/`.

- Add `contracts-facade` crate that re-exports all generated bindings
- Update all workspace crates to import from the new generated crates
- Add missing contract artifacts (Balancer factory variants, DEX
  router variants like Honeyswap, Pancake, SushiSwap, Swapr)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@jmg-duarte jmg-duarte marked this pull request as ready for review March 24, 2026 18:07
@jmg-duarte jmg-duarte requested a review from a team as a code owner March 24, 2026 18:07
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new contracts sub-project to generate Rust bindings for various Solidity smart contracts within the CoW Protocol ecosystem. This involves adding a contracts-generate tool, new Cargo.toml and Cargo.lock files for the sub-project, and numerous JSON ABI files. The generated bindings are organized into individual crates and re-exported through a facade crate. The root Cargo.toml and Cargo.lock are updated to reflect this new structure, and a setup command is added to the Justfile to automate the vending and generation process. A review comment noted that several BalancerV2ComposableStablePoolFactory ABI files (V3, V4, V5, V6) are identical, suggesting consolidation into a single file to reduce maintenance overhead and prevent inconsistencies.

Copy link
Copy Markdown
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed.

@github-actions github-actions bot added the stale label Apr 2, 2026
@jmg-duarte jmg-duarte removed the stale label Apr 6, 2026
@jmg-duarte
Copy link
Copy Markdown
Contributor Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new contracts directory to manage and generate Rust bindings for various smart contracts using Alloy. It includes a contracts-generate tool, updates the workspace configuration to exclude the new directory, and adds generated code for multiple contracts. A high-severity issue was identified regarding incorrect deployment block numbers for Gnosis and Sepolia chains in the generated code for BalancerV2NoProtocolFeeLiquidityBootstrappingPoolFactory.

Copy link
Copy Markdown
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement!
Given that the main motivation stated in the PR description is compile time improvements could you please add some measurements?
I did a quick test on my machine and compile time dropped from 94s to 67s.

@jmg-duarte jmg-duarte changed the title Refactor contracts crate out of the repo for faster compilation 🚀 Refactor contracts crate out of the main folder for faster compilation 🚀 Apr 8, 2026
@jmg-duarte jmg-duarte added this pull request to the merge queue Apr 8, 2026
Merged via the queue into main with commit cfbec98 Apr 8, 2026
19 checks passed
@jmg-duarte jmg-duarte deleted the jmgd/contracts branch April 8, 2026 12:30
@github-actions github-actions bot locked and limited conversation to collaborators Apr 8, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants