Skip to content

Fix constructor network handling#465

Merged
liquid-8 merged 2 commits into
uniswap-python:dev/v4-finfrom
liquid-8:uniswap4_RC
Jun 4, 2026
Merged

Fix constructor network handling#465
liquid-8 merged 2 commits into
uniswap-python:dev/v4-finfrom
liquid-8:uniswap4_RC

Conversation

@liquid-8

@liquid-8 liquid-8 commented Jun 4, 2026

Copy link
Copy Markdown
Member

No description provided.

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.83%. Comparing base (4d19b29) to head (1eccd7a).
⚠️ Report is 31 commits behind head on dev/v4-fin.

Files with missing lines Patch % Lines
uniswap/uniswap4.py 60.00% 2 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff               @@
##           dev/v4-fin     #465      +/-   ##
==============================================
- Coverage       76.87%   76.83%   -0.05%     
==============================================
  Files              12       12              
  Lines            2249     2253       +4     
==============================================
+ Hits             1729     1731       +2     
- Misses            520      522       +2     

☔ 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.

@greptile-apps

greptile-apps Bot commented Jun 4, 2026

Copy link
Copy Markdown

Greptile Summary

This PR adds an explicit check in the Uniswap4 constructor so that connecting to a completely unknown chain ID raises a descriptive Exception instead of a raw KeyError from _netid_to_name.

  • The guard correctly catches chain IDs that are entirely absent from _netid_to_name, giving callers a readable error message.
  • However, _netid_to_name contains several networks (e.g. sepolia, fantom, xdai, ropsten, legacy testnets) that have no entries in the v4 contract address maps, so those chains pass the new check and then immediately fail with an opaque KeyError on the _quoter_contract_addresses_v4 / _router_contract_addresses_v4 lookups that follow — the very situation the PR intends to prevent.
  • Exception is used as the raised type; ValueError would be more idiomatic and easier for callers to catch specifically.

Confidence Score: 3/5

The constructor's network validation is only partially fixed — chains known to the library but unsupported by v4 contracts still crash with a raw KeyError.

The change improves the error path for truly unrecognized chain IDs, but leaves a real gap: networks such as Sepolia, Fantom, and several legacy testnets are in _netid_to_name and will pass the new guard, then immediately raise an unhandled KeyError in the v4 contract address lookups. Callers cannot rely on getting a descriptive error when connecting to a known-but-v4-unsupported network.

uniswap/uniswap4.py — the constructor network check and the downstream contract address lookups need to be reviewed together.

Important Files Changed

Filename Overview
uniswap/uniswap4.py Replaces a silent KeyError on unknown chain IDs with an explicit guard, but the guard only covers chains absent from _netid_to_name; chains present in _netid_to_name but missing from the v4 contract address maps (e.g. sepolia, fantom, xdai) still produce a raw KeyError on the subsequent address lookups.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Uniswap4.__init__ called] --> B[Fetch chain_id via w3.net.version]
    B --> C{chain_id in _netid_to_name?}
    C -- No --> D[raise Exception: Unknown netid\n✅ New explicit error]
    C -- Yes --> E[self.net_name = _netid_to_name chain_id]
    E --> F{net_name in v4 contract maps?\ne.g. sepolia, fantom, xdai}
    F -- No --> G[KeyError on address lookup\n⚠️ Still unhandled]
    F -- Yes --> H[Load quoter, router, stateview,\npermit2, pool_manager, etc.]
    H --> I[Constructor complete]
Loading

Reviews (1): Last reviewed commit: "Fix constructor network handling" | Re-trigger Greptile

Comment thread uniswap/uniswap4.py
Comment on lines +129 to 136
if self.net_id in _netid_to_name:
self.net_name = _netid_to_name[self.net_id]
else:
raise Exception(f"Unknown netid: {self.net_id}")

logger.info(f"Using {self.w3} ('{self.net_name}', netid: {self.net_id})")
quoter_address = _quoter_contract_addresses_v4[self.net_name]
router_address = _router_contract_addresses_v4[self.net_name]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Incomplete error guard — v4-unsupported networks still produce raw KeyError

_netid_to_name contains many networks that have no entries in the v4 contract address maps (_quoter_contract_addresses_v4, _router_contract_addresses_v4, etc.): sepolia, fantom, xdai, ropsten, görli, kovan, binance_testnet, arbitrum_testnet, harmony_mainnet, and harmony_testnet. A caller connecting to any of those chains will pass the new if self.net_id in _netid_to_name guard but then immediately hit an uncaught KeyError: 'sepolia' on the contract address lookup (line 135 onward), producing a cryptic error that is harder to diagnose than the message raised on line 132. Since the purpose of this change is explicit network error handling, the guard should also cover the case where the network name has no corresponding v4 contract addresses.

Comment thread uniswap/uniswap4.py
Comment on lines +129 to +132
if self.net_id in _netid_to_name:
self.net_name = _netid_to_name[self.net_id]
else:
raise Exception(f"Unknown netid: {self.net_id}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Using bare Exception is less Pythonic and harder for callers to catch specifically. ValueError is the idiomatic built-in for receiving an invalid value, and it is consistent with how the rest of the library signals bad user input.

Suggested change
if self.net_id in _netid_to_name:
self.net_name = _netid_to_name[self.net_id]
else:
raise Exception(f"Unknown netid: {self.net_id}")
if self.net_id in _netid_to_name:
self.net_name = _netid_to_name[self.net_id]
else:
raise ValueError(f"Unknown netid: {self.net_id}")

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@liquid-8 liquid-8 merged commit e079370 into uniswap-python:dev/v4-fin Jun 4, 2026
5 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.

1 participant