Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions pallets/system_tables/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,45 +359,21 @@ pub mod pallet {
let staker_id =
sxt_core::utils::eth_address_to_substrate_account_id::<T>(&staker)?;

// Make sure they're staked
let Some(old_staking_ledger) = pallet_staking::Ledger::<T>::get(&staker_id)
else {
return Err(Error::<T>::AccountNotStaked.into());
};

// Make sure they're in an unbonding state
if !(pallet_staking::Pallet::<T>::is_unbonding(&staker_id)?) {
return Err(Error::<T>::AccountNotUnbonding.into());
}

let staker_signer: OriginFor<T> =
RawOrigin::Signed(staker_id.clone()).into();

// The struct that represents when a particular batch of funds will unlock
// has all fields private to the staking crate. For now the
// best we can do is to call the withdraw function and see if there are any
// balance changes.
let old_total = pallet_staking::Ledger::<T>::get(&staker_id)
.map(|ledger| ledger.total)
.unwrap_or(0);

pallet_staking::Pallet::<T>::withdraw_unbonded(staker_signer.clone(), 0u32)
.map_err(|e| e.error)?;

// Check if the user still has entries in the staking ledger
let maybe_new_staking_ledger = pallet_staking::Ledger::<T>::get(&staker_id);

if maybe_new_staking_ledger.is_some() {
// If this was a partial unlock we need to actually check if the locks
// were removed by the withdrawal call. If they weren't the claim is
// unsuccessful and we return an error. Otherwise we emit the event
if pallet_staking::Pallet::<T>::is_unbonding(&staker_id)? {
// The user's unstaked funds are still locked
return Err(Error::<T>::FundsLocked.into());
}
}

let new_total = maybe_new_staking_ledger
let new_total = pallet_staking::Ledger::<T>::get(&staker_id)
.map(|ledger| ledger.total)
.unwrap_or(0);

let amount_withdrawn = old_staking_ledger.total.saturating_sub(new_total);
let amount_withdrawn = old_total.saturating_sub(new_total);

ClaimedUnstakes::<T>::insert(
&staker_id,
Expand Down
Loading