Skip to content

Commit

Permalink
Merge pull request #562 from bifrost-finance/slp-rc7
Browse files Browse the repository at this point in the history
fx due era refund number perblock
  • Loading branch information
herryho committed Apr 24, 2022
2 parents bf438aa + a6802fa commit 151f1df
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
33 changes: 22 additions & 11 deletions pallets/slp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ pub mod pallet {
/// on_initialize queue.
#[pallet::constant]
type MaxTypeEntryPerBlock: Get<u32>;

#[pallet::constant]
type MaxRefundPerBlock: Get<u32>;
}

#[pallet::error]
Expand Down Expand Up @@ -1011,14 +1014,23 @@ 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::<T>::TimeUnitNotExist)?;
let rs = T::VtokenMinting::get_unlock_records(currency_id, time_unit.clone());

// 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);
Expand Down Expand Up @@ -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::<T>::UnderFlow)?;
Expand All @@ -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(())
}

Expand Down
4 changes: 3 additions & 1 deletion pallets/slp/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ impl Get<ParaId> for ParachainId {
}

parameter_types! {
pub const MaxTypeEntryPerBlock: u32 = 50;
pub const MaxTypeEntryPerBlock: u32 = 10;
pub const MaxRefundPerBlock: u32 = 10;
}

impl QueryResponseManager<QueryId, MultiLocation, u64> for () {
Expand All @@ -247,6 +248,7 @@ impl Config for Runtime {
type XcmExecutor = ();
type SubstrateResponseManager = ();
type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock;
type MaxRefundPerBlock = MaxRefundPerBlock;
}

pub struct ExtBuilder {
Expand Down
9 changes: 7 additions & 2 deletions pallets/slp/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,17 @@ fn refund_currency_due_unbond_works() {
assert_eq!(bifrost_vtoken_minting::UserUnlockLedger::<Runtime>::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);
});
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1733,6 +1734,7 @@ impl bifrost_slp::Config for Runtime {
type XcmExecutor = XcmExecutor<XcmConfig>;
type SubstrateResponseManager = SubstrateResponseManager;
type MaxTypeEntryPerBlock = MaxTypeEntryPerBlock;
type MaxRefundPerBlock = MaxRefundPerBlock;
}

impl bifrost_vstoken_conversion::Config for Runtime {
Expand Down

0 comments on commit 151f1df

Please sign in to comment.