Conditional Orders
Conditional orders are programmable orders that execute automatically when predefined conditions are met. They are a key component of the CoW Protocol’s programmatic order framework, enabling sophisticated trading strategies without manual intervention.What are Conditional Orders?
Conditional orders allow traders to define complex order logic that executes based on specific blockchain conditions such as price thresholds, time intervals, or custom smart contract logic. Unlike traditional orders that are immediately placed, conditional orders are monitored by the Watch Tower and only posted to the OrderBook when their conditions are satisfied.Conditional orders are created through the ComposableCoW contract, which emits events that the Watch Tower monitors.
Order Structure
Each conditional order contains the following components:Order Parameters
TheConditionalOrderParams contain the core logic:
- handler: The smart contract address that implements the conditional order logic
- salt: A unique identifier to distinguish orders with identical parameters
- staticInput: Immutable data passed to the handler for order evaluation
Merkle Proofs
Conditional orders can be created individually or as part of a merkle tree:Merkle trees allow efficient batch creation of multiple conditional orders in a single transaction, reducing gas costs.
Order Lifecycle
Conditional orders follow a specific lifecycle from creation to completion:-
Creation: An order is created via the ComposableCoW contract, which emits either a
ConditionalOrderCreatedevent (single order) orMerkleRootSetevent (batch orders) - Registration: The Watch Tower detects the event and adds the order to its registry, tracking it by owner
- Polling: On each block, the Watch Tower polls the conditional order to check if conditions are met
- Execution: When conditions are satisfied, the Watch Tower posts the discrete order to the CoW Protocol OrderBook API
- Tracking: The order is tracked with status updates (SUBMITTED or FILLED)
- Cleanup: Expired or cancelled orders are removed from the registry
Order Status
Each discrete order generated from a conditional order has a status:How They Work in ComposableCoW
The ComposableCoW contract provides the framework for creating and managing conditional orders:- Handler registration: Custom order types register handler contracts that implement specific order logic
- Event emission: The contract emits events when orders are created or modified
- Order validation: Handlers validate whether order conditions are met when polled
- Signature generation: Valid orders return order details and signatures for submission
The Watch Tower calls the
getTradeableOrderWithSignature function on the ComposableCoW contract to check if an order should be placed.