@@ -3412,3 +3412,42 @@ fn test_coinbase_alpha_in_more_than_alpha_emission() {
34123412 ) ;
34133413 } ) ;
34143414}
3415+
3416+ // Tests for the excess TAO condition
3417+ #[ test]
3418+ fn test_coinbase_inject_and_maybe_swap_does_not_skew_reserves ( ) {
3419+ new_test_ext ( 1 ) . execute_with ( || {
3420+ let zero = U96F32 :: saturating_from_num ( 0 ) ;
3421+ let netuid0 = add_dynamic_network ( & U256 :: from ( 1 ) , & U256 :: from ( 2 ) ) ;
3422+ mock:: setup_reserves (
3423+ netuid0,
3424+ TaoCurrency :: from ( 1_000_000_000_000_000 ) ,
3425+ AlphaCurrency :: from ( 1_000_000_000_000_000 ) ,
3426+ ) ;
3427+ // Initialize swap v3
3428+ Swap :: maybe_initialize_v3 ( netuid0) ;
3429+
3430+ let tao_in = BTreeMap :: from ( [ ( netuid0, U96F32 :: saturating_from_num ( 123 ) ) ] ) ;
3431+ let alpha_in = BTreeMap :: from ( [ ( netuid0, U96F32 :: saturating_from_num ( 456 ) ) ] ) ;
3432+ let excess_tao = BTreeMap :: from ( [ ( netuid0, U96F32 :: saturating_from_num ( 789100 ) ) ] ) ;
3433+
3434+ // Run the inject and maybe swap
3435+ SubtensorModule :: inject_and_maybe_swap ( & [ netuid0] , & tao_in, & alpha_in, & excess_tao) ;
3436+
3437+ let tao_in_after = SubnetTAO :: < Test > :: get ( netuid0) ;
3438+ let alpha_in_after = SubnetAlphaIn :: < Test > :: get ( netuid0) ;
3439+
3440+ // Make sure that when we inject and swap, we do it in the right order.
3441+ // Thereby not skewing the ratio away from the price.
3442+ let ratio_after: U96F32 = U96F32 :: saturating_from_num ( alpha_in_after. to_u64 ( ) )
3443+ . saturating_div ( U96F32 :: saturating_from_num ( tao_in_after. to_u64 ( ) ) ) ;
3444+ let price_after: U96F32 = U96F32 :: saturating_from_num (
3445+ pallet_subtensor_swap:: Pallet :: < Test > :: current_alpha_price ( netuid0) . to_num :: < f64 > ( ) ,
3446+ ) ;
3447+ assert_abs_diff_eq ! (
3448+ ratio_after. to_num:: <f64 >( ) ,
3449+ price_after. to_num:: <f64 >( ) ,
3450+ epsilon = 1.0
3451+ ) ;
3452+ } ) ;
3453+ }
0 commit comments