System Overview
ComposableCoW is built on a modular architecture that separates order authorization, validation, and execution. The system consists of several key components that work together to enable automated conditional trading on CoW Protocol.Core Components
ComposableCoW Contract
TheComposableCoW contract is the central coordinator of the system. It implements the ISafeSignatureVerifier interface and manages order authorization and validation.
Source: src/ComposableCoW.sol:23
Key Responsibilities
Order Authorization
Order Authorization
Signature Verification
Signature Verification
ComposableCoW validates orders through the EIP-1271 signature verification flow:
Order Generation
Order Generation
For watchtowers and integrators, ComposableCoW provides a view function to generate tradeable orders:
Cabinet Storage
Cabinet Storage
ComposableCoW includes an on-chain storage system called “cabinet” for storing context values:This enables orders to reference dynamic values like creation timestamps:
ExtensibleFallbackHandler
TheExtensibleFallbackHandler is a Safe fallback handler that enables EIP-1271 signature verification with domain-specific verifiers.
Deployment: 0x2f55e8b20D0B9FEFA187AA7d00B6Cbe563605bF5 (all networks)
How It Works
isValidSignature() on the Safe:
- The call is forwarded to
ExtensibleFallbackHandler - The handler checks which verifier is registered for the CoW Protocol domain
- It delegates to
ComposableCoW.isValidSafeSignature() - ComposableCoW validates the conditional order and returns the magic value
Order Handlers
Order handlers implement the conditional logic for different order types. All handlers implement theIConditionalOrder interface.
Source: src/interfaces/IConditionalOrder.sol
Handler Interface
Order Generator Interface
Handlers that can generate orders implementIConditionalOrderGenerator:
BaseConditionalOrder
Most handlers extendBaseConditionalOrder which provides default verification logic:
Source: src/BaseConditionalOrder.sol
TWAP Handler
The TWAP handler splits an order into time-based parts. Source:src/types/twap/TWAP.sol
Deployment: 0x6cF1e9cA41f7611dEf408122793c358a3d11E5a5
TWAP Order Logic
Source:src/types/twap/libraries/TWAPOrder.sol
Swap Guards
Swap guards provide additional validation logic for orders. Source:src/interfaces/ISwapGuard.sol
Setting a Swap Guard
- Restrict trading to specific token pairs
- Enforce maximum slippage limits
- Require minimum time between trades
- Implement multi-signature approval for large trades
Data Flow
Order Creation Flow
Order Execution Flow
Authorization Modes
Single Orders
Best for:- Small number of conditional orders (< 10)
- Frequently updated orders
- Orders that need individual management
Merkle Trees
Best for:- Large number of conditional orders (> 10)
- Static order sets
- Gas-efficient batch operations
Error Handling
Handlers use specific error types to communicate with watchtowers:Security Considerations
Authorization Verification
ComposableCoW verifies authorization before calling handlers:Handler Validation
Handlers must validate all inputs and ensure generated orders are correct:- Input Validation: Check all parameters are within acceptable ranges
- Hash Verification: Ensure generated order hash matches expected hash
- Reentrancy Protection: Use view functions only, no state changes
- Oracle Validation: Verify oracle data is fresh and valid
Safe Configuration
Proper Safe setup is critical:- Fallback Handler: Must be
ExtensibleFallbackHandler - Domain Verifier: Must be set to
ComposableCoWfor CoW Protocol domain - Token Approvals: Only approve necessary amounts to
GPv2VaultRelayer
Gas Optimization
Stateless Design
Conditional orders are stateless - all validation happens in view functions. This means:- No gas cost for unsuccessful order checks
- Only pay gas when orders actually execute
- Watchtowers can poll frequently without cost to users
Merkle Trees
For many orders, Merkle trees provide significant gas savings:- Single
setRoot()call authorizes unlimited orders - Update root to add/remove multiple orders atomically
- Proof verification is O(log n) complexity
Cabinet Storage
The cabinet storage system is optimized for common patterns:- Default slot (bytes32(0)) saves calldata for Merkle roots
- Values stored only when needed, reducing unnecessary storage costs
Next Steps
Order Types
Explore all available conditional order types
Custom Handlers
Learn how to build your own conditional order handlers