Skip to content

Commit

Permalink
Refactor trade pool iteration logic
Browse files Browse the repository at this point in the history
Updated loops in trade.rs to use `enumerate()` for better clarity. Changed pool exclusion to utilize `take` and `skip` for more concise and readable code. These adjustments improve code readability and maintainability.
  • Loading branch information
shuhuiluo committed Nov 4, 2024
1 parent 09d37bd commit 02bbb00
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/entities/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,7 @@ where
None => currency_amount_in.wrapped()?,
};
let token_out = currency_out.wrapped();
for i in 0..pools.len() {
let pool = &pools[i];
for (i, pool) in pools.iter().enumerate() {
// pool irrelevant
if !pool.involves_token(&amount_in.currency) {
continue;
Expand All @@ -691,9 +690,10 @@ where
)?;
sorted_insert(best_trades, trade, max_num_results, trade_comparator)?;
} else if max_hops > 1 && pools.len() > 1 {
let pools_excluding_this_pool = pools[..i]
let pools_excluding_this_pool = pools
.iter()
.chain(pools[i + 1..].iter())
.take(i)
.chain(pools.iter().skip(i + 1))
.cloned()
.collect();
// otherwise, consider all the other paths that lead from this token as long as we
Expand Down Expand Up @@ -758,8 +758,7 @@ where
None => currency_amount_out.wrapped()?,
};
let token_in = currency_in.wrapped();
for i in 0..pools.len() {
let pool = &pools[i];
for (i, pool) in pools.iter().enumerate() {
// pool irrelevant
if !pool.involves_token(&amount_out.currency) {
continue;
Expand All @@ -784,9 +783,10 @@ where
)?;
sorted_insert(best_trades, trade, max_num_results, trade_comparator)?;
} else if max_hops > 1 && pools.len() > 1 {
let pools_excluding_this_pool = pools[..i]
let pools_excluding_this_pool = pools
.iter()
.chain(pools[i + 1..].iter())
.take(i)
.chain(pools.iter().skip(i + 1))
.cloned()
.collect();
// otherwise, consider all the other paths that arrive at this token as long as we
Expand Down

0 comments on commit 02bbb00

Please sign in to comment.