Overview
Settlement is the core mechanism by which CoW Protocol executes trades. Unlike traditional AMMs that process orders sequentially, CoW Protocol batches multiple orders together and settles them atomically at uniform clearing prices, providing MEV protection and optimal execution.The Settle Function
Thesettle() function is the primary entry point for executing batches of trades:
src/contracts/GPv2Settlement.sol
Settlement Execution Flow
Clearing Prices
CoW Protocol settles all trades in a batch at uniform clearing prices. This is fundamentally different from AMMs:Traditional AMM
Each trade affects price sequentially, creating MEV opportunities and worse execution for later trades
CoW Protocol
All trades execute at the same price vector, eliminating intra-batch MEV and ensuring fair execution
Price Vector Format
Prices are passed as parallel arrays:Trade Execution
Computing Executions (src/contracts/GPv2Settlement.sol:290)
The contract validates each trade and computes exact execution amounts:src/contracts/GPv2Settlement.sol
Price Verification
The protocol ensures users get at least their limit price using the equation:This formula comes from the basic economic principle that value exchanged must be equal:
amount_x × price_x = amount_y × price_yInteraction Hooks
Interactions are arbitrary smart contract calls that extend settlement capabilities:Three Execution Phases
Pre-Interactions (interactions[0])
Pre-Interactions (interactions[0])
Execute before pulling tokens from users. Common uses:
- Unwrapping tokens (e.g., stETH → wstETH)
- Setting up flash loans
- Pre-funding the settlement contract
Intra-Interactions (interactions[1])
Intra-Interactions (interactions[1])
Execute after pulling user tokens but before sending tokens to users. Common uses:
- Trading on external DEXs (Uniswap, Curve, etc.)
- Arbitrage to balance the settlement
- Liquidity provision
Post-Interactions (interactions[2])
Post-Interactions (interactions[2])
Execute after all tokens have been distributed. Common uses:
- Returning dust amounts
- Claiming rewards
- Cleanup operations
Interaction Structure
src/contracts/libraries/GPv2Interaction.sol
Security Restrictions
The VaultRelayer cannot be targeted by interactions (src/contracts/GPv2Settlement.sol:454):Events
The settlement process emits detailed events for monitoring:Trade Event
src/contracts/GPv2Settlement.sol
Interaction Event
src/contracts/GPv2Settlement.sol
Settlement Event
src/contracts/GPv2Settlement.sol
Partial Fills
Orders can be marked as partially fillable, allowing execution across multiple settlements:src/contracts/GPv2Settlement.sol
Partial fills enable better price discovery by allowing large orders to be filled gradually as liquidity becomes available.
Filled Amount Tracking
The contract tracks how much of each order has been filled:src/contracts/GPv2Settlement.sol
sellAmount executed. For buy orders, it tracks the cumulative buyAmount received.
Preventing Overfills
src/contracts/GPv2Settlement.sol
Direct Balancer Integration
Theswap() function allows direct trading against Balancer V2 pools:
src/contracts/GPv2Settlement.sol
This is a gas-optimized path for orders that can be fully filled against Balancer liquidity without needing a complex batch settlement.
Gas Refunds
To incentivize cleaning up expired order storage, the contract provides refund mechanisms:src/contracts/GPv2Settlement.sol
These functions can only be called as interactions during settlement, ensuring they’re batched with other operations for gas efficiency.
Invariants
The settlement contract enforces critical invariants:- All orders are valid and signed - Verified during order recovery
- Accounts have sufficient balance and approval - Enforced by the Vault
- Limit prices are respected - Checked using clearing prices
- Orders don’t get overfilled - Tracked via
filledAmountmapping - Settlements are atomic - All trades execute or the entire transaction reverts
- No reentrancy - Protected by
nonReentrantmodifier
