Skip to content

Commit

Permalink
refactor: add derive_more and remove manual trait impls (#87)
Browse files Browse the repository at this point in the history
* refactor: add `derive_more` for `Deref` and remove manual impls

Replaced manual `Deref` implementations with the `derive_more` crate. This reduces redundancy and simplifies the code. Also updated Cargo.toml to include `derive_more` dependency.

* refactor: error handling using `derive_more::From`

Replaced manual `From` implementations with `derive_more::From` to reduce boilerplate. This change streamlines the `Error` enum by deriving `From` for core, contract, and lens errors.
  • Loading branch information
shuhuiluo authored Oct 7, 2024
1 parent be1c802 commit 15c7e88
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ alloy-sol-types = "0.8"
anyhow = { version = "1.0", optional = true }
base64 = { version = "0.22", optional = true }
bigdecimal = "0.4.5"
derive_more = { version = "1.0.0", features = ["deref", "from"] }
num-bigint = "0.4"
num-integer = "0.1"
num-traits = "0.2"
Expand Down
13 changes: 2 additions & 11 deletions src/entities/tick_list_data_provider.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::prelude::*;
use core::ops::Deref;
use derive_more::Deref;

/// A data provider for ticks that is backed by an in-memory array of ticks.
#[derive(Clone, Debug, Default, PartialEq)]
#[derive(Clone, Debug, Default, PartialEq, Deref)]
pub struct TickListDataProvider<I = i32>(Vec<Tick<I>>);

impl<I: TickIndex> TickListDataProvider<I> {
Expand All @@ -13,15 +13,6 @@ impl<I: TickIndex> TickListDataProvider<I> {
}
}

impl<I> Deref for TickListDataProvider<I> {
type Target = Vec<Tick<I>>;

#[inline]
fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
37 changes: 6 additions & 31 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ 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;

#[cfg_attr(
not(feature = "extensions"),
derive(Clone, Copy, Debug, Hash, PartialEq, Eq)
)]
#[cfg_attr(feature = "extensions", derive(Debug))]
#[derive(Debug, From)]
#[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(CoreError),
Core(#[cfg_attr(not(feature = "std"), 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"))]
Expand Down Expand Up @@ -64,32 +62,9 @@ pub enum Error {

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

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

impl From<CoreError> for Error {
#[inline]
fn from(error: CoreError) -> Self {
Self::Core(error)
}
}

#[cfg(feature = "extensions")]
impl From<ContractError> for Error {
#[inline]
fn from(error: ContractError) -> Self {
Self::ContractError(error)
}
}

#[cfg(feature = "extensions")]
impl From<LensError> for Error {
#[inline]
fn from(error: LensError) -> Self {
Self::LensError(error)
}
LensError(#[cfg_attr(not(feature = "std"), from)] LensError),
}
14 changes: 3 additions & 11 deletions src/extensions/ephemeral_tick_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
use crate::prelude::*;
use alloy::{eips::BlockId, providers::Provider, transports::Transport};
use alloy_primitives::{aliases::I24, Address};
use core::ops::Deref;
use derive_more::Deref;
use uniswap_lens::pool_lens;

/// A data provider that fetches ticks using an ephemeral contract in a single `eth_call`.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Deref)]
pub struct EphemeralTickDataProvider<I = I24> {
pub pool: Address,
pub tick_lower: I,
pub tick_upper: I,
pub tick_spacing: I,
pub block_id: Option<BlockId>,
#[deref]
pub ticks: Vec<Tick<I>>,
}

Expand Down Expand Up @@ -59,15 +60,6 @@ impl<I: TickIndex> EphemeralTickDataProvider<I> {
}
}

impl<I> Deref for EphemeralTickDataProvider<I> {
type Target = Vec<Tick<I>>;

#[inline]
fn deref(&self) -> &Self::Target {
&self.ticks
}
}

impl<I: TickIndex> From<EphemeralTickDataProvider<I>> for TickListDataProvider<I> {
#[inline]
fn from(provider: EphemeralTickDataProvider<I>) -> Self {
Expand Down
14 changes: 3 additions & 11 deletions src/extensions/ephemeral_tick_map_data_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@
use crate::prelude::*;
use alloy::{eips::BlockId, providers::Provider, transports::Transport};
use alloy_primitives::{aliases::I24, Address};
use core::ops::Deref;
use derive_more::Deref;

/// A data provider that fetches ticks using an ephemeral contract in a single `eth_call`.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deref)]
pub struct EphemeralTickMapDataProvider<I = I24> {
pub pool: Address,
pub tick_lower: I,
pub tick_upper: I,
pub tick_spacing: I,
pub block_id: Option<BlockId>,
#[deref]
pub tick_map: TickMap<I>,
}

Expand Down Expand Up @@ -44,15 +45,6 @@ impl<I: TickIndex> EphemeralTickMapDataProvider<I> {
}
}

impl<I> Deref for EphemeralTickMapDataProvider<I> {
type Target = TickMap<I>;

#[inline]
fn deref(&self) -> &Self::Target {
&self.tick_map
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 15c7e88

Please sign in to comment.