Skip to content

Commit

Permalink
Convert functions to const in math and tick utils
Browse files Browse the repository at this point in the history
Functions in the `sqrt_price_math.rs` and `nearest_usable_tick.rs` files, as well as the `constants.rs` file, were changed from regular to `const` functions. This provides the capacity to have these functions evaluated at compile time wherever their usage throughout the codebase makes it possible for constant evaluation. Also, updated the tick comparison in `nearest_usable_tick.rs` which ensures more readability.
  • Loading branch information
shuhuiluo committed Jan 1, 2024
1 parent 95f9b78 commit 1b10770
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
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
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

0 comments on commit 1b10770

Please sign in to comment.