Skip to content

Commit

Permalink
Merge pull request #104 from propeller-heads/zz/reduce-uniswaps-pub-i…
Browse files Browse the repository at this point in the history
…nterfaces

refactor(protocols): make uniswaps functions more private
  • Loading branch information
zizou0x authored Dec 17, 2024
2 parents 4e61922 + 321b6e4 commit c40e5cc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/evm/protocol/uniswap_v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Uniswap V2 Decentralized Exchange
pub mod reserve_price;
mod reserve_price;
pub mod state;
pub mod tycho_decoder;
2 changes: 1 addition & 1 deletion src/evm/protocol/uniswap_v2/reserve_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::evm::protocol::u256_num::u256_to_f64;
///
/// assert_eq!(res, 2.0f64);
/// ```
pub fn spot_price_from_reserves(
pub(super) fn spot_price_from_reserves(
r0: U256,
r1: U256,
token_0_decimals: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/evm/protocol/uniswap_v3/liquidity_math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Solidity spec: function addDelta(uint128 x, int128 y) internal pure returns (uint128 z) {
pub fn add_liquidity_delta(x: u128, y: i128) -> u128 {
pub(super) fn add_liquidity_delta(x: u128, y: i128) -> u128 {
if y < 0 {
x - ((-y) as u128)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/evm/protocol/uniswap_v3/solidity_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
protocol::errors::SimulationError,
};

pub fn mul_div_rounding_up(a: U256, b: U256, denom: U256) -> Result<U256, SimulationError> {
pub(super) fn mul_div_rounding_up(a: U256, b: U256, denom: U256) -> Result<U256, SimulationError> {
let a_big = U512::from(a);
let b_big = U512::from(b);
let product = safe_mul_u512(a_big, b_big)?;
Expand All @@ -16,7 +16,7 @@ pub fn mul_div_rounding_up(a: U256, b: U256, denom: U256) -> Result<U256, Simula
truncate_to_u256(result)
}

pub fn mul_div(a: U256, b: U256, denom: U256) -> Result<U256, SimulationError> {
pub(super) fn mul_div(a: U256, b: U256, denom: U256) -> Result<U256, SimulationError> {
let a_big = U512::from(a);
let b_big = U512::from(b);
let product = safe_mul_u512(a_big, b_big)?;
Expand Down
8 changes: 4 additions & 4 deletions src/evm/protocol/uniswap_v3/sqrt_price_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn div_rounding_up(a: U256, b: U256) -> Result<U256, SimulationError> {
}
}

pub fn get_amount0_delta(
pub(super) fn get_amount0_delta(
a: U256,
b: U256,
liquidity: u128,
Expand All @@ -51,7 +51,7 @@ pub fn get_amount0_delta(
}
}

pub fn get_amount1_delta(
pub(super) fn get_amount1_delta(
a: U256,
b: U256,
liquidity: u128,
Expand All @@ -68,7 +68,7 @@ pub fn get_amount1_delta(
}
}

pub fn get_next_sqrt_price_from_input(
pub(super) fn get_next_sqrt_price_from_input(
sqrt_price: U256,
liquidity: u128,
amount_in: U256,
Expand All @@ -83,7 +83,7 @@ pub fn get_next_sqrt_price_from_input(
}
}

pub fn get_next_sqrt_price_from_output(
pub(super) fn get_next_sqrt_price_from_output(
sqrt_price: U256,
liquidity: u128,
amount_in: U256,
Expand Down
2 changes: 1 addition & 1 deletion src/evm/protocol/uniswap_v3/swap_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
};
use crate::{evm::protocol::safe_math::safe_sub_u256, protocol::errors::SimulationError};

pub fn compute_swap_step(
pub(super) fn compute_swap_step(
sqrt_ratio_current: U256,
sqrt_ratio_target: U256,
liquidity: u128,
Expand Down
36 changes: 19 additions & 17 deletions src/evm/protocol/uniswap_v3/tick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use super::tick_math;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct TickInfo {
pub index: i32,
pub net_liquidity: i128,
pub sqrt_price: U256,
pub(super) index: i32,
pub(super) net_liquidity: i128,
pub(super) sqrt_price: U256,
}

impl TickInfo {
Expand All @@ -27,26 +27,26 @@ impl PartialOrd for TickInfo {
}

#[derive(Debug)]
pub struct TickListError {
pub kind: TickListErrorKind,
pub(super) struct TickListError {
pub(super) kind: TickListErrorKind,
}

#[derive(Debug, PartialEq)]
pub enum TickListErrorKind {
pub(super) enum TickListErrorKind {
NotFound,
BelowSmallest,
AtOrAboveLargest,
TicksExeeded,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TickList {
pub(super) struct TickList {
tick_spacing: u16,
ticks: Vec<TickInfo>,
}

impl TickList {
pub fn from(spacing: u16, ticks: Vec<TickInfo>) -> Self {
pub(super) fn from(spacing: u16, ticks: Vec<TickInfo>) -> Self {
let tick_list = TickList { tick_spacing: spacing, ticks };
let valid = tick_list.valid_ticks();
if valid.is_ok() {
Expand Down Expand Up @@ -87,11 +87,13 @@ impl TickList {
Ok(true)
}

pub fn apply_liquidity_change(&mut self, lower: i32, upper: i32, delta: i128) {
#[allow(dead_code)]
fn apply_liquidity_change(&mut self, lower: i32, upper: i32, delta: i128) {
self.upsert_tick(lower, delta);
self.upsert_tick(upper, -delta);
}

#[allow(dead_code)]
fn upsert_tick(&mut self, tick: i32, delta: i128) {
match self
.ticks
Expand All @@ -111,7 +113,7 @@ impl TickList {
}
}

pub fn set_tick_liquidity(&mut self, tick: i32, liquidity: i128) {
pub(super) fn set_tick_liquidity(&mut self, tick: i32, liquidity: i128) {
match self
.ticks
.binary_search_by(|t| t.index.cmp(&tick))
Expand All @@ -130,27 +132,27 @@ impl TickList {
}
}

pub fn is_below_smallest(&self, tick: i32) -> bool {
fn is_below_smallest(&self, tick: i32) -> bool {
tick < self.ticks[0].index
}

pub fn is_below_safe_tick(&self, tick: i32) -> bool {
fn is_below_safe_tick(&self, tick: i32) -> bool {
let smallest = self.ticks[0].index;
let minimum = smallest - self.tick_spacing as i32;
tick < minimum
}

pub fn is_at_or_above_largest(&self, tick: i32) -> bool {
fn is_at_or_above_largest(&self, tick: i32) -> bool {
tick >= self.ticks[self.ticks.len() - 1].index
}

pub fn is_at_or_above_safe_tick(&self, tick: i32) -> bool {
fn is_at_or_above_safe_tick(&self, tick: i32) -> bool {
let largest = self.ticks[self.ticks.len() - 1].index;
let maximum = largest + self.tick_spacing as i32;
tick >= maximum
}

pub fn get_tick(&self, index: i32) -> Result<&TickInfo, TickListError> {
pub(super) fn get_tick(&self, index: i32) -> Result<&TickInfo, TickListError> {
match self
.ticks
.binary_search_by(|el| el.index.cmp(&index))
Expand All @@ -160,7 +162,7 @@ impl TickList {
}
}

pub fn next_initialized_tick(&self, index: i32, lte: bool) -> Result<&TickInfo, TickListError> {
fn next_initialized_tick(&self, index: i32, lte: bool) -> Result<&TickInfo, TickListError> {
if lte {
if self.is_below_smallest(index) {
return Err(TickListError { kind: TickListErrorKind::BelowSmallest });
Expand Down Expand Up @@ -194,7 +196,7 @@ impl TickList {
}
}

pub fn next_initialized_tick_within_one_word(
pub(super) fn next_initialized_tick_within_one_word(
&self,
tick: i32,
lte: bool,
Expand Down
12 changes: 6 additions & 6 deletions src/evm/protocol/uniswap_v3/tick_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ use crate::{
protocol::errors::SimulationError,
};

pub const MIN_TICK: i32 = -887272;
pub const MAX_TICK: i32 = 887272;
pub(super) const MIN_TICK: i32 = -887272;
pub(super) const MAX_TICK: i32 = 887272;

// MIN_SQRT_RATIO: 4295128739
pub const MIN_SQRT_RATIO: U256 = U256::from_limbs([4295128739u64, 0, 0, 0]);
pub(super) const MIN_SQRT_RATIO: U256 = U256::from_limbs([4295128739u64, 0, 0, 0]);

// MAX_SQRT_RATIO: 1461446703485210103287273052203988822378723970342
pub const MAX_SQRT_RATIO: U256 =
pub(super) const MAX_SQRT_RATIO: U256 =
U256::from_limbs([6743328256752651558u64, 17280870778742802505u64, 4294805859u64, 0]);

pub fn get_sqrt_ratio_at_tick(tick: i32) -> Result<U256, SimulationError> {
pub(super) fn get_sqrt_ratio_at_tick(tick: i32) -> Result<U256, SimulationError> {
assert!(tick.abs() <= MAX_TICK);
let abs_tick = U256::from(tick.unsigned_abs());
let mut ratio = if abs_tick.bit(0) {
Expand Down Expand Up @@ -154,7 +154,7 @@ fn most_significant_bit(x: U256) -> usize {
x.bit_len() - 1
}

pub fn get_tick_at_sqrt_ratio(sqrt_price: U256) -> Result<i32, SimulationError> {
pub(super) fn get_tick_at_sqrt_ratio(sqrt_price: U256) -> Result<i32, SimulationError> {
assert!(sqrt_price >= MIN_SQRT_RATIO && sqrt_price < MAX_SQRT_RATIO);
let ratio_x128 = sqrt_price << 32;
let msb = most_significant_bit(ratio_x128);
Expand Down

0 comments on commit c40e5cc

Please sign in to comment.