lib.rs 68.8 KiB
Newer Older
										Ok(actual)
									},
									Err(index) => {
										let remain =
											<Self as ReservableCurrency<_>>::repatriate_reserved(
												slashed,
												beneficiary,
												to_change,
												status,
											)?;

										// remain should always be zero but just to be defensive here
										let actual = to_change.saturating_sub(remain);

										reserves
											.try_insert(
												index,
												ReserveData { id: id.clone(), amount: actual },
											)
											.map_err(|_| Error::<T, I>::TooManyReserves)?;

										Ok(actual)
									},
								}
							},
						)?
Xiliang Chen's avatar
Xiliang Chen committed
					} else {
						let remain = <Self as ReservableCurrency<_>>::repatriate_reserved(
							slashed,
							beneficiary,
							to_change,
							status,
						)?;
Xiliang Chen's avatar
Xiliang Chen committed

						// remain should always be zero but just to be defensive here
						to_change.saturating_sub(remain)
					};

					// `actual <= to_change` and `to_change <= amount`; qed;
					reserves[index].amount -= actual;

					Ok(value - actual)
				},
				Err(_) => Ok(value),
impl<T: Config<I>, I: 'static> LockableCurrency<T::AccountId> for Pallet<T, I>
	T::Balance: MaybeSerializeDeserialize + Debug,
	type MaxLocks = T::MaxLocks;

	// Set a lock on the balance of `who`.
	// Is a no-op if lock amount is zero or `reasons` `is_none()`.
	fn set_lock(
		id: LockIdentifier,
		who: &T::AccountId,
		amount: T::Balance,
		reasons: WithdrawReasons,
	) {
		if amount.is_zero() || reasons.is_empty() {
			return
		}
		let mut new_lock = Some(BalanceLock { id, amount, reasons: reasons.into() });
		let mut locks = Self::locks(who)
			.into_iter()
Gavin Wood's avatar
Gavin Wood committed
			.filter_map(|l| if l.id == id { new_lock.take() } else { Some(l) })
			.collect::<Vec<_>>();
		if let Some(lock) = new_lock {
			locks.push(lock)
		}
		Self::update_locks(who, &locks[..]);
	// Extend a lock on the balance of `who`.
	// Is a no-op if lock amount is zero or `reasons` `is_none()`.
	fn extend_lock(
		id: LockIdentifier,
		who: &T::AccountId,
		amount: T::Balance,
		reasons: WithdrawReasons,
	) {
		if amount.is_zero() || reasons.is_empty() {
			return
		}
		let mut new_lock = Some(BalanceLock { id, amount, reasons: reasons.into() });
		let mut locks = Self::locks(who)
			.into_iter()
			.filter_map(|l| {
				if l.id == id {
					new_lock.take().map(|nl| BalanceLock {
						id: l.id,
						amount: l.amount.max(nl.amount),
						reasons: l.reasons | nl.reasons,
					})
				} else {
					Some(l)
				}
			})
			.collect::<Vec<_>>();
		if let Some(lock) = new_lock {
			locks.push(lock)
		}
		Self::update_locks(who, &locks[..]);
	fn remove_lock(id: LockIdentifier, who: &T::AccountId) {
		let mut locks = Self::locks(who);
		locks.retain(|l| l.id != id);
		Self::update_locks(who, &locks[..]);