Skip to main content

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 amountsAndCosts to see the real cost.
Use the check-and-approve pattern before posting an order:
If you skip the approval step, the order will be created successfully but solvers will not be able to settle it. The order will eventually expire unfilled.

Basic Flow

The basic trading flow consists of three steps:
1

Get a quote (price) for a trade

The SDK fetches the current market price from the CoW Protocol API
2

Sign the order

Sign the order using EIP-712 signature
3

Post the order to the order-book

Submit the signed order to the CoW Protocol order book

Using postSwapOrder

The postSwapOrder method combines all three steps into one convenient function.

Required Parameters

  • kind - The order kind (OrderKind.SELL or OrderKind.BUY)
  • sellToken - The sell token address
  • sellTokenDecimals - The sell token decimals
  • buyToken - The buy token address
  • buyTokenDecimals - The buy token decimals
  • amount - The amount to sell/buy in atoms (smallest unit)

Example

Optional Parameters

You can customize your swap order with these optional parameters:

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, use SwapAdvancedSettings:

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

The quoteResults object contains:
  • tradeParameters - Trade type, assets, amounts and optional parameters
  • amountsAndCosts - Order sell/buy amounts including network costs, fees and slippage
  • orderToSign - Order parameters ready for signing
  • quoteResponse - Raw DTO from the Quote API
  • appDataInfo - Order’s metadata
  • orderTypedData - EIP-712 typed data for signing
Quotes are time-sensitive and typically valid for 30 minutes. Make sure to post the order within the validity period.

Order Kinds

The SDK supports two order kinds:
Sell orders specify the exact amount you want to sell:
You’ll receive at least the quoted buy amount (minus slippage).

After Order Creation

Once postSwapOrder returns successfully, the order has been signed and submitted to the CoW Protocol order book. Here is what happens next:
  1. Solvers compete: The order enters the CoW Protocol auction where solvers compete to find the best execution path for your trade.
  2. Settlement: The winning solver settles the order on-chain, transferring tokens between parties.
  3. Surplus: If solvers find a better price than your limit, the surplus goes back to you.

Checking Order Status

Use the order UID returned by postSwapOrder to check the current status:
Order status values:

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 calling postSwapOrder, 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)
  • slippageBps is set appropriately (default 50 = 0.5%)

Troubleshooting

Common issues when creating swap orders:

Next Steps

Last modified on March 17, 2026