Skip to content

Commit

Permalink
refactor: loosen trait bound for TickDataProvider (#135)
Browse files Browse the repository at this point in the history
Removed unnecessary `Clone` trait bound from multiple implementations and functions, streamlining the code where cloning wasn't needed. Updated tests and function signatures accordingly to ensure consistency. Also incremented the package version to reflect these changes.
  • Loading branch information
shuhuiluo authored Feb 9, 2025
1 parent 72d0193 commit 29f5b2f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ std = [
"base64?/std",
"derive_more/std",
"fastnum/std",
"once_cell/std",
"once_cell?/std",
"serde_json?/std",
"thiserror/std",
"uniswap-lens?/std",
Expand Down
2 changes: 0 additions & 2 deletions src/entities/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ impl<TP: TickDataProvider> Pool<TP> {
sqrt_price_limit_x96,
)
}
}

impl<TP: Clone + TickDataProvider> Pool<TP> {
/// Given an input amount of a token, return the computed output amount
///
/// ## Arguments
Expand Down
14 changes: 7 additions & 7 deletions src/entities/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,7 @@ where
self.minimum_amount_out_cached(slippage_tolerance, None)?,
))
}
}

impl<TInput, TOutput, TP> Trade<TInput, TOutput, TP>
where
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: Clone + TickDataProvider,
{
/// Constructs an exact in trade with the given amount in and route
///
/// ## Arguments
Expand Down Expand Up @@ -619,7 +612,14 @@ where
}
Self::new(populated_routes, trade_type)
}
}

impl<TInput, TOutput, TP> Trade<TInput, TOutput, TP>
where
TInput: BaseCurrency,
TOutput: BaseCurrency,
TP: Clone + TickDataProvider,
{
/// Given a list of pools, and a fixed amount in, returns the top `max_num_results` trades that
/// go from an input token amount to an output token, making at most `max_hops` hops.
///
Expand Down
28 changes: 14 additions & 14 deletions src/extensions/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ where
/// * `new_tick_upper`: The new upper tick.
#[inline]
pub fn get_rebalanced_position<TP>(
position: &mut Position<TP>,
mut position: Position<TP>,
new_tick_lower: TP::Index,
new_tick_upper: TP::Index,
) -> Result<Position<TP>, Error>
where
TP: Clone + TickDataProvider,
TP: TickDataProvider,
{
let price = position.pool.token0_price();
// Calculate the position equity denominated in token1 before rebalance.
Expand All @@ -414,7 +414,7 @@ where
// token0's equity denominated in token1 divided by the price
let amount0_after = (equity_before - amount1_after) / price;
Position::from_amounts(
position.pool.clone(),
position.pool,
new_tick_lower,
new_tick_upper,
U256::from_big_uint(amount0_after.to_big_uint()),
Expand Down Expand Up @@ -470,10 +470,10 @@ pub fn get_rebalanced_position_at_price<TP>(
new_tick_upper: TP::Index,
) -> Result<Position<TP>, Error>
where
TP: Clone + TickDataProvider,
TP: TickDataProvider,
{
get_rebalanced_position(
&mut get_position_at_price(position, new_price)?,
get_position_at_price(position, new_price)?,
new_tick_lower,
new_tick_upper,
)
Expand Down Expand Up @@ -571,17 +571,17 @@ mod tests {

#[tokio::test]
async fn test_get_rebalanced_position() {
let mut position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
let position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
.await
.unwrap();
// rebalance to an out of range position
let new_tick_lower = position.tick_upper;
let new_tick_upper = new_tick_lower + 10 * FeeAmount::MEDIUM.tick_spacing().as_i32();
let mut new_position =
get_rebalanced_position(&mut position, new_tick_lower, new_tick_upper).unwrap();
let new_position =
get_rebalanced_position(position.clone(), new_tick_lower, new_tick_upper).unwrap();
assert!(new_position.amount1().unwrap().quotient().is_zero());
let reverted_position =
get_rebalanced_position(&mut new_position, position.tick_lower, position.tick_upper)
get_rebalanced_position(new_position, position.tick_lower, position.tick_upper)
.unwrap();
let amount0 = position.amount0().unwrap().quotient();
assert!(amount0 - reverted_position.amount0().unwrap().quotient() < BigInt::from(10));
Expand Down Expand Up @@ -614,7 +614,7 @@ mod tests {
-887220,
52980,
);
let mut position1 = get_position_at_price(position.clone(), small_price).unwrap();
let position1 = get_position_at_price(position.clone(), small_price).unwrap();
assert!(position1.amount0().unwrap().quotient().is_positive());
assert!(position1.amount1().unwrap().quotient().is_zero());
let position2 = get_position_at_price(
Expand All @@ -631,7 +631,7 @@ mod tests {
.unwrap();
assert!(position2.amount0().unwrap().quotient().is_zero());
assert!(position2.amount1().unwrap().quotient().is_positive());
let rebalanced_position = get_rebalanced_position(&mut position1, 46080, 62160).unwrap();
let rebalanced_position = get_rebalanced_position(position1, 46080, 62160).unwrap();
assert!(rebalanced_position
.amount0()
.unwrap()
Expand All @@ -642,22 +642,22 @@ mod tests {

#[tokio::test]
async fn test_get_rebalanced_position_at_price() {
let mut position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
let position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
.await
.unwrap();
// rebalance to an out of range position
let new_tick_lower = position.tick_upper;
let new_tick_upper = new_tick_lower + 10 * FeeAmount::MEDIUM.tick_spacing().as_i32();
let position_rebalanced_at_current_price =
get_rebalanced_position(&mut position, new_tick_lower, new_tick_upper).unwrap();
get_rebalanced_position(position.clone(), new_tick_lower, new_tick_upper).unwrap();
let price_upper = tick_to_price(
position.pool.token0.clone(),
position.pool.token1.clone(),
position.tick_upper.try_into().unwrap(),
)
.unwrap();
let position_rebalanced_at_tick_upper = get_rebalanced_position_at_price(
position.clone(),
position,
fraction_to_big_decimal(&price_upper),
new_tick_lower,
new_tick_upper,
Expand Down

0 comments on commit 29f5b2f

Please sign in to comment.