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.

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).

Next Steps