SwapV1V2 Contract #55
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Scope
We have a problem with aggregators like CoW swap being unable to connect the V1 and V2 liquidity together.
Solution
The
SwapV1V2contract that enables 1:1 token swapping between V1 and V2 tokens that share the same balance storage. The contract supports three swap methods:swapExactIn: Basic 1:1 swap with slippage protectionswapWithPermitStrict: Swap with ERC-2612 permit for gasless approvals (strict mode - fails if permit fails)swapWithPermitBestEffort: Swap with optional permit that continues even if permit failsKey features:
ReentrancyGuardUpgradeableOwnableUpgradeableContract Implementation
SwapV1V2.sol: Main swap contract that facilitates 1:1 swaps between two tokensInitializable,OwnableUpgradeable,UUPSUpgradeable, andReentrancyGuardUpgradeableUpgradeable Pattern Implementation
The contract follows OpenZeppelin's recommended upgradeable pattern:
Key Benefits:
Key Technical Details
1:1 Price Guarantee
The contract ensures a guaranteed 1:1 exchange rate through:
This makes the contract suitable for pathfinders and aggregators that need predictable pricing.
Test Coverage
How to test
Run the full test suite:
forge test --match-contract SwapV1V2TestRun with coverage:
forge coverage --match-contract SwapV1V2TestKey Test Cases:
All tests pass (33/33) and achieve 100% line and branch coverage for the SwapV1V2 contract.
Deployment Instructions
For upgradeable contracts, deployment follows the proxy pattern: