Skip to content

1 #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2024
Merged

1 #388

Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion uniswap/uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions uniswap/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy