Creating Swap Orders
Swap orders are market orders that automatically fetch a quote and create an order in one step. The SDK handles all the complexity of parameters, calculations, and signing.Prerequisites
Before creating a swap order, make sure the following conditions are met:- Token approval: The sell token must be approved to the CoW Protocol Vault Relayer contract before the order can settle. Without approval, solvers cannot pull tokens from your wallet. See Token Approvals for full details.
- Sufficient balance: Your wallet must hold enough of the sell token to cover the trade amount.
- Buy order amounts: For buy orders, the actual sell amount will be higher than the raw exchange rate because it includes protocol fees and slippage tolerance. Always check the quote’s
amountsAndCoststo see the real cost.
Basic Flow
The basic trading flow consists of three steps:Using postSwapOrder
ThepostSwapOrder method combines all three steps into one convenient function.
Required Parameters
kind- The order kind (OrderKind.SELLorOrderKind.BUY)sellToken- The sell token addresssellTokenDecimals- The sell token decimalsbuyToken- The buy token addressbuyTokenDecimals- The buy token decimalsamount- The amount to sell/buy in atoms (smallest unit)
Example
Optional Parameters
You can customize your swap order with these optional parameters:| Parameter | Type | Default | Description |
|---|---|---|---|
env | Env | prod | The environment to use (prod or staging) |
partiallyFillable | boolean | false | Whether the order can be partially filled |
slippageBps | number | 50 | Slippage tolerance in basis points (BPS). One basis point = 0.01% |
receiver | string | order creator | The address that will receive the tokens |
validFor | number | 1800 (30 mins) | Order expiration time in seconds |
partnerFee | PartnerFee | - | Partner fee configuration (fee in BPS and recipient address) |
Example with Optional Parameters
Slippage is expressed in basis points (BPS). For example:
- 50 BPS = 0.5%
- 100 BPS = 1%
- 200 BPS = 2%
Advanced Settings
For more control over the order creation process, useSwapAdvancedSettings:
Quote Request Customization
Adding Hooks
You can add pre-hooks that execute before your order is settled:Two-Step Process: getQuote + postSwapOrderFromQuote
If you want to show the quote to users before creating an order, use the two-step approach:Quote Results
ThequoteResults object contains:
tradeParameters- Trade type, assets, amounts and optional parametersamountsAndCosts- Order sell/buy amounts including network costs, fees and slippageorderToSign- Order parameters ready for signingquoteResponse- Raw DTO from the Quote APIappDataInfo- Order’s metadataorderTypedData- EIP-712 typed data for signing
Order Kinds
The SDK supports two order kinds:- SELL Orders
- BUY Orders
Sell orders specify the exact amount you want to sell:You’ll receive at least the quoted buy amount (minus slippage).
After Order Creation
OncepostSwapOrder returns successfully, the order has been signed and submitted to the CoW Protocol order book. Here is what happens next:
- Solvers compete: The order enters the CoW Protocol auction where solvers compete to find the best execution path for your trade.
- Settlement: The winning solver settles the order on-chain, transferring tokens between parties.
- Surplus: If solvers find a better price than your limit, the surplus goes back to you.
Checking Order Status
Use the order UID returned bypostSwapOrder to check the current status:
| Status | Meaning |
|---|---|
open | Order is in the order book, waiting to be filled |
fulfilled | Order has been fully executed on-chain |
cancelled | Order was cancelled by the owner |
expired | Order passed its validTo timestamp without being filled |
presignaturePending | Order requires an on-chain pre-signature (smart contract flow) |
Polling for Completion
If you need to wait for the order to fill:Cancelling an Order
If you need to cancel a pending order, use off-chain cancellation:Off-chain cancellation asks solvers to stop filling the order. It takes effect almost immediately but is not enforced on-chain. For guaranteed cancellation of smart contract orders, see Order Management.
Pre-Flight Checklist
Before callingpostSwapOrder, verify:
- Sell token approved to the Vault Relayer contract
- Wallet has sufficient sell token balance
- Token addresses are correct for the target chain
- Amount is in the token’s smallest unit (atoms, e.g. wei for 18-decimal tokens)
slippageBpsis set appropriately (default 50 = 0.5%)
Troubleshooting
Common issues when creating swap orders:| Problem | Likely Cause | Fix |
|---|---|---|
InsufficientAllowance error | Sell token not approved to Vault Relayer | Call sdk.approveCowProtocol() before posting the order |
InsufficientBalance error | Wallet doesn’t hold enough sell token | Check balance before posting |
| Order created but never fills | Low liquidity or tight slippage tolerance | Increase slippageBps, try more common token pairs |
| Order expires immediately | validFor too short | Use at least 300 seconds (5 minutes) |
Quote fails with NoLiquidity | Token pair not supported or no available pools | Verify token addresses are correct, try larger amounts |
Next Steps
- Learn about Token Approvals before trading
- Explore Limit Orders for price-specific trades
- See Order Management to track and cancel orders