Skip to content

fix: avoid duplicate WETH in swap path when input/output is WETH (closes #363) - #459

Merged
liquid-8 merged 4 commits into
uniswap-python:masterfrom
botbikamordehai2-sketch:fix/issue-363-1779952888
Jul 29, 2026
Merged

fix: avoid duplicate WETH in swap path when input/output is WETH (closes #363)#459
liquid-8 merged 4 commits into
uniswap-python:masterfrom
botbikamordehai2-sketch:fix/issue-363-1779952888

Conversation

@botbikamordehai2-sketch

Copy link
Copy Markdown
Contributor

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 either input_token or output_token is WETH, this results in a path like [USDT, WETH, WETH], which causes the DEX router to revert with IDENTICAL_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

@greptile-apps

greptile-apps Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes the IDENTICAL_ADDRESSES router revert in Uniswap v2 token-to-token swaps when either the input or output token is WETH. Previously, both _token_to_token_swap_input and _token_to_token_swap_output unconditionally built a three-hop path [input, WETH, output], which collapses to [WETH, WETH, ...] or [..., WETH, WETH] when one side is already WETH.

  • _token_to_token_swap_input: Now uses a direct two-hop path [input, output] when either token is WETH, correctly using is_same_address() for comparison.
  • _token_to_token_swap_output: Same guard is applied with swapTokensForExactTokens, and the price functions already delegated to the two-hop ETH/WETH path — so the swap path and quote path are now consistent.

Confidence Score: 5/5

Safe 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

Filename Overview
uniswap/uniswap.py Adds WETH-dedup path guards to both _token_to_token_swap_input (v2 branch) and _token_to_token_swap_output (v2 branch) using is_same_address(); fix is consistent with the existing price-query functions that already delegated to two-hop paths for WETH tokens.

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]
Loading

Reviews (3): Last reviewed commit: "fix: use is_same_address() for WETH comp..." | Re-trigger Greptile

Comment thread uniswap/uniswap.py
botbikamordehai2-sketch and others added 2 commits May 28, 2026 08:30
…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

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.21%. Comparing base (f5a9e86) to head (c2e0aae).
⚠️ Report is 4 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@liquid-8
liquid-8 merged commit e771a29 into uniswap-python:master Jul 29, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fails when swapping USDT=>WBNB

2 participants