A Python library for validating CoW Protocol settlement transactions. This library provides core validation logic to ensure solver settlements comply with CoW Protocol rules by comparing on-chain execution data with off-chain auction proposals.
The Competition Monitoring library enforces three main validation rules for settlement transactions:
- Solver Identity Verification - Ensures the on-chain settlement was submitted by the winning solver
- Order Validation - Validates trade execution, amounts, and surplus rules
- Score Validation - Verifies computed scores match reported auction scores
-
OnchainSettlementData- Settlement data from blockchainauction_id: Auction identifiertx_hash: Transaction hashsolver: Solver addresstrades: List of executed trades
-
OffchainSettlementData- Settlement data from orderbookauction_id: Auction identifiersolver: Winning solver addresstrades: List of proposed tradesscore: Reported auction scoretrade_fee_policies: Fee policies per ordervalid_orders: Set of valid order UIDsjit_order_addresses: Whitelisted JIT order addressesnative_prices: Token prices in native currency
-
OnchainTrade- Executed trade data -
OffchainTrade- Proposed trade data -
Quote- Quote information for price improvement calculation -
Fee Policy Models -
VolumeFeePolicy,SurplusFeePolicy,PriceImprovementFeePolicy
Main validation orchestrator that runs all checks. Raises InvalidSettlement if any check fails.
-
check_solver(onchain_data, offchain_data) -> bool- Validates solver address matches between on-chain and off-chain data
-
check_orders(onchain_data, offchain_data) -> bool- Performs three ordered checks:
- 1-to-1 Mapping - Executed trades must exactly match proposed trades
- Amount Matching - Sell/buy amounts must match between execution and proposal
- Surplus Validation - Orders with surplus must be valid or whitelisted JIT orders
- Performs three ordered checks:
-
check_score(onchain_data, offchain_data) -> bool- Validates computed score matches reported auction score (within threshold)
Computes the settlement score by summing raw surplus values across all trades.
InvalidSettlement- Raised when settlement violates protocol rulesWhitelistedSolver- Raised when solver is whitelisted (skip validation)CriticalDataFetchingError- Critical error in data fetchingNoncriticalDataFetchingError- Non-critical error (may retry)MissingOnchainData- On-chain data unavailable
Protocol constants including:
- Contract addresses (
SETTLEMENT_CONTRACT_ADDRESS,GPV2_AUTHENTICATOR, etc.) - Chain-specific configurations (
CHAIN_ID_TO_ZODIAC_MODULE_ADDRESS) - Call signatures and special addresses
- On-chain solver address must match off-chain winning solver
- Whitelisted solvers (team multisig) skip validation
The library enforces strict 1-to-1 trade mapping:
- Every executed trade must have been proposed in the bid
- Every proposed trade must be executed
- All JIT orders must be revealed during bidding, regardless of surplus
onchain.sell_amount == offchain.sell_amountonchain.buy_amount == offchain.buy_amount- No deviation allowed from proposed amounts
- Orders with non-zero surplus must either:
- Be part of the auction (in
valid_orders), OR - Have whitelisted JIT order owner (in
jit_order_addresses)
- Be part of the auction (in
- Computed score must not be lower than reported score by more than threshold
- Threshold:
10^12atoms (SCORE_CHECK_THRESHOLD)
To use this library in a monitoring system like Circuit Breaker, you need to provide:
Implement a component to fetch settlement data from blockchain:
- Transaction details and receipts
- Decoded settlement calldata
- Trade events from transaction logs
Implement a component to fetch auction data:
- Winning solver and solution details
- Proposed trades and amounts
- Fee policies for each order
- Valid order UIDs and JIT order addresses
- Native token prices
Use the library's inspect() function to validate settlements:
Handle the library's exceptions appropriately:
InvalidSettlement- Settlement violated rules → blacklist solverWhitelistedSolver- Whitelisted solver → skip validation- Data fetching errors - Retry or log for manual review
python -m pytest tests/# Create onchain settlement data
onchain_data = OnchainSettlementData(
auction_id=12345,
tx_hash=HexBytes("0x..."),
solver=HexBytes("0xSOLVER_ADDRESS"),
trades=[
OnchainTrade(
order_uid=HexBytes("0xORDER_UID"),
sell_amount=1000000,
buy_amount=2000000,
owner=HexBytes("0xOWNER"),
sell_token=HexBytes("0xTOKEN_A"),
buy_token=HexBytes("0xTOKEN_B"),
limit_sell_amount=1000000,
limit_buy_amount=1900000,
kind="sell",
)
],
)
# Create offchain settlement data
offchain_data = OffchainSettlementData(
auction_id=12345,
solver=HexBytes("0xSOLVER_ADDRESS"),
trades=[
OffchainTrade(
order_uid=HexBytes("0xORDER_UID"),
sell_amount=1000000,
buy_amount=2000000,
)
],
score=100000000000000000,
trade_fee_policies={},
valid_orders={HexBytes("0xORDER_UID")},
jit_order_addresses=set(),
native_prices={HexBytes("0xTOKEN_B"): 1000000000000000000},
)
# Validate settlement
try:
inspect(onchain_data, offchain_data)
print("✅ Settlement passed all validation checks")
except InvalidSettlement as e:
print(f"❌ Settlement validation failed: {e}")