Skip to content

Commit

Permalink
chore(doc): Update code formatting and documentation (#41)
Browse files Browse the repository at this point in the history
* chore(doc): Update code formatting and documentation

Reformatted code to adhere to a maximum line width of 100 characters and better comment wrapping. Updated package version and certain dependency versions. Updated access modifiers for utility functions and entity functions to public. Improved code readability and maintainability without changing functionality.

* Update GitHub Actions cache key in Rust workflow

The cache key in the Rust GitHub Actions workflow has been updated. Previously, it was based on hashing the Cargo.lock file, but now this key is based on hashing the Cargo.toml file, as it offers more consistency.
  • Loading branch information
shuhuiluo authored Mar 4, 2024
1 parent 55a44fb commit 5730e4c
Show file tree
Hide file tree
Showing 26 changed files with 241 additions and 174 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.toml') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Install Rust toolchain via rustup
Expand Down Expand Up @@ -55,7 +55,7 @@ jobs:
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.toml') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Build
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "uniswap-v3-sdk"
version = "0.25.0"
version = "0.26.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand Down Expand Up @@ -30,7 +30,7 @@ regex = { version = "1.10", optional = true }
ruint = "1.12"
serde_json = { version = "1.0", optional = true }
thiserror = "1.0"
uniswap-sdk-core = "0.16"
uniswap-sdk-core = "0.18"
uniswap_v3_math = "0.4"

[features]
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ imports_granularity = "Crate"
max_width = 100
unstable_features = true
use_field_init_shorthand = true
wrap_comments = true
comment_width = 100
14 changes: 7 additions & 7 deletions src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod pool;
mod position;
mod route;
mod tick;
mod tick_data_provider;
mod tick_list_data_provider;
mod trade;
pub mod pool;
pub mod position;
pub mod route;
pub mod tick;
pub mod tick_data_provider;
pub mod tick_list_data_provider;
pub mod trade;

pub use pool::{get_address, Pool};
pub use position::{MintAmounts, Position};
Expand Down
31 changes: 20 additions & 11 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ impl Pool<NoTickDataProvider> {
///
/// * `token_a`: One of the tokens in the pool
/// * `token_b`: The other token in the pool
/// * `fee`: The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
/// * `fee`: The fee in hundredths of a bips of the input amount of every swap that is collected
/// by the pool
/// * `sqrt_ratio_x96`: The sqrt of the current ratio of amounts of token1 to token0
/// * `liquidity`: The current value of in range liquidity
/// * `tick_current`: The current tick of the pool
Expand Down Expand Up @@ -142,7 +143,8 @@ impl<P> Pool<P> {
self.token0.equals(token) || self.token1.equals(token)
}

/// Returns the current mid price of the pool in terms of token0, i.e. the ratio of token1 over token0
/// Returns the current mid price of the pool in terms of token0, i.e. the ratio of token1 over
/// token0
pub fn token0_price(&self) -> Price<Token, Token> {
let sqrt_ratio_x96: BigUint = u256_to_big_uint(self.sqrt_ratio_x96);
Price::new(
Expand All @@ -153,7 +155,8 @@ impl<P> Pool<P> {
)
}

/// Returns the current mid price of the pool in terms of token1, i.e. the ratio of token0 over token1
/// Returns the current mid price of the pool in terms of token1, i.e. the ratio of token0 over
/// token1
pub fn token1_price(&self) -> Price<Token, Token> {
let sqrt_ratio_x96: BigUint = u256_to_big_uint(self.sqrt_ratio_x96);
Price::new(
Expand Down Expand Up @@ -192,7 +195,8 @@ where
///
/// * `token_a`: One of the tokens in the pool
/// * `token_b`: The other token in the pool
/// * `fee`: The fee in hundredths of a bips of the input amount of every swap that is collected by the pool
/// * `fee`: The fee in hundredths of a bips of the input amount of every swap that is collected
/// by the pool
/// * `sqrt_ratio_x96`: The sqrt of the current ratio of amounts of token1 to token0
/// * `liquidity`: The current value of in range liquidity
/// * `tick_current`: The current tick of the pool
Expand Down Expand Up @@ -221,7 +225,8 @@ where
})
}

/// Given an input amount of a token, return the computed output amount, and a pool with state updated after the trade
/// Given an input amount of a token, return the computed output amount, and a pool with state
/// updated after the trade
///
/// ## Arguments
///
Expand Down Expand Up @@ -261,13 +266,15 @@ where
))
}

/// Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
/// Given a desired output amount of a token, return the computed input amount and a pool with
/// state updated after the trade
///
/// ## Arguments
///
/// * `output_amount`: the output amount for which to quote the input amount
/// * `sqrt_price_limit_x96`: The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value
/// after the swap. If one for zero, the price cannot be greater than this value after the swap
/// * `sqrt_price_limit_x96`: The Q64.96 sqrt price limit. If zero for one, the price cannot be
/// less than this value after the swap. If one for zero, the price cannot be greater than
/// this value after the swap
///
/// returns: The input amount and the pool with updated state
pub fn get_input_amount(
Expand Down Expand Up @@ -350,8 +357,9 @@ where
};

step.sqrt_price_start_x96 = state.sqrt_price_x96;
// because each iteration of the while loop rounds, we can't optimize this code (relative to the smart contract)
// by simply traversing to the next available tick, we instead need to exactly replicate
// because each iteration of the while loop rounds, we can't optimize this code
// (relative to the smart contract) by simply traversing to the next available tick, we
// instead need to exactly replicate
(step.tick_next, step.initialized) = self
.tick_data_provider
.next_initialized_tick_within_one_word(
Expand Down Expand Up @@ -414,7 +422,8 @@ where
}
state.tick = step.tick_next - zero_for_one as i32;
} else {
// recompute unless we're on a lower tick boundary (i.e. already transitioned ticks), and haven't moved
// recompute unless we're on a lower tick boundary (i.e. already transitioned
// ticks), and haven't moved
state.tick = get_tick_at_sqrt_ratio(state.sqrt_price_x96)?;
}
}
Expand Down
42 changes: 24 additions & 18 deletions src/entities/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ impl<P> Position<P> {
)
}

/// Returns the amount of token0 that this position's liquidity could be burned for at the current pool price
/// Returns the amount of token0 that this position's liquidity could be burned for at the
/// current pool price
pub fn amount0(&mut self) -> Result<CurrencyAmount<Token>> {
if self._token0_amount.is_none() {
if self.pool.tick_current < self.tick_lower {
Expand Down Expand Up @@ -123,7 +124,8 @@ impl<P> Position<P> {
Ok(self._token0_amount.clone().unwrap())
}

/// Returns the amount of token1 that this position's liquidity could be burned for at the current pool price
/// Returns the amount of token1 that this position's liquidity could be burned for at the
/// current pool price
pub fn amount1(&mut self) -> Result<CurrencyAmount<Token>> {
if self._token1_amount.is_none() {
if self.pool.tick_current < self.tick_lower {
Expand Down Expand Up @@ -156,11 +158,13 @@ impl<P> Position<P> {
Ok(self._token1_amount.clone().unwrap())
}

/// Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance percentage
/// Returns the lower and upper sqrt ratios if the price 'slips' up to slippage tolerance
/// percentage
///
/// ## Arguments
///
/// * `slippage_tolerance`: The amount by which the price can 'slip' before the transaction will revert
/// * `slippage_tolerance`: The amount by which the price can 'slip' before the transaction will
/// revert
///
/// ## Returns
///
Expand All @@ -187,7 +191,8 @@ impl<P> Position<P> {
(sqrt_ratio_x96_lower, sqrt_ratio_x96_upper)
}

/// Returns the minimum amounts that must be sent in order to safely mint the amount of liquidity held by the position
/// Returns the minimum amounts that must be sent in order to safely mint the amount of
/// liquidity held by the position
///
/// ## Arguments
///
Expand Down Expand Up @@ -220,7 +225,8 @@ impl<P> Position<P> {
0, // liquidity doesn't matter
)?;

// Because the router is imprecise, we need to calculate the position that will be created (assuming no slippage)
// Because the router is imprecise, we need to calculate the position that will be created
// (assuming no slippage)
let MintAmounts { amount0, amount1 } = self.mint_amounts()?;
let position_that_will_be_created = Position::from_amounts(
Pool::new(
Expand Down Expand Up @@ -260,8 +266,8 @@ impl<P> Position<P> {
Ok(MintAmounts { amount0, amount1 })
}

/// Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
/// position with the given slippage tolerance
/// Returns the minimum amounts that should be requested in order to safely burn the amount of
/// liquidity held by the position with the given slippage tolerance
///
/// ## Arguments
///
Expand Down Expand Up @@ -304,8 +310,8 @@ impl<P> Position<P> {
Ok((big_int_to_u256(amount0), big_int_to_u256(amount1)))
}

/// Returns the minimum amounts that must be sent in order to mint the amount of liquidity held by the position at
/// the current price for the pool
/// Returns the minimum amounts that must be sent in order to mint the amount of liquidity held
/// by the position at the current price for the pool
pub fn mint_amounts(&mut self) -> Result<MintAmounts> {
if self._mint_amounts.is_none() {
if self.pool.tick_current < self.tick_lower {
Expand Down Expand Up @@ -358,8 +364,8 @@ impl<P> Position<P> {
/// * `tick_upper`: The upper tick of the position
/// * `amount0`: token0 amount
/// * `amount1`: token1 amount
/// * `use_full_precision`: If false, liquidity will be maximized according to what the router can calculate,
/// not what core can theoretically support
/// * `use_full_precision`: If false, liquidity will be maximized according to what the router
/// can calculate, not what core can theoretically support
///
/// ## Returns
///
Expand Down Expand Up @@ -390,17 +396,17 @@ impl<P> Position<P> {
))
}

/// Computes a position with the maximum amount of liquidity received for a given amount of token0,
/// assuming an unlimited amount of token1
/// Computes a position with the maximum amount of liquidity received for a given amount of
/// token0, assuming an unlimited amount of token1
///
/// ## Arguments
///
/// * `pool`: The pool for which the position is created
/// * `tick_lower`: The lower tick
/// * `tick_upper`: The upper tick
/// * `amount0`: The desired amount of token0
/// * `use_full_precision`: If true, liquidity will be maximized according to what the router can calculate,
/// not what core can theoretically support
/// * `use_full_precision`: If true, liquidity will be maximized according to what the router
/// can calculate, not what core can theoretically support
pub fn from_amount0(
pool: Pool<P>,
tick_lower: i32,
Expand All @@ -418,8 +424,8 @@ impl<P> Position<P> {
)
}

/// Computes a position with the maximum amount of liquidity received for a given amount of token1,
/// assuming an unlimited amount of token0
/// Computes a position with the maximum amount of liquidity received for a given amount of
/// token1, assuming an unlimited amount of token0
///
/// ## Arguments
///
Expand Down
4 changes: 2 additions & 2 deletions src/entities/tick_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ pub trait TickDataProvider: Clone {
#[error("No tick data provider was given")]
pub struct NoTickDataError;

/// This tick data provider does not know how to fetch any tick data. It throws whenever it is required.
/// Useful if you do not need to load tick data for your use case.
/// This tick data provider does not know how to fetch any tick data. It throws whenever it is
/// required. Useful if you do not need to load tick data for your use case.
#[derive(Clone, Debug)]
pub struct NoTickDataProvider;

Expand Down
Loading

0 comments on commit 5730e4c

Please sign in to comment.