Programmatic Orders
Programmatic orders (sometimes called conditional orders) are programmable orders that execute automatically when predefined conditions are met. They are implemented by theComposableCoW contract and are a key component of the CoW Protocol’s programmatic order framework, enabling sophisticated trading strategies without manual intervention.
What are Programmatic Orders?
Programmatic 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, programmatic orders are monitored by the Watch Tower and only posted to the OrderBook when their conditions are satisfied.Programmatic orders are created through the
ComposableCoW contract, which
emits events that the Watch Tower monitors.Order Structure
Each programmatic order contains the following components:Order Parameters
TheConditionalOrderParams contain the core logic:
- handler: The smart contract address that implements the programmatic order logic
- salt: A unique identifier to distinguish orders with identical parameters
- staticInput: Immutable data passed to the handler for order evaluation
Merkle Proofs
Programmatic orders can be created individually or as part of a merkle tree:Merkle trees allow efficient batch creation of multiple programmatic orders in
a single transaction, reducing gas costs.
Order Lifecycle
Programmatic orders follow a specific lifecycle from creation to completion:-
Creation: An order is created via the
ComposableCoWcontract, which emits either aConditionalOrderCreatedevent (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 programmatic 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 programmatic order has a status:How They Work in ComposableCoW
TheComposableCoW contract provides the framework for creating and managing programmatic 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.