Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ pub mod pallet {
NetworkMinLockCostSet(u64), // Event created when the network minimum locking cost is set.
SubnetLimitSet(u16), // Event created when the maximum number of subnets is set
NetworkLockCostReductionIntervalSet(u64), // Event created when the lock cost reduction is set
TakeDecreased( T::AccountId, T::AccountId, u16 ), // Event created when the take for a delegate is decreased.
HotkeySwapped {
coldkey: T::AccountId,
old_hotkey: T::AccountId,
Expand Down Expand Up @@ -937,6 +938,7 @@ pub mod pallet {
StakeTooLowForRoot, // --- Thrown when a hotkey attempts to join the root subnet with too little stake
AllNetworksInImmunity, // --- Thrown when all subnets are in the immunity period
NotEnoughBalance,
InvalidTake, // -- Thrown when take being set is invalid.
}

// ==================
Expand Down Expand Up @@ -1279,6 +1281,38 @@ pub mod pallet {
Self::do_become_delegate(origin, hotkey, Self::get_default_take())
}

// --- Allows delegates to decrease its take value.
//
// # Args:
// * 'origin': (<T as frame_system::Config>::Origin):
// - The signature of the caller's coldkey.
//
// * 'hotkey' (T::AccountId):
// - The hotkey we are delegating (must be owned by the coldkey.)
//
// * 'take' (u16):
// - The new stake proportion that this hotkey takes from delegations.
//
// # Event:
// * TakeDecreased;
// - On successfully setting a decreased take for this hotkey.
//
// # Raises:
// * 'NotRegistered':
// - The hotkey we are delegating is not registered on the network.
//
// * 'NonAssociatedColdKey':
// - The hotkey we are delegating is not owned by the calling coldkey.
//
// * 'InvalidTransaction':
// - The delegate is setting a take which is not lower than the previous.
//
#[pallet::call_index(63)]
#[pallet::weight((0, DispatchClass::Normal, Pays::No))]
pub fn decrease_take(origin: OriginFor<T>, hotkey: T::AccountId, take: u16) -> DispatchResult {
Self::do_decrease_take(origin, hotkey, take)
}

// --- Adds stake to a hotkey. The call is made from the
// coldkey account linked in the hotkey.
// Only the associated coldkey is allowed to make staking and
Expand Down Expand Up @@ -1325,6 +1359,7 @@ pub mod pallet {
Self::do_add_stake(origin, hotkey, amount_staked)
}


// ---- Remove stake from the staking account. The call must be made
// from the coldkey account attached to the neuron metadata. Only this key
// has permission to make staking and unstaking requests.
Expand Down
81 changes: 81 additions & 0 deletions pallets/subtensor/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,87 @@ impl<T: Config> Pallet<T> {
Ok(())
}

// ---- The implementation for the extrinsic decrease_take
//
// # Args:
// * 'origin': (<T as frame_system::Config>::RuntimeOrigin):
// - The signature of the caller's coldkey.
//
// * 'hotkey' (T::AccountId):
// - The hotkey we are delegating (must be owned by the coldkey.)
//
// * 'take' (u16):
// - The stake proportion that this hotkey takes from delegations.
//
// # Event:
// * TakeDecreased;
// - On successfully setting a decreased take for this hotkey.
//
// # Raises:
// * 'NotRegistered':
// - The hotkey we are delegating is not registered on the network.
//
// * 'NonAssociatedColdKey':
// - The hotkey we are delegating is not owned by the calling coldket.
//
// * 'TxRateLimitExceeded':
// - Thrown if key has hit transaction rate limit
Comment on lines +112 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// * 'TxRateLimitExceeded':
// - Thrown if key has hit transaction rate limit
// * 'InvalidTransaction':
// - The delegate is setting a take which is not lower than the previous.

//
pub fn do_decrease_take(
origin: T::RuntimeOrigin,
hotkey: T::AccountId,
take: u16,
) -> dispatch::DispatchResult {
// --- 1. We check the coldkey signature.
let coldkey = ensure_signed(origin)?;
log::info!(
"do_decrease_take( origin:{:?} hotkey:{:?}, take:{:?} )",
coldkey,
hotkey,
take
);

// --- 2. Ensure we are delegating a known key.
ensure!(
Self::hotkey_account_exists(&hotkey),
Error::<T>::NotRegistered
);

// --- 3. Ensure that the coldkey is the owner.
ensure!(
Self::coldkey_owns_hotkey(&coldkey, &hotkey),
Error::<T>::NonAssociatedColdKey
);

// --- 4. Ensure we are not already a delegate (dont allow changing delegate take.)
ensure!(
!Self::hotkey_is_delegate(&hotkey),
Error::<T>::AlreadyDelegate
);

// --- 5. Ensure we are always decreasing take never increasing.
let current_take: u16 = Delegates::<T>::get(hotkey.clone());
ensure!(
take < current_take,
Error::<T>::InvalidTake
);

// --- 6. Set the new take value.
Delegates::<T>::insert(hotkey.clone(), take);

// --- 7. Emit the take value.
log::info!(
"TakeDecreased( coldkey:{:?}, hotkey:{:?}, take:{:?} )",
coldkey,
hotkey,
take
);
Self::deposit_event(Event::TakeDecreased(coldkey, hotkey, take));

// --- 8. Ok and return.
Ok(())
}

// ---- The implementation for the extrinsic add_stake: Adds stake to a hotkey account.
//
// # Args:
Expand Down