@@ -10,6 +10,7 @@ use pallet_subtensor_swap::position::PositionId;
1010use sp_core:: U256 ;
1111use substrate_fixed:: types:: { I64F64 , I96F32 , U96F32 } ;
1212use subtensor_runtime_common:: AlphaCurrency ;
13+ use subtensor_swap_interface:: SwapHandler ;
1314
1415#[ allow( clippy:: arithmetic_side_effects) ]
1516fn close ( value : u64 , target : u64 , eps : u64 ) {
@@ -158,22 +159,49 @@ fn test_coinbase_tao_issuance_different_prices() {
158159 let emission: u64 = 100_000_000 ;
159160 add_network ( netuid1, 1 , 0 ) ;
160161 add_network ( netuid2, 1 , 0 ) ;
162+
163+ // Setup prices 0.1 and 0.2
164+ let initial_tao: u64 = 100_000_u64 ;
165+ let initial_alpha1: u64 = initial_tao * 10 ;
166+ let initial_alpha2: u64 = initial_tao * 5 ;
167+ mock:: setup_reserves ( netuid1, initial_tao, initial_alpha1. into ( ) ) ;
168+ mock:: setup_reserves ( netuid2, initial_tao, initial_alpha2. into ( ) ) ;
169+
170+ // Force the swap to initialize
171+ SubtensorModule :: swap_tao_for_alpha ( netuid1, 0 , 1_000_000_000_000 , false ) . unwrap ( ) ;
172+ SubtensorModule :: swap_tao_for_alpha ( netuid2, 0 , 1_000_000_000_000 , false ) . unwrap ( ) ;
173+
161174 // Make subnets dynamic.
162175 SubnetMechanism :: < Test > :: insert ( netuid1, 1 ) ;
163176 SubnetMechanism :: < Test > :: insert ( netuid2, 1 ) ;
177+
164178 // Set subnet prices.
165179 SubnetMovingPrice :: < Test > :: insert ( netuid1, I96F32 :: from_num ( 1 ) ) ;
166180 SubnetMovingPrice :: < Test > :: insert ( netuid2, I96F32 :: from_num ( 2 ) ) ;
181+
167182 // Assert initial TAO reserves.
168- assert_eq ! ( SubnetTAO :: <Test >:: get( netuid1) , 0 ) ;
169- assert_eq ! ( SubnetTAO :: <Test >:: get( netuid2) , 0 ) ;
183+ assert_eq ! ( SubnetTAO :: <Test >:: get( netuid1) , initial_tao) ;
184+ assert_eq ! ( SubnetTAO :: <Test >:: get( netuid2) , initial_tao) ;
185+
170186 // Run the coinbase with the emission amount.
171187 SubtensorModule :: run_coinbase ( U96F32 :: from_num ( emission) ) ;
188+
172189 // Assert tao emission is split evenly.
173- assert_eq ! ( SubnetTAO :: <Test >:: get( netuid1) , emission / 3 ) ;
174- assert_eq ! ( SubnetTAO :: <Test >:: get( netuid2) , emission / 3 + emission / 3 ) ;
175- close ( TotalIssuance :: < Test > :: get ( ) , emission, 2 ) ;
176- close ( TotalStake :: < Test > :: get ( ) , emission, 2 ) ;
190+ assert_abs_diff_eq ! (
191+ SubnetTAO :: <Test >:: get( netuid1) ,
192+ initial_tao + emission / 3 ,
193+ epsilon = 1 ,
194+ ) ;
195+ assert_abs_diff_eq ! (
196+ SubnetTAO :: <Test >:: get( netuid2) ,
197+ initial_tao + 2 * emission / 3 ,
198+ epsilon = 1 ,
199+ ) ;
200+
201+ // Prices are low => we limit tao issued (buy alpha with it)
202+ let tao_issued = ( ( 0.1 + 0.2 ) * emission as f64 ) as u64 ;
203+ assert_abs_diff_eq ! ( TotalIssuance :: <Test >:: get( ) , tao_issued, epsilon = 10 ) ;
204+ assert_abs_diff_eq ! ( TotalStake :: <Test >:: get( ) , emission, epsilon = 10 ) ;
177205 } ) ;
178206}
179207
@@ -391,17 +419,11 @@ fn test_coinbase_alpha_issuance_with_cap_trigger() {
391419 SubtensorModule :: run_coinbase ( U96F32 :: from_num ( emission) ) ;
392420 // tao_in = 333_333
393421 // alpha_in = 333_333/price > 1_000_000_000 --> 1_000_000_000 + initial_alpha
394- assert_eq ! (
395- SubnetAlphaIn :: <Test >:: get( netuid1) ,
396- ( initial_alpha + 1_000_000_000 ) . into( )
397- ) ;
422+ assert ! ( SubnetAlphaIn :: <Test >:: get( netuid1) < ( initial_alpha + 1_000_000_000 ) . into( ) ) ;
398423 assert_eq ! ( SubnetAlphaOut :: <Test >:: get( netuid2) , 1_000_000_000 . into( ) ) ;
399424 // tao_in = 666_666
400425 // alpha_in = 666_666/price > 1_000_000_000 --> 1_000_000_000 + initial_alpha
401- assert_eq ! (
402- SubnetAlphaIn :: <Test >:: get( netuid2) ,
403- ( initial_alpha + 1_000_000_000 ) . into( )
404- ) ;
426+ assert ! ( SubnetAlphaIn :: <Test >:: get( netuid2) < ( initial_alpha + 1_000_000_000 ) . into( ) ) ;
405427 assert_eq ! ( SubnetAlphaOut :: <Test >:: get( netuid2) , 1_000_000_000 . into( ) ) ; // Gets full block emission.
406428 } ) ;
407429}
@@ -415,39 +437,56 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
415437 let emission: u64 = 1_000_000 ;
416438 add_network ( netuid1, 1 , 0 ) ;
417439 add_network ( netuid2, 1 , 0 ) ;
440+
418441 // Make subnets dynamic.
419442 SubnetMechanism :: < Test > :: insert ( netuid1, 1 ) ;
420443 SubnetMechanism :: < Test > :: insert ( netuid2, 1 ) ;
421- // Setup prices 1000000
422- let initial: u64 = 1_000 ;
423- let initial_alpha = AlphaCurrency :: from ( initial * 1000000 ) ;
424- SubnetTAO :: < Test > :: insert ( netuid1, initial) ;
425- SubnetAlphaIn :: < Test > :: insert ( netuid1, initial_alpha) ; // Make price extremely low.
426- SubnetTAO :: < Test > :: insert ( netuid2, initial) ;
427- SubnetAlphaIn :: < Test > :: insert ( netuid2, initial_alpha) ; // Make price extremely low.
428- // Set issuance to greater than 21M
429- SubnetAlphaOut :: < Test > :: insert ( netuid1, AlphaCurrency :: from ( 22_000_000_000_000_000 ) ) ; // Set issuance above 21M
430- SubnetAlphaOut :: < Test > :: insert ( netuid2, AlphaCurrency :: from ( 22_000_000_000_000_000 ) ) ; // Set issuance above 21M
431- // Set subnet prices.
444+
445+ // Setup prices 0.000001
446+ let initial_tao: u64 = 10_000_u64 ;
447+ let initial_alpha: u64 = initial_tao * 100_000_u64 ;
448+ mock:: setup_reserves ( netuid1, initial_tao, initial_alpha. into ( ) ) ;
449+ mock:: setup_reserves ( netuid2, initial_tao, initial_alpha. into ( ) ) ;
450+
451+ // Enable emission
452+ FirstEmissionBlockNumber :: < Test > :: insert ( netuid1, 0 ) ;
453+ FirstEmissionBlockNumber :: < Test > :: insert ( netuid2, 0 ) ;
432454 SubnetMovingPrice :: < Test > :: insert ( netuid1, I96F32 :: from_num ( 1 ) ) ;
433455 SubnetMovingPrice :: < Test > :: insert ( netuid2, I96F32 :: from_num ( 2 ) ) ;
456+
457+ // Force the swap to initialize
458+ SubtensorModule :: swap_tao_for_alpha ( netuid1, 0 , 1_000_000_000_000 , false ) . unwrap ( ) ;
459+ SubtensorModule :: swap_tao_for_alpha ( netuid2, 0 , 1_000_000_000_000 , false ) . unwrap ( ) ;
460+
461+ // Get the prices before the run_coinbase
462+ let price_1_before = <Test as pallet:: Config >:: SwapInterface :: current_alpha_price ( netuid1) ;
463+ let price_2_before = <Test as pallet:: Config >:: SwapInterface :: current_alpha_price ( netuid2) ;
464+
465+ // Set issuance at 21M
466+ SubnetAlphaOut :: < Test > :: insert ( netuid1, AlphaCurrency :: from ( 21_000_000_000_000_000 ) ) ; // Set issuance above 21M
467+ SubnetAlphaOut :: < Test > :: insert ( netuid2, AlphaCurrency :: from ( 21_000_000_000_000_000 ) ) ; // Set issuance above 21M
468+
434469 // Run coinbase
435470 SubtensorModule :: run_coinbase ( U96F32 :: from_num ( emission) ) ;
436- // tao_in = 333_333
437- // alpha_in = 333_333/price > 1_000_000_000 --> 0 + initial_alpha
438- assert_eq ! ( SubnetAlphaIn :: <Test >:: get( netuid1) , initial_alpha) ;
471+
472+ // Get the prices after the run_coinbase
473+ let price_1_after = <Test as pallet:: Config >:: SwapInterface :: current_alpha_price ( netuid1) ;
474+ let price_2_after = <Test as pallet:: Config >:: SwapInterface :: current_alpha_price ( netuid2) ;
475+
476+ // AlphaIn gets decreased beacuse of a buy
477+ assert ! ( u64 :: from( SubnetAlphaIn :: <Test >:: get( netuid1) ) < initial_alpha) ;
439478 assert_eq ! (
440- SubnetAlphaOut :: <Test >:: get( netuid2) ,
441- 22_000_000_000_000_000 . into ( )
479+ u64 :: from ( SubnetAlphaOut :: <Test >:: get( netuid2) ) ,
480+ 21_000_000_000_000_000_u64
442481 ) ;
443- // tao_in = 666_666
444- // alpha_in = 666_666/price > 1_000_000_000 --> 0 + initial_alpha
445- assert_eq ! ( SubnetAlphaIn :: <Test >:: get( netuid2) , initial_alpha) ;
482+ assert ! ( u64 :: from( SubnetAlphaIn :: <Test >:: get( netuid2) ) < initial_alpha) ;
446483 assert_eq ! (
447- SubnetAlphaOut :: <Test >:: get( netuid2) ,
448- 22_000_000_000_000_000 . into ( )
484+ u64 :: from ( SubnetAlphaOut :: <Test >:: get( netuid2) ) ,
485+ 21_000_000_000_000_000_u64
449486 ) ;
450- // No emission.
487+
488+ assert ! ( price_1_after > price_1_before) ;
489+ assert ! ( price_2_after > price_2_before) ;
451490 } ) ;
452491}
453492
@@ -2388,7 +2427,7 @@ fn test_coinbase_v3_liquidity_update() {
23882427 let netuid = add_dynamic_network ( & owner_hotkey, & owner_coldkey) ;
23892428
23902429 // Force the swap to initialize
2391- SubtensorModule :: swap_tao_for_alpha ( netuid, 0 , 1_000_000_000_000 ) . unwrap ( ) ;
2430+ SubtensorModule :: swap_tao_for_alpha ( netuid, 0 , 1_000_000_000_000 , false ) . unwrap ( ) ;
23922431
23932432 let protocol_account_id = pallet_subtensor_swap:: Pallet :: < Test > :: protocol_account_id ( ) ;
23942433 let position = pallet_subtensor_swap:: Positions :: < Test > :: get ( (
0 commit comments