Overview
TheTradingSdk class provides a comprehensive interface for interacting with CoW Protocol’s trading functionality. It simplifies order creation, quote fetching, order management, and token approvals.
Installation
Constructor
Partial<TraderParameters>
Default trader parameters to use for all operations
Partial<TradingSdkOptions>
SDK configuration options
AbstractProviderAdapter
Provider adapter (ViemAdapter, EthersV5Adapter, or EthersV6Adapter)
Basic Setup
Methods
postSwapOrder
Create a market order by fetching a quote and posting it in a single call.TradeParameters
required
Trade parameters
SwapAdvancedSettings
Advanced trading settings
string
Unique order identifier (UID)
Example
getQuote
Get a quote for a trade and optionally post it later.QuoteResults
Quote information including amounts, costs, and order data
() => Promise<OrderPostingResult>
Function to post the order using the fetched quote
Example
getQuoteOnly
Get a quote without requiring a wallet connection. Useful for displaying quotes before users connect.string
required
Address to quote for (doesn’t need to be connected)
Example
postLimitOrder
Create a limit order with specific buy and sell amounts.LimitTradeParameters
required
Limit order parameters
Example
postSellNativeCurrencyOrder
Create an order to sell native currency (ETH, xDAI, etc.) using EthFlow.Use the special native currency address
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE as the sellToken.Example
getOrder
Retrieve details of an existing order.string
required
Unique order identifier
EnrichedOrder
Complete order information including status, amounts, and execution details
Example
offChainCancelOrder
Cancel an order off-chain (soft cancel - fast and free).string
required
Order UID to cancel
boolean
True if cancellation was successful
Example
onChainCancelOrder
Cancel an order on-chain (hard cancel - requires gas but guaranteed).string
required
Order UID to cancel
string
Transaction hash of the cancellation
Example
getCowProtocolAllowance
Check current token allowance for CoW Protocol.string
required
ERC-20 token contract address
string
required
Token owner address
bigint
Current allowance amount
Example
approveCowProtocol
Approve CoW Protocol to spend tokens.string
required
ERC-20 token contract address
bigint
required
Amount to approve
string
Transaction hash of approval
Example
getPreSignTransaction
Get pre-sign transaction for smart contract wallets.string
required
Order UID to pre-sign
string
required
Smart contract wallet address
{ to: string, data: string }
Transaction to execute for pre-signing
Example
setTraderParams
Update default trader parameters.Partial<TraderParameters>
required
New trader parameters to merge with existing ones
Example
Smart Contract Wallet Support
For smart contract wallets (like Safe), use thePRESIGN signing scheme: