Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: update error handling and dependencies #103

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 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 = "2.4.1"
version = "2.5.0"
edition = "2021"
authors = ["Shuhui Luo <twitter.com/aureliano_law>"]
description = "Uniswap V3 SDK for Rust"
Expand Down Expand Up @@ -28,14 +28,14 @@ once_cell = "1.20"
regex = { version = "1.11", optional = true }
rustc-hash = "2.0"
serde_json = { version = "1.0", optional = true }
thiserror = { version = "1.0", optional = true }
uniswap-lens = { version = "0.5", optional = true }
uniswap-sdk-core = "3.0.0"
thiserror = { version = "2", default-features = false }
uniswap-lens = { version = "0.6", optional = true }
uniswap-sdk-core = "3.1.0"

[features]
default = []
extensions = ["alloy", "anyhow", "base64", "regex", "serde_json", "uniswap-lens"]
std = ["alloy?/std", "thiserror", "uniswap-sdk-core/std", "uniswap-lens?/std"]
std = ["alloy?/std", "thiserror/std", "uniswap-sdk-core/std", "uniswap-lens?/std"]
shuhuiluo marked this conversation as resolved.
Show resolved Hide resolved

[dev-dependencies]
alloy-signer = "0.5"
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.4.0", features = ["extensions", "std"] }
uniswap-v3-sdk = { version = "2.5.0", features = ["extensions", "std"] }
```

### Usage
Expand Down
4 changes: 2 additions & 2 deletions src/entities/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ where
currency_amount_in.wrapped()?,
TradeType::ExactInput,
)?;
sorted_insert(best_trades, trade, max_num_results, trade_comparator)?;
sorted_insert(best_trades, trade, max_num_results, trade_comparator);
} else if max_hops > 1 && pools.len() > 1 {
let pools_excluding_this_pool = pools
.iter()
Expand Down Expand Up @@ -781,7 +781,7 @@ where
currency_amount_out.wrapped()?,
TradeType::ExactOutput,
)?;
sorted_insert(best_trades, trade, max_num_results, trade_comparator)?;
sorted_insert(best_trades, trade, max_num_results, trade_comparator);
} else if max_hops > 1 && pools.len() > 1 {
let pools_excluding_this_pool = pools
.iter()
Expand Down
55 changes: 27 additions & 28 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::missing_inline_in_public_items)]

#[cfg(doc)]
use crate::prelude::*;

Expand All @@ -7,78 +9,75 @@ use alloy::contract::Error as ContractError;
use uniswap_lens::error::Error as LensError;

use alloy_primitives::{aliases::I24, U160};
use derive_more::From;
use uniswap_sdk_core::error::Error as CoreError;

#[derive(Debug, From)]
#[derive(Debug, thiserror::Error)]
#[cfg_attr(not(feature = "extensions"), derive(Clone, Copy, Hash, PartialEq, Eq))]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum Error {
/// Thrown when an error occurs in the core library.
#[cfg_attr(feature = "std", error("{0}"))]
Core(#[cfg_attr(not(feature = "std"), from)] CoreError),
#[error("{0}")]
Core(#[from] CoreError),

/// Thrown when the token passed to [`Pool::price_of`] is not one of the pool's tokens.
#[cfg_attr(feature = "std", error("Invalid token"))]
#[error("Invalid token")]
InvalidToken,

/// Thrown when the tick passed to [`get_sqrt_ratio_at_tick`] is not between [`MIN_TICK`] and
/// [`MAX_TICK`].
#[cfg_attr(feature = "std", error("Invalid tick: {0}"))]
#[error("Invalid tick: {0}")]
InvalidTick(I24),

/// Thrown when the price passed to [`get_tick_at_sqrt_ratio`] does not correspond to a price
/// between [`MIN_TICK`] and [`MAX_TICK`].
#[cfg_attr(feature = "std", error("Invalid square root price: {0}"))]
#[error("Invalid square root price: {0}")]
InvalidSqrtPrice(U160),

#[cfg_attr(feature = "std", error("Invalid price or liquidity"))]
#[error("Invalid price or liquidity")]
InvalidPriceOrLiquidity,

#[cfg_attr(feature = "std", error("Invalid price"))]
#[error("Invalid price")]
InvalidPrice,

#[cfg(feature = "extensions")]
#[cfg_attr(feature = "std", error("Invalid tick range"))]
#[error("Invalid tick range")]
InvalidRange,

#[cfg_attr(feature = "std", error("Overflow in full math mulDiv"))]
#[error("Invalid tick range")]
MulDivOverflow,

#[cfg_attr(feature = "std", error("Overflow when adding liquidity delta"))]
#[error("Invalid tick range")]
shuhuiluo marked this conversation as resolved.
Show resolved Hide resolved
AddDeltaOverflow,

#[cfg_attr(feature = "std", error("Overflow when casting to U160"))]
#[error("Overflow when casting to U160")]
SafeCastToU160Overflow,

#[cfg_attr(feature = "std", error("Overflow in price calculation"))]
#[error("Overflow in price calculation")]
PriceOverflow,

#[cfg_attr(feature = "std", error("Insufficient liquidity"))]
#[error("Insufficient liquidity")]
InsufficientLiquidity,

#[cfg_attr(feature = "std", error("No tick data provider was given"))]
#[error("No tick data provider was given")]
NoTickDataError,

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

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

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

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