@@ -2206,6 +2206,122 @@ fn test_stake_below_min_validate() {
2206
2206
} ) ;
2207
2207
}
2208
2208
2209
+ // cargo test --package pallet-subtensor --lib -- tests::staking::test_add_stake_limit_validate --exact --show-output
2210
+ #[ test]
2211
+ fn test_add_stake_limit_validate ( ) {
2212
+ // Testing the signed extension validate function
2213
+ // correctly filters the `add_stake` transaction.
2214
+
2215
+ new_test_ext ( 0 ) . execute_with ( || {
2216
+ let hotkey = U256 :: from ( 533453 ) ;
2217
+ let coldkey = U256 :: from ( 55453 ) ;
2218
+ let amount = 300_000_000_000 ;
2219
+
2220
+ // add network
2221
+ let netuid: u16 = add_dynamic_network ( & hotkey, & coldkey) ;
2222
+
2223
+ // Force-set alpha in and tao reserve to make price equal 1.5
2224
+ let tao_reserve: U96F32 = U96F32 :: from_num ( 150_000_000_000_u64 ) ;
2225
+ let alpha_in: U96F32 = U96F32 :: from_num ( 100_000_000_000_u64 ) ;
2226
+ SubnetTAO :: < Test > :: insert ( netuid, tao_reserve. to_num :: < u64 > ( ) ) ;
2227
+ SubnetAlphaIn :: < Test > :: insert ( netuid, alpha_in. to_num :: < u64 > ( ) ) ;
2228
+ let current_price: U96F32 = U96F32 :: from_num ( SubtensorModule :: get_alpha_price ( netuid) ) ;
2229
+ assert_eq ! ( current_price, U96F32 :: from_num( 1.5 ) ) ;
2230
+
2231
+ // Give it some $$$ in his coldkey balance
2232
+ SubtensorModule :: add_balance_to_coldkey_account ( & coldkey, amount) ;
2233
+
2234
+ // Setup limit price so that it doesn't peak above 4x of current price
2235
+ // The amount that can be executed at this price is 150 TAO only
2236
+ // Alpha produced will be equal to 50 = 100 - 150*100/300
2237
+ let limit_price = 6_000_000_000 ;
2238
+
2239
+ // Add stake limit call
2240
+ let call = RuntimeCall :: SubtensorModule ( SubtensorCall :: add_stake_limit {
2241
+ hotkey,
2242
+ netuid,
2243
+ amount_staked : amount,
2244
+ limit_price,
2245
+ allow_partial : false ,
2246
+ } ) ;
2247
+
2248
+ let info: crate :: DispatchInfo =
2249
+ crate :: DispatchInfoOf :: < <Test as frame_system:: Config >:: RuntimeCall > :: default ( ) ;
2250
+
2251
+ let extension = crate :: SubtensorSignedExtension :: < Test > :: new ( ) ;
2252
+ // Submit to the signed extension validate function
2253
+ let result_no_stake = extension. validate ( & coldkey, & call. clone ( ) , & info, 10 ) ;
2254
+
2255
+ // Should fail due to slippage
2256
+ assert_err ! (
2257
+ result_no_stake,
2258
+ crate :: TransactionValidityError :: Invalid ( crate :: InvalidTransaction :: Custom (
2259
+ CustomTransactionError :: SlippageTooHigh . into( )
2260
+ ) )
2261
+ ) ;
2262
+ } ) ;
2263
+ }
2264
+
2265
+ // cargo test --package pallet-subtensor --lib -- tests::staking::test_remove_stake_limit_validate --exact --show-output
2266
+ #[ test]
2267
+ fn test_remove_stake_limit_validate ( ) {
2268
+ // Testing the signed extension validate function
2269
+ // correctly filters the `add_stake` transaction.
2270
+
2271
+ new_test_ext ( 0 ) . execute_with ( || {
2272
+ let hotkey = U256 :: from ( 533453 ) ;
2273
+ let coldkey = U256 :: from ( 55453 ) ;
2274
+ let stake_amount = 300_000_000_000 ;
2275
+ let unstake_amount = 150_000_000_000 ;
2276
+
2277
+ // add network
2278
+ let netuid: u16 = add_dynamic_network ( & hotkey, & coldkey) ;
2279
+
2280
+ // Give the neuron some stake to remove
2281
+ SubtensorModule :: increase_stake_for_hotkey_and_coldkey_on_subnet (
2282
+ & hotkey,
2283
+ & coldkey,
2284
+ netuid,
2285
+ stake_amount,
2286
+ ) ;
2287
+
2288
+ // Forse-set alpha in and tao reserve to make price equal 1.5
2289
+ let tao_reserve: U96F32 = U96F32 :: from_num ( 150_000_000_000_u64 ) ;
2290
+ let alpha_in: U96F32 = U96F32 :: from_num ( 100_000_000_000_u64 ) ;
2291
+ SubnetTAO :: < Test > :: insert ( netuid, tao_reserve. to_num :: < u64 > ( ) ) ;
2292
+ SubnetAlphaIn :: < Test > :: insert ( netuid, alpha_in. to_num :: < u64 > ( ) ) ;
2293
+ let current_price: U96F32 = U96F32 :: from_num ( SubtensorModule :: get_alpha_price ( netuid) ) ;
2294
+ assert_eq ! ( current_price, U96F32 :: from_num( 1.5 ) ) ;
2295
+
2296
+ // Setup limit price so that it doesn't drop by more than 10% from current price
2297
+ let limit_price = 1_350_000_000 ;
2298
+
2299
+ // Remove stake limit call
2300
+ let call = RuntimeCall :: SubtensorModule ( SubtensorCall :: remove_stake_limit {
2301
+ hotkey,
2302
+ netuid,
2303
+ amount_unstaked : unstake_amount,
2304
+ limit_price,
2305
+ allow_partial : false ,
2306
+ } ) ;
2307
+
2308
+ let info: crate :: DispatchInfo =
2309
+ crate :: DispatchInfoOf :: < <Test as frame_system:: Config >:: RuntimeCall > :: default ( ) ;
2310
+
2311
+ let extension = crate :: SubtensorSignedExtension :: < Test > :: new ( ) ;
2312
+ // Submit to the signed extension validate function
2313
+ let result_no_stake = extension. validate ( & coldkey, & call. clone ( ) , & info, 10 ) ;
2314
+
2315
+ // Should fail due to slippage
2316
+ assert_err ! (
2317
+ result_no_stake,
2318
+ crate :: TransactionValidityError :: Invalid ( crate :: InvalidTransaction :: Custom (
2319
+ CustomTransactionError :: SlippageTooHigh . into( )
2320
+ ) )
2321
+ ) ;
2322
+ } ) ;
2323
+ }
2324
+
2209
2325
#[ test]
2210
2326
fn test_stake_overflow ( ) {
2211
2327
new_test_ext ( 1 ) . execute_with ( || {
0 commit comments