@@ -2219,72 +2219,71 @@ fn test_remove_stake_fee_goes_to_subnet_tao() {
2219
2219
} ) ;
2220
2220
}
2221
2221
2222
- // cargo test --package pallet-subtensor --lib -- tests::staking::test_add_stake_fee_equals_dividends --exact --show-output --nocapture
2222
+ // cargo test --package pallet-subtensor --lib -- tests::staking::test_remove_stake_fee_realistic_values --exact --show-output --nocapture
2223
2223
#[ test]
2224
- fn test_add_stake_fee_equals_dividends ( ) {
2224
+ fn test_remove_stake_fee_realistic_values ( ) {
2225
2225
new_test_ext ( 1 ) . execute_with ( || {
2226
2226
let subnet_owner_coldkey = U256 :: from ( 1001 ) ;
2227
2227
let subnet_owner_hotkey = U256 :: from ( 1002 ) ;
2228
2228
let hotkey = U256 :: from ( 2 ) ;
2229
2229
let coldkey = U256 :: from ( 3 ) ;
2230
- let tao_to_stake = DefaultMinStake :: < Test > :: get ( ) * 10_000 ;
2230
+ let alpha_to_unstake = 111_180_000_000 ;
2231
+ let alpha_divs = 2_816_190 ;
2231
2232
2232
2233
let netuid = add_dynamic_network ( & subnet_owner_hotkey, & subnet_owner_coldkey) ;
2233
2234
SubtensorModule :: create_account_if_non_existent ( & coldkey, & hotkey) ;
2234
2235
2236
+ // Mock a realistic scenario:
2237
+ // Subnet 1 has 3896 TAO and 128_011 Alpha in reserves, which
2238
+ // makes its price ~0.03.
2239
+ // A hotkey has 111 Alpha stake and is unstaking all Alpha.
2240
+ // Alpha dividends of this hotkey are ~0.0028
2241
+ // This makes fee be equal ~0.0028 Alpha ~= 84000 rao
2242
+ let tao_reserve: U96F32 = U96F32 :: from_num ( 3_896_056_559_708_u64 ) ;
2243
+ let alpha_in: U96F32 = U96F32 :: from_num ( 128_011_331_299_964_u64 ) ;
2244
+ SubnetTAO :: < Test > :: insert ( netuid, tao_reserve. to_num :: < u64 > ( ) ) ;
2245
+ SubnetAlphaIn :: < Test > :: insert ( netuid, alpha_in. to_num :: < u64 > ( ) ) ;
2246
+ AlphaDividendsPerSubnet :: < Test > :: insert ( netuid, hotkey, alpha_divs) ;
2247
+ let current_price = SubtensorModule :: get_alpha_price ( netuid) . to_num :: < f64 > ( ) ;
2248
+
2235
2249
// Add stake first time to init TotalHotkeyAlpha
2236
- SubtensorModule :: add_balance_to_coldkey_account (
2250
+ SubtensorModule :: increase_stake_for_hotkey_and_coldkey_on_subnet (
2251
+ & hotkey,
2237
2252
& coldkey,
2238
- 2 * ( tao_to_stake + ExistentialDeposit :: get ( ) ) ,
2239
- ) ;
2240
- assert_ok ! ( SubtensorModule :: add_stake(
2241
- RuntimeOrigin :: signed( coldkey) ,
2242
- hotkey,
2243
- netuid,
2244
- tao_to_stake
2245
- ) ) ;
2246
-
2247
- // Mock 0.001 per alpha dividends
2248
- AlphaDividendsPerSubnet :: < Test > :: insert (
2249
2253
netuid,
2250
- hotkey,
2251
- TotalHotkeyAlpha :: < Test > :: get ( hotkey, netuid) / 1000 ,
2254
+ alpha_to_unstake,
2252
2255
) ;
2253
2256
2254
- // Mock price to be 1 and a lot of liquidity
2255
- let tao_reserve: U96F32 = U96F32 :: from_num ( 100_000_000_000_000_u64 ) ;
2256
- let alpha_in: U96F32 = U96F32 :: from_num ( 100_000_000_000_000_u64 ) ;
2257
- SubnetTAO :: < Test > :: insert ( netuid, tao_reserve. to_num :: < u64 > ( ) ) ;
2258
- SubnetAlphaIn :: < Test > :: insert ( netuid, alpha_in. to_num :: < u64 > ( ) ) ;
2257
+ // Estimate fees
2258
+ let expected_fee: f64 = current_price * alpha_divs as f64 ;
2259
2259
2260
- // Add stake again to measure fee
2261
- let alpha_before =
2262
- SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, netuid) ;
2263
- assert_ok ! ( SubtensorModule :: add_stake(
2260
+ // Remove stake to measure fee
2261
+ let balance_before = SubtensorModule :: get_coldkey_balance ( & coldkey) ;
2262
+ let expected_tao_no_fee =
2263
+ SubtensorModule :: sim_swap_alpha_for_tao ( netuid, alpha_to_unstake) . unwrap ( ) ;
2264
+
2265
+ assert_ok ! ( SubtensorModule :: remove_stake(
2264
2266
RuntimeOrigin :: signed( coldkey) ,
2265
2267
hotkey,
2266
2268
netuid,
2267
- tao_to_stake
2269
+ alpha_to_unstake
2268
2270
) ) ;
2269
2271
2270
2272
// Calculate expected fee
2271
- let expected_alpha_no_fee = tao_to_stake;
2272
- let alpha_after =
2273
- SubtensorModule :: get_stake_for_hotkey_and_coldkey_on_subnet ( & hotkey, & coldkey, netuid) ;
2274
-
2275
- let actual_fee = expected_alpha_no_fee - ( alpha_after - alpha_before) ;
2276
- let expected_fee =
2277
- ( expected_alpha_no_fee as f64 * 0.001 * Tempo :: < Test > :: get ( netuid) as f64 ) as u64 ;
2273
+ let balance_after = SubtensorModule :: get_coldkey_balance ( & coldkey) ;
2274
+ let actual_fee = expected_tao_no_fee as f64 - ( balance_after - balance_before) as f64 ;
2275
+ log:: info!( "Actual fee: {:?}" , actual_fee) ;
2278
2276
2279
- assert_abs_diff_eq ! ( actual_fee, expected_fee, epsilon = expected_fee / 1000 ) ;
2277
+ assert_abs_diff_eq ! (
2278
+ actual_fee as u64 ,
2279
+ expected_fee as u64 ,
2280
+ epsilon = expected_fee as u64 / 1000
2281
+ ) ;
2280
2282
} ) ;
2281
2283
}
2282
2284
2283
2285
#[ test]
2284
2286
fn test_stake_below_min_validate ( ) {
2285
- // Testing the signed extension validate function
2286
- // correctly filters the `add_stake` transaction.
2287
-
2288
2287
new_test_ext ( 0 ) . execute_with ( || {
2289
2288
let subnet_owner_coldkey = U256 :: from ( 1001 ) ;
2290
2289
let subnet_owner_hotkey = U256 :: from ( 1002 ) ;
0 commit comments