From 312c7c50cbe55b0808b8eee60938d72a81c2a288 Mon Sep 17 00:00:00 2001 From: "tim.nugent" Date: Mon, 27 Jan 2025 17:52:25 +0000 Subject: [PATCH 1/3] v3 and v4 getters --- src/evm/protocol/uniswap_v3/state.rs | 38 ++++++++++++++++----- src/evm/protocol/uniswap_v4/state.rs | 38 ++++++++++++++++----- src/evm/protocol/utils/uniswap/tick_list.rs | 16 +++++++++ 3 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/evm/protocol/uniswap_v3/state.rs b/src/evm/protocol/uniswap_v3/state.rs index 0ff45138..a3c3add5 100644 --- a/src/evm/protocol/uniswap_v3/state.rs +++ b/src/evm/protocol/uniswap_v3/state.rs @@ -60,6 +60,26 @@ impl UniswapV3State { UniswapV3State { liquidity, sqrt_price, fee, tick, ticks: tick_list } } + pub fn get_liquidity(&self) -> u128 { + self.liquidity + } + + pub fn get_sqrt_price(&self) -> U256 { + self.sqrt_price + } + + pub fn get_fee(&self) -> FeeAmount { + self.fee + } + + pub fn get_tick(&self) -> i32 { + self.tick + } + + pub fn get_ticks(&self) -> &Vec { + self.ticks.get_ticks() + } + fn get_spacing(fee: FeeAmount) -> u16 { match fee { FeeAmount::Lowest => 1, @@ -105,8 +125,8 @@ impl UniswapV3State { }; let mut gas_used = U256::from(130_000); - while state.amount_remaining != I256::from_raw(U256::from(0u64)) && - state.sqrt_price != price_limit + while state.amount_remaining != I256::from_raw(U256::from(0u64)) + && state.sqrt_price != price_limit { let (mut next_tick, initialized) = match self .ticks @@ -224,8 +244,8 @@ impl ProtocolSim for UniswapV3State { if a < b { Ok(sqrt_price_q96_to_f64(self.sqrt_price, a.decimals as u32, b.decimals as u32)) } else { - Ok(1.0f64 / - sqrt_price_q96_to_f64(self.sqrt_price, b.decimals as u32, a.decimals as u32)) + Ok(1.0f64 + / sqrt_price_q96_to_f64(self.sqrt_price, b.decimals as u32, a.decimals as u32)) } } @@ -363,11 +383,11 @@ impl ProtocolSim for UniswapV3State { .as_any() .downcast_ref::() { - self.liquidity == other_state.liquidity && - self.sqrt_price == other_state.sqrt_price && - self.fee == other_state.fee && - self.tick == other_state.tick && - self.ticks == other_state.ticks + self.liquidity == other_state.liquidity + && self.sqrt_price == other_state.sqrt_price + && self.fee == other_state.fee + && self.tick == other_state.tick + && self.ticks == other_state.ticks } else { false } diff --git a/src/evm/protocol/uniswap_v4/state.rs b/src/evm/protocol/uniswap_v4/state.rs index e0c93930..efaa800b 100644 --- a/src/evm/protocol/uniswap_v4/state.rs +++ b/src/evm/protocol/uniswap_v4/state.rs @@ -80,6 +80,26 @@ impl UniswapV4State { UniswapV4State { liquidity, sqrt_price, fees, tick, ticks: tick_list } } + pub fn get_liquidity(&self) -> u128 { + self.liquidity + } + + pub fn get_sqrt_price(&self) -> U256 { + self.sqrt_price + } + + pub fn get_fees(&self) -> &UniswapV4Fees { + &self.fees + } + + pub fn get_tick(&self) -> i32 { + self.tick + } + + pub fn get_ticks(&self) -> &Vec { + self.ticks.get_ticks() + } + fn swap( &self, zero_for_one: bool, @@ -116,8 +136,8 @@ impl UniswapV4State { }; let mut gas_used = U256::from(130_000); - while state.amount_remaining != I256::from_raw(U256::from(0u64)) && - state.sqrt_price != price_limit + while state.amount_remaining != I256::from_raw(U256::from(0u64)) + && state.sqrt_price != price_limit { let (mut next_tick, initialized) = match self .ticks @@ -238,8 +258,8 @@ impl ProtocolSim for UniswapV4State { if base < quote { Ok(sqrt_price_q96_to_f64(self.sqrt_price, base.decimals as u32, quote.decimals as u32)) } else { - Ok(1.0f64 / - sqrt_price_q96_to_f64( + Ok(1.0f64 + / sqrt_price_q96_to_f64( self.sqrt_price, quote.decimals as u32, base.decimals as u32, @@ -364,11 +384,11 @@ impl ProtocolSim for UniswapV4State { .as_any() .downcast_ref::() { - self.liquidity == other_state.liquidity && - self.sqrt_price == other_state.sqrt_price && - self.fees == other_state.fees && - self.tick == other_state.tick && - self.ticks == other_state.ticks + self.liquidity == other_state.liquidity + && self.sqrt_price == other_state.sqrt_price + && self.fees == other_state.fees + && self.tick == other_state.tick + && self.ticks == other_state.ticks } else { false } diff --git a/src/evm/protocol/utils/uniswap/tick_list.rs b/src/evm/protocol/utils/uniswap/tick_list.rs index cca7e432..151c9a22 100644 --- a/src/evm/protocol/utils/uniswap/tick_list.rs +++ b/src/evm/protocol/utils/uniswap/tick_list.rs @@ -18,6 +18,18 @@ impl TickInfo { let sqrt_price = tick_math::get_sqrt_ratio_at_tick(index).unwrap(); TickInfo { index, net_liquidity, sqrt_price } } + + pub fn get_index(&self) -> i32 { + self.index + } + + pub fn get_net_liquidity(&self) -> i128 { + self.net_liquidity + } + + pub fn get_sqrt_price(&self) -> U256 { + self.sqrt_price + } } impl PartialOrd for TickInfo { @@ -162,6 +174,10 @@ impl TickList { } } + pub fn get_ticks(&self) -> &Vec { + &self.ticks + } + fn next_initialized_tick(&self, index: i32, lte: bool) -> Result<&TickInfo, TickListError> { if lte { if self.is_below_smallest(index) { From 726f20473d899ce7817da6e7ca5c90b74481a068 Mon Sep 17 00:00:00 2001 From: "tim.nugent" Date: Tue, 28 Jan 2025 09:45:42 +0000 Subject: [PATCH 2/3] fee getters --- src/evm/protocol/uniswap_v4/state.rs | 34 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/evm/protocol/uniswap_v4/state.rs b/src/evm/protocol/uniswap_v4/state.rs index efaa800b..ec4c4e28 100644 --- a/src/evm/protocol/uniswap_v4/state.rs +++ b/src/evm/protocol/uniswap_v4/state.rs @@ -53,6 +53,18 @@ impl UniswapV4Fees { Self { zero_for_one, one_for_zero, lp_fee } } + pub fn get_zero_for_one(&self) -> u32 { + self.zero_for_one + } + + pub fn one_for_zero(&self) -> u32 { + self.one_for_zero + } + + pub fn lp_fee(&self) -> u32 { + self.lp_fee + } + fn calculate_swap_fees_pips(&self, zero_for_one: bool) -> u32 { let protocol_fees = if zero_for_one { self.zero_for_one } else { self.one_for_zero }; protocol_fees + self.lp_fee @@ -107,7 +119,7 @@ impl UniswapV4State { sqrt_price_limit: Option, ) -> Result { if self.liquidity == 0 { - return Err(SimulationError::RecoverableError("No liquidity".to_string())); + return Err(SimulationError::RecoverableError("No liquidity".to_string())) } let price_limit = if let Some(limit) = sqrt_price_limit { limit @@ -136,8 +148,8 @@ impl UniswapV4State { }; let mut gas_used = U256::from(130_000); - while state.amount_remaining != I256::from_raw(U256::from(0u64)) - && state.sqrt_price != price_limit + while state.amount_remaining != I256::from_raw(U256::from(0u64)) && + state.sqrt_price != price_limit { let (mut next_tick, initialized) = match self .ticks @@ -157,7 +169,7 @@ impl UniswapV4State { u256_to_biguint(gas_used), Box::new(new_state), )), - )); + )) } _ => return Err(SimulationError::FatalError("Unknown error".to_string())), }, @@ -258,8 +270,8 @@ impl ProtocolSim for UniswapV4State { if base < quote { Ok(sqrt_price_q96_to_f64(self.sqrt_price, base.decimals as u32, quote.decimals as u32)) } else { - Ok(1.0f64 - / sqrt_price_q96_to_f64( + Ok(1.0f64 / + sqrt_price_q96_to_f64( self.sqrt_price, quote.decimals as u32, base.decimals as u32, @@ -384,11 +396,11 @@ impl ProtocolSim for UniswapV4State { .as_any() .downcast_ref::() { - self.liquidity == other_state.liquidity - && self.sqrt_price == other_state.sqrt_price - && self.fees == other_state.fees - && self.tick == other_state.tick - && self.ticks == other_state.ticks + self.liquidity == other_state.liquidity && + self.sqrt_price == other_state.sqrt_price && + self.fees == other_state.fees && + self.tick == other_state.tick && + self.ticks == other_state.ticks } else { false } From 5373008360b40f33acf61bf25c605e5adfca836e Mon Sep 17 00:00:00 2001 From: "tim.nugent" Date: Tue, 28 Jan 2025 09:48:22 +0000 Subject: [PATCH 3/3] rename getters --- src/evm/protocol/uniswap_v4/state.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/evm/protocol/uniswap_v4/state.rs b/src/evm/protocol/uniswap_v4/state.rs index ec4c4e28..29ec3f25 100644 --- a/src/evm/protocol/uniswap_v4/state.rs +++ b/src/evm/protocol/uniswap_v4/state.rs @@ -57,11 +57,11 @@ impl UniswapV4Fees { self.zero_for_one } - pub fn one_for_zero(&self) -> u32 { + pub fn get_one_for_zero(&self) -> u32 { self.one_for_zero } - pub fn lp_fee(&self) -> u32 { + pub fn get_lp_fee(&self) -> u32 { self.lp_fee }