Skip to content

Commit

Permalink
refactor: add TickListError and refactor tick list handling methods (
Browse files Browse the repository at this point in the history
…#95)

* refactor: add `TickListError` and refactor tick list handling methods

Introduce a new `TickListError` enum to handle various tick list errors. Methods within `TickList` and `TickDataProvider` are refactored to utilize the new error type, ensuring better error handling and more robust code. Tests are updated to reflect these changes.

* Restructure test organization in tick_list.rs

Group related test cases within modules to enhance readability and maintenance. Also, rename test functions for better clarity and update Cargo.toml version in README.md.
  • Loading branch information
shuhuiluo authored Oct 20, 2024
1 parent 143e4e9 commit a90ad4b
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 293 deletions.
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"
version = "2.1.1"
version = "2.2.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ It is feature-complete with unit tests matching the TypeScript SDK.
Add the following to your `Cargo.toml` file:

```toml
uniswap-v3-sdk = { version = "2.1.1", features = ["extensions", "std"] }
uniswap-v3-sdk = { version = "2.2.0", features = ["extensions", "std"] }
```

### Usage
Expand Down
13 changes: 4 additions & 9 deletions src/entities/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ impl<TP: TickDataProvider> Position<TP> {
)?
.to_big_int(),
)
.map_err(Error::Core)
} else if self.pool.tick_current < self.tick_upper {
CurrencyAmount::from_raw_amount(
self.pool.token0.clone(),
Expand All @@ -137,11 +136,10 @@ impl<TP: TickDataProvider> Position<TP> {
)?
.to_big_int(),
)
.map_err(Error::Core)
} else {
CurrencyAmount::from_raw_amount(self.pool.token0.clone(), BigInt::zero())
.map_err(Error::Core)
}
.map_err(Error::Core)
}

/// Returns the amount of token0 that this position's liquidity could be burned for at the
Expand All @@ -162,7 +160,6 @@ impl<TP: TickDataProvider> Position<TP> {
pub fn amount1(&self) -> Result<CurrencyAmount<Token>, Error> {
if self.pool.tick_current < self.tick_lower {
CurrencyAmount::from_raw_amount(self.pool.token1.clone(), BigInt::zero())
.map_err(Error::Core)
} else if self.pool.tick_current < self.tick_upper {
CurrencyAmount::from_raw_amount(
self.pool.token1.clone(),
Expand All @@ -174,7 +171,6 @@ impl<TP: TickDataProvider> Position<TP> {
)?
.to_big_int(),
)
.map_err(Error::Core)
} else {
CurrencyAmount::from_raw_amount(
self.pool.token1.clone(),
Expand All @@ -186,8 +182,8 @@ impl<TP: TickDataProvider> Position<TP> {
)?
.to_big_int(),
)
.map_err(Error::Core)
}
.map_err(Error::Core)
}

/// Returns the amount of token1 that this position's liquidity could be burned for at the
Expand Down Expand Up @@ -216,9 +212,8 @@ impl<TP: TickDataProvider> Position<TP> {
fn ratios_after_slippage(&self, slippage_tolerance: &Percent) -> (U160, U160) {
let one = Percent::new(1, 1);
let token0_price = self.pool.token0_price().as_fraction();
let price_lower =
token0_price.clone() * ((one.clone() - slippage_tolerance.clone()).as_fraction());
let price_upper = token0_price * ((one + slippage_tolerance.clone()).as_fraction());
let price_lower = (one.clone() - slippage_tolerance).as_fraction() * &token0_price;
let price_upper = token0_price * ((one + slippage_tolerance).as_fraction());

let mut sqrt_ratio_x96_lower =
encode_sqrt_ratio_x96(price_lower.numerator, price_lower.denominator);
Expand Down
7 changes: 5 additions & 2 deletions src/entities/tick_list_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ mod tests {
}

#[test]
#[should_panic(expected = "NOT_CONTAINED")]
#[cfg(not(feature = "extensions"))]
fn throws_if_tick_not_in_list() {
PROVIDER.get_tick(0).unwrap();
assert_eq!(
PROVIDER.get_tick(0).unwrap_err(),
TickListError::NotContained.into()
);
}

#[test]
Expand Down
14 changes: 14 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ pub enum Error {
#[cfg(feature = "extensions")]
#[cfg_attr(feature = "std", error("{0}"))]
LensError(#[cfg_attr(not(feature = "std"), from)] LensError),

#[cfg_attr(feature = "std", error("{0}"))]
TickListError(#[cfg_attr(not(feature = "std"), from)] TickListError),
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum TickListError {
#[cfg_attr(feature = "std", error("Below smallest tick"))]
BelowSmallest,
#[cfg_attr(feature = "std", error("At or above largest tick"))]
AtOrAboveLargest,
#[cfg_attr(feature = "std", error("Not contained in tick list"))]
NotContained,
}
Loading

0 comments on commit a90ad4b

Please sign in to comment.