Newer
Older
fn transfer(
transactor: &T::AccountId,
dest: &T::AccountId,
value: Self::Balance,
existence_requirement: ExistenceRequirement,
if value.is_zero() || transactor == dest { return Ok(()) }
let old_from_account = Self::account(transactor);
let mut from_account = old_from_account.clone();
let old_to_account = Self::account(dest);
let mut to_account = old_to_account.clone();
let would_create = to_account.total().is_zero();
let fee = if would_create { T::CreationFee::get() } else { Zero::zero() };
let liability = value.checked_add(&fee).ok_or(Error::<T, I>::Overflow)?;
from_account.free = from_account.free.checked_sub(&liability)
.ok_or(Error::<T, I>::InsufficientBalance)?;
// NOTE: total stake being stored in the same type means that this could never overflow
// but better to be safe than sorry.
to_account.free = to_account.free.checked_add(&value).ok_or(Error::<T, I>::Overflow)?;
let ed = T::ExistentialDeposit::get();
ensure!(to_account.free >= ed, Error::<T, I>::ExistentialDeposit);
Self::ensure_can_withdraw(
transactor,
value,
WithdrawReason::Transfer.into(),
from_account.free,
)?;
let allow_death = existence_requirement == ExistenceRequirement::AllowDeath;
ensure!(allow_death || from_account.free >= ed, Error::<T, I>::KeepAlive);
Self::set_account(transactor, &from_account, &old_from_account);
// Take action on the set_account call.
// This will emit events that _resulted_ from the transfer.
Self::set_account(dest, &to_account, &old_to_account);
// Emit transfer event.
Self::deposit_event(RawEvent::Transfer(transactor.clone(), dest.clone(), value, fee));
T::TransferPayment::on_unbalanced(NegativeImbalance::new(fee));
Ok(())
}
// Withdraw some free balance from an account, respecting existence requirements.
// Is a no-op if value to be withdrawn is zero.
fn withdraw(
who: &T::AccountId,
value: Self::Balance,
reasons: WithdrawReasons,
liveness: ExistenceRequirement,
) -> result::Result<Self::NegativeImbalance, DispatchError> {
if value.is_zero() { return Ok(NegativeImbalance::zero()); }
let old_account = Self::account(who);
let mut account = old_account.clone();
if let Some(new_free_account) = account.free.checked_sub(&value) {
// if we need to keep the account alive...
if liveness == ExistenceRequirement::KeepAlive
// ...and it would be dead afterwards...
&& new_free_account < T::ExistentialDeposit::get()
// ...yet is was alive before
&& account.free >= T::ExistentialDeposit::get()
Err(Error::<T, I>::KeepAlive)?
Self::ensure_can_withdraw(who, value, reasons, new_free_account)?;
account.free = new_free_account;
Self::set_account(who, &account, &old_account);
Ok(NegativeImbalance::new(value))
Err(Error::<T, I>::InsufficientBalance)?
/// Slash a target account `who`, returning the negative imbalance created and any left over
/// amount that could not be slashed.
///
/// Is a no-op if `value` to be slashed is zero.
///
/// NOTE: `slash()` prefers free balance, but assumes that reserve balance can be drawn
/// from in extreme circumstances. `can_slash()` should be used prior to `slash()` to avoid having
/// to draw from reserved funds, however we err on the side of punishment if things are inconsistent
/// or `can_slash` wasn't used appropriately.
fn slash(
who: &T::AccountId,
value: Self::Balance
) -> (Self::NegativeImbalance, Self::Balance) {
if value.is_zero() { return (NegativeImbalance::zero(), Zero::zero()) }
let old_account = Self::account(who);
let mut account = old_account.clone();
let free_slash = cmp::min(account.free, value);
account.free -= free_slash;
let remaining_slash = value - free_slash;
let result = if !remaining_slash.is_zero() {
let reserved_slash = cmp::min(account.reserved, remaining_slash);
account.reserved -= reserved_slash;
(NegativeImbalance::new(free_slash + reserved_slash), remaining_slash - reserved_slash)
(NegativeImbalance::new(value), Zero::zero())
};
Self::set_account(who, &account, &old_account);
result
/// Deposit some `value` into the free balance of an existing target account `who`.
///
/// Is a no-op if the `value` to be deposited is zero.
fn deposit_into_existing(
who: &T::AccountId,
value: Self::Balance
) -> result::Result<Self::PositiveImbalance, DispatchError> {
if value.is_zero() { return Ok(PositiveImbalance::zero()) }
let old_account = Self::account(who);
let mut account = old_account.clone();
ensure!(!account.total().is_zero(), Error::<T, I>::DeadAccount);
account.free = account.free.checked_add(&value).ok_or(Error::<T, I>::Overflow)?;
Self::set_account(who, &account, &old_account);
Ok(PositiveImbalance::new(value))
/// Deposit some `value` into the free balance of `who`, possibly creating a new account.
///
/// This function is a no-op if:
/// - the `value` to be deposited is zero; or
/// - if the `value` to be deposited is less than the ED and the account does not yet exist; or
/// - `value` is so large it would cause the balance of `who` to overflow.
fn deposit_creating(
who: &T::AccountId,
value: Self::Balance,
) -> Self::PositiveImbalance {
if value.is_zero() { return Self::PositiveImbalance::zero() }
let old_account = Self::account(who);
let mut account = old_account.clone();
let ed = T::ExistentialDeposit::get();
// bail if not yet created and this operation wouldn't be enough to create it.
if value < ed && account.total().is_zero() { return Self::PositiveImbalance::zero() }
// defensive only: overflow should never happen, however in case it does, then this
// operation is a no-op.
account.free = match account.free.checked_add(&value) {
Some(f) => f,
None => return Self::PositiveImbalance::zero(),
};
Self::set_account(who, &account, &old_account);
PositiveImbalance::new(value)
/// Force the new free balance of a target account `who` to some new value `balance`.
fn make_free_balance_be(who: &T::AccountId, value: Self::Balance) -> (
SignedImbalance<Self::Balance, Self::PositiveImbalance>,
UpdateBalanceOutcome
) {
let old_account = Self::account(who);
let mut account = old_account.clone();
if value < T::ExistentialDeposit::get() && account.free.is_zero() {
// If we're attempting to set an existing account to less than ED, then
// bypass the entire operation. It's a no-op if you follow it through, but
// since this is an instance where we might account for a negative imbalance
// (in the dust cleaner of set_account) before we account for its actual
// equal and opposite cause (returned as an Imbalance), then in the
// instance that there's no other accounts on the system at all, we might
// underflow the issuance and our arithmetic will be off.
return (
SignedImbalance::Positive(Self::PositiveImbalance::zero()),
UpdateBalanceOutcome::AccountKilled,
)
}
let imbalance = if account.free <= value {
SignedImbalance::Positive(PositiveImbalance::new(value - account.free))
SignedImbalance::Negative(NegativeImbalance::new(account.free - value))
// If the balance is too low, then the account is reaped.
// Free balance can never be less than ED. If that happens, it gets reduced to zero
// and the account information relevant to this subsystem is deleted (i.e. the
// account is reaped).
let outcome = Self::set_account(who, &account, &old_account);
impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I> where
T::Balance: MaybeSerializeDeserialize + Debug
/// Check if `who` can reserve `value` from their free balance.
///
/// Always `true` if value to be reserved is zero.
fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool {
if value.is_zero() { return true }
.checked_sub(&value)
.map_or(false, |new_balance|
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), new_balance).is_ok()
)
}
fn reserved_balance(who: &T::AccountId) -> Self::Balance {
/// Move `value` from the free balance from `who` to their reserved balance.
///
/// Is a no-op if value to be reserved is zero.
fn reserve(who: &T::AccountId, value: Self::Balance) -> result::Result<(), DispatchError> {
if value.is_zero() { return Ok(()) }
let old_account = Self::account(who);
let mut account = old_account.clone();
account.free = account.free.checked_sub(&value).ok_or(Error::<T, I>::InsufficientBalance)?;
account.reserved = account.reserved.checked_add(&value).ok_or(Error::<T, I>::Overflow)?;
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), account.free)?;
Self::set_account(who, &account, &old_account);
/// Unreserve some funds, returning any amount that was unable to be unreserved.
///
/// Is a no-op if the value to be unreserved is zero.
fn unreserve(who: &T::AccountId, value: Self::Balance) -> Self::Balance {
if value.is_zero() { return Zero::zero() }
let old_account = Self::account(who);
let mut account = old_account.clone();
let actual = cmp::min(account.reserved, value);
account.reserved -= actual;
// defensive only: this can never fail since total issuance which is at least free+reserved
// fits into the same datatype.
account.free = account.free.saturating_add(actual);
Self::set_account(who, &account, &old_account);
/// Slash from reserved balance, returning the negative imbalance created,
/// and any amount that was unable to be slashed.
///
/// Is a no-op if the value to be slashed is zero.
fn slash_reserved(
who: &T::AccountId,
value: Self::Balance
) -> (Self::NegativeImbalance, Self::Balance) {
if value.is_zero() { return (NegativeImbalance::zero(), Zero::zero()) }
let old_account = Self::account(who);
let mut account = old_account.clone();
// underflow should never happen, but it if does, there's nothing to be done here.
let actual = cmp::min(account.reserved, value);
account.reserved -= actual;
Self::set_account(who, &account, &old_account);
(NegativeImbalance::new(actual), value - actual)
/// Move the reserved balance of one account into the free balance of another.
///
/// Is a no-op if the value to be moved is zero.
fn repatriate_reserved(
slashed: &T::AccountId,
beneficiary: &T::AccountId,
) -> result::Result<Self::Balance, DispatchError> {
if value.is_zero() { return Ok (Zero::zero()) }
if slashed == beneficiary {
return Ok(Self::unreserve(slashed, value));
let old_to_account = Self::account(beneficiary);
let mut to_account = old_to_account.clone();
ensure!(!to_account.total().is_zero(), Error::<T, I>::DeadAccount);
let old_from_account = Self::account(slashed);
let mut from_account = old_from_account.clone();
let actual = cmp::min(from_account.reserved, value);
to_account.free = to_account.free.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?;
from_account.reserved -= actual;
Self::set_account(slashed, &from_account, &old_from_account);
Self::set_account(beneficiary, &to_account, &old_to_account);
Ok(value - actual)
impl<T: Trait<I>, I: Instance> LockableCurrency<T::AccountId> for Module<T, I>
T::Balance: MaybeSerializeDeserialize + Debug
{
type Moment = T::BlockNumber;
// 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_none() { 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()
} else {
}).collect::<Vec<_>>();
if let Some(lock) = new_lock {
locks.push(lock)
}
// 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_none() { 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 {
}).collect::<Vec<_>>();
if let Some(lock) = new_lock {
locks.push(lock)
}
}
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[..]);
impl<T: Trait<I>, I: Instance> IsDeadAccount<T::AccountId> for Module<T, I>
T::Balance: MaybeSerializeDeserialize + Debug
fn is_dead_account(who: &T::AccountId) -> bool {
// this should always be exactly equivalent to `Self::account(who).total().is_zero()`
!Account::<T, I>::contains_key(who)