Skip to content

Commit

Permalink
Merge pull request #106 from AurevoirXavier/fix-bug
Browse files Browse the repository at this point in the history
fix and clear code
  • Loading branch information
hackfisher authored Nov 23, 2019
2 parents b5a4354 + 7ea7c06 commit 88fc3f1
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,56 +627,58 @@ decl_module! {
fn claim_deposits_with_punish(origin, expire_time: T::Moment) {
let controller = ensure_signed(origin)?;
let mut ledger = Self::ledger(&controller).ok_or("not a controller")?;

let now = <timestamp::Module<T>>::now();
ensure!(expire_time > now, "use unbond instead.");

let StakingLedger {
stash,
active_deposit_ring,
deposit_items,
..
} = &mut ledger;

let now = <timestamp::Module<T>>::now();

ensure!(expire_time > now, "use unbond instead.");

deposit_items.retain(|item| {
if item.expire_time == expire_time {
let passed_duration =
(now - item.start_time).saturated_into::<u32>()
/ MONTH_IN_SECONDS
;

let plan_duration =
(item.expire_time - item.start_time).saturated_into::<u32>()
/ MONTH_IN_SECONDS
;

let kton_slash = (inflation::compute_kton_return::<T>(item.value, plan_duration) - inflation::compute_kton_return::<T>(item.value, passed_duration)) * 3.into();

// check total free balance and locked one
// strict on punishing in kton
if T::Kton::free_balance(stash).checked_sub(&kton_slash).and_then(
|new_balance| {
T::Kton::ensure_can_withdraw(
stash,
kton_slash,
WithdrawReason::Transfer.into(),
new_balance
).ok()
}
)
.is_some()
{
*active_deposit_ring = active_deposit_ring.saturating_sub(item.value);

let (imbalance, _) = T::Kton::slash(stash, kton_slash);
T::KtonSlash::on_unbalanced(imbalance);

return false;
}
if item.expire_time != expire_time {
return true;
}

true
let kton_slash = {
let passed_duration = (now - item.start_time).saturated_into::<u32>() / MONTH_IN_SECONDS;
let plan_duration = (item.expire_time - item.start_time).saturated_into::<u32>() / MONTH_IN_SECONDS;

(
inflation::compute_kton_return::<T>(item.value, plan_duration)
-
inflation::compute_kton_return::<T>(item.value, passed_duration)
) * 3.into()
};
// check total free balance and locked one
// strict on punishing in kton
if T::Kton::free_balance(stash)
.checked_sub(&kton_slash)
.and_then(|new_balance| {
T::Kton::ensure_can_withdraw(
stash,
kton_slash,
WithdrawReason::Transfer.into(),
new_balance
).ok()
})
.is_some()
{
*active_deposit_ring = active_deposit_ring.saturating_sub(item.value);

let (imbalance, _) = T::Kton::slash(stash, kton_slash);
T::KtonSlash::on_unbalanced(imbalance);

false
} else {
true
}
});

<Ledger<T>>::insert(&controller, ledger);
}

fn validate(origin, name: Vec<u8>, ratio: u32, unstake_threshold: u32) {
Expand Down

0 comments on commit 88fc3f1

Please sign in to comment.