Order Polling
Order polling serves as the fundamental mechanism enabling the Watch Tower to assess conditional orders and determine their readiness for CoW Protocol OrderBook submission. This continuous process evaluates every active conditional order with each new blockchain block.Polling overview
The Watch Tower evaluates conditional orders by invoking their handler contracts to verify whether order conditions have been satisfied. Upon successful condition validation, the handler delivers a discrete order suitable for OrderBook posting.Polling executes on every block for active orders, unless prior poll results indicate the order should be evaluated at a designated future timestamp or block.
How polling works
The polling mechanism consists of four primary steps:1. Order iteration
For each block, the Watch Tower processes all registered owners and their associated conditional orders:2. Poll scheduling
Before polling, the Watch Tower verifies whether the order qualifies for evaluation based on previous poll outcomes: Skip until epoch:3. Handler invocation
The Watch Tower engages the conditional order handler to assess condition satisfaction:4. Result handling
The poll result communicates the appropriate action to perform:Poll parameters
During order polling, the Watch Tower furnishes comprehensive operational context:The Watch Tower utilizes the processing block’s timestamp and number, not the latest block, to guarantee consistent indexing.
Poll results and discrete orders
Successful polling yields a discrete order prepared for submission:Order validation
Prior to submission, the Watch Tower authenticates the order:- Valid token addresses
- Non-zero amounts
- Reasonable expiration times
- Supported order types
Order UID calculation
Each discrete order receives a distinctive identifier:Submitting to the OrderBook
Upon readiness, the order undergoes posting to the CoW Protocol OrderBook API:Duplicate detection
The Watch Tower maintains records of submitted orders to circumvent redundant submissions:Integration with the SDK
The Watch Tower leverages the CoW Protocol SDK for polling functionality:Handler registry
The SDK sustains a registry of recognized order handlers:- TWAP: Time-weighted average price orders
- Limit orders: Orders at specific price points
- Stop-loss: Orders that trigger at price thresholds
- Custom handlers: User-defined order logic
If a handler remains unrecognized by the SDK, the Watch Tower defaults to legacy polling using direct contract calls.
Legacy polling
For unsupported order types, the Watch Tower engages direct contract calls:- Implements multicall for gas efficiency
- Gracefully manages contract reverts
- Retrieves custom error hints from the handler
Error handling
The Watch Tower addresses multiple error scenarios throughout polling:API errors
When OrderBook submission encounters difficulties: Temporary errors (retry next block):- Quote not found
- Invalid quote
- Invalid EIP-1271 signature
- Insufficient allowance (retry in 10 minutes)
- Insufficient balance (retry in 10 minutes)
- Too many limit orders (retry in 1 hour)
- Zero amount
- Unsupported tokens
- Invalid order parameters
On-chain errors
Contract reverts may communicate hints:PollTryAtEpoch(uint256 epoch): Try again at specific timePollTryAtBlock(uint256 blockNumber): Try again at specific blockPollNever(string reason): Stop trying
Performance optimization
Batched persistence
The Watch Tower persists the registry at intervals during polling:Filter policies
Orders may undergo filtering prior to polling to diminish computational load:Filter policies enable operators to specify which orders receive monitoring, supporting RPC cost management and concentration on particular order categories.
Metrics and monitoring
The Watch Tower compiles comprehensive polling metrics:pollingRunsTotal: Total number of poll attemptspollingOnChainDurationSeconds: Time spent in on-chain callspollingOnChainChecksTotal: Count of on-chain checkspollingUnexpectedErrorsTotal: Count of unexpected errorsorderBookDiscreteOrdersTotal: Orders successfully postedorderBookErrorsTotal: API submission errors by type