@@ -95,6 +95,46 @@ forge snapshot
9595- ` SignerECDSA.sol ` : ECDSA signature utilities for tests
9696- ` EmptyWrapper.sol ` : Minimal wrapper for testing wrapper chaining
9797
98+ ### Writing CoW Protocol Settlement Tests
99+
100+ When creating settlement tests, especially multi-user scenarios:
101+
102+ ** 1. Leverage Coincidence of Wants**
103+ - CoW Protocol nets out opposing trades within a settlement
104+ - Only swap the NET difference between opposing directions
105+ - Example: If User1+User2 need 10k SUSDS worth of WETH and User3 provides 5k SUSDS worth of WETH, only swap the 5k SUSDS difference
106+ - Don't create separate swaps for each direction - calculate the minimal swaps needed
107+
108+ ** 2. Proper Price Ratio Calculations**
109+ - Use ` clearingPrices[tokenIndex] ` in withdrawal/swap calculations
110+ - Calculate amounts based on what the settlement actually needs: ` amount * clearingPrices[buyToken] / clearingPrices[sellToken] `
111+ - Ensure the math balances: withdrawals + swaps must provide exactly what's needed for all trades
112+
113+ ** 3. Logical Token Array Ordering**
114+ - Organize tokens in a readable order: base assets first (SUSDS, WETH), then vault tokens (ESUSDS, EWETH)
115+ - Consistent ordering makes trade setup less error-prone
116+ - Use meaningful comments to clarify token indices
117+
118+ ** 4. Realistic Trade Amounts**
119+ - Fine-tune amounts so withdrawals, swaps, and repayments balance properly
120+ - The numbers need to actually work for the settlement to succeed
121+ - Test will fail if amounts don't align with vault balances and clearing prices
122+
123+ ** 5. Simplified Interaction Design**
124+ - Keep interactions minimal and purposeful - only include what's needed
125+ - Common pattern: withdrawals from vaults → net swaps → implicit transfers via settlement
126+ - Avoid redundant operations
127+
128+ ** 6. Helper Functions for DRY Tests**
129+ - Create parameterized helpers like ` _setupLeveragedPositionFor() ` instead of repeating setup code
130+ - Use helpers for approvals (` _setupClosePositionApprovalsFor() ` ) and signatures (` _createPermitSignatureFor() ` )
131+ - This significantly reduces test length and improves maintainability
132+
133+ ** 7. Clear Explanatory Comments**
134+ - Explain the economic logic, not just the technical operations
135+ - Examples: "We only need to swap the difference" or "Coincidence of wants between User1/User2 and User3"
136+ - Help readers understand why the settlement is structured this way
137+
98138## Important Implementation Details
99139
100140### Security Considerations
0 commit comments