Skip to content

Commit

Permalink
chore: Refactor code and update dependencies (#55)
Browse files Browse the repository at this point in the history
Modified various invocations and calculations to remove unnecessary conversions to ethers, yielding better clarity and performance. Adjusted test cases to adapt these changes. Updated dependencies to recent versions. Extended the test coverage to contain not only the core library but also extensions. Lastly, configured use of API key for accessing Ethereum Mainnet via Infura, ensuring this key is not committed into the repository.
  • Loading branch information
shuhuiluo authored Jun 24, 2024
1 parent 97ade98 commit 6ed68af
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 72 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

env:
CARGO_TERM_COLOR: always
INFURA_API_KEY: ${{ secrets.INFURA_API_KEY }}

jobs:
lint:
Expand Down Expand Up @@ -59,5 +60,7 @@ jobs:
${{ runner.os }}-cargo-registry-
- name: Build
run: cargo build --all-features
- name: Run tests
run: cargo test --all-features
- name: Run tests for core library
run: cargo test
- name: Run tests for extensions
run: cargo test --all-features --lib extensions -- --test-threads=1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ Cargo.lock
# IDEs
.idea
.vscode

.env*
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-v3-sdk"
version = "0.29.2"
version = "0.30.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand All @@ -14,8 +14,8 @@ exclude = [".github", ".gitignore", "rustfmt.toml"]
all-features = true

[dependencies]
alloy-primitives = "0.7"
alloy-sol-types = "0.7"
alloy-primitives = "0.7.6"
alloy-sol-types = "0.7.6"
anyhow = "1.0"
aperture-lens = { version = "0.4", optional = true }
base64 = { version = "0.22", optional = true }
Expand All @@ -30,14 +30,15 @@ regex = { version = "1.10", optional = true }
ruint = "1.12"
serde_json = { version = "1.0", optional = true }
thiserror = "1.0"
uniswap-sdk-core = "0.21.0"
uniswap_v3_math = "0.4"
uniswap-sdk-core = "0.23.0"
uniswap_v3_math = "0.5.1"

[features]
extensions = ["aperture-lens", "base64", "ethers", "regex", "serde_json"]

[dev-dependencies]
criterion = "0.5.1"
dotenv = "0.15.0"
ethers = "2.0"
tokio = { version = "1.37", features = ["full"] }

Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ expect. The error handling is still a work in progress.
Add the following to your `Cargo.toml` file:

```toml
uniswap-v3-sdk = { version = "0.25.0", features = ["extensions"] }
uniswap-v3-sdk = { version = "0.30.0", features = ["extensions"] }
```

### Usage
Expand All @@ -64,7 +64,19 @@ Contributions are welcome. Please open an issue if you have any questions or sug

### Testing

Tests are run with `cargo test`. To test a specific module, use `cargo test --test <module_name>`.
Tests are run with

```shell
cargo test
```

for the core library. To run the tests for the extensions, use

```shell
cargo test --all-features --lib extensions -- --test-threads=1
```

To test a specific module, use `cargo test --test <module_name>`.

### Linting

Expand Down
4 changes: 2 additions & 2 deletions benches/bit_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn most_significant_bit_benchmark_ref(c: &mut Criterion) {
c.bench_function("most_significant_bit_ref", |b| {
b.iter(|| {
for i in 1u8..=255 {
let _ = bit_math::most_significant_bit(U256::from(1).shl(i).to_ethers());
let _ = bit_math::most_significant_bit(U256::from(1).shl(i));
}
})
});
Expand All @@ -38,7 +38,7 @@ fn least_significant_bit_benchmark_ref(c: &mut Criterion) {
c.bench_function("least_significant_bit_ref", |b| {
b.iter(|| {
for i in 1u8..=255 {
let _ = bit_math::least_significant_bit(U256::from(1).shl(i).to_ethers());
let _ = bit_math::least_significant_bit(U256::from(1).shl(i));
}
});
});
Expand Down
24 changes: 12 additions & 12 deletions benches/sqrt_price_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ fn get_next_sqrt_price_from_input_benchmark_ref(c: &mut Criterion) {
b.iter(|| {
for (sqrt_price_x_96, liquidity, amount, add) in &inputs {
let _ = sqrt_price_math::get_next_sqrt_price_from_input(
sqrt_price_x_96.to_ethers(),
*sqrt_price_x_96,
*liquidity,
amount.to_ethers(),
*amount,
*add,
);
}
Expand All @@ -71,9 +71,9 @@ fn get_next_sqrt_price_from_output_benchmark_ref(c: &mut Criterion) {
b.iter(|| {
for (sqrt_price_x_96, liquidity, amount, add) in &inputs {
let _ = sqrt_price_math::get_next_sqrt_price_from_output(
sqrt_price_x_96.to_ethers(),
*sqrt_price_x_96,
*liquidity,
amount.to_ethers(),
*amount,
*add,
);
}
Expand All @@ -99,8 +99,8 @@ fn get_amount_0_delta_benchmark_ref(c: &mut Criterion) {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, round_up) in &inputs {
let _ = sqrt_price_math::_get_amount_0_delta(
sqrt_ratio_a_x96.to_ethers(),
sqrt_ratio_b_x96.to_ethers(),
*sqrt_ratio_a_x96,
*sqrt_ratio_b_x96,
*liquidity,
*round_up,
);
Expand All @@ -127,8 +127,8 @@ fn get_amount_1_delta_benchmark_ref(c: &mut Criterion) {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, round_up) in &inputs {
let _ = sqrt_price_math::_get_amount_1_delta(
sqrt_ratio_a_x96.to_ethers(),
sqrt_ratio_b_x96.to_ethers(),
*sqrt_ratio_a_x96,
*sqrt_ratio_b_x96,
*liquidity,
*round_up,
);
Expand Down Expand Up @@ -158,8 +158,8 @@ fn get_amount_0_delta_signed_benchmark_ref(c: &mut Criterion) {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, _) in &inputs {
let _ = sqrt_price_math::get_amount_0_delta(
sqrt_ratio_a_x96.to_ethers(),
sqrt_ratio_b_x96.to_ethers(),
*sqrt_ratio_a_x96,
*sqrt_ratio_b_x96,
*liquidity as i128,
);
}
Expand Down Expand Up @@ -188,8 +188,8 @@ fn get_amount_1_delta_signed_benchmark_ref(c: &mut Criterion) {
b.iter(|| {
for (sqrt_ratio_a_x96, liquidity, sqrt_ratio_b_x96, _) in &inputs {
let _ = sqrt_price_math::get_amount_1_delta(
sqrt_ratio_a_x96.to_ethers(),
sqrt_ratio_b_x96.to_ethers(),
*sqrt_ratio_a_x96,
*sqrt_ratio_b_x96,
*liquidity as i128,
);
}
Expand Down
15 changes: 1 addition & 14 deletions benches/swap_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,7 @@ fn compute_swap_step_benchmark(c: &mut Criterion) {
}

fn compute_swap_step_benchmark_ref(c: &mut Criterion) {
use ethers::types::{I256, U256};

let inputs: Vec<(U256, U256, u128, I256, u32)> = generate_inputs()
.into_iter()
.map(|i| {
(
i.0.to_ethers(),
i.1.to_ethers(),
i.2,
I256::from_raw(i.3.into_raw().to_ethers()),
i.4,
)
})
.collect();
let inputs: Vec<(U256, U256, u128, I256, u32)> = generate_inputs();
c.bench_function("compute_swap_step_ref", |b| {
b.iter(|| {
for (
Expand Down
2 changes: 1 addition & 1 deletion benches/tick_math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn get_tick_at_sqrt_ratio_benchmark_ref(c: &mut Criterion) {
c.bench_function("get_tick_at_sqrt_ratio_ref", |b| {
b.iter(|| {
for i in 33u8..=191 {
let _ = tick_math::get_tick_at_sqrt_ratio(U256::from(1).shl(i).to_ethers());
let _ = tick_math::get_tick_at_sqrt_ratio(U256::from(1).shl(i));
}
});
});
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/ephemeral_tick_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ impl From<EphemeralTickDataProvider> for TickListDataProvider {
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::*;
use alloy_primitives::address;
use ethers::prelude::{Provider, MAINNET};
use ethers::prelude::Provider;

const TICK_SPACING: i32 = 10;

Expand All @@ -98,7 +99,7 @@ mod tests {
async fn test_ephemeral_tick_data_provider() -> Result<()> {
let provider = EphemeralTickDataProvider::new(
address!("88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
Arc::new(Provider::new_client(MAINNET.provider().url().as_str(), 3, 1000).unwrap()),
Arc::new(Provider::new_client(make_provider().await.url().as_str(), 3, 1000).unwrap()),
None,
None,
Some(BlockId::from(17000000)),
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub async fn get_liquidity_array_for_pool<M: Middleware, P>(
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::make_provider;
use alloy_primitives::address;

async fn pool() -> Pool<NoTickDataProvider> {
Expand All @@ -206,7 +207,7 @@ mod tests {
address!("2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"),
address!("C02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
FeeAmount::LOW,
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17000000)),
)
.await
Expand Down Expand Up @@ -235,7 +236,7 @@ mod tests {
pool,
tick_lower,
tick_upper,
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17000000)),
None,
None,
Expand Down
15 changes: 8 additions & 7 deletions src/extensions/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::make_provider;
use alloy_primitives::{address, uint};
use num_traits::Signed;

Expand All @@ -386,7 +387,7 @@ mod tests {
1,
NPM,
uint!(4_U256),
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17188000)),
)
.await
Expand All @@ -398,7 +399,7 @@ mod tests {

#[tokio::test]
async fn test_get_all_positions_by_owner() {
let client = Arc::new(MAINNET.provider());
let client = Arc::new(make_provider().await);
let block_id = BlockId::from(17188000);
let owner = address!("4bD047CA72fa05F0B89ad08FE5Ba5ccdC07DFFBF");
let positions = get_all_positions_by_owner(NPM, owner, client.clone(), Some(block_id))
Expand Down Expand Up @@ -433,7 +434,7 @@ mod tests {
1,
NPM,
uint!(4_U256),
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17188000)),
)
.await
Expand All @@ -447,7 +448,7 @@ mod tests {
let svg = get_token_svg(
NPM,
uint!(4_U256),
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17188000)),
)
.await
Expand All @@ -464,7 +465,7 @@ mod tests {
1,
NPM,
uint!(4_U256),
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17188000)),
)
.await
Expand Down Expand Up @@ -494,7 +495,7 @@ mod tests {
1,
NPM,
uint!(4_U256),
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17188000)),
)
.await
Expand Down Expand Up @@ -547,7 +548,7 @@ mod tests {
1,
NPM,
uint!(4_U256),
Arc::new(MAINNET.provider()),
Arc::new(make_provider().await),
Some(BlockId::from(17188000)),
)
.await
Expand Down
14 changes: 14 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::prelude::*;
use alloy_primitives::U256;
use dotenv::dotenv;
use ethers::prelude::*;
use once_cell::sync::Lazy;
use uniswap_sdk_core::{prelude::*, token};

Expand Down Expand Up @@ -119,3 +121,15 @@ pub fn make_pool(token0: Token, token1: Token) -> Pool<TickListDataProvider> {
)
.unwrap()
}

pub static RPC_URL: Lazy<String> = Lazy::new(|| {
dotenv().ok();
format!(
"https://mainnet.infura.io/v3/{}",
std::env::var("INFURA_API_KEY").unwrap()
)
});

pub async fn make_provider() -> Provider<Http> {
Provider::<Http>::connect(&RPC_URL).await
}
2 changes: 1 addition & 1 deletion src/utils/price_tick_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn tick_to_price(
/// ## Arguments
///
/// * `price`: for which to return the closest tick that represents a price less than or equal to
/// the input price, i.e. the price of the returned tick is less than or equal to the input price
/// the input price, i.e. the price of the returned tick is less than or equal to the input price
pub fn price_to_closest_tick(price: &Price<Token, Token>) -> Result<i32> {
let sorted = price.base_currency.sorts_before(&price.quote_currency)?;
let sqrt_ratio_x96 = if sorted {
Expand Down
Loading

0 comments on commit 6ed68af

Please sign in to comment.