fix: avoid duplicate WETH in swap path when input/output is WETH (closes #363) - #459
Conversation
Greptile SummaryThis PR fixes the
Confidence Score: 5/5Safe to merge. Both swap functions now correctly skip the redundant WETH intermediate hop when input or output is already WETH. The two-line guard added to each function uses the codebase-standard is_same_address() helper for address comparison, is consistent with how the existing price-query functions already route WETH tokens, and correctly aligns the swap path with the quote path in both the exact-input and exact-output code paths. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[make_trade or make_trade_output] --> B{input or output is ETH_ADDRESS?}
B -- Yes --> C[Raise ValueError]
B -- No --> D{Uniswap version?}
D -- v2 --> E{input or output token is WETH?}
E -- Yes --> F["path = direct 2-hop: input to output"]
E -- No --> G["path = 3-hop: input to WETH to output"]
F --> H[swapExactTokensForTokens or swapTokensForExactTokens]
G --> H
D -- v3 --> I[exactInputSingle or exactOutputSingle]
D -- v1 --> J[tokenToTokenSwapInput or tokenToTokenSwapOutput]
Reviews (3): Last reviewed commit: "fix: use is_same_address() for WETH comp..." | Re-trigger Greptile |
…p_output Raw == fails when input_token/output_token is bytes or lowercase hex, falling through to the wrong 3-hop path. Consistent with the rest of the codebase (line 746 and the v3 path use is_same_address).
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #459 +/- ##
===========================================
+ Coverage 60.52% 73.21% +12.69%
===========================================
Files 11 12 +1
Lines 1074 2315 +1241
===========================================
+ Hits 650 1695 +1045
- Misses 424 620 +196 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What
When swapping a token to WETH (or WETH to a token) using v2, the routing path was always constructed as
[input_token, weth_address, output_token]. If eitherinput_tokenoroutput_tokenis WETH, this results in a path like[USDT, WETH, WETH], which causes the DEX router to revert withIDENTICAL_ADDRESSES.Fix
Before building the swap path, check whether the input or output token is already WETH. If so, use a direct path
[input_token, output_token]instead of routing through WETH as an intermediate hop.Closes #363