Skip to content

Commit

Permalink
Impl, test and bench sqrt_price_math
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhuiluo committed Dec 31, 2023
1 parent 1dba589 commit c604524
Show file tree
Hide file tree
Showing 9 changed files with 720 additions and 22 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ criterion = "0.5.1"
name = "bit_math"
harness = false

[[bench]]
name = "sqrt_price_math"
harness = false

[[bench]]
name = "tick_math"
harness = false
2 changes: 1 addition & 1 deletion benches/bit_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ fn least_significant_bit_benchmark_ref(c: &mut Criterion) {
criterion_group!(
benches,
most_significant_bit_benchmark,
least_significant_bit_benchmark,
most_significant_bit_benchmark_ref,
least_significant_bit_benchmark,
least_significant_bit_benchmark_ref
);
criterion_main!(benches);
218 changes: 218 additions & 0 deletions benches/sqrt_price_math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
use alloy_primitives::{keccak256, U256};
use alloy_sol_types::SolValue;
use criterion::{criterion_group, criterion_main, Criterion};
use uniswap_v3_math::{sqrt_price_math, utils::ruint_to_u256};
use uniswap_v3_sdk_rs::utils::{
get_amount_0_delta, get_amount_0_delta_signed, get_amount_1_delta, get_amount_1_delta_signed,
get_next_sqrt_price_from_input, get_next_sqrt_price_from_output,
};

fn pseudo_random(seed: u64) -> U256 {
keccak256(seed.abi_encode()).into()
}

fn pseudo_random_128(seed: u64) -> u128 {
let s: U256 = keccak256(seed.abi_encode()).into();
u128::from_be_bytes(s.to_be_bytes::<32>()[..16].try_into().unwrap())
}

fn generate_inputs() -> Vec<(U256, u128, U256, bool)> {
(0u64..100)
.map(|i| {
(
pseudo_random(i),
pseudo_random_128(i.pow(2)),
pseudo_random(i.pow(3)),
i % 2 == 0,
)
})
.collect()
}

fn get_next_sqrt_price_from_input_benchmark(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_next_sqrt_price_from_input", |b| {
b.iter(|| {
for (sqrt_price_x_96, liquidity, amount, add) in &inputs {
let _ = get_next_sqrt_price_from_input(*sqrt_price_x_96, *liquidity, *amount, *add);
}
})
});
}

fn get_next_sqrt_price_from_input_benchmark_ref(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_next_sqrt_price_from_input_ref", |b| {
b.iter(|| {
for (sqrt_price_x_96, liquidity, amount, add) in &inputs {
let _ = sqrt_price_math::get_next_sqrt_price_from_input(
ruint_to_u256(*sqrt_price_x_96),
*liquidity,
ruint_to_u256(*amount),
*add,
);
}
})
});
}

fn get_next_sqrt_price_from_output_benchmark(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_next_sqrt_price_from_output", |b| {
b.iter(|| {
for (sqrt_price_x_96, liquidity, amount, add) in &inputs {
let _ =
get_next_sqrt_price_from_output(*sqrt_price_x_96, *liquidity, *amount, *add);
}
});
});
}

fn get_next_sqrt_price_from_output_benchmark_ref(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_next_sqrt_price_from_output_ref", |b| {
b.iter(|| {
for (sqrt_price_x_96, liquidity, amount, add) in &inputs {
let _ = sqrt_price_math::get_next_sqrt_price_from_output(
ruint_to_u256(*sqrt_price_x_96),
*liquidity,
ruint_to_u256(*amount),
*add,
);
}
});
});
}

fn get_amount_0_delta_benchmark(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_0_delta", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, round_up) in &inputs {
let _ =
get_amount_0_delta(*sqrt_ratio_a_x96, *sqrt_ratio_b_x96, *liquidity, *round_up);
}
});
});
}

fn get_amount_0_delta_benchmark_ref(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_0_delta_ref", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, round_up) in &inputs {
let _ = sqrt_price_math::_get_amount_0_delta(
ruint_to_u256(*sqrt_ratio_a_x96),
ruint_to_u256(*sqrt_ratio_b_x96),
*liquidity,
*round_up,
);
}
});
});
}

fn get_amount_1_delta_benchmark(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_1_delta", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, round_up) in &inputs {
let _ =
get_amount_1_delta(*sqrt_ratio_a_x96, *sqrt_ratio_b_x96, *liquidity, *round_up);
}
});
});
}

fn get_amount_1_delta_benchmark_ref(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_1_delta_ref", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, round_up) in &inputs {
let _ = sqrt_price_math::_get_amount_1_delta(
ruint_to_u256(*sqrt_ratio_a_x96),
ruint_to_u256(*sqrt_ratio_b_x96),
*liquidity,
*round_up,
);
}
});
});
}

fn get_amount_0_delta_signed_benchmark(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_0_delta_signed", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, _) in &inputs {
let _ = get_amount_0_delta_signed(
*sqrt_ratio_a_x96,
*sqrt_ratio_b_x96,
*liquidity as i128,
);
}
});
});
}

fn get_amount_0_delta_signed_benchmark_ref(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_0_delta_signed_ref", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, _) in &inputs {
let _ = sqrt_price_math::get_amount_0_delta(
ruint_to_u256(*sqrt_ratio_a_x96),
ruint_to_u256(*sqrt_ratio_b_x96),
*liquidity as i128,
);
}
});
});
}

fn get_amount_1_delta_signed_benchmark(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_1_delta_signed", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, _) in &inputs {
let _ = get_amount_1_delta_signed(
*sqrt_ratio_a_x96,
*sqrt_ratio_b_x96,
*liquidity as i128,
);
}
});
});
}

fn get_amount_1_delta_signed_benchmark_ref(c: &mut Criterion) {
let inputs = generate_inputs();
c.bench_function("get_amount_1_delta_signed_ref", |b| {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, _) in &inputs {
let _ = sqrt_price_math::get_amount_1_delta(
ruint_to_u256(*sqrt_ratio_a_x96),
ruint_to_u256(*sqrt_ratio_b_x96),
*liquidity as i128,
);
}
});
});
}

criterion_group!(
benches,
get_next_sqrt_price_from_input_benchmark,
get_next_sqrt_price_from_input_benchmark_ref,
get_next_sqrt_price_from_output_benchmark,
get_next_sqrt_price_from_output_benchmark_ref,
get_amount_0_delta_benchmark,
get_amount_0_delta_benchmark_ref,
get_amount_1_delta_benchmark,
get_amount_1_delta_benchmark_ref,
get_amount_0_delta_signed_benchmark,
get_amount_0_delta_signed_benchmark_ref,
get_amount_1_delta_signed_benchmark,
get_amount_1_delta_signed_benchmark_ref,
);
criterion_main!(benches);
2 changes: 1 addition & 1 deletion benches/tick_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ fn get_tick_at_sqrt_ratio_benchmark_ref(c: &mut Criterion) {
criterion_group!(
benches,
get_sqrt_ratio_at_tick_benchmark,
get_tick_at_sqrt_ratio_benchmark,
get_sqrt_ratio_at_tick_benchmark_ref,
get_tick_at_sqrt_ratio_benchmark,
get_tick_at_sqrt_ratio_benchmark_ref
);
criterion_main!(benches);
10 changes: 4 additions & 6 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use alloy_primitives::{Address, B256, U256};
use num_bigint::BigUint;
use once_cell::sync::Lazy;
use std::str::FromStr;
use uniswap_sdk_core_rust::entities::{
base_currency::BaseCurrency,
currency::CurrencyTrait,
fractions::{currency_amount::CurrencyAmount, price::Price},
token::Token,
use uniswap_sdk_core_rust::{
entities::fractions::price::Price,
prelude::{BaseCurrency, CurrencyAmount, CurrencyTrait, Token},
};

static Q192: Lazy<BigUint> = Lazy::new(|| {
Expand Down Expand Up @@ -177,7 +175,7 @@ impl Pool {
#[cfg(test)]
mod tests {
use super::*;
use uniswap_sdk_core_rust::entities::weth9::WETH9;
use uniswap_sdk_core_rust::prelude::WETH9;

const ONE_ETHER: U256 = U256::from_limbs([10u64.pow(18), 0, 0, 0]);

Expand Down
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod liquidity_math;
mod nearest_usable_tick;
mod position;
mod price_tick_conversions;
mod sqrt_price_math;
mod tick_math;

use alloy_primitives::U256;
Expand All @@ -17,6 +18,7 @@ pub use liquidity_math::add_delta;
pub use nearest_usable_tick::nearest_usable_tick;
pub use position::get_tokens_owed;
pub use price_tick_conversions::*;
pub use sqrt_price_math::*;
pub use tick_math::*;

pub const Q96: U256 = U256::from_limbs([0, 4294967296, 0, 0]);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/price_tick_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use alloy_primitives::U256;
use anyhow::Result;
use num_bigint::BigUint;
use num_traits::ToBytes;
use uniswap_sdk_core_rust::entities::{
fractions::{fraction::FractionTrait, price::Price},
token::Token,
use uniswap_sdk_core_rust::{
entities::fractions::price::Price,
prelude::{FractionTrait, Token},
};

/// Returns a price object corresponding to the input tick and the base/quote token.
Expand Down
Loading

0 comments on commit c604524

Please sign in to comment.