From 1f4a865d1a07b476594c5df4f6dddcfb1277a063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Tue, 25 May 2021 22:12:52 +0200 Subject: [PATCH] feat: started working on multihop swaps for v3 --- uniswap/util.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/uniswap/util.py b/uniswap/util.py index 221c0cea..ddfe8d0f 100644 --- a/uniswap/util.py +++ b/uniswap/util.py @@ -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)) + 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