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) diff --git a/uniswap/util.py b/uniswap/util.py index 814ec9d..888aa39 100644 --- a/uniswap/util.py +++ b/uniswap/util.py @@ -99,6 +99,39 @@ 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) + + # 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]
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: