Core Libraries
CoW Protocol employs specialized libraries designed for gas efficiency and security, using low-level optimizations where appropriate.GPv2Order
Defines the complete order structure and provides utilities for order management.Data Structure
Key Functions
hash(Data memory order)- Computes EIP-712 digest for signingactualReceiver(Data memory order)- Resolves recipient address (handles zero-address as owner)packOrderUidParams()- Encodes 56-byte UID (32-byte digest + 20-byte owner + 4-byte timestamp)extractOrderUidParams()- Decodes UID components
Pre-computed Constants
GPv2Trade
Handles compact settlement representation through a flags field that encodes order parameters into a singleuint256 value.
Flag Encoding
The default value
0x00 represents the most common case: a fill-or-kill sell order by an externally owned account using EIP-712 signatures.
Key Functions
extractOrder()- Converts trade data into a full order structextractFlags()- Unpacks flags into separate order parameters
GPv2Interaction
Enables arbitrary contract calls during settlement using inline assembly for gas optimization.Data Structure
Key Functions
execute(Data calldata interaction)- Performs the contract interactionselector(Data calldata interaction)- Extracts 4-byte function selector
Settlement Phases
Interactions run at three distinct phases:- Pre-settlement - Setup operations like approvals
- Intra-settlement - Liquidity transformations after input transfers
- Post-settlement - Cleanup tasks like token unwrapping
GPv2Signing
Supports four signature schemes:Key Features
- Careful memory management to avoid unnecessary copies
- Extensive use of
calldatainstead ofmemoryfor read-only data - Domain separator includes contract address for replay protection
- Pre-computed hash constants for common types