Skip to main content

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

Use getOrder to fetch complete information about an order:

Method Signature

Parameters

  • orderUid - The unique identifier of the order
  • chainId - (Optional) Chain ID, uses trader params if not provided

Returns

  • Promise<EnrichedOrder> - Full order details including status, amounts, and metadata

Example

Order Status Values

The status field can be one of:
  • open - Order is active and waiting to be filled
  • fulfilled - Order has been completely filled
  • cancelled - Order has been cancelled
  • expired - Order has passed its expiration time
  • presignaturePending - 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 cancel
  • chainId - (Optional) Chain ID, uses trader params if not provided
  • signer - (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 cancel
  • chainId - (Optional) Chain ID, uses trader params if not provided
  • signer - (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
At 50 gwei gas price and $2000 ETH:
  • Regular: ~4.50−4.50-6.00
  • ETH-Flow: ~5.00−5.00-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

Advantages:
  • Free (no gas cost)
  • Instant
  • Simple to use
  • Recommended for most cases
Disadvantages:
  • “Soft” cancel only
  • Requires order book cooperation
  • Signature remains valid on-chain
Best for:
  • 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

  1. Always store order IDs: Save order UIDs for future reference
  2. Monitor important orders: Track order status for large trades
  3. Try off-chain first: Start with off-chain cancellation to save gas
  4. Handle errors gracefully: Orders might already be filled when you try to cancel
  5. Set reasonable expiration: Don’t set validFor too long if you might want to cancel
  6. Check status before operations: Verify order state before cancelling
  7. 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