@@ -31,7 +31,10 @@ use sp_state_machine::BasicExternalities;
31
31
32
32
use frame_support:: {
33
33
derive_impl, parameter_types, traits:: { ConstU64 , WithdrawReasons , Currency , ReservableCurrency ,
34
- SignedImbalance , NamedReservableCurrency , BalanceStatus }
34
+ SignedImbalance , tokens:: { fungible:: {
35
+ hold:: { Mutate as HoldMutate , Inspect as HoldInspect , Unbalanced as UnbalancedHold } ,
36
+ Inspect as FungibleInspect , Unbalanced as FungibleUnbalanced , Dust
37
+ } , Preservation , Fortitude } } ,
35
38
} ;
36
39
37
40
impl_opaque_keys ! {
@@ -263,10 +266,10 @@ pub(crate) const DISABLING_LIMIT_FACTOR: usize = 3;
263
266
pub type SessionKeysId = [ u8 ; 12 ] ;
264
267
pub const TEST_SESSION_KEYS_ID : SessionKeysId = * b"session_keys" ;
265
268
266
- // Define TestReserveIdentifier with the necessary traits
269
+ // Define TestHoldReason with the necessary traits
267
270
#[ derive( Debug , Clone , PartialEq , Eq , codec:: Encode , codec:: Decode , scale_info:: TypeInfo ) ]
268
- pub struct TestReserveIdentifier ;
269
- impl Get < SessionKeysId > for TestReserveIdentifier {
271
+ pub struct TestHoldReason ;
272
+ impl Get < SessionKeysId > for TestHoldReason {
270
273
fn get ( ) -> SessionKeysId {
271
274
TEST_SESSION_KEYS_ID
272
275
}
@@ -288,7 +291,7 @@ impl Config for Test {
288
291
disabling:: UpToLimitWithReEnablingDisablingStrategy < DISABLING_LIMIT_FACTOR > ;
289
292
type WeightInfo = ( ) ;
290
293
type Currency = pallet_balances:: Pallet < Test > ;
291
- type ReserveIdentifier = TestReserveIdentifier ;
294
+ type HoldReason = TestHoldReason ;
292
295
type KeyDeposit = KeyDeposit ;
293
296
}
294
297
@@ -301,6 +304,10 @@ impl crate::historical::Config for Test {
301
304
pub mod pallet_balances {
302
305
use super :: * ;
303
306
use frame_support:: pallet_prelude:: * ;
307
+ use frame_support:: traits:: {
308
+ tokens:: { WithdrawConsequence , Provenance , DepositConsequence } ,
309
+ fungible:: Dust ,
310
+ } ;
304
311
305
312
pub struct Pallet < T > ( core:: marker:: PhantomData < T > ) ;
306
313
@@ -466,12 +473,76 @@ pub mod pallet_balances {
466
473
}
467
474
}
468
475
469
- impl < T > NamedReservableCurrency < u64 > for Pallet < T > {
470
- type ReserveIdentifier = [ u8 ; 12 ] ;
476
+ impl < T > FungibleInspect < u64 > for Pallet < T > {
477
+ type Balance = u64 ;
478
+
479
+ fn total_issuance ( ) -> Self :: Balance {
480
+ CurrencyBalance :: get ( )
481
+ }
482
+
483
+ fn minimum_balance ( ) -> Self :: Balance {
484
+ 0
485
+ }
486
+
487
+ fn balance ( _who : & u64 ) -> Self :: Balance {
488
+ CurrencyBalance :: get ( )
489
+ }
490
+
491
+ fn total_balance ( _who : & u64 ) -> Self :: Balance {
492
+ CurrencyBalance :: get ( )
493
+ }
494
+
495
+ fn reducible_balance (
496
+ _who : & u64 ,
497
+ _keep_alive : Preservation ,
498
+ _force : Fortitude ,
499
+ ) -> Self :: Balance {
500
+ CurrencyBalance :: get ( )
501
+ }
502
+
503
+ fn can_deposit (
504
+ _who : & u64 ,
505
+ _amount : Self :: Balance ,
506
+ _provenance : Provenance ,
507
+ ) -> DepositConsequence {
508
+ DepositConsequence :: Success
509
+ }
510
+
511
+ fn can_withdraw (
512
+ who : & u64 ,
513
+ amount : Self :: Balance ,
514
+ ) -> WithdrawConsequence < Self :: Balance > {
515
+ if * who == 999 || amount > CurrencyBalance :: get ( ) {
516
+ WithdrawConsequence :: BalanceLow
517
+ } else {
518
+ WithdrawConsequence :: Success
519
+ }
520
+ }
521
+ }
522
+
523
+ impl < T > FungibleUnbalanced < u64 > for Pallet < T > {
524
+ fn handle_dust ( _dust : Dust < u64 , Self > ) {
525
+ // No-op in mock
526
+ }
527
+
528
+ fn write_balance (
529
+ _who : & u64 ,
530
+ _amount : Self :: Balance
531
+ ) -> Result < Option < Self :: Balance > , DispatchError > {
532
+ Ok ( None )
533
+ }
534
+
535
+ fn set_total_issuance ( _amount : Self :: Balance ) {
536
+ // No-op in mock
537
+ }
538
+ }
539
+
540
+ impl < T > HoldInspect < u64 > for Pallet < T > {
541
+ type Reason = SessionKeysId ;
471
542
472
- fn reserved_balance_named ( id : & Self :: ReserveIdentifier , who : & u64 ) -> Self :: Balance {
543
+ fn balance_on_hold ( reason : & Self :: Reason , who : & u64 ) -> Self :: Balance {
473
544
// Convert fixed array to Vec for lookup
474
- let id_vec = id . to_vec ( ) ;
545
+ let id_vec = reason . to_vec ( ) ;
475
546
476
547
ReservedBalances :: get ( )
477
548
. get ( who)
@@ -480,81 +551,42 @@ pub mod pallet_balances {
480
551
. unwrap_or ( 0 )
481
552
}
482
553
483
- fn slash_reserved_named (
484
- _id : & Self :: ReserveIdentifier ,
485
- _who : & u64 ,
486
- _amount : Self :: Balance ,
487
- ) -> ( Self :: NegativeImbalance , Self :: Balance ) {
488
- ( ( ) , 0 )
554
+ fn total_balance_on_hold ( who : & u64 ) -> Self :: Balance {
555
+ // Sum up all held balances for the account
556
+ ReservedBalances :: get ( )
557
+ . get ( who )
558
+ . map ( |holds| holds . values ( ) . sum ( ) )
559
+ . unwrap_or ( 0 )
489
560
}
490
561
491
- fn reserve_named (
492
- id : & Self :: ReserveIdentifier ,
562
+ fn can_hold ( _reason : & Self :: Reason , who : & u64 , amount : Self :: Balance ) -> bool {
563
+ // Account 999 is special and always has insufficient funds for testing
564
+ if * who == 999 {
565
+ return false
566
+ }
567
+ CurrencyBalance :: get ( ) >= amount
568
+ }
569
+ }
570
+
571
+ impl < T > UnbalancedHold < u64 > for Pallet < T > {
572
+ fn set_balance_on_hold (
573
+ reason : & Self :: Reason ,
493
574
who : & u64 ,
494
575
amount : Self :: Balance ,
495
576
) -> DispatchResult {
496
- if !Self :: can_reserve ( who, amount) {
497
- return Err ( DispatchError :: Other ( "InsufficientBalance" ) )
498
- }
499
-
500
577
// Convert fixed array to Vec for storage
501
- let id_vec = id . to_vec ( ) ;
578
+ let id_vec = reason . to_vec ( ) ;
502
579
503
- // Update the reserved balance
580
+ // Update the held balance
504
581
ReservedBalances :: mutate ( |balances| {
505
- let account_reserves = balances. entry ( * who) . or_insert_with ( BTreeMap :: new) ;
506
- let reserved = account_reserves. entry ( id_vec) . or_insert ( 0 ) ;
507
- * reserved += amount;
582
+ let account_holds = balances. entry ( * who) . or_insert_with ( BTreeMap :: new) ;
583
+ account_holds. insert ( id_vec, amount) ;
508
584
} ) ;
509
585
510
586
Ok ( ( ) )
511
587
}
512
-
513
- fn unreserve_named (
514
- id : & Self :: ReserveIdentifier ,
515
- who : & u64 ,
516
- amount : Self :: Balance ,
517
- ) -> Self :: Balance {
518
- // Convert fixed array to Vec for lookup/storage
519
- let id_vec = id. to_vec ( ) ;
520
-
521
- // Get the current reserved amount
522
- let mut remaining = amount;
523
- ReservedBalances :: mutate ( |balances| {
524
- if let Some ( account_reserves) = balances. get_mut ( who) {
525
- if let Some ( reserved) = account_reserves. get_mut ( & id_vec) {
526
- if * reserved >= amount {
527
- * reserved -= amount;
528
- remaining = 0 ;
529
- } else {
530
- remaining = amount - * reserved;
531
- * reserved = 0 ;
532
- }
533
-
534
- // Clean up empty reserves
535
- if * reserved == 0 {
536
- account_reserves. remove ( & id_vec) ;
537
- }
538
- }
539
-
540
- // Clean up empty accounts
541
- if account_reserves. is_empty ( ) {
542
- balances. remove ( who) ;
543
- }
544
- }
545
- } ) ;
546
-
547
- remaining
548
- }
549
-
550
- fn repatriate_reserved_named (
551
- _id : & Self :: ReserveIdentifier ,
552
- _slashed : & u64 ,
553
- _beneficiary : & u64 ,
554
- _amount : Self :: Balance ,
555
- _status : BalanceStatus ,
556
- ) -> Result < Self :: Balance , DispatchError > {
557
- Ok ( 0 )
558
- }
559
588
}
589
+
590
+ impl < T > HoldMutate < u64 > for Pallet < T > { }
560
591
}
592
+
0 commit comments