diff --git a/pallets/slp/src/lib.rs b/pallets/slp/src/lib.rs index 68fe4b5cad..408f01959a 100644 --- a/pallets/slp/src/lib.rs +++ b/pallets/slp/src/lib.rs @@ -134,6 +134,9 @@ pub mod pallet { /// on_initialize queue. #[pallet::constant] type MaxTypeEntryPerBlock: Get; + + #[pallet::constant] + type MaxRefundPerBlock: Get; } #[pallet::error] @@ -1011,6 +1014,10 @@ pub mod pallet { let mut exit_account_balance = T::MultiCurrency::free_balance(currency_id, &exit_account); + if exit_account_balance.is_zero() { + return Ok(()); + } + // Get the currency due unlocking records let time_unit = T::VtokenMinting::get_ongoing_time_unit(currency_id) .ok_or(Error::::TimeUnitNotExist)?; @@ -1018,7 +1025,12 @@ pub mod pallet { // Refund due unlocking records one by one. if let Some((_locked_amount, idx_vec)) = rs { + let mut counter = 0; + for idx in idx_vec.iter() { + if counter >= T::MaxRefundPerBlock::get() { + break; + } // get idx record amount let idx_record_amount_op = T::VtokenMinting::get_token_unlock_ledger(currency_id, *idx); @@ -1048,6 +1060,8 @@ pub mod pallet { amount: deduct_amount, }); + counter = counter.saturating_add(1); + exit_account_balance = exit_account_balance .checked_sub(&deduct_amount) .ok_or(Error::::UnderFlow)?; @@ -1056,19 +1070,16 @@ pub mod pallet { } } } + } else { + // Automatically move the rest amount in exit account to entrance account. + T::MultiCurrency::transfer( + currency_id, + &exit_account, + &entrance_account, + exit_account_balance, + )?; } - // Automatically move the rest amount in exit account to entrance account. - let new_exit_account_balance = - T::MultiCurrency::free_balance(currency_id, &exit_account); - - T::MultiCurrency::transfer( - currency_id, - &exit_account, - &entrance_account, - new_exit_account_balance, - )?; - Ok(()) } diff --git a/pallets/slp/src/mock.rs b/pallets/slp/src/mock.rs index 8e55c3212d..fc63e0c7cc 100644 --- a/pallets/slp/src/mock.rs +++ b/pallets/slp/src/mock.rs @@ -218,7 +218,8 @@ impl Get for ParachainId { } parameter_types! { - pub const MaxTypeEntryPerBlock: u32 = 50; + pub const MaxTypeEntryPerBlock: u32 = 10; + pub const MaxRefundPerBlock: u32 = 10; } impl QueryResponseManager for () { @@ -245,6 +246,7 @@ impl Config for Runtime { type XcmExecutor = (); type SubstrateResponseManager = (); type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock; + type MaxRefundPerBlock = MaxRefundPerBlock; } pub struct ExtBuilder { diff --git a/pallets/slp/src/tests.rs b/pallets/slp/src/tests.rs index ded3dc4686..a594d39a0d 100644 --- a/pallets/slp/src/tests.rs +++ b/pallets/slp/src/tests.rs @@ -374,12 +374,17 @@ fn refund_currency_due_unbond_works() { assert_eq!(bifrost_vtoken_minting::UserUnlockLedger::::get(EDDIE, KSM,), None); // check account balances - assert_eq!(Tokens::free_balance(KSM, &exit_acc), 0); - assert_eq!(Tokens::free_balance(KSM, &entrance_acc), 17); + assert_eq!(Tokens::free_balance(KSM, &exit_acc), 17); + assert_eq!(Tokens::free_balance(KSM, &entrance_acc), 0); assert_eq!(Tokens::free_balance(KSM, &BOB), 0); assert_eq!(Tokens::free_balance(KSM, &CHARLIE), 28); assert_eq!(Tokens::free_balance(KSM, &DAVE), 22); assert_eq!(Tokens::free_balance(KSM, &EDDIE), 13); + assert_ok!(Slp::refund_currency_due_unbond(Origin::signed(ALICE), KSM)); + + // check account balances + assert_eq!(Tokens::free_balance(KSM, &exit_acc), 0); + assert_eq!(Tokens::free_balance(KSM, &entrance_acc), 17); }); } diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index a9c64cbdc1..f15554da1f 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -1691,7 +1691,8 @@ impl xcm_interface::Config for Runtime { } parameter_types! { - pub const MaxTypeEntryPerBlock: u32 = 50; + pub const MaxTypeEntryPerBlock: u32 = 10; + pub const MaxRefundPerBlock: u32 = 10; } pub struct SubstrateResponseManager; @@ -1733,6 +1734,7 @@ impl bifrost_slp::Config for Runtime { type XcmExecutor = XcmExecutor; type SubstrateResponseManager = SubstrateResponseManager; type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock; + type MaxRefundPerBlock = MaxRefundPerBlock; } impl bifrost_vstoken_conversion::Config for Runtime {