Order Management
Track, monitor, and cancel orders using the CoW Protocol SDK.Overview
After creating an order, you can:- Retrieve order details - Get full information about an order
- Cancel off-chain - Free and fast soft cancellation
- Cancel on-chain - Gas-required hard cancellation
All order management methods require an order UID (unique identifier) returned when creating an order.
Retrieving Order Details
UsegetOrder to fetch complete information about an order:
Method Signature
Parameters
orderUid- The unique identifier of the orderchainId- (Optional) Chain ID, uses trader params if not provided
Returns
Promise<EnrichedOrder>- Full order details including status, amounts, and metadata
Example
Order Status Values
Thestatus field can be one of:
EnrichedOrder Properties
Tracking Order Progress
Off-Chain Order Cancellation
Off-chain cancellation is the recommended way to cancel orders. It’s free, fast, and doesn’t require gas.Method Signature
Parameters
orderUid- The unique identifier of the order to cancelchainId- (Optional) Chain ID, uses trader params if not providedsigner- (Optional) Custom signer, uses trader params signer if not provided
Returns
Promise<boolean>- True if cancellation was successful
Example
How It Works
- Sign cancellation message - The SDK creates and signs a cancellation message using EIP-712
- Send to order book - The signed cancellation is sent to the CoW Protocol order book API
- Order removed - The order book marks the order as cancelled and stops including it in solutions
Off-chain cancellation is a “soft” cancel. While the order book will stop trying to fill the order, the order signature remains valid on-chain. For complete security, use on-chain cancellation.
On-Chain Order Cancellation
On-chain cancellation provides a hard guarantee that the order cannot be filled. It requires gas but is the most secure method.Method Signature
Parameters
orderUid- The unique identifier of the order to cancelchainId- (Optional) Chain ID, uses trader params if not providedsigner- (Optional) Custom signer, uses trader params signer if not provided
Returns
Promise<string>- Transaction hash of the cancellation
Example
How It Works
The SDK automatically detects the order type and uses the appropriate contract:- Regular orders: Calls
invalidateOrder()on the Settlement contract - ETH-Flow orders: Calls
invalidateOrder()on the EthFlow contract
Gas Costs
On-chain cancellation costs gas:Comparing Cancellation Methods
Complete Example: Order Lifecycle
Here’s a complete example showing order creation, tracking, and cancellation:Batch Order Monitoring
Monitor multiple orders efficiently:Best Practices
- Always store order IDs: Save order UIDs for future reference
- Monitor important orders: Track order status for large trades
- Try off-chain first: Start with off-chain cancellation to save gas
- Handle errors gracefully: Orders might already be filled when you try to cancel
- Set reasonable expiration: Don’t set
validFortoo long if you might want to cancel - Check status before operations: Verify order state before cancelling
- Use appropriate cancellation: Choose based on order value and security needs
Common Issues and Solutions
Order Not Found
Problem:getOrder throws “Order not found” error.
Solution:
- Verify the order UID is correct
- Ensure the order has been created (wait a few seconds after posting)
- Check you’re using the correct chain ID
Cancellation Fails
Problem: Off-chain cancellation returns false or throws error. Solution:- Check if the order is already filled or expired
- Verify you’re using the same signer that created the order
- Try on-chain cancellation as a fallback
Order Still Executes After Cancellation
Problem: Order filled despite cancellation. Solution:- This can happen with off-chain cancellation if there’s a race condition
- Use on-chain cancellation for critical orders
- Always wait for confirmation before assuming cancellation succeeded
Next Steps
- Learn about Creating Swap Orders to create orders
- See Creating Limit Orders for price-specific orders
- Explore Token Approvals for managing approvals