Newer
Older
// deposit_consequence already did overflow checking
<TotalIssuance<T>>::mutate(asset_id, |t| *t += amount);
Ok(())
}
fn burn_from(
asset_id: Self::AssetId,
who: &T::AccountId,
amount: Self::Balance,
) -> Result<Self::Balance, DispatchError> {
if amount.is_zero() {
return Ok(Self::Balance::zero());
}
let actual = Pallet::<T>::try_mutate_account(
who,
asset_id,
|account, _existed| -> Result<T::Balance, DispatchError> {
let extra = Pallet::<T>::withdraw_consequence(who, asset_id, amount, &account).into_result()?;
// withdraw_consequence already did underflow checking
let actual = amount + extra;
account.free -= actual;
Ok(actual)
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
// withdraw_consequence already did underflow checking
<TotalIssuance<T>>::mutate(asset_id, |t| *t -= actual);
Ok(actual)
}
}
impl<T: Config> fungibles::Transfer<T::AccountId> for Pallet<T> {
fn transfer(
asset_id: Self::AssetId,
source: &T::AccountId,
dest: &T::AccountId,
amount: T::Balance,
keep_alive: bool,
) -> Result<T::Balance, DispatchError> {
let er = if keep_alive {
ExistenceRequirement::KeepAlive
} else {
ExistenceRequirement::AllowDeath
};
Self::do_transfer(asset_id, source, dest, amount, er).map(|_| amount)
}
}
impl<T: Config> fungibles::Unbalanced<T::AccountId> for Pallet<T> {
fn set_balance(asset_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
// Balance is the same type and will not overflow
Pallet::<T>::mutate_account(who, asset_id, |account, _| account.free = amount);
Ok(())
}
fn set_total_issuance(asset_id: Self::AssetId, amount: Self::Balance) {
// Balance is the same type and will not overflow
<TotalIssuance<T>>::mutate(asset_id, |t| *t = amount);
}
}
impl<T: Config> fungibles::InspectHold<T::AccountId> for Pallet<T> {
fn balance_on_hold(asset_id: Self::AssetId, who: &T::AccountId) -> T::Balance {
Pallet::<T>::accounts(who, asset_id).reserved
}
fn can_hold(asset_id: Self::AssetId, who: &T::AccountId, amount: T::Balance) -> bool {
let a = Pallet::<T>::accounts(who, asset_id);
let min_balance = T::ExistentialDeposits::get(&asset_id).max(a.frozen);
if a.reserved.checked_add(&amount).is_none() {
return false;
}
// We require it to be min_balance + amount to ensure that the full reserved
// funds may be slashed without compromising locked funds or destroying the
// account.
let required_free = match min_balance.checked_add(&amount) {
Some(x) => x,
None => return false,
};
a.free >= required_free
}
}
impl<T: Config> fungibles::MutateHold<T::AccountId> for Pallet<T> {
fn hold(asset_id: Self::AssetId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
if amount.is_zero() {
return Ok(());
}
ensure!(
Pallet::<T>::can_reserve(asset_id, who, amount),
Error::<T>::BalanceTooLow
);
Pallet::<T>::mutate_account(who, asset_id, |a, _| {
// `can_reserve` has did underflow checking
a.free -= amount;
// Cannot overflow as `amount` is from `a.free`
a.reserved += amount;
});
Ok(())
}
fn release(
asset_id: Self::AssetId,
who: &T::AccountId,
amount: Self::Balance,
best_effort: bool,
) -> Result<T::Balance, DispatchError> {
if amount.is_zero() {
return Ok(amount);
}
// Done on a best-effort basis.
Pallet::<T>::try_mutate_account(who, asset_id, |a, _existed| {
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
let new_free = a.free.saturating_add(amount.min(a.reserved));
let actual = new_free - a.free;
// Guaranteed to be <= amount and <= a.reserved
ensure!(best_effort || actual == amount, Error::<T>::BalanceTooLow);
a.free = new_free;
a.reserved = a.reserved.saturating_sub(actual);
Ok(actual)
})
}
fn transfer_held(
asset_id: Self::AssetId,
source: &T::AccountId,
dest: &T::AccountId,
amount: Self::Balance,
_best_effort: bool,
on_hold: bool,
) -> Result<Self::Balance, DispatchError> {
let status = if on_hold { Status::Reserved } else { Status::Free };
Pallet::<T>::repatriate_reserved(asset_id, source, dest, amount, status)
}
}
pub struct CurrencyAdapter<T, GetCurrencyId>(marker::PhantomData<(T, GetCurrencyId)>);
impl<T, GetCurrencyId> PalletCurrency<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
type Balance = T::Balance;
type PositiveImbalance = PositiveImbalance<T, GetCurrencyId>;
type NegativeImbalance = NegativeImbalance<T, GetCurrencyId>;
fn total_balance(who: &T::AccountId) -> Self::Balance {
Pallet::<T>::total_balance(GetCurrencyId::get(), who)
}
fn can_slash(who: &T::AccountId, value: Self::Balance) -> bool {
Pallet::<T>::can_slash(GetCurrencyId::get(), who, value)
}
fn total_issuance() -> Self::Balance {
Pallet::<T>::total_issuance(GetCurrencyId::get())
}
fn minimum_balance() -> Self::Balance {
Pallet::<T>::minimum_balance(GetCurrencyId::get())
}
fn burn(mut amount: Self::Balance) -> Self::PositiveImbalance {
if amount.is_zero() {
return PositiveImbalance::zero();
<TotalIssuance<T>>::mutate(GetCurrencyId::get(), |issued| {
*issued = issued.checked_sub(&amount).unwrap_or_else(|| {
amount = *issued;
Zero::zero()
});
PositiveImbalance::new(amount)
}
fn issue(mut amount: Self::Balance) -> Self::NegativeImbalance {
if amount.is_zero() {
return NegativeImbalance::zero();
<TotalIssuance<T>>::mutate(GetCurrencyId::get(), |issued| {
*issued = issued.checked_add(&amount).unwrap_or_else(|| {
amount = Self::Balance::max_value() - *issued;
Self::Balance::max_value()
})
});
NegativeImbalance::new(amount)
}
fn free_balance(who: &T::AccountId) -> Self::Balance {
Pallet::<T>::free_balance(GetCurrencyId::get(), who)
}
fn ensure_can_withdraw(
who: &T::AccountId,
amount: Self::Balance,
_reasons: WithdrawReasons,
_new_balance: Self::Balance,
) -> DispatchResult {
Pallet::<T>::ensure_can_withdraw(GetCurrencyId::get(), who, amount)
}
fn transfer(
source: &T::AccountId,
dest: &T::AccountId,
value: Self::Balance,
existence_requirement: ExistenceRequirement,
Pallet::<T>::do_transfer(GetCurrencyId::get(), &source, &dest, value, existence_requirement)
fn slash(who: &T::AccountId, value: Self::Balance) -> (Self::NegativeImbalance, Self::Balance) {
if value.is_zero() {
return (Self::NegativeImbalance::zero(), value);
}
let currency_id = GetCurrencyId::get();
let account = Pallet::<T>::accounts(who, currency_id);
let free_slashed_amount = account.free.min(value);
let mut remaining_slash = value - free_slashed_amount;
// slash free balance
if !free_slashed_amount.is_zero() {
Pallet::<T>::set_free_balance(currency_id, who, account.free - free_slashed_amount);
// slash reserved balance
if !remaining_slash.is_zero() {
let reserved_slashed_amount = account.reserved.min(remaining_slash);
remaining_slash -= reserved_slashed_amount;
Pallet::<T>::set_reserved_balance(currency_id, who, account.reserved - reserved_slashed_amount);
(
Self::NegativeImbalance::new(free_slashed_amount + reserved_slashed_amount),
remaining_slash,
)
} else {
(Self::NegativeImbalance::new(value), remaining_slash)
fn deposit_into_existing(
who: &T::AccountId,
value: Self::Balance,
) -> sp_std::result::Result<Self::PositiveImbalance, DispatchError> {
if value.is_zero() {
return Ok(Self::PositiveImbalance::zero());
let currency_id = GetCurrencyId::get();
let new_total = Pallet::<T>::free_balance(currency_id, who)
.checked_add(&value)
.ok_or(ArithmeticError::Overflow)?;
Pallet::<T>::set_free_balance(currency_id, who, new_total);
Ok(Self::PositiveImbalance::new(value))
}
fn deposit_creating(who: &T::AccountId, value: Self::Balance) -> Self::PositiveImbalance {
Self::deposit_into_existing(who, value).unwrap_or_else(|_| Self::PositiveImbalance::zero())
}
fn withdraw(
who: &T::AccountId,
value: Self::Balance,
_reasons: WithdrawReasons,
liveness: ExistenceRequirement,
) -> sp_std::result::Result<Self::NegativeImbalance, DispatchError> {
if value.is_zero() {
return Ok(Self::NegativeImbalance::zero());
let currency_id = GetCurrencyId::get();
Pallet::<T>::try_mutate_account(who, currency_id, |account, _existed| -> DispatchResult {
account.free = account.free.checked_sub(&value).ok_or(Error::<T>::BalanceTooLow)?;
Pallet::<T>::ensure_can_withdraw(currency_id, who, value)?;
let ed = T::ExistentialDeposits::get(¤cy_id);
let allow_death = liveness == ExistenceRequirement::AllowDeath;
let allow_death = allow_death && !frame_system::Pallet::<T>::is_provider_required(who);
ensure!(allow_death || account.total() >= ed, Error::<T>::KeepAlive);
Ok(())
})?;
Ok(Self::NegativeImbalance::new(value))
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
fn make_free_balance_be(
who: &T::AccountId,
value: Self::Balance,
) -> SignedImbalance<Self::Balance, Self::PositiveImbalance> {
let currency_id = GetCurrencyId::get();
Pallet::<T>::try_mutate_account(
who,
currency_id,
|account, existed| -> Result<SignedImbalance<Self::Balance, Self::PositiveImbalance>, ()> {
// 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.
let ed = T::ExistentialDeposits::get(¤cy_id);
ensure!(value.saturating_add(account.reserved) >= ed || existed, ());
let imbalance = if account.free <= value {
SignedImbalance::Positive(PositiveImbalance::new(value - account.free))
} else {
SignedImbalance::Negative(NegativeImbalance::new(account.free - value))
};
account.free = value;
Ok(imbalance)
},
)
.unwrap_or_else(|_| SignedImbalance::Positive(Self::PositiveImbalance::zero()))
}
}
impl<T, GetCurrencyId> PalletReservableCurrency<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool {
Pallet::<T>::can_reserve(GetCurrencyId::get(), who, value)
}
fn slash_reserved(who: &T::AccountId, value: Self::Balance) -> (Self::NegativeImbalance, Self::Balance) {
let actual = Pallet::<T>::slash_reserved(GetCurrencyId::get(), who, value);
(Self::NegativeImbalance::zero(), actual)
}
fn reserved_balance(who: &T::AccountId) -> Self::Balance {
Pallet::<T>::reserved_balance(GetCurrencyId::get(), who)
}
fn reserve(who: &T::AccountId, value: Self::Balance) -> DispatchResult {
Pallet::<T>::reserve(GetCurrencyId::get(), who, value)
}
fn unreserve(who: &T::AccountId, value: Self::Balance) -> Self::Balance {
Pallet::<T>::unreserve(GetCurrencyId::get(), who, value)
fn repatriate_reserved(
slashed: &T::AccountId,
beneficiary: &T::AccountId,
value: Self::Balance,
status: Status,
) -> sp_std::result::Result<Self::Balance, DispatchError> {
Pallet::<T>::repatriate_reserved(GetCurrencyId::get(), slashed, beneficiary, value, status)
}
}
impl<T, GetCurrencyId> PalletLockableCurrency<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
type Moment = T::BlockNumber;
type MaxLocks = ();
fn set_lock(id: LockIdentifier, who: &T::AccountId, amount: Self::Balance, _reasons: WithdrawReasons) {
let _ = Pallet::<T>::set_lock(id, GetCurrencyId::get(), who, amount);
}
fn extend_lock(id: LockIdentifier, who: &T::AccountId, amount: Self::Balance, _reasons: WithdrawReasons) {
let _ = Pallet::<T>::extend_lock(id, GetCurrencyId::get(), who, amount);
fn remove_lock(id: LockIdentifier, who: &T::AccountId) {
let _ = Pallet::<T>::remove_lock(id, GetCurrencyId::get(), who);
}
}
impl<T: Config> TransferAll<T::AccountId> for Pallet<T> {
fn transfer_all(source: &T::AccountId, dest: &T::AccountId) -> DispatchResult {
Accounts::<T>::iter_prefix(source).try_for_each(|(currency_id, account_data)| -> DispatchResult {
<Self as MultiCurrency<T::AccountId>>::transfer(currency_id, source, dest, account_data.free)
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
impl<T, GetCurrencyId> fungible::Inspect<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
type Balance = T::Balance;
fn total_issuance() -> Self::Balance {
<Pallet<T> as fungibles::Inspect<_>>::total_issuance(GetCurrencyId::get())
}
fn minimum_balance() -> Self::Balance {
<Pallet<T> as fungibles::Inspect<_>>::minimum_balance(GetCurrencyId::get())
}
fn balance(who: &T::AccountId) -> Self::Balance {
<Pallet<T> as fungibles::Inspect<_>>::balance(GetCurrencyId::get(), who)
}
fn reducible_balance(who: &T::AccountId, keep_alive: bool) -> Self::Balance {
<Pallet<T> as fungibles::Inspect<_>>::reducible_balance(GetCurrencyId::get(), who, keep_alive)
}
fn can_deposit(who: &T::AccountId, amount: Self::Balance) -> DepositConsequence {
<Pallet<T> as fungibles::Inspect<_>>::can_deposit(GetCurrencyId::get(), who, amount)
}
fn can_withdraw(who: &T::AccountId, amount: Self::Balance) -> WithdrawConsequence<Self::Balance> {
<Pallet<T> as fungibles::Inspect<_>>::can_withdraw(GetCurrencyId::get(), who, amount)
}
}
impl<T, GetCurrencyId> fungible::Mutate<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
fn mint_into(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
<Pallet<T> as fungibles::Mutate<_>>::mint_into(GetCurrencyId::get(), who, amount)
}
fn burn_from(who: &T::AccountId, amount: Self::Balance) -> Result<Self::Balance, DispatchError> {
<Pallet<T> as fungibles::Mutate<_>>::burn_from(GetCurrencyId::get(), who, amount)
}
}
impl<T, GetCurrencyId> fungible::Transfer<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
fn transfer(
source: &T::AccountId,
dest: &T::AccountId,
amount: T::Balance,
keep_alive: bool,
) -> Result<T::Balance, DispatchError> {
<Pallet<T> as fungibles::Transfer<_>>::transfer(GetCurrencyId::get(), source, dest, amount, keep_alive)
}
}
impl<T, GetCurrencyId> fungible::Unbalanced<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
fn set_balance(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
<Pallet<T> as fungibles::Unbalanced<_>>::set_balance(GetCurrencyId::get(), who, amount)
}
fn set_total_issuance(amount: Self::Balance) {
<Pallet<T> as fungibles::Unbalanced<_>>::set_total_issuance(GetCurrencyId::get(), amount)
}
}
impl<T, GetCurrencyId> fungible::InspectHold<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
fn balance_on_hold(who: &T::AccountId) -> T::Balance {
<Pallet<T> as fungibles::InspectHold<_>>::balance_on_hold(GetCurrencyId::get(), who)
}
fn can_hold(who: &T::AccountId, amount: T::Balance) -> bool {
<Pallet<T> as fungibles::InspectHold<_>>::can_hold(GetCurrencyId::get(), who, amount)
}
}
impl<T, GetCurrencyId> fungible::MutateHold<T::AccountId> for CurrencyAdapter<T, GetCurrencyId>
where
T: Config,
GetCurrencyId: Get<T::CurrencyId>,
{
fn hold(who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
<Pallet<T> as fungibles::MutateHold<_>>::hold(GetCurrencyId::get(), who, amount)
}
fn release(who: &T::AccountId, amount: Self::Balance, best_effort: bool) -> Result<T::Balance, DispatchError> {
<Pallet<T> as fungibles::MutateHold<_>>::release(GetCurrencyId::get(), who, amount, best_effort)
}
fn transfer_held(
source: &T::AccountId,
dest: &T::AccountId,
amount: Self::Balance,
best_effort: bool,
on_hold: bool,
) -> Result<Self::Balance, DispatchError> {
<Pallet<T> as fungibles::MutateHold<_>>::transfer_held(
GetCurrencyId::get(),
source,
dest,
amount,
best_effort,
on_hold,
)
}
}