Skip to content

Commit eae43ff

Browse files
authored
Merge pull request #1248 from opentensor/nonclaim
nonclaim
2 parents 95665f1 + c28cac9 commit eae43ff

21 files changed

+729
-44
lines changed

pallets/admin-utils/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,26 @@ pub mod pallet {
12871287
ensure_root(origin)?;
12881288
T::Grandpa::schedule_change(next_authorities, in_blocks, forced)
12891289
}
1290+
1291+
/// Enables or disables Liquid Alpha for a given subnet.
1292+
///
1293+
/// # Parameters
1294+
/// - `origin`: The origin of the call, which must be the root account or subnet owner.
1295+
/// - `netuid`: The unique identifier for the subnet.
1296+
/// - `enabled`: A boolean flag to enable or disable Liquid Alpha.
1297+
///
1298+
/// # Weight
1299+
/// This function has a fixed weight of 0 and is classified as an operational transaction that does not incur any fees.
1300+
#[pallet::call_index(61)]
1301+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1302+
pub fn sudo_set_toggle_transfer(
1303+
origin: OriginFor<T>,
1304+
netuid: u16,
1305+
toggle: bool,
1306+
) -> DispatchResult {
1307+
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
1308+
pallet_subtensor::Pallet::<T>::toggle_transfer(netuid, toggle)
1309+
}
12901310
}
12911311
}
12921312

pallets/subtensor/src/coinbase/root.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,14 @@ impl<T: Config> Pallet<T> {
722722
Self::deposit_event(Event::NetworkLockCostReductionIntervalSet(interval));
723723
}
724724
pub fn get_lock_reduction_interval() -> u64 {
725-
NetworkLockReductionInterval::<T>::get()
725+
let interval: I64F64 =
726+
I64F64::saturating_from_num(NetworkLockReductionInterval::<T>::get());
727+
let block_emission: I64F64 =
728+
I64F64::saturating_from_num(Self::get_block_emission().unwrap_or(1_000_000_000));
729+
let halving: I64F64 = block_emission
730+
.checked_div(I64F64::saturating_from_num(1_000_000_000))
731+
.unwrap_or(I64F64::saturating_from_num(0.0));
732+
let halved_interval: I64F64 = interval.saturating_mul(halving);
733+
halved_interval.saturating_to_num::<u64>()
726734
}
727735
}

pallets/subtensor/src/coinbase/run_coinbase.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ impl<T: Config> Pallet<T> {
138138
for netuid_i in subnets.iter() {
139139
// Get alpha out.
140140
let alpha_out_i: I96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0));
141+
log::debug!("alpha_out_i: {:?}", alpha_out_i);
141142
// Calculate the owner cut.
142143
let owner_cut_i: I96F32 = alpha_out_i.saturating_mul(cut_percent);
144+
log::debug!("owner_cut_i: {:?}", owner_cut_i);
143145
// Save owner cut.
144146
*owner_cuts.entry(*netuid_i).or_insert(asfloat!(0)) = owner_cut_i;
145147
// Save new alpha_out.
@@ -155,24 +157,33 @@ impl<T: Config> Pallet<T> {
155157
for netuid_i in subnets.iter() {
156158
// Get remaining alpha out.
157159
let alpha_out_i: I96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0.0));
160+
log::debug!("alpha_out_i: {:?}", alpha_out_i);
158161
// Get total TAO on root.
159162
let root_tao: I96F32 = asfloat!(SubnetTAO::<T>::get(0));
163+
log::debug!("root_tao: {:?}", root_tao);
160164
// Get total ALPHA on subnet.
161165
let alpha_issuance: I96F32 = asfloat!(Self::get_alpha_issuance(*netuid_i));
166+
log::debug!("alpha_issuance: {:?}", alpha_issuance);
162167
// Get tao_weight
163168
let tao_weight: I96F32 = root_tao.saturating_mul(Self::get_tao_weight());
169+
log::debug!("tao_weight: {:?}", tao_weight);
164170
// Get root proportional dividends.
165171
let root_proportion: I96F32 = tao_weight
166172
.checked_div(tao_weight.saturating_add(alpha_issuance))
167173
.unwrap_or(asfloat!(0.0));
174+
log::debug!("root_proportion: {:?}", root_proportion);
168175
// Get root proportion of alpha_out dividends.
169176
let root_alpha: I96F32 = root_proportion
170177
.saturating_mul(alpha_out_i) // Total alpha emission per block remaining.
171178
.saturating_mul(asfloat!(0.5)); // 50% to validators.
172179
// Remove root alpha from alpha_out.
180+
log::debug!("root_alpha: {:?}", root_alpha);
181+
// Get pending alpha as original alpha_out - root_alpha.
173182
let pending_alpha: I96F32 = alpha_out_i.saturating_sub(root_alpha);
183+
log::debug!("pending_alpha: {:?}", pending_alpha);
174184
// Sell root emission through the pool.
175185
let root_tao: u64 = Self::swap_alpha_for_tao(*netuid_i, tou64!(root_alpha));
186+
log::debug!("root_tao: {:?}", root_tao);
176187
// Accumulate alpha emission in pending.
177188
PendingAlphaSwapped::<T>::mutate(*netuid_i, |total| {
178189
*total = total.saturating_add(tou64!(root_alpha));
@@ -238,6 +249,11 @@ impl<T: Config> Pallet<T> {
238249
BlocksSinceLastStep::<T>::mutate(netuid, |total| *total = total.saturating_add(1));
239250
}
240251
}
252+
253+
// --- 8. Apply pending childkeys of this subnet for the next epoch
254+
for netuid in subnets.iter() {
255+
Self::do_set_pending_children(*netuid);
256+
}
241257
}
242258

243259
pub fn drain_pending_emission(
@@ -261,6 +277,7 @@ impl<T: Config> Pallet<T> {
261277
// Run the epoch.
262278
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
263279
Self::epoch(netuid, pending_alpha.saturating_add(pending_swapped));
280+
log::debug!("hotkey_emission: {:?}", hotkey_emission);
264281

265282
// Accumulate emission of dividends and incentive per hotkey.
266283
let mut incentives: BTreeMap<T::AccountId, u64> = BTreeMap::new();
@@ -282,8 +299,10 @@ impl<T: Config> Pallet<T> {
282299
.or_insert(asfloat!(parent_div));
283300
}
284301
}
302+
log::debug!("incentives: {:?}", incentives);
303+
log::debug!("dividends: {:?}", dividends);
285304

286-
// Accumulate root divs and alpha_divs. For each hotkye we compute their
305+
// Accumulate root divs and alpha_divs. For each hotkey we compute their
287306
// local and root dividend proportion based on their alpha_stake/root_stake
288307
let mut total_root_divs: I96F32 = asfloat!(0);
289308
let mut root_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
@@ -300,19 +319,15 @@ impl<T: Config> Pallet<T> {
300319
let root_alpha: I96F32 = root_stake.saturating_mul(Self::get_tao_weight());
301320
// Get total from root and local
302321
let total_alpha: I96F32 = alpha_stake.saturating_add(root_alpha);
303-
// Compute alpha prop.
304-
let alpha_prop: I96F32 = alpha_stake.checked_div(total_alpha).unwrap_or(zero);
305322
// Copmute root prop.
306323
let root_prop: I96F32 = root_alpha.checked_div(total_alpha).unwrap_or(zero);
307-
// Compute alpha dividends
308-
let alpha_divs: I96F32 = dividend.saturating_mul(alpha_prop);
309324
// Compute root dividends
310325
let root_divs: I96F32 = dividend.saturating_mul(root_prop);
311326
// Record the root dividends.
312327
alpha_dividends
313328
.entry(hotkey.clone())
314-
.and_modify(|e| *e = e.saturating_add(alpha_divs))
315-
.or_insert(alpha_divs);
329+
.and_modify(|e| *e = e.saturating_add(dividend))
330+
.or_insert(dividend);
316331
// Record the alpha_dividends.
317332
root_dividends
318333
.entry(hotkey.clone())
@@ -321,6 +336,8 @@ impl<T: Config> Pallet<T> {
321336
// Accumulate total root divs.
322337
total_root_divs = total_root_divs.saturating_add(root_divs);
323338
}
339+
log::debug!("alpha_dividends: {:?}", alpha_dividends);
340+
log::debug!("root_dividends: {:?}", root_dividends);
324341

325342
// Compute root divs as TAO. Here we take
326343
let mut tao_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
@@ -335,6 +352,7 @@ impl<T: Config> Pallet<T> {
335352
.and_modify(|e| *e = root_tao)
336353
.or_insert(root_tao);
337354
}
355+
log::debug!("tao_dividends: {:?}", tao_dividends);
338356

339357
// Distribute the owner cut.
340358
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
@@ -363,6 +381,8 @@ impl<T: Config> Pallet<T> {
363381
// Distribute alpha divs.
364382
let _ = AlphaDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
365383
for (hotkey, mut alpha_divs) in alpha_dividends {
384+
log::debug!("hotkey: {:?} alpha_divs: {:?}", hotkey, alpha_divs);
385+
366386
// Get take prop
367387
let alpha_take: I96F32 =
368388
Self::get_hotkey_take_float(&hotkey).saturating_mul(alpha_divs);
@@ -386,6 +406,7 @@ impl<T: Config> Pallet<T> {
386406
// Distribute root tao divs.
387407
let _ = TaoDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
388408
for (hotkey, mut root_tao) in tao_dividends {
409+
log::debug!("hotkey: {:?} root_tao: {:?}", hotkey, root_tao);
389410
// Get take prop
390411
let tao_take: I96F32 = Self::get_hotkey_take_float(&hotkey).saturating_mul(root_tao);
391412
// Remove take prop from root_tao

pallets/subtensor/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,9 @@ pub mod pallet {
11141114
/// ============================
11151115
/// ==== Subnet Locks =====
11161116
/// ============================
1117+
#[pallet::storage] // --- MAP ( netuid ) --> transfer_toggle
1118+
pub type TransferToggle<T: Config> =
1119+
StorageMap<_, Identity, u16, bool, ValueQuery, DefaultTrue<T>>;
11171120
#[pallet::storage] // --- MAP ( netuid ) --> total_subnet_locked
11181121
pub type SubnetLocked<T: Config> =
11191122
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64<T>>;
@@ -1645,6 +1648,7 @@ pub enum CustomTransactionError {
16451648
RateLimitExceeded,
16461649
InsufficientLiquidity,
16471650
SlippageTooHigh,
1651+
TransferDisallowed,
16481652
BadRequest,
16491653
}
16501654

@@ -1660,6 +1664,7 @@ impl From<CustomTransactionError> for u8 {
16601664
CustomTransactionError::RateLimitExceeded => 6,
16611665
CustomTransactionError::InsufficientLiquidity => 7,
16621666
CustomTransactionError::SlippageTooHigh => 8,
1667+
CustomTransactionError::TransferDisallowed => 9,
16631668
CustomTransactionError::BadRequest => 255,
16641669
}
16651670
}
@@ -1735,6 +1740,10 @@ where
17351740
CustomTransactionError::SlippageTooHigh.into(),
17361741
)
17371742
.into()),
1743+
Error::<T>::TransferDisallowed => Err(InvalidTransaction::Custom(
1744+
CustomTransactionError::TransferDisallowed.into(),
1745+
)
1746+
.into()),
17381747
_ => Err(
17391748
InvalidTransaction::Custom(CustomTransactionError::BadRequest.into()).into(),
17401749
),
@@ -1959,6 +1968,7 @@ where
19591968
*alpha_amount,
19601969
*alpha_amount,
19611970
None,
1971+
false,
19621972
))
19631973
}
19641974
Some(Call::transfer_stake {
@@ -1979,6 +1989,7 @@ where
19791989
*alpha_amount,
19801990
*alpha_amount,
19811991
None,
1992+
true,
19821993
))
19831994
}
19841995
Some(Call::swap_stake {
@@ -1998,6 +2009,7 @@ where
19982009
*alpha_amount,
19992010
*alpha_amount,
20002011
None,
2012+
false,
20012013
))
20022014
}
20032015
Some(Call::swap_stake_limit {
@@ -2026,6 +2038,7 @@ where
20262038
*alpha_amount,
20272039
max_amount,
20282040
Some(*allow_partial),
2041+
false,
20292042
))
20302043
}
20312044
Some(Call::register { netuid, .. } | Call::burned_register { netuid, .. }) => {

pallets/subtensor/src/macros/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,7 @@ mod errors {
189189
InsufficientLiquidity,
190190
/// Slippage is too high for the transaction.
191191
SlippageTooHigh,
192+
/// Subnet disallows transfer.
193+
TransferDisallowed,
192194
}
193195
}

pallets/subtensor/src/macros/events.rs

+6
Original file line numberDiff line numberDiff line change
@@ -265,5 +265,11 @@ mod events {
265265
/// Parameters:
266266
/// (coldkey, hotkey, origin_netuid, destination_netuid, amount)
267267
StakeSwapped(T::AccountId, T::AccountId, u16, u16, u64),
268+
269+
/// Event called when transfer is toggled on a subnet.
270+
///
271+
/// Parameters:
272+
/// (netuid, bool)
273+
TransferToggle(u16, bool),
268274
}
269275
}

pallets/subtensor/src/migrations/migrate_rao.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use sp_runtime::format;
66
use substrate_fixed::types::U64F64;
77

88
use super::*;
9-
use crate::subnets::subnet::POOL_INITIAL_TAO;
109

1110
pub fn migrate_rao<T: Config>() -> Weight {
1211
let migration_name = b"migrate_rao".to_vec();
@@ -76,18 +75,23 @@ pub fn migrate_rao<T: Config>() -> Weight {
7675

7776
// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
7877
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
79-
let pool_initial_tao = POOL_INITIAL_TAO.min(lock.max(1));
78+
let pool_initial_tao = Pallet::<T>::get_network_min_lock();
79+
if lock < pool_initial_tao {
80+
let difference: u64 = pool_initial_tao.saturating_sub(lock);
81+
TotalIssuance::<T>::mutate(|total| {
82+
*total = total.saturating_add(difference);
83+
});
84+
}
8085

8186
let remaining_lock = lock.saturating_sub(pool_initial_tao);
8287
// Refund the owner for the remaining lock.
8388
Pallet::<T>::add_balance_to_coldkey_account(&owner, remaining_lock);
84-
SubnetTAO::<T>::insert(netuid, pool_initial_tao); // Set TAO to the lock.
85-
86-
SubnetAlphaIn::<T>::insert(
87-
netuid,
88-
pool_initial_tao.saturating_mul(netuids.len() as u64),
89-
); // Set AlphaIn to the initial alpha distribution.
90-
89+
SubnetLocked::<T>::insert(netuid, 0); // Clear lock amount.
90+
SubnetTAO::<T>::insert(netuid, pool_initial_tao);
91+
TotalStake::<T>::mutate(|total| {
92+
*total = total.saturating_add(pool_initial_tao);
93+
}); // Increase total stake.
94+
SubnetAlphaIn::<T>::insert(netuid, pool_initial_tao); // Set initial alpha to pool initial tao.
9195
SubnetAlphaOut::<T>::insert(netuid, 0); // Set zero subnet alpha out.
9296
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
9397
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());

pallets/subtensor/src/staking/move_stake.rs

+16
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl<T: Config> Pallet<T> {
4747
alpha_amount,
4848
None,
4949
None,
50+
false,
5051
)?;
5152

5253
// Log the event.
@@ -96,6 +97,16 @@ impl<T: Config> Pallet<T> {
9697
///
9798
/// # Events
9899
/// Emits a `StakeTransferred` event upon successful completion of the transfer.
100+
pub fn toggle_transfer(netuid: u16, toggle: bool) -> dispatch::DispatchResult {
101+
TransferToggle::<T>::insert(netuid, toggle);
102+
log::debug!(
103+
"TransferToggle( netuid: {:?}, toggle: {:?} ) ",
104+
netuid,
105+
toggle
106+
);
107+
Self::deposit_event(Event::TransferToggle(netuid, toggle));
108+
Ok(())
109+
}
99110
pub fn do_transfer_stake(
100111
origin: T::RuntimeOrigin,
101112
destination_coldkey: T::AccountId,
@@ -118,6 +129,7 @@ impl<T: Config> Pallet<T> {
118129
alpha_amount,
119130
None,
120131
None,
132+
true,
121133
)?;
122134

123135
// 9. Emit an event for logging/monitoring.
@@ -187,6 +199,7 @@ impl<T: Config> Pallet<T> {
187199
alpha_amount,
188200
None,
189201
None,
202+
false,
190203
)?;
191204

192205
// Emit an event for logging.
@@ -258,6 +271,7 @@ impl<T: Config> Pallet<T> {
258271
alpha_amount,
259272
Some(limit_price),
260273
Some(allow_partial),
274+
false,
261275
)?;
262276

263277
// Emit an event for logging.
@@ -293,6 +307,7 @@ impl<T: Config> Pallet<T> {
293307
alpha_amount: u64,
294308
maybe_limit_price: Option<u64>,
295309
maybe_allow_partial: Option<bool>,
310+
check_transfer_toggle: bool,
296311
) -> Result<u64, Error<T>> {
297312
// Calculate the maximum amount that can be executed
298313
let max_amount = if let Some(limit_price) = maybe_limit_price {
@@ -312,6 +327,7 @@ impl<T: Config> Pallet<T> {
312327
alpha_amount,
313328
max_amount,
314329
maybe_allow_partial,
330+
check_transfer_toggle,
315331
)?;
316332

317333
// Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).

pallets/subtensor/src/staking/stake_utils.rs

+13
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,7 @@ impl<T: Config> Pallet<T> {
845845
alpha_amount: u64,
846846
max_amount: u64,
847847
maybe_allow_partial: Option<bool>,
848+
check_transfer_toggle: bool,
848849
) -> Result<(), Error<T>> {
849850
// Ensure that both subnets exist.
850851
ensure!(
@@ -899,6 +900,18 @@ impl<T: Config> Pallet<T> {
899900
}
900901
}
901902

903+
if check_transfer_toggle {
904+
// Ensure transfer is toggled.
905+
ensure!(
906+
TransferToggle::<T>::get(origin_netuid),
907+
Error::<T>::TransferDisallowed
908+
);
909+
ensure!(
910+
TransferToggle::<T>::get(destination_netuid),
911+
Error::<T>::TransferDisallowed
912+
);
913+
}
914+
902915
Ok(())
903916
}
904917
}

0 commit comments

Comments
 (0)