Skip to content

Commit f4f5add

Browse files
committed
improve efficiency
1 parent 576720e commit f4f5add

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

pallets/swap/src/pallet/impls.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,23 +1362,22 @@ impl<T: Config> Pallet<T> {
13621362
let mechid = T::SubnetInfo::mechanism(netuid.into());
13631363
let v3_initialized = SwapV3Initialized::<T>::get(netuid);
13641364
let user_lp_enabled =
1365-
<Self as subtensor_swap_interface::SwapHandler<T::AccountId>>::is_user_liquidity_enabled(netuid);
1365+
<Self as subtensor_swap_interface::SwapHandler<T::AccountId>>::is_user_liquidity_enabled(netuid);
13661366

13671367
let is_v3_mode = mechid == 1 && v3_initialized;
13681368

13691369
if is_v3_mode {
13701370
// -------- V3: close every position, aggregate refunds, clear state --------
13711371

1372-
// 1) Snapshot all (owner, position_id) under this netuid to avoid iterator aliasing.
1372+
// 1) Snapshot all (owner, position_id).
13731373
struct CloseItem<A> {
13741374
owner: A,
13751375
pos_id: PositionId,
13761376
}
13771377
let mut to_close: sp_std::vec::Vec<CloseItem<T::AccountId>> = sp_std::vec::Vec::new();
1378-
for ((n, owner, pos_id), _pos) in Positions::<T>::iter() {
1379-
if n == netuid {
1380-
to_close.push(CloseItem { owner, pos_id });
1381-
}
1378+
1379+
for ((owner, pos_id), _pos) in Positions::<T>::iter_prefix((netuid,)) {
1380+
to_close.push(CloseItem { owner, pos_id });
13821381
}
13831382

13841383
let protocol_account = Self::protocol_account_id();
@@ -1428,22 +1427,17 @@ impl<T: Config> Pallet<T> {
14281427
ActiveTickIndexManager::<T>::remove(netuid, ti);
14291428
}
14301429

1431-
// 5) Clear storage:
1432-
// Positions (StorageNMap) – prefix is **(netuid,)** not just netuid.
1430+
// 5) Clear storage for this netuid.
14331431
let _ = Positions::<T>::clear_prefix((netuid,), u32::MAX, None);
1434-
1435-
// Ticks (DoubleMap) – OK to pass netuid as first key.
14361432
let _ = Ticks::<T>::clear_prefix(netuid, u32::MAX, None);
14371433

1438-
// Fee globals, price/tick/liquidity, v3 init flag.
14391434
FeeGlobalTao::<T>::remove(netuid);
14401435
FeeGlobalAlpha::<T>::remove(netuid);
14411436
CurrentLiquidity::<T>::remove(netuid);
14421437
CurrentTick::<T>::remove(netuid);
14431438
AlphaSqrtPrice::<T>::remove(netuid);
14441439
SwapV3Initialized::<T>::remove(netuid);
14451440

1446-
// Active tick bitmap words (StorageNMap) – prefix is **(netuid,)**.
14471441
let _ = TickIndexBitmapWords::<T>::clear_prefix((netuid,), u32::MAX, None);
14481442
FeeRate::<T>::remove(netuid);
14491443
EnabledUserLiquidity::<T>::remove(netuid);

pallets/swap/src/pallet/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2295,8 +2295,8 @@ fn refund_alpha_single_provider_exact() {
22952295
let (_pos_id, tao_needed, alpha_needed) =
22962296
Pallet::<Test>::do_add_liquidity(netuid, &cold, &hot, tick_low, tick_high, liquidity)
22972297
.expect("add alpha-only liquidity");
2298-
assert_eq!(tao_needed, 0, "alphaonly position must not require TAO");
2299-
assert!(alpha_needed > 0, "alphaonly position must require ALPHA");
2298+
assert_eq!(tao_needed, 0, "alpha-only position must not require TAO");
2299+
assert!(alpha_needed > 0, "alpha-only position must require ALPHA");
23002300

23012301
// --- Snapshot BEFORE we withdraw funds (baseline for conservation).
23022302
let alpha_before_hot =

0 commit comments

Comments
 (0)