Skip to content

Commit

Permalink
Merge pull request #1 from shuhuiluo/tick_list
Browse files Browse the repository at this point in the history
Add `TickList` trait and tests
  • Loading branch information
shuhuiluo authored Jan 1, 2024
2 parents 4e92c9c + 1b10770 commit 200ec9f
Show file tree
Hide file tree
Showing 10 changed files with 440 additions and 36 deletions.
60 changes: 30 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-v3-sdk-rs"
version = "0.3.0"
version = "0.4.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum FeeAmount {

impl FeeAmount {
/// The default factory tick spacings by fee amount.
pub fn tick_spacing(&self) -> i32 {
pub const fn tick_spacing(&self) -> i32 {
match self {
Self::LOWEST => 1,
Self::LOW => 10,
Expand Down
6 changes: 5 additions & 1 deletion src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
pub mod pool;
mod pool;
mod tick;

pub use pool::Pool;
pub use tick::{Tick, TickTrait};
58 changes: 58 additions & 0 deletions src/entities/tick.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::utils::{MAX_TICK, MIN_TICK};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Tick {
pub index: i32,
pub liquidity_gross: u128,
pub liquidity_net: i128,
}

pub trait TickTrait: PartialOrd {
fn index(&self) -> i32;

fn liquidity_gross(&self) -> u128;

fn liquidity_net(&self) -> i128;
}

impl TickTrait for Tick {
fn index(&self) -> i32 {
self.index
}

fn liquidity_gross(&self) -> u128 {
self.liquidity_gross
}

fn liquidity_net(&self) -> i128 {
self.liquidity_net
}
}

impl Tick {
pub const fn new(index: i32, liquidity_gross: u128, liquidity_net: i128) -> Self {
assert!(index >= MIN_TICK && index <= MAX_TICK, "TICK");
Self {
index,
liquidity_gross,
liquidity_net,
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic(expected = "TICK")]
fn test_tick_below_min_tick() {
Tick::new(MIN_TICK - 1, 0, 0);
}

#[test]
#[should_panic(expected = "TICK")]
fn test_tick_above_max_tick() {
Tick::new(MAX_TICK + 1, 0, 0);
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(int_roundings)]
#![feature(is_sorted)]
//! # v3-sdk-rs
//!
//! Migration of Uniswap V3 SDK to Rust
Expand Down
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod position;
mod price_tick_conversions;
mod sqrt_price_math;
mod swap_math;
mod tick_list;
mod tick_math;

pub use bit_math::*;
Expand All @@ -22,6 +23,7 @@ pub use position::get_tokens_owed;
pub use price_tick_conversions::*;
pub use sqrt_price_math::*;
pub use swap_math::compute_swap_step;
pub use tick_list::TickList;
pub use tick_math::*;

use alloy_primitives::U256;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/nearest_usable_tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use super::tick_math::{MAX_TICK, MIN_TICK};
///
/// returns: i32
///
pub fn nearest_usable_tick(tick: i32, tick_spacing: i32) -> i32 {
pub const fn nearest_usable_tick(tick: i32, tick_spacing: i32) -> i32 {
assert!(tick_spacing > 0, "TICK_SPACING");
assert!((MIN_TICK..=MAX_TICK).contains(&tick), "TICK_BOUND");
assert!(tick >= MIN_TICK && tick <= MAX_TICK, "TICK_BOUND");
let rounded = tick.div_floor(tick_spacing) * tick_spacing;
let rounded = rounded + (tick - rounded + tick_spacing / 2) / tick_spacing * tick_spacing;
if rounded < MIN_TICK {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/sqrt_price_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn to_uint160(x: U256) -> Result<U256, UniswapV3MathError> {
}
}

fn to_uint256(x: u128) -> U256 {
const fn to_uint256(x: u128) -> U256 {
U256::from_limbs([x as u64, (x >> 64) as u64, 0, 0])
}

Expand Down
Loading

0 comments on commit 200ec9f

Please sign in to comment.