@@ -462,6 +462,10 @@ impl<T: Config> Pallet<T> {
462462 SubnetVolume :: < T > :: remove ( netuid) ;
463463 SubnetMovingPrice :: < T > :: remove ( netuid) ;
464464
465+ // --- 12. Add the balance back to the owner.
466+ SubnetOwner :: < T > :: remove ( netuid) ;
467+
468+ // --- 13. Remove subnet identity if it exists.
465469 if SubnetIdentitiesV3 :: < T > :: contains_key ( netuid) {
466470 SubnetIdentitiesV3 :: < T > :: remove ( netuid) ;
467471 Self :: deposit_event ( Event :: SubnetIdentityRemoved ( netuid) ) ;
@@ -584,49 +588,54 @@ impl<T: Config> Pallet<T> {
584588
585589 // 2. Basic info.
586590 let owner_coldkey: T :: AccountId = SubnetOwner :: < T > :: get ( netuid) ;
587- let lock_cost : u64 = Self :: get_subnet_locked_balance ( netuid) ;
591+ let lock_cost_u64 : u64 = Self :: get_subnet_locked_balance ( netuid) . into ( ) ;
588592
589593 // Owner-cut already received from emissions.
590- let total_emission: u64 = Emission :: < T > :: get ( netuid) . iter ( ) . sum ( ) ;
594+ let total_emission_u64: u64 = Emission :: < T > :: get ( netuid)
595+ . into_iter ( )
596+ . map ( Into :: < u64 > :: into)
597+ . sum ( ) ;
591598 let owner_fraction = Self :: get_float_subnet_owner_cut ( ) ;
592- let owner_received_emission = U96F32 :: from_num ( total_emission )
599+ let owner_received_emission_u64 = U96F32 :: from_num ( total_emission_u64 )
593600 . saturating_mul ( owner_fraction)
594601 . floor ( )
595602 . saturating_to_num :: < u64 > ( ) ;
596603
597- // 3. Gather α-out stakers.
598- let mut total_alpha_out : u128 = 0 ;
604+ // 3. Gather α-out stakers (U64F64 -> use raw bits as weights) .
605+ let mut total_alpha_bits : u128 = 0 ;
599606 let mut stakers: Vec < ( T :: AccountId , T :: AccountId , u128 ) > = Vec :: new ( ) ;
600607
601608 for ( ( hot, cold, this_netuid) , alpha) in Alpha :: < T > :: iter ( ) {
602609 if this_netuid == netuid {
603- let a = alpha. saturating_to_num :: < u128 > ( ) ;
604- total_alpha_out = total_alpha_out . saturating_add ( a ) ;
605- stakers. push ( ( hot, cold, a ) ) ;
610+ let a_bits : u128 = alpha. to_bits ( ) ; // <- was `alpha.into()`; that doesn't exist
611+ total_alpha_bits = total_alpha_bits . saturating_add ( a_bits ) ;
612+ stakers. push ( ( hot, cold, a_bits ) ) ;
606613 }
607614 }
608615
609- // 4. Pro- rata distribution – TAO restaked to ROOT.
610- let subnet_tao : u128 = SubnetTAO :: < T > :: get ( netuid) as u128 ;
616+ // 4. Pro‑ rata distribution – TAO restaked to ROOT.
617+ let subnet_tao_u64 : u64 = SubnetTAO :: < T > :: get ( netuid) . into ( ) ;
611618 let root_netuid = NetUid :: ROOT ;
612619
613- if total_alpha_out > 0 && subnet_tao > 0 && !stakers. is_empty ( ) {
620+ if total_alpha_bits > 0 && subnet_tao_u64 > 0 && !stakers. is_empty ( ) {
614621 struct Portion < A , C > {
615622 hot : A ,
616623 cold : C ,
617624 share : u64 ,
618625 rem : u128 ,
619626 }
627+
628+ let pot_u128 = subnet_tao_u64 as u128 ;
620629 let mut portions: Vec < Portion < _ , _ > > = Vec :: with_capacity ( stakers. len ( ) ) ;
621630 let mut distributed: u128 = 0 ;
622631
623- for ( hot, cold, a ) in & stakers {
624- let prod = subnet_tao . saturating_mul ( * a ) ;
625- let share_u128 = prod. checked_div ( total_alpha_out ) . unwrap_or_default ( ) ;
632+ for ( hot, cold, a_bits ) in & stakers {
633+ let prod = pot_u128 . saturating_mul ( * a_bits ) ;
634+ let share_u128 = prod. checked_div ( total_alpha_bits ) . unwrap_or_default ( ) ;
626635 let share_u64 = share_u128. min ( u64:: MAX as u128 ) as u64 ;
627636 distributed = distributed. saturating_add ( share_u64 as u128 ) ;
628637
629- let rem = prod. checked_rem ( total_alpha_out ) . unwrap_or_default ( ) ;
638+ let rem = prod. checked_rem ( total_alpha_bits ) . unwrap_or_default ( ) ;
630639 portions. push ( Portion {
631640 hot : hot. clone ( ) ,
632641 cold : cold. clone ( ) ,
@@ -635,20 +644,27 @@ impl<T: Config> Pallet<T> {
635644 } ) ;
636645 }
637646
638- // Handle leftover (< stakers.len() ).
639- let leftover = subnet_tao . saturating_sub ( distributed) ;
647+ // Largest‑remainder method; clamp for wasm32 (usize = 32‑bit ).
648+ let leftover = pot_u128 . saturating_sub ( distributed) ;
640649 if leftover > 0 {
641650 portions. sort_by ( |a, b| b. rem . cmp ( & a. rem ) ) ;
642- for p in portions. iter_mut ( ) . take ( leftover as usize ) {
651+ let give = core:: cmp:: min ( leftover, portions. len ( ) as u128 ) as usize ;
652+ for p in portions. iter_mut ( ) . take ( give) {
643653 p. share = p. share . saturating_add ( 1 ) ;
644654 }
645655 }
646656
647657 // Restake into root and clean α records.
648658 for p in portions {
649659 if p. share > 0 {
650- // Zero-fee restake of TAO into the root network.
651- Self :: stake_into_subnet ( & p. hot , & p. cold , root_netuid, p. share , 0u64 ) ?;
660+ Self :: stake_into_subnet (
661+ & p. hot ,
662+ & p. cold ,
663+ root_netuid,
664+ p. share . into ( ) ,
665+ TaoCurrency :: from ( 0 ) ,
666+ false ,
667+ ) ?;
652668 }
653669 Alpha :: < T > :: remove ( ( & p. hot , & p. cold , netuid) ) ;
654670 }
@@ -659,42 +675,44 @@ impl<T: Config> Pallet<T> {
659675 }
660676 }
661677
662- // 5. Reset α in/out counters.
663- SubnetAlphaIn :: < T > :: insert ( netuid, 0 ) ;
664- SubnetAlphaOut :: < T > :: insert ( netuid, 0 ) ;
678+ // 5. Reset α in/out counters — use typed zeros (no inference issues) .
679+ SubnetAlphaIn :: < T > :: insert ( netuid, AlphaCurrency :: from ( 0 ) ) ;
680+ SubnetAlphaOut :: < T > :: insert ( netuid, AlphaCurrency :: from ( 0 ) ) ;
665681
666682 // 6. Refund remaining lock to subnet owner.
667- let refund = lock_cost. saturating_sub ( owner_received_emission) ;
668- Self :: set_subnet_locked_balance ( netuid, 0 ) ;
669- if refund > 0 {
670- Self :: add_balance_to_coldkey_account ( & owner_coldkey, refund) ;
683+ let refund_u64 = lock_cost_u64. saturating_sub ( owner_received_emission_u64) ;
684+ Self :: set_subnet_locked_balance ( netuid, TaoCurrency :: from ( 0 ) ) ;
685+ if refund_u64 > 0 {
686+ // This helper expects runtime Balance (u64), not TaoCurrency.
687+ Self :: add_balance_to_coldkey_account ( & owner_coldkey, refund_u64) ;
671688 }
672689
673690 Ok ( ( ) )
674691 }
675-
676692 pub fn get_network_to_prune ( ) -> Option < NetUid > {
677693 let current_block: u64 = Self :: get_current_block_as_u64 ( ) ;
678694 let total_networks: u16 = TotalNetworks :: < T > :: get ( ) ;
679695
680696 let mut candidate_netuid: Option < NetUid > = None ;
681- let mut candidate_emission = u64:: MAX ;
682- let mut candidate_timestamp = u64:: MAX ;
697+ let mut candidate_emission: u64 = u64:: MAX ;
698+ let mut candidate_timestamp: u64 = u64:: MAX ;
683699
684700 for net in 1 ..=total_networks {
685701 let netuid: NetUid = net. into ( ) ;
686702 let registered_at = NetworkRegisteredAt :: < T > :: get ( netuid) ;
687703
688- // Skip immune networks
704+ // Skip immune networks.
689705 if current_block < registered_at. saturating_add ( Self :: get_network_immunity_period ( ) ) {
690706 continue ;
691707 }
692708
693- // We want total emission across all UIDs in this subnet:
694- let emission_vec = Emission :: < T > :: get ( netuid) ;
695- let total_emission = emission_vec. iter ( ) . sum :: < u64 > ( ) ;
709+ // Sum AlphaCurrency as u64 for comparison.
710+ let total_emission: u64 = Emission :: < T > :: get ( netuid)
711+ . into_iter ( )
712+ . map ( Into :: < u64 > :: into)
713+ . sum ( ) ;
696714
697- // If tie on total_emission, earliest registration wins
715+ // If tie on total_emission, earliest registration wins.
698716 if total_emission < candidate_emission
699717 || ( total_emission == candidate_emission && registered_at < candidate_timestamp)
700718 {
0 commit comments