@@ -572,53 +572,54 @@ impl<T: Config> Pallet<T> {
572572 ) -> Result < SwapResult , DispatchError > {
573573 // Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic)
574574 let mechanism_id: u16 = SubnetMechanism :: < T > :: get ( netuid) ;
575- if mechanism_id == 1 {
576- let swap_result = T :: SwapInterface :: swap (
575+ let swap_result = if mechanism_id == 1 {
576+ T :: SwapInterface :: swap (
577577 netuid. into ( ) ,
578578 OrderType :: Buy ,
579579 tao. into ( ) ,
580580 price_limit. into ( ) ,
581581 drop_fees,
582582 false ,
583- ) ?;
584- let alpha_decrease =
585- AlphaCurrency :: from ( swap_result. alpha_reserve_delta . unsigned_abs ( ) ) ;
586-
587- // Decrease Alpha reserves.
588- Self :: decrease_provided_alpha_reserve ( netuid. into ( ) , alpha_decrease) ;
589-
590- // Increase Alpha outstanding.
591- SubnetAlphaOut :: < T > :: mutate ( netuid, |total| {
592- * total = total. saturating_add ( swap_result. amount_paid_out . into ( ) ) ;
593- } ) ;
594-
595- // Increase only the protocol TAO reserve. We only use the sum of
596- // (SubnetTAO + SubnetTaoProvided) in tao_reserve(), so it is irrelevant
597- // which one to increase.
598- SubnetTAO :: < T > :: mutate ( netuid, |total| {
599- * total = total. saturating_add ( ( swap_result. tao_reserve_delta as u64 ) . into ( ) ) ;
600- } ) ;
601-
602- // Increase Total Tao reserves.
603- TotalStake :: < T > :: mutate ( |total| * total = total. saturating_add ( tao) ) ;
604-
605- // Increase total subnet TAO volume.
606- SubnetVolume :: < T > :: mutate ( netuid, |total| {
607- * total = total. saturating_add ( tao. to_u64 ( ) as u128 ) ;
608- } ) ;
609-
610- // Return the alpha received.
611- Ok ( swap_result)
583+ ) ?
612584 } else {
585+ let abs_delta: u64 = tao. into ( ) ;
586+
613587 // Step 3.b.1: Stable mechanism, just return the value 1:1
614- Ok ( SwapResult {
588+ SwapResult {
615589 amount_paid_in : tao. into ( ) ,
616590 amount_paid_out : tao. into ( ) ,
617591 fee_paid : 0 ,
618- tao_reserve_delta : 0 ,
619- alpha_reserve_delta : 0 ,
620- } )
621- }
592+ tao_reserve_delta : abs_delta as i64 ,
593+ alpha_reserve_delta : ( abs_delta as i64 ) . neg ( ) ,
594+ }
595+ } ;
596+
597+ let alpha_decrease = AlphaCurrency :: from ( swap_result. alpha_reserve_delta . unsigned_abs ( ) ) ;
598+
599+ // Decrease Alpha reserves.
600+ Self :: decrease_provided_alpha_reserve ( netuid. into ( ) , alpha_decrease) ;
601+
602+ // Increase Alpha outstanding.
603+ SubnetAlphaOut :: < T > :: mutate ( netuid, |total| {
604+ * total = total. saturating_add ( swap_result. amount_paid_out . into ( ) ) ;
605+ } ) ;
606+
607+ // Increase only the protocol TAO reserve. We only use the sum of
608+ // (SubnetTAO + SubnetTaoProvided) in tao_reserve(), so it is irrelevant
609+ // which one to increase.
610+ SubnetTAO :: < T > :: mutate ( netuid, |total| {
611+ * total = total. saturating_add ( ( swap_result. tao_reserve_delta as u64 ) . into ( ) ) ;
612+ } ) ;
613+
614+ // Increase Total Tao reserves.
615+ TotalStake :: < T > :: mutate ( |total| * total = total. saturating_add ( tao) ) ;
616+
617+ // Increase total subnet TAO volume.
618+ SubnetVolume :: < T > :: mutate ( netuid, |total| {
619+ * total = total. saturating_add ( tao. to_u64 ( ) as u128 ) ;
620+ } ) ;
621+
622+ Ok ( swap_result)
622623 }
623624
624625 /// Swaps a subnet's Alpha token for TAO.
@@ -633,62 +634,64 @@ impl<T: Config> Pallet<T> {
633634 // Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic)
634635 let mechanism_id: u16 = SubnetMechanism :: < T > :: get ( netuid) ;
635636 // Step 2: Swap alpha and attain tao
636- if mechanism_id == 1 {
637- let swap_result = T :: SwapInterface :: swap (
637+ let swap_result = if mechanism_id == 1 {
638+ T :: SwapInterface :: swap (
638639 netuid. into ( ) ,
639640 OrderType :: Sell ,
640641 alpha. into ( ) ,
641642 price_limit. into ( ) ,
642643 drop_fees,
643644 false ,
644- ) ?;
645-
646- // Increase only the protocol Alpha reserve. We only use the sum of
647- // (SubnetAlphaIn + SubnetAlphaInProvided) in alpha_reserve(), so it is irrelevant
648- // which one to increase.
649- SubnetAlphaIn :: < T > :: mutate ( netuid, |total| {
650- * total = total. saturating_add ( ( swap_result. alpha_reserve_delta as u64 ) . into ( ) ) ;
651- } ) ;
652-
653- // Decrease Alpha outstanding.
654- // TODO: Deprecate, not accurate in v3 anymore
655- SubnetAlphaOut :: < T > :: mutate ( netuid, |total| {
656- * total = total. saturating_sub ( ( swap_result. alpha_reserve_delta as u64 ) . into ( ) ) ;
657- } ) ;
658-
659- // Decrease tao reserves.
660- Self :: decrease_provided_tao_reserve (
661- netuid. into ( ) ,
662- swap_result
663- . tao_reserve_delta
664- . abs ( )
665- . try_into ( )
666- . unwrap_or ( 0 )
667- . into ( ) ,
668- ) ;
669-
670- // Reduce total TAO reserves.
671- TotalStake :: < T > :: mutate ( |total| {
672- * total = total. saturating_sub ( swap_result. amount_paid_out . into ( ) )
673- } ) ;
674-
675- // Increase total subnet TAO volume.
676- SubnetVolume :: < T > :: mutate ( netuid, |total| {
677- * total = total. saturating_add ( swap_result. amount_paid_out . into ( ) )
678- } ) ;
679-
680- // Return the tao received.
681- Ok ( swap_result)
645+ ) ?
682646 } else {
647+ let abs_delta: u64 = alpha. into ( ) ;
648+
683649 // Step 3.b.1: Stable mechanism, just return the value 1:1
684- Ok ( SwapResult {
650+ SwapResult {
685651 amount_paid_in : alpha. into ( ) ,
686652 amount_paid_out : alpha. into ( ) ,
687653 fee_paid : 0 ,
688- tao_reserve_delta : 0 ,
689- alpha_reserve_delta : 0 ,
690- } )
691- }
654+ tao_reserve_delta : ( abs_delta as i64 ) . neg ( ) ,
655+ alpha_reserve_delta : abs_delta as i64 ,
656+ }
657+ } ;
658+
659+ // Increase only the protocol Alpha reserve. We only use the sum of
660+ // (SubnetAlphaIn + SubnetAlphaInProvided) in alpha_reserve(), so it is irrelevant
661+ // which one to increase.
662+ SubnetAlphaIn :: < T > :: mutate ( netuid, |total| {
663+ * total = total. saturating_add ( ( swap_result. alpha_reserve_delta as u64 ) . into ( ) ) ;
664+ } ) ;
665+
666+ // Decrease Alpha outstanding.
667+ // TODO: Deprecate, not accurate in v3 anymore
668+ SubnetAlphaOut :: < T > :: mutate ( netuid, |total| {
669+ * total = total. saturating_sub ( ( swap_result. alpha_reserve_delta as u64 ) . into ( ) ) ;
670+ } ) ;
671+
672+ // Decrease tao reserves.
673+ Self :: decrease_provided_tao_reserve (
674+ netuid. into ( ) ,
675+ swap_result
676+ . tao_reserve_delta
677+ . abs ( )
678+ . try_into ( )
679+ . unwrap_or ( 0 )
680+ . into ( ) ,
681+ ) ;
682+
683+ // Reduce total TAO reserves.
684+ TotalStake :: < T > :: mutate ( |total| {
685+ * total = total. saturating_sub ( swap_result. amount_paid_out . into ( ) )
686+ } ) ;
687+
688+ // Increase total subnet TAO volume.
689+ SubnetVolume :: < T > :: mutate ( netuid, |total| {
690+ * total = total. saturating_add ( swap_result. amount_paid_out . into ( ) )
691+ } ) ;
692+
693+ // Return the tao received.
694+ Ok ( swap_result)
692695 }
693696
694697 /// Unstakes alpha from a subnet for a given hotkey and coldkey pair.
0 commit comments