Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: v3 and v4 getters #128

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/evm/protocol/uniswap_v3/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TickInfo> {
self.ticks.get_ticks()
}

fn get_spacing(fee: FeeAmount) -> u16 {
match fee {
FeeAmount::Lowest => 1,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -363,11 +383,11 @@ impl ProtocolSim for UniswapV3State {
.as_any()
.downcast_ref::<UniswapV3State>()
{
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
}
Expand Down
36 changes: 34 additions & 2 deletions src/evm/protocol/uniswap_v4/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 get_one_for_zero(&self) -> u32 {
self.one_for_zero
}

pub fn get_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
Expand Down Expand Up @@ -80,14 +92,34 @@ 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<TickInfo> {
self.ticks.get_ticks()
}

fn swap(
&self,
zero_for_one: bool,
amount_specified: I256,
sqrt_price_limit: Option<U256>,
) -> Result<SwapResults, SimulationError> {
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
Expand Down Expand Up @@ -137,7 +169,7 @@ impl UniswapV4State {
u256_to_biguint(gas_used),
Box::new(new_state),
)),
));
))
}
_ => return Err(SimulationError::FatalError("Unknown error".to_string())),
},
Expand Down
16 changes: 16 additions & 0 deletions src/evm/protocol/utils/uniswap/tick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -162,6 +174,10 @@ impl TickList {
}
}

pub fn get_ticks(&self) -> &Vec<TickInfo> {
&self.ticks
}

fn next_initialized_tick(&self, index: i32, lte: bool) -> Result<&TickInfo, TickListError> {
if lte {
if self.is_below_smallest(index) {
Expand Down
Loading