Skip to content

Commit 29f5b2f

Browse files
authored
refactor: loosen trait bound for TickDataProvider (#135)
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.
1 parent 72d0193 commit 29f5b2f

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ std = [
5353
"base64?/std",
5454
"derive_more/std",
5555
"fastnum/std",
56-
"once_cell/std",
56+
"once_cell?/std",
5757
"serde_json?/std",
5858
"thiserror/std",
5959
"uniswap-lens?/std",

src/entities/pool.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,7 @@ impl<TP: TickDataProvider> Pool<TP> {
248248
sqrt_price_limit_x96,
249249
)
250250
}
251-
}
252251

253-
impl<TP: Clone + TickDataProvider> Pool<TP> {
254252
/// Given an input amount of a token, return the computed output amount
255253
///
256254
/// ## Arguments

src/entities/trade.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,14 +504,7 @@ where
504504
self.minimum_amount_out_cached(slippage_tolerance, None)?,
505505
))
506506
}
507-
}
508507

509-
impl<TInput, TOutput, TP> Trade<TInput, TOutput, TP>
510-
where
511-
TInput: BaseCurrency,
512-
TOutput: BaseCurrency,
513-
TP: Clone + TickDataProvider,
514-
{
515508
/// Constructs an exact in trade with the given amount in and route
516509
///
517510
/// ## Arguments
@@ -619,7 +612,14 @@ where
619612
}
620613
Self::new(populated_routes, trade_type)
621614
}
615+
}
622616

617+
impl<TInput, TOutput, TP> Trade<TInput, TOutput, TP>
618+
where
619+
TInput: BaseCurrency,
620+
TOutput: BaseCurrency,
621+
TP: Clone + TickDataProvider,
622+
{
623623
/// Given a list of pools, and a fixed amount in, returns the top `max_num_results` trades that
624624
/// go from an input token amount to an output token, making at most `max_hops` hops.
625625
///

src/extensions/position.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -394,12 +394,12 @@ where
394394
/// * `new_tick_upper`: The new upper tick.
395395
#[inline]
396396
pub fn get_rebalanced_position<TP>(
397-
position: &mut Position<TP>,
397+
mut position: Position<TP>,
398398
new_tick_lower: TP::Index,
399399
new_tick_upper: TP::Index,
400400
) -> Result<Position<TP>, Error>
401401
where
402-
TP: Clone + TickDataProvider,
402+
TP: TickDataProvider,
403403
{
404404
let price = position.pool.token0_price();
405405
// Calculate the position equity denominated in token1 before rebalance.
@@ -414,7 +414,7 @@ where
414414
// token0's equity denominated in token1 divided by the price
415415
let amount0_after = (equity_before - amount1_after) / price;
416416
Position::from_amounts(
417-
position.pool.clone(),
417+
position.pool,
418418
new_tick_lower,
419419
new_tick_upper,
420420
U256::from_big_uint(amount0_after.to_big_uint()),
@@ -470,10 +470,10 @@ pub fn get_rebalanced_position_at_price<TP>(
470470
new_tick_upper: TP::Index,
471471
) -> Result<Position<TP>, Error>
472472
where
473-
TP: Clone + TickDataProvider,
473+
TP: TickDataProvider,
474474
{
475475
get_rebalanced_position(
476-
&mut get_position_at_price(position, new_price)?,
476+
get_position_at_price(position, new_price)?,
477477
new_tick_lower,
478478
new_tick_upper,
479479
)
@@ -571,17 +571,17 @@ mod tests {
571571

572572
#[tokio::test]
573573
async fn test_get_rebalanced_position() {
574-
let mut position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
574+
let position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
575575
.await
576576
.unwrap();
577577
// rebalance to an out of range position
578578
let new_tick_lower = position.tick_upper;
579579
let new_tick_upper = new_tick_lower + 10 * FeeAmount::MEDIUM.tick_spacing().as_i32();
580-
let mut new_position =
581-
get_rebalanced_position(&mut position, new_tick_lower, new_tick_upper).unwrap();
580+
let new_position =
581+
get_rebalanced_position(position.clone(), new_tick_lower, new_tick_upper).unwrap();
582582
assert!(new_position.amount1().unwrap().quotient().is_zero());
583583
let reverted_position =
584-
get_rebalanced_position(&mut new_position, position.tick_lower, position.tick_upper)
584+
get_rebalanced_position(new_position, position.tick_lower, position.tick_upper)
585585
.unwrap();
586586
let amount0 = position.amount0().unwrap().quotient();
587587
assert!(amount0 - reverted_position.amount0().unwrap().quotient() < BigInt::from(10));
@@ -614,7 +614,7 @@ mod tests {
614614
-887220,
615615
52980,
616616
);
617-
let mut position1 = get_position_at_price(position.clone(), small_price).unwrap();
617+
let position1 = get_position_at_price(position.clone(), small_price).unwrap();
618618
assert!(position1.amount0().unwrap().quotient().is_positive());
619619
assert!(position1.amount1().unwrap().quotient().is_zero());
620620
let position2 = get_position_at_price(
@@ -631,7 +631,7 @@ mod tests {
631631
.unwrap();
632632
assert!(position2.amount0().unwrap().quotient().is_zero());
633633
assert!(position2.amount1().unwrap().quotient().is_positive());
634-
let rebalanced_position = get_rebalanced_position(&mut position1, 46080, 62160).unwrap();
634+
let rebalanced_position = get_rebalanced_position(position1, 46080, 62160).unwrap();
635635
assert!(rebalanced_position
636636
.amount0()
637637
.unwrap()
@@ -642,22 +642,22 @@ mod tests {
642642

643643
#[tokio::test]
644644
async fn test_get_rebalanced_position_at_price() {
645-
let mut position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
645+
let position = get_position(1, NPM, uint!(4_U256), PROVIDER.clone(), BLOCK_ID)
646646
.await
647647
.unwrap();
648648
// rebalance to an out of range position
649649
let new_tick_lower = position.tick_upper;
650650
let new_tick_upper = new_tick_lower + 10 * FeeAmount::MEDIUM.tick_spacing().as_i32();
651651
let position_rebalanced_at_current_price =
652-
get_rebalanced_position(&mut position, new_tick_lower, new_tick_upper).unwrap();
652+
get_rebalanced_position(position.clone(), new_tick_lower, new_tick_upper).unwrap();
653653
let price_upper = tick_to_price(
654654
position.pool.token0.clone(),
655655
position.pool.token1.clone(),
656656
position.tick_upper.try_into().unwrap(),
657657
)
658658
.unwrap();
659659
let position_rebalanced_at_tick_upper = get_rebalanced_position_at_price(
660-
position.clone(),
660+
position,
661661
fraction_to_big_decimal(&price_upper),
662662
new_tick_lower,
663663
new_tick_upper,

0 commit comments

Comments
 (0)