Skip to content

Commit 0b0b7e0

Browse files
committed
Merge branch 'feat/open-position-wrapper' into feat/close-position-wrapper
2 parents 0e06bcc + d70bf35 commit 0b0b7e0

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

CLAUDE.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/CowEvcOpenPositionWrapper.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,12 @@ contract CowEvcOpenPositionWrapperTest is CowBaseTest {
497497
// Deposit SUSDS to eSUSDS vault for both user1 and user2
498498
interactions[1][1] = getDepositInteraction(ESUSDS, 10000 ether);
499499
// Deposit WETH to eWETH vault
500-
interactions[1][2] = getDepositInteraction(ESUSDS, 2 ether);
500+
interactions[1][2] = getDepositInteraction(EWETH, 2 ether);
501501

502502
// Skim eSUSDS vault
503-
interactions[1][3] = getSkimInteraction(tokens[2]);
503+
interactions[1][3] = getSkimInteraction(ESUSDS);
504504
// Skim eWETH vault
505-
interactions[1][4] = getSkimInteraction(tokens[3]);
505+
interactions[1][4] = getSkimInteraction(EWETH);
506506

507507
// Encode settlement data
508508
bytes memory settleData = abi.encodeCall(ICowSettlement.settle, (tokens, clearingPrices, trades, interactions));

test/helpers/CowBaseTest.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ contract CowBaseTest is Test {
8787
milkSwap.setPrice(SUSDS, 1e18); // 1 USDS = 1 USD
8888

8989
// deal small amount to the settlement contract that serve as buffer (just makes tests easier...)
90-
deal(SUSDS, address(COW_SETTLEMENT), 100e18);
91-
deal(WETH, address(COW_SETTLEMENT), 100e18);
92-
deal(ESUSDS, address(COW_SETTLEMENT), 100e18);
93-
deal(EWETH, address(COW_SETTLEMENT), 100e18);
90+
deal(SUSDS, address(COW_SETTLEMENT), 200e18);
91+
deal(WETH, address(COW_SETTLEMENT), 0.2e18);
92+
deal(ESUSDS, address(COW_SETTLEMENT), 200e18);
93+
deal(EWETH, address(COW_SETTLEMENT), 0.2e18);
9494

9595
// Set the approval for MilkSwap in the settlement as a convenience
9696
vm.startPrank(address(COW_SETTLEMENT));

0 commit comments

Comments
 (0)