Skip to content

Commit

Permalink
replaced inaccurate sqrt price with actual sqrt price (didn't solve d…
Browse files Browse the repository at this point in the history
…elta in mint)
  • Loading branch information
rafalum committed Oct 17, 2023
1 parent 5185302 commit 584745b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
20 changes: 10 additions & 10 deletions src/position.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,37 @@ def tick_to_price(self, tick):

def amount_x(self, current_tick, current_sqrt_price=None):

price_lower_tick = tick_to_sqrt_price(self.lower_tick)
price_upper_tick = tick_to_sqrt_price(self.upper_tick)
price_lower_tick = int(tick_to_sqrt_price(self.lower_tick) * 2**96)
price_upper_tick = int(tick_to_sqrt_price(self.upper_tick) * 2**96)

# if the exact sqrt price is given, use it
if current_sqrt_price:
price_current_tick = current_sqrt_price
else:
price_current_tick = tick_to_sqrt_price(current_tick)
price_current_tick = int(tick_to_sqrt_price(current_tick) * 2**96)


if current_tick < self.lower_tick:
value = 1 / price_lower_tick - 1 / price_upper_tick
value = (price_upper_tick - price_lower_tick) / (price_lower_tick * price_upper_tick)

elif current_tick >= self.upper_tick:
value = 0

else:
value = 1 / price_current_tick - 1 / price_upper_tick
value = (price_upper_tick - price_current_tick) / (price_current_tick * price_upper_tick)

return self.liquidity * value
return self.liquidity * value * 2**96

def amount_y(self, current_tick, current_sqrt_price=None):

price_lower_tick = tick_to_sqrt_price(self.lower_tick)
price_upper_tick = tick_to_sqrt_price(self.upper_tick)
price_lower_tick = int(tick_to_sqrt_price(self.lower_tick) * 2**96)
price_upper_tick = int(tick_to_sqrt_price(self.upper_tick) * 2**96)

# if the exact sqrt price is given, use it
if current_sqrt_price:
price_current_tick = current_sqrt_price
else:
price_current_tick = tick_to_sqrt_price(current_tick)
price_current_tick = int(tick_to_sqrt_price(current_tick) * 2**96)

if current_tick < self.lower_tick:
value = 0
Expand All @@ -70,7 +70,7 @@ def amount_y(self, current_tick, current_sqrt_price=None):
else:
value = price_current_tick - price_lower_tick

return self.liquidity * value
return self.liquidity * value / 2**96

#
#
Expand Down
2 changes: 1 addition & 1 deletion src/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_current_block(self) -> int:
def get_current_sqrt_price(self, block) -> int:

slot0 = self.pool_contract.functions.slot0().call(block_identifier=int(block))
return slot0[0] / 2**96
return slot0[0]

def get_current_tick(self, block) -> int:

Expand Down
17 changes: 8 additions & 9 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,26 @@ def check_enough_balance(current_tick, balance_token0, balance_token1, amount_to

def real_reservers_to_virtal_reserves(lower_tick, upper_tick, current_tick, current_sqrt_price, x_real=None, y_real=None):

lower_sqrtPrice = tick_to_sqrt_price(lower_tick)
current_sqrtPrice = current_sqrt_price
upper_sqrtPrice = tick_to_sqrt_price(upper_tick)
lower_sqrt_price = int(tick_to_sqrt_price(lower_tick) * 2**96)
upper_sqrt_price = int(tick_to_sqrt_price(upper_tick) * 2**96)

if y_real:

liquidity = (1 / (current_sqrtPrice - lower_sqrtPrice)) * y_real
liquidity = (y_real * 2**96 / (current_sqrt_price - lower_sqrt_price))

real = ((upper_sqrtPrice - current_sqrtPrice) / (upper_sqrtPrice * current_sqrtPrice)) * liquidity # x_real
real = ((upper_sqrt_price - current_sqrt_price) / (upper_sqrt_price * current_sqrt_price)) * liquidity * 2**96 # x_real

elif x_real:

liquidity = (1 / (1 / current_sqrtPrice - 1 / upper_sqrtPrice)) * x_real
liquidity = ((current_sqrt_price * upper_sqrt_price) / (upper_sqrt_price - current_sqrt_price)) * x_real / 2**96

real = (current_sqrtPrice - lower_sqrtPrice) * liquidity # y_real
real = (current_sqrt_price - lower_sqrt_price) * liquidity / 2**96 # y_real

else:
return None, None

x_virt = liquidity / current_sqrtPrice
y_virt = liquidity * current_sqrtPrice
x_virt = (liquidity / current_sqrt_price) * 2**96
y_virt = (liquidity * current_sqrt_price) / 2**96

return x_virt, y_virt, real

Expand Down
2 changes: 1 addition & 1 deletion test/test_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TestPosition(unittest.TestCase):

def setUp(self):

x_virt, y_virt, x_real = real_reservers_to_virtal_reserves(90000, 110000, 100000, tick_to_sqrt_price(100000), y_real=10**18)
x_virt, y_virt, x_real = real_reservers_to_virtal_reserves(90000, 110000, 100000, tick_to_sqrt_price(100000) * 2**96, y_real=10**18)

self.x_real = x_real
self.y_real = 10**18
Expand Down

0 comments on commit 584745b

Please sign in to comment.