Order Management
The SDK provides methods to retrieve order details and cancel orders both off-chain and on-chain.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:
open- Order is active and waiting to be filledfulfilled- Order has been completely filledcancelled- Order has been cancelledexpired- Order has passed its expiration timepresignaturePending- Order is waiting for pre-signature (smart contract wallets)
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
1
Sign cancellation message
The SDK creates and signs a cancellation message using EIP-712
2
Send to order book
The signed cancellation is sent to the CoW Protocol order book API
3
Order removed
The order book marks the order as cancelled and stops including it in solutions
Soft cancel: 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.
When to Use Off-Chain Cancellation
- ✅ Regular orders that you want to cancel quickly
- ✅ When you need to update order parameters (cancel and recreate)
- ✅ When minimizing costs is important
- ❌ Not suitable if you need absolute guarantee the order won’t be filled
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:- Regular orders: ~45,000-60,000 gas
- ETH-Flow orders: ~50,000-70,000 gas
- Regular: ~6.00
- ETH-Flow: ~7.00
When to Use On-Chain Cancellation
- ✅ When you need absolute certainty the order won’t execute
- ✅ For high-value orders where security is paramount
- ✅ When you suspect your off-chain cancellation might not be respected
- ✅ For orders that others might try to fill maliciously
- ❌ Not cost-effective for small orders
Comparing Cancellation Methods
- Off-Chain
- On-Chain
Advantages:
- Free (no gas cost)
- Instant
- Simple to use
- Recommended for most cases
- “Soft” cancel only
- Requires order book cooperation
- Signature remains valid on-chain
- Regular order cancellations
- Cost-sensitive applications
- Quick order updates
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 validFor too 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