Overview
The settlement module provides tools for encoding trades, interactions, and prices into the format required by the CoW Protocol settlement contract. TheSettlementEncoder class is the primary interface for building settlement calldata.
SettlementEncoder Class
TheSettlementEncoder class manages the encoding of orders, interactions, and prices into settlement calldata:
Constructor
The EIP-712 domain for signing orders. Used to ensure orders are valid for the specific settlement contract and chain.
Encoding Trades
encodeTrade
Adds a signed order to the settlement:The order to encode.
The order signature (ECDSA, EIP-1271, or PreSign).
Trade execution details. Required for partially fillable orders.
signEncodeTrade
Signs an order and encodes it in one step:Trade Interface
The encoded trade format used by the settlement contract:TradeExecution
The amount executed in this trade:
- Partially fillable sell orders: Amount of sell tokens to trade
- Partially fillable buy orders: Amount of buy tokens to trade
- Fill-or-kill orders: Ignored (set to 0)
Flag Encoding
Trade Flags
Trade flags encode order and signature information into a single bitfield:encodeTradeFlags
decodeTradeFlags
Token Registry
Trades reference tokens by index rather than address to save gas. TheTokenRegistry class manages this mapping:
Prices
Settlements require clearing prices for all tokens:clearingPrices
Converts a price map to an array aligned with the token registry:Encoded Settlement
EncodedSettlement Type
encodedSettlement
Generates the final encoded settlement:InteractionStage Enum
Defines when interactions execute during settlement:Executed before any trading. Use for:
- EIP-2612 permit calls
- Setting up allowances
- Pre-trade state changes
Executed during trading (after sell transfers, before buy transfers). Use for:
- AMM swaps
- Liquidity interactions
- Complex trade routing
Executed after all trading completes. Use for:
- Post-trade cleanup
- Fee distributions
- Order refunds
Complete Example
Decoding
decodeOrder
Decodes a trade back into an order:Static Methods
encodedSetup
Creates a settlement with only interactions (no trades):Best Practices
Token Order
Tokens are indexed in the order they’re first encountered. Be consistent with token ordering for predictable indices.
Price Precision
Use high precision for clearing prices to minimize rounding errors in settlements.
Partial Fills
Always specify
executedAmount for partially fillable orders to avoid errors.Interaction Stages
Choose the correct interaction stage based on when the interaction needs to execute relative to token transfers.
Next Steps
Interactions
Learn how to create and encode interactions
Orders
Review order creation and management
