Skip to content

Commit

Permalink
Merge pull request #1115 from opentensor/feat/unstake-all
Browse files Browse the repository at this point in the history
Implement unstake_all
  • Loading branch information
gztensor authored Dec 23, 2024
2 parents 2662338 + 62a5aad commit 27a5fba
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,5 +1392,19 @@ mod dispatches {
) -> DispatchResult {
Self::do_register_network(origin, &hotkey, mechid, identity)
}

/// Unstake all
#[pallet::call_index(83)]
#[pallet::weight((Weight::from_parts(111_000_000, 0)
.saturating_add(Weight::from_parts(0, 43991))
.saturating_add(T::DbWeight::get().reads(20))
.saturating_add(T::DbWeight::get().writes(14)), DispatchClass::Normal, Pays::No))]
pub fn unstake_all(
origin: OriginFor<T>,
hotkey: T::AccountId,
) -> DispatchResult {
Self::do_unstake_all(origin, hotkey)
}

}
}
62 changes: 62 additions & 0 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,66 @@ impl<T: Config> Pallet<T> {
// Done and ok.
Ok(())
}

/// ---- The implementation for unstake_all.
///
pub fn do_unstake_all(
origin: T::RuntimeOrigin,
hotkey: T::AccountId,
) -> dispatch::DispatchResult {
let coldkey = ensure_signed(origin)?;
log::info!(
"do_unstake_all( origin:{:?} hotkey:{:?} )",
coldkey,
hotkey,
);

// Ensure we don't exceed stake rate limit (once for all subnets)
let unstakes_this_interval =
Self::get_stakes_this_interval_for_coldkey_hotkey(&coldkey, &hotkey);
ensure!(
unstakes_this_interval < Self::get_target_stakes_per_interval(),
Error::<T>::UnstakeRateLimitExceeded
);
// Set last block for rate limiting
let block: u64 = Self::get_current_block_as_u64();
Self::set_last_tx_block(&coldkey, block);
Self::set_stakes_this_interval_for_coldkey_hotkey(
&coldkey,
&hotkey,
unstakes_this_interval.saturating_add(1),
block,
);

// Iterate over all netuids and unstake
let subnets: Vec<u16> = Self::get_all_subnet_netuids();
subnets.iter().for_each(|&netuid| {
// Get hotkey's stake in this netuid
let current_stake =
Self::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);

if current_stake > 0 {
// Convert and unstake from the subnet.
let tao_unstaked: u64 =
Self::unstake_from_subnet(&hotkey, &coldkey, netuid, current_stake);

// We add the balance to the coldkey. If the above fails we will not credit this coldkey.
Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked);

// The stake is below the minimum, we clear the nomination from storage.
Self::clear_small_nomination_if_required(&hotkey, &coldkey, netuid, 0_u64);

log::info!(
"StakeRemoved( hotkey:{:?}, netuid: {:?}, tao_unstaked:{:?} )",
hotkey.clone(),
netuid,
tao_unstaked
);
}
});

// Done and ok.
Ok(())
}

}

0 comments on commit 27a5fba

Please sign in to comment.