Skip to main content

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 signing
  • actualReceiver(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 single uint256 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 struct
  • extractFlags() - 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 interaction
  • selector(Data calldata interaction) - Extracts 4-byte function selector

Settlement Phases

Interactions run at three distinct phases:
  1. Pre-settlement - Setup operations like approvals
  2. Intra-settlement - Liquidity transformations after input transfers
  3. Post-settlement - Cleanup tasks like token unwrapping

GPv2Signing

Supports four signature schemes:

Key Features

  • Careful memory management to avoid unnecessary copies
  • Extensive use of calldata instead of memory for read-only data
  • Domain separator includes contract address for replay protection
  • Pre-computed hash constants for common types
Last modified on March 4, 2026