Skip to content

Commit 51bb6a3

Browse files
committed
Separate defaults for tx fee from min stake. Make min stake work like ED for stakes.
1 parent 9b05314 commit 51bb6a3

File tree

11 files changed

+108
-84
lines changed

11 files changed

+108
-84
lines changed

pallets/subtensor/src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,11 +702,17 @@ pub mod pallet {
702702
#[pallet::type_value]
703703
/// Default minimum stake.
704704
/// 500k rao matches $0.25 at $500/TAO
705-
/// Also used as staking fee
706705
pub fn DefaultMinStake<T: Config>() -> u64 {
707706
500_000
708707
}
709708

709+
#[pallet::type_value]
710+
/// Default staking fee.
711+
/// 500k rao matches $0.25 at $500/TAO
712+
pub fn DefaultStakingFee<T: Config>() -> u64 {
713+
500_000
714+
}
715+
710716
#[pallet::type_value]
711717
/// Default unicode vector for tau symbol.
712718
pub fn DefaultUnicodeVecU8<T: Config>() -> Vec<u8> {
@@ -1545,13 +1551,15 @@ pub enum CallType {
15451551
pub enum CustomTransactionError {
15461552
ColdkeyInSwapSchedule,
15471553
StakeAmountTooLow,
1554+
BalanceTooLow,
15481555
}
15491556

15501557
impl From<CustomTransactionError> for u8 {
15511558
fn from(variant: CustomTransactionError) -> u8 {
15521559
match variant {
15531560
CustomTransactionError::ColdkeyInSwapSchedule => 0,
15541561
CustomTransactionError::StakeAmountTooLow => 1,
1562+
CustomTransactionError::BalanceTooLow => 2,
15551563
}
15561564
}
15571565
}
@@ -1704,7 +1712,7 @@ where
17041712
Some(Call::add_stake {
17051713
hotkey: _,
17061714
netuid: _,
1707-
amount_staked,
1715+
amount,
17081716
}) => {
17091717
// Check that amount parameter is at least the min stake
17101718
// also check the coldkey balance
@@ -1713,12 +1721,14 @@ where
17131721
>>::reducible_balance(
17141722
who, Preservation::Expendable, Fortitude::Polite
17151723
);
1724+
let min_amount =
1725+
DefaultMinStake::<T>::get().saturating_add(DefaultStakingFee::<T>::get());
17161726

1717-
if (*amount_staked < DefaultMinStake::<T>::get())
1718-
|| (coldkey_balance < DefaultMinStake::<T>::get())
1719-
{
1727+
if *amount < min_amount {
17201728
InvalidTransaction::Custom(CustomTransactionError::StakeAmountTooLow.into())
17211729
.into()
1730+
} else if coldkey_balance < min_amount {
1731+
InvalidTransaction::Custom(CustomTransactionError::BalanceTooLow.into()).into()
17221732
} else {
17231733
Ok(ValidTransaction {
17241734
priority: Self::get_priority_vanilla(),

pallets/subtensor/src/staking/add_stake.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,17 @@ impl<T: Config> Pallet<T> {
6262
Error::<T>::HotKeyAccountNotExists
6363
);
6464

65-
// Ensure stake_to_be_added is at least DefaultMinStake
66-
ensure!(
67-
stake_to_be_added >= DefaultMinStake::<T>::get(),
68-
Error::<T>::AmountTooLow
69-
);
65+
// Ensure stake_to_be_added is at least DefaultMinStake plus fee
66+
let min_amount = DefaultMinStake::<T>::get().saturating_add(DefaultStakingFee::<T>::get());
67+
ensure!(stake_to_be_added >= min_amount, Error::<T>::AmountTooLow);
7068

7169
// 5. Ensure the remove operation from the coldkey is a success.
7270
let tao_staked: u64 =
7371
Self::remove_balance_from_coldkey_account(&coldkey, stake_to_be_added)?;
7472

7573
// 6. Swap the stake into alpha on the subnet and increase counters.
7674
// Emit the staking event.
77-
let fee = DefaultMinStake::<T>::get();
75+
let fee = DefaultStakingFee::<T>::get();
7876
Self::stake_into_subnet(&hotkey, &coldkey, netuid, tao_staked, fee);
7977

8078
// Ok and return.

pallets/subtensor/src/staking/move_stake.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<T: Config> Pallet<T> {
6868
);
6969

7070
// --- 7. Unstake the amount of alpha from the origin subnet, converting it to TAO
71-
let fee = DefaultMinStake::<T>::get().saturating_div(2); // fee is half of min stake because it is applied twice
71+
let fee = DefaultStakingFee::<T>::get().saturating_div(2); // fee is half of min stake because it is applied twice
7272
let origin_tao = Self::unstake_from_subnet(
7373
&origin_hotkey.clone(),
7474
&coldkey.clone(),
@@ -79,7 +79,7 @@ impl<T: Config> Pallet<T> {
7979

8080
// Ensure origin_tao is at least DefaultMinStake
8181
ensure!(
82-
origin_tao >= DefaultMinStake::<T>::get(),
82+
origin_tao.saturating_sub(fee) >= DefaultMinStake::<T>::get(),
8383
Error::<T>::AmountTooLow
8484
);
8585

@@ -181,13 +181,13 @@ impl<T: Config> Pallet<T> {
181181
);
182182

183183
// 6. Unstake from the origin coldkey; this returns an amount of TAO.
184-
let fee = DefaultMinStake::<T>::get().saturating_div(2);
184+
let fee = DefaultStakingFee::<T>::get().saturating_div(2);
185185
let origin_tao =
186186
Self::unstake_from_subnet(&hotkey, &coldkey, origin_netuid, alpha_amount, fee);
187187

188188
// 7. Ensure the returned TAO meets a minimum stake requirement (if required).
189189
ensure!(
190-
origin_tao >= DefaultMinStake::<T>::get(),
190+
origin_tao.saturating_sub(fee) >= DefaultMinStake::<T>::get(),
191191
Error::<T>::AmountTooLow
192192
);
193193

@@ -288,17 +288,18 @@ impl<T: Config> Pallet<T> {
288288
);
289289

290290
// 6. Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).
291+
let fee = DefaultStakingFee::<T>::get().saturating_div(2);
291292
let tao_unstaked =
292-
Self::unstake_from_subnet(&hotkey, &coldkey, origin_netuid, alpha_amount);
293+
Self::unstake_from_subnet(&hotkey, &coldkey, origin_netuid, alpha_amount, fee);
293294

294295
// 7. Check that the unstaked amount is above the minimum stake threshold.
295296
ensure!(
296-
tao_unstaked >= DefaultMinStake::<T>::get(),
297+
tao_unstaked.saturating_sub(fee) >= DefaultMinStake::<T>::get(),
297298
Error::<T>::AmountTooLow
298299
);
299300

300301
// 8. Stake the unstaked amount into the destination subnet, using the same coldkey/hotkey.
301-
Self::stake_into_subnet(&hotkey, &coldkey, destination_netuid, tao_unstaked);
302+
Self::stake_into_subnet(&hotkey, &coldkey, destination_netuid, tao_unstaked, fee);
302303

303304
// 9. Emit an event for logging.
304305
log::info!(

pallets/subtensor/src/staking/remove_stake.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<T: Config> Pallet<T> {
6666
);
6767

6868
// 6. Swap the alpba to tao and update counters for this subnet.
69-
let fee = DefaultMinStake::<T>::get();
69+
let fee = DefaultStakingFee::<T>::get();
7070
let tao_unstaked: u64 =
7171
Self::unstake_from_subnet(&hotkey, &coldkey, netuid, alpha_unstaked, fee);
7272

@@ -117,7 +117,7 @@ impl<T: Config> Pallet<T> {
117117
origin: T::RuntimeOrigin,
118118
hotkey: T::AccountId,
119119
) -> dispatch::DispatchResult {
120-
let fee = DefaultMinStake::<T>::get();
120+
let fee = DefaultStakingFee::<T>::get();
121121

122122
// 1. We check the transaction is signed by the caller and retrieve the T::AccountId coldkey information.
123123
let coldkey = ensure_signed(origin)?;
@@ -185,7 +185,7 @@ impl<T: Config> Pallet<T> {
185185
origin: T::RuntimeOrigin,
186186
hotkey: T::AccountId,
187187
) -> dispatch::DispatchResult {
188-
let fee = DefaultMinStake::<T>::get();
188+
let fee = DefaultStakingFee::<T>::get();
189189

190190
// 1. We check the transaction is signed by the caller and retrieve the T::AccountId coldkey information.
191191
let coldkey = ensure_signed(origin)?;

pallets/subtensor/src/tests/epoch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,7 +1505,7 @@ fn test_set_alpha_disabled() {
15051505
signer.clone(),
15061506
hotkey,
15071507
netuid,
1508-
DefaultMinStake::<Test>::get()
1508+
DefaultMinStake::<Test>::get() + DefaultStakingFee::<Test>::get()
15091509
));
15101510
// Only owner can set alpha values
15111511
assert_ok!(SubtensorModule::register_network(signer.clone(), hotkey));
@@ -2600,7 +2600,7 @@ fn test_get_set_alpha() {
26002600
signer.clone(),
26012601
hotkey,
26022602
netuid,
2603-
DefaultMinStake::<Test>::get()
2603+
DefaultMinStake::<Test>::get() + DefaultStakingFee::<Test>::get()
26042604
));
26052605

26062606
assert_ok!(SubtensorModule::do_set_alpha_values(

0 commit comments

Comments
 (0)