Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions uniswap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ def _load_contract_erc20(w3: Web3, address: AddressLike) -> Contract:


def _encode_path(token_in: AddressLike, route: List[Tuple[int, AddressLike]]) -> bytes:
"""
Needed for multi-hop swaps in V3.

https://github.com/Uniswap/uniswap-v3-sdk/blob/1a74d5f0a31040fec4aeb1f83bba01d7c03f4870/src/utils/encodeRouteToPath.ts
"""
raise NotImplementedError
"""Encode a Uniswap V3 route as tightly packed token and fee values."""
path = bytearray(_str_to_addr(token_in))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Empty routes produce invalid paths

An empty route skips the loop and returns only the 20-byte input-token address, while a valid Uniswap V3 path requires at least one fee-and-token hop. Rejecting this input locally prevents callers from passing malformed path bytes to a router or quoter and receiving an opaque contract revert.

Suggested change
path = bytearray(_str_to_addr(token_in))
if not route:
raise ValueError("route must contain at least one hop")
path = bytearray(_str_to_addr(token_in))

Knowledge Base Used: Token Registry, Fee Tiers, Constants, and Shared Utilities

for fee, token in route:
if not 0 <= fee < 2**24:
raise ValueError(f"fee must fit in uint24: {fee}")
path.extend(fee.to_bytes(3, byteorder="big"))
path.extend(_str_to_addr(token))
return bytes(path)


# Adapted from: https://github.com/Uniswap/v3-sdk/blob/main/src/utils/encodeSqrtRatioX96.ts
Expand Down
Loading