Skip to content

Commit

Permalink
refresh self.update_block correctly when force=True
Browse files Browse the repository at this point in the history
fixes bug where in-range liquidity was being modified too many times on pools created with liquidity snapshot
  • Loading branch information
BowTiedDevil committed Jun 3, 2023
1 parent 595f034 commit 8eb0556
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions uniswap/v3/v3_liquidity_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,13 @@ def check_liquidity_update_block(block) -> bool:
with self.tick_lock:
# Mint/Burn events may affect the current liquidity if the current tick is
# in the tick range associated with this event, so check and adjust
if lower_tick <= self.tick <= upper_tick:
if (
lower_tick <= self.tick <= upper_tick
and block_number > self.update_block
):
logger.debug(
f"Adjusting in-range liquidity {block_number=}, {self.update_block=}, {self.tick=}"
)
self.liquidity += liquidity_delta

for i, tick in enumerate([lower_tick, upper_tick]):
Expand Down Expand Up @@ -1028,15 +1034,24 @@ def check_liquidity_update_block(block) -> bool:
updated_state = True

if not silent:
logger.info(f"Liquidity: {self.liquidity}")
logger.info(f"SqrtPriceX96: {self.sqrt_price_x96}")
logger.info(f"Tick: {self.tick}")
logger.info(
logger.debug(f"Liquidity: {self.liquidity}")
logger.debug(f"SqrtPriceX96: {self.sqrt_price_x96}")
logger.debug(f"Tick: {self.tick}")
logger.debug(
f"liquidity event: {liquidity_delta} in tick range [{lower_tick},{upper_tick}], pool: {self.name}"
"\n"
f"old liquidity: {tick_liquidity_net} net, {tick_liquidity_gross} gross"
"\n"
f"new liquidity: {new_liquidity_net} net, {new_liquidity_gross} gross"
)
logger.debug(
f"update block: {block_number} (last={self.update_block})"
)

if updated_state:
self.update_block = block_number
# if the update was forced, do not refresh the update block
if not force:
self.update_block = block_number
self.state.update(
{
"last_liquidity_update": self.liquidity_update_block,
Expand Down

0 comments on commit 8eb0556

Please sign in to comment.