Overview
CoW Protocol’s smart contract architecture is built around three core contracts that work together to enable batch auction settlements with MEV protection. The system leverages Balancer V2’s Vault for secure fund management while maintaining a permissioned solver network.Core Components
GPv2Settlement
The main contract orchestrating trade execution, order validation, and settlement logic
GPv2VaultRelayer
Intermediary contract managing token transfers between users and the Balancer Vault
GPv2AllowListAuthentication
Access control system managing the permissioned solver network
Architecture Diagram
Contract Relationships
GPv2Settlement (src/contracts/GPv2Settlement.sol:21)
The settlement contract is the protocol’s entry point, responsible for:- Order Validation: Verifies signatures, expiry times, and limit prices
- Trade Execution: Computes clearing prices and executes batched trades
- Interaction Hooks: Allows arbitrary contract calls before, during, and after settlements
- Reentrancy Protection: Inherits from
ReentrancyGuardto prevent attacks
src/contracts/GPv2Settlement.sol
GPv2VaultRelayer (src/contracts/GPv2VaultRelayer.sol:11)
The vault relayer acts as a trusted intermediary:src/contracts/GPv2VaultRelayer.sol
The VaultRelayer is created during
GPv2Settlement deployment and is the only address authorized to pull funds from users’ Balancer Vault allowances.GPv2AllowListAuthentication (src/contracts/GPv2AllowListAuthentication.sol:11)
Manages solver permissions through an allowlist:src/contracts/GPv2AllowListAuthentication.sol
Token Flow
The architecture ensures secure token handling through a multi-step process:1. User Approval
Users approve the Balancer Vault (not the settlement contract) to spend their tokens.2. Inbound Transfers
3. Interactions (Optional)
4. Outbound Transfers
Security Features
Reentrancy Protection
Reentrancy Protection
The settlement contract uses the
nonReentrant modifier on all entry points to prevent reentrancy attacks during token transfers and interactions.Solver Authorization
Solver Authorization
Interaction Restrictions
Interaction Restrictions
The VaultRelayer cannot be called as an interaction target to prevent attacks on user funds:
Creator-Only Access
Creator-Only Access
The VaultRelayer enforces that only its creator (the Settlement contract) can invoke transfers:
Immutable References
Critical contract addresses are stored as immutable variables set during deployment:Immutable variables are embedded in the contract bytecode at deployment, making them gas-efficient and impossible to modify.
Design Principles
- Separation of Concerns: Each contract handles a specific responsibility
- Minimal Trust: Users only approve the Balancer Vault, a battle-tested DeFi primitive
- Gas Efficiency: Batch settlements amortize gas costs across multiple orders
- Extensibility: Interaction hooks enable future protocol upgrades without redeployment
- MEV Protection: Uniform clearing prices prevent sandwich attacks within batches
