From 143e4e9770b9dbb135baa1e3f46b32bb0b78dd45 Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Fri, 18 Oct 2024 02:25:09 -0400 Subject: [PATCH] fix(tick_map): fix `next_initialized_tick_within_one_word` (#94) Adjusted the tick map logic to start from the next word when the current tick state is irrelevant. Also updated related tests and documentation to reflect these changes. Updated version numbers in Cargo.toml and README.md to 2.1.1. --- Cargo.toml | 2 +- README.md | 2 +- src/extensions/ephemeral_tick_map_data_provider.rs | 11 ++++++++++- src/extensions/tick_map.rs | 2 ++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2421ca9..995101b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "uniswap-v3-sdk" -version = "2.1.0" +version = "2.1.1" edition = "2021" authors = ["Shuhui Luo "] description = "Uniswap V3 SDK for Rust" diff --git a/README.md b/README.md index 734aab2..58a4e6e 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ It is feature-complete with unit tests matching the TypeScript SDK. Add the following to your `Cargo.toml` file: ```toml -uniswap-v3-sdk = { version = "2.0.0", features = ["extensions", "std"] } +uniswap-v3-sdk = { version = "2.1.1", features = ["extensions", "std"] } ``` ### Usage diff --git a/src/extensions/ephemeral_tick_map_data_provider.rs b/src/extensions/ephemeral_tick_map_data_provider.rs index d9f2ca2..602f725 100644 --- a/src/extensions/ephemeral_tick_map_data_provider.rs +++ b/src/extensions/ephemeral_tick_map_data_provider.rs @@ -63,6 +63,7 @@ mod tests { *BLOCK_ID, ) .await?; + // [-887270, -92110, 100, 110, 22990, ...] let tick = provider.get_tick(-92110)?; assert_eq!(tick.liquidity_gross, 398290794261); assert_eq!(tick.liquidity_net, 398290794261); @@ -73,10 +74,18 @@ mod tests { )?; assert_eq!(tick, -887270); assert!(initialized); + let (tick, initialized) = + provider.next_initialized_tick_within_one_word(-92120, true, TICK_SPACING)?; + assert_eq!(tick, -92160); + assert!(!initialized); let (tick, initialized) = provider.next_initialized_tick_within_one_word(0, false, TICK_SPACING)?; - assert!(initialized); assert_eq!(tick, 100); + assert!(initialized); + let (tick, initialized) = + provider.next_initialized_tick_within_one_word(110, false, TICK_SPACING)?; + assert_eq!(tick, 2550); + assert!(!initialized); Ok(()) } } diff --git a/src/extensions/tick_map.rs b/src/extensions/tick_map.rs index 9f52e1c..56dbc15 100644 --- a/src/extensions/tick_map.rs +++ b/src/extensions/tick_map.rs @@ -69,6 +69,8 @@ impl TickDataProvider for TickMap { let next = (compressed - Self::Index::try_from(bit_pos).unwrap()) * tick_spacing; Ok((next, initialized)) } else { + // start from the word of the next tick, since the current tick state doesn't matter + let compressed = compressed + Self::Index::ONE; let (word_pos, bit_pos) = compressed.position(); // all the 1s at or to the left of the `bit_pos` let mask = U256::ZERO - (uint!(1_U256) << bit_pos);