Custom Orders
Custom orders leverage theIConditionalOrder or IConditionalOrderGenerator interface to enable sophisticated trading strategies based on on-chain conditions. The framework recommends extending BaseConditionalOrder for most implementations, as it handles signature verification automatically.
Key Interfaces
- IConditionalOrder provides the foundational verification method
- IConditionalOrderGenerator extends this by adding order generation capabilities on-chain
verify function checks discrete order validity, while getTradeableOrder produces executable orders for the CoW Protocol API.
BaseConditionalOrder Advantages
This abstract contract streamlines development by automating:- Order hash verification
- ERC165 interface support
getTradeableOrder function, which must return a valid GPv2Order.Data struct.
Implementation Examples
TradeAboveThreshold
Executes when an owner’s token balance exceeds a specified threshold, selling the entire balance.PerpetualStableSwap
Automatically rebalances between two tokens based on relative holdings, applying a configurable spread.Error Communication
Specialized revert errors guide watchtowers on retry behavior:| Error | Behavior |
|---|---|
PollTryNextBlock | Temporary condition, retry soon |
PollTryAtBlock / PollTryAtEpoch | Order valid at specific time |
PollNever | Permanent invalidity, stop monitoring |
OrderNotValid | Generic failure condition |
Advanced Features
- Context-based cabinet storage for persistent values
offchainInputparameters for dynamic watchtower-provided data- Validity bucketing prevents hash collisions across sequential queries
Best Practices
- Maintain gas efficiency in
getTradeableOrder - Ensure deterministic logic within validity buckets
- Employ clear error messaging
- Implement proper ERC165 interface support
Security Considerations
- Validate external calls
- Manage arithmetic carefully — unhandled overflows will cause the Watch Tower to permanently drop your order
- Address reentrancy risks
Watch Tower Integration
Custom handlers are automatically discovered and executed by the Watch Tower. When a programmatic order is created viaComposableCoW, the Watch Tower:
- Detects the
ConditionalOrderCreatedevent on-chain - Polls your handler’s
getTradeableOrder()on every block (or per your error hints) - Submits the resulting discrete order to the CoW Protocol OrderBook when conditions are met
ComposableCoW.create() or ComposableCoW.createWithContext().
Test your handler on Sepolia first. The Watch Tower supports Sepolia and will poll your handler the same way it does on mainnet. See Watch Tower Configuration for Sepolia setup.