File tree 3 files changed +26
-5
lines changed
pallets/dapp-staking-v3/src
3 files changed +26
-5
lines changed Original file line number Diff line number Diff line change @@ -838,7 +838,7 @@ pub mod pallet {
838
838
839
839
// Calculate & check amount available for locking
840
840
let available_balance =
841
- T :: Currency :: total_balance ( & account) . saturating_sub ( ledger. active_locked_amount ( ) ) ;
841
+ T :: Currency :: total_balance ( & account) . saturating_sub ( ledger. total_locked_amount ( ) ) ;
842
842
let amount_to_lock = available_balance. min ( amount) ;
843
843
ensure ! ( !amount_to_lock. is_zero( ) , Error :: <T >:: ZeroAmount ) ;
844
844
Original file line number Diff line number Diff line change @@ -86,9 +86,9 @@ impl MemorySnapshot {
86
86
/// Returns locked balance in dApp staking for the specified account.
87
87
/// In case no balance is locked, returns zero.
88
88
pub fn locked_balance ( & self , account : & AccountId ) -> Balance {
89
- self . ledger
90
- . get ( & account )
91
- . map_or ( Balance :: zero ( ) , |ledger| ledger . active_locked_amount ( ) )
89
+ self . ledger . get ( & account ) . map_or ( Balance :: zero ( ) , |ledger| {
90
+ ledger . locked + ledger . unlocking . iter ( ) . fold ( 0 , |acc , x| acc + x . amount )
91
+ } )
92
92
}
93
93
}
94
94
@@ -240,10 +240,15 @@ pub(crate) fn assert_lock(account: AccountId, amount: Balance) {
240
240
"Total locked balance should be increased by the amount locked."
241
241
) ;
242
242
243
+ let post_frozen_balance = Balances :: balance_frozen ( & FreezeReason :: DAppStaking . into ( ) , & account) ;
243
244
assert_eq ! (
244
245
init_frozen_balance + expected_lock_amount,
245
- Balances :: balance_frozen ( & FreezeReason :: DAppStaking . into ( ) , & account )
246
+ post_frozen_balance
246
247
) ;
248
+ assert ! (
249
+ Balances :: total_balance( & account) >= post_frozen_balance,
250
+ "Total balance should never be less than frozen balance."
251
+ )
247
252
}
248
253
249
254
/// Start the unlocking process for locked funds and assert success.
Original file line number Diff line number Diff line change @@ -3286,3 +3286,19 @@ fn unstake_correctly_reduces_future_contract_stake() {
3286
3286
assert_unstake ( staker_1, & smart_contract, amount_2 + 3 ) ;
3287
3287
} )
3288
3288
}
3289
+
3290
+ #[ test]
3291
+ fn lock_correctly_considers_unlocking_amount ( ) {
3292
+ ExtBuilder :: build ( ) . execute_with ( || {
3293
+ // Lock the entire amount & immediately start the unlocking process
3294
+ let ( staker, unlock_amount) = ( 1 , 13 ) ;
3295
+ let total_balance = Balances :: total_balance ( & staker) ;
3296
+ assert_lock ( staker, total_balance) ;
3297
+ assert_unlock ( staker, unlock_amount) ;
3298
+
3299
+ assert_noop ! (
3300
+ DappStaking :: lock( RuntimeOrigin :: signed( staker) , 1 ) ,
3301
+ Error :: <Test >:: ZeroAmount
3302
+ ) ;
3303
+ } )
3304
+ }
You can’t perform that action at this time.
0 commit comments