Skip to content
2 changes: 2 additions & 0 deletions pallets/subtensor/src/macros/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,7 @@ mod errors {
NeedWaitingMoreBlocksToStarCall,
/// Not enough AlphaOut on the subnet to recycle
NotEnoughAlphaOutToRecycle,
/// Cannot burn or recycle TAO from root subnet
CannotBurnOrRecycleOnRootSubnet,
}
}
69 changes: 45 additions & 24 deletions pallets/subtensor/src/staking/recycle_alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ impl<T: Config> Pallet<T> {
amount: u64,
netuid: u16,
) -> DispatchResult {
let coldkey = ensure_signed(origin)?;
let coldkey: T::AccountId = ensure_signed(origin)?;

ensure!(
Self::if_subnet_exist(netuid),
Error::<T>::SubNetworkDoesNotExist
);

ensure!(
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
Error::<T>::NonAssociatedColdKey
netuid != Self::get_root_netuid(),
Error::<T>::CannotBurnOrRecycleOnRootSubnet
);

// Ensure that the hotkey account exists this is only possible through registration.
ensure!(
TotalHotkeyAlpha::<T>::get(&hotkey, netuid) >= amount,
Self::hotkey_account_exists(&hotkey),
Error::<T>::HotKeyAccountNotExists
);

// Ensure that the hotkey has enough stake to withdraw.
ensure!(
Self::has_enough_stake_on_subnet(&hotkey, &coldkey, netuid, amount),
Error::<T>::NotEnoughStakeToWithdraw
);

Expand All @@ -42,19 +49,22 @@ impl<T: Config> Pallet<T> {
Error::<T>::InsufficientLiquidity
);

if TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| {
*v = v.saturating_sub(amount);
*v
}) == 0
{
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
}
// Deduct from the coldkey's stake.
let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid, amount,
);

// Recycle means we should decrease the alpha issuance tracker.
SubnetAlphaOut::<T>::mutate(netuid, |total| {
*total = total.saturating_sub(amount);
*total = total.saturating_sub(actual_alpha_decrease);
});

Self::deposit_event(Event::AlphaRecycled(coldkey, hotkey, amount, netuid));
Self::deposit_event(Event::AlphaRecycled(
coldkey,
hotkey,
actual_alpha_decrease,
netuid,
));

Ok(())
}
Expand Down Expand Up @@ -85,12 +95,19 @@ impl<T: Config> Pallet<T> {
);

ensure!(
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
Error::<T>::NonAssociatedColdKey
netuid != Self::get_root_netuid(),
Error::<T>::CannotBurnOrRecycleOnRootSubnet
);

// Ensure that the hotkey account exists this is only possible through registration.
ensure!(
TotalHotkeyAlpha::<T>::get(&hotkey, netuid) >= amount,
Self::hotkey_account_exists(&hotkey),
Error::<T>::HotKeyAccountNotExists
);

// Ensure that the hotkey has enough stake to withdraw.
ensure!(
Self::has_enough_stake_on_subnet(&hotkey, &coldkey, netuid, amount),
Error::<T>::NotEnoughStakeToWithdraw
);

Expand All @@ -99,16 +116,20 @@ impl<T: Config> Pallet<T> {
Error::<T>::InsufficientLiquidity
);

if TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| {
*v = v.saturating_sub(amount);
*v
}) == 0
{
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
}
// Deduct from the coldkey's stake.
let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid, amount,
);

// This is a burn, so we don't need to update AlphaOut.

// Deposit event
Self::deposit_event(Event::AlphaBurned(coldkey, hotkey, amount, netuid));
Self::deposit_event(Event::AlphaBurned(
coldkey,
hotkey,
actual_alpha_decrease,
netuid,
));

Ok(())
}
Expand Down
Loading
Loading