From 7c1d83f81ed5cbdda774df43bfc72420e6512dd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Wed, 1 May 2024 12:58:50 +0200 Subject: [PATCH 1/3] nit: fixed typo --- uniswap/uniswap.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/uniswap/uniswap.py b/uniswap/uniswap.py index 5edbc85..183c349 100644 --- a/uniswap/uniswap.py +++ b/uniswap/uniswap.py @@ -1403,7 +1403,7 @@ def approve(self, token: AddressLike, max_approval: Optional[int] = None) -> Non tx = self._build_and_send_tx(function) self.w3.eth.wait_for_transaction_receipt(tx, timeout=6000) - # Add extra sleep to let tx propogate correctly + # Add extra sleep to let tx propagate correctly time.sleep(1) def _is_approved(self, token: AddressLike) -> bool: @@ -1413,6 +1413,8 @@ def _is_approved(self, token: AddressLike) -> bool: contract_addr = self._exchange_address_from_token(token) elif self.version in [2, 3]: contract_addr = self.router_address + else: + raise ValueError amount = ( _load_contract_erc20(self.w3, token) .functions.allowance(self.address, contract_addr) From 7f4d5c740fefd8355c2837ee2e7e3c6f0c455980 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Wed, 22 May 2024 15:49:06 +0200 Subject: [PATCH 2/3] fix: added get_tick_at_sqrt util function --- uniswap/util.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/uniswap/util.py b/uniswap/util.py index 814ec9d..63d5147 100644 --- a/uniswap/util.py +++ b/uniswap/util.py @@ -99,6 +99,32 @@ def encode_sqrt_ratioX96(amount_0: int, amount_1: int) -> int: return int(math.sqrt(ratioX192)) +def get_tick_at_sqrt(sqrtPriceX96: int) -> int: + sqrtPriceX96 = int(sqrtPriceX96) + + # Define constants + Q96 = 2**96 + + # Calculate the price from the sqrt ratio + ratio = sqrtPriceX96 / Q96 + price = ratio**2 + + # Calculate the natural logarithm of the price + logPrice = math.log(price) + + # Calculate the log base 1.0001 of the price + logBase = math.log(1.0001) + tick = logPrice / logBase + + # Round tick to nearest integer + tick = int(round(tick)) + + # Ensure the tick is within the valid range + assert tick >= MIN_TICK and tick <= MAX_TICK + + return tick + + # Adapted from: https://github.com/tradingstrategy-ai/web3-ethereum-defi/blob/c3c68bc723d55dda0cc8252a0dadb534c4fdb2c5/eth_defi/uniswap_v3/utils.py#L77 def get_min_tick(fee: int) -> int: min_tick_spacing: int = _tick_spacing[fee] From 4f15d2ef7e9d7cb502e6b2ce44772fae46d18781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bj=C3=A4reholt?= Date: Thu, 23 May 2024 13:54:12 +0200 Subject: [PATCH 3/3] fix: added decode_sqrt_ratioX96 util function --- uniswap/util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/uniswap/util.py b/uniswap/util.py index 63d5147..888aa39 100644 --- a/uniswap/util.py +++ b/uniswap/util.py @@ -99,6 +99,13 @@ def encode_sqrt_ratioX96(amount_0: int, amount_1: int) -> int: return int(math.sqrt(ratioX192)) +def decode_sqrt_ratioX96(sqrtPriceX96: int) -> float: + Q96 = 2**96 + ratio = sqrtPriceX96 / Q96 + price = ratio**2 + return price + + def get_tick_at_sqrt(sqrtPriceX96: int) -> int: sqrtPriceX96 = int(sqrtPriceX96)