mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 19:21:13 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -19,23 +19,26 @@
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use super::*;
|
||||
use sp_runtime::traits::Bounded;
|
||||
use frame_system::RawOrigin as SystemOrigin;
|
||||
use frame_benchmarking::{
|
||||
benchmarks_instance_pallet, account, whitelisted_caller, whitelist_account, impl_benchmark_test_suite
|
||||
account, benchmarks_instance_pallet, impl_benchmark_test_suite, whitelist_account,
|
||||
whitelisted_caller,
|
||||
};
|
||||
use frame_support::traits::Get;
|
||||
use frame_support::{traits::EnsureOrigin, dispatch::UnfilteredDispatchable};
|
||||
use frame_support::{
|
||||
dispatch::UnfilteredDispatchable,
|
||||
traits::{EnsureOrigin, Get},
|
||||
};
|
||||
use frame_system::RawOrigin as SystemOrigin;
|
||||
use sp_runtime::traits::Bounded;
|
||||
use sp_std::prelude::*;
|
||||
|
||||
use crate::Pallet as Assets;
|
||||
|
||||
const SEED: u32 = 0;
|
||||
|
||||
fn create_default_asset<T: Config<I>, I: 'static>(is_sufficient: bool)
|
||||
-> (T::AccountId, <T::Lookup as StaticLookup>::Source)
|
||||
{
|
||||
fn create_default_asset<T: Config<I>, I: 'static>(
|
||||
is_sufficient: bool,
|
||||
) -> (T::AccountId, <T::Lookup as StaticLookup>::Source) {
|
||||
let caller: T::AccountId = whitelisted_caller();
|
||||
let caller_lookup = T::Lookup::unlookup(caller.clone());
|
||||
let root = SystemOrigin::Root.into();
|
||||
@@ -45,14 +48,16 @@ fn create_default_asset<T: Config<I>, I: 'static>(is_sufficient: bool)
|
||||
caller_lookup.clone(),
|
||||
is_sufficient,
|
||||
1u32.into(),
|
||||
).is_ok());
|
||||
)
|
||||
.is_ok());
|
||||
(caller, caller_lookup)
|
||||
}
|
||||
|
||||
fn create_default_minted_asset<T: Config<I>, I: 'static>(is_sufficient: bool, amount: T::Balance)
|
||||
-> (T::AccountId, <T::Lookup as StaticLookup>::Source)
|
||||
{
|
||||
let (caller, caller_lookup) = create_default_asset::<T, I>(is_sufficient);
|
||||
fn create_default_minted_asset<T: Config<I>, I: 'static>(
|
||||
is_sufficient: bool,
|
||||
amount: T::Balance,
|
||||
) -> (T::AccountId, <T::Lookup as StaticLookup>::Source) {
|
||||
let (caller, caller_lookup) = create_default_asset::<T, I>(is_sufficient);
|
||||
if !is_sufficient {
|
||||
T::Currency::make_free_balance_be(&caller, T::Currency::minimum_balance());
|
||||
}
|
||||
@@ -61,14 +66,17 @@ fn create_default_minted_asset<T: Config<I>, I: 'static>(is_sufficient: bool, am
|
||||
Default::default(),
|
||||
caller_lookup.clone(),
|
||||
amount,
|
||||
).is_ok());
|
||||
)
|
||||
.is_ok());
|
||||
(caller, caller_lookup)
|
||||
}
|
||||
|
||||
fn swap_is_sufficient<T: Config<I>, I: 'static>(s: &mut bool) {
|
||||
Asset::<T, I>::mutate(&T::AssetId::default(), |maybe_a|
|
||||
if let Some(ref mut a) = maybe_a { sp_std::mem::swap(s, &mut a.is_sufficient) }
|
||||
);
|
||||
Asset::<T, I>::mutate(&T::AssetId::default(), |maybe_a| {
|
||||
if let Some(ref mut a) = maybe_a {
|
||||
sp_std::mem::swap(s, &mut a.is_sufficient)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn add_consumers<T: Config<I>, I: 'static>(minter: T::AccountId, n: u32) {
|
||||
@@ -79,7 +87,13 @@ fn add_consumers<T: Config<I>, I: 'static>(minter: T::AccountId, n: u32) {
|
||||
let target = account("consumer", i, SEED);
|
||||
T::Currency::make_free_balance_be(&target, T::Currency::minimum_balance());
|
||||
let target_lookup = T::Lookup::unlookup(target);
|
||||
assert!(Assets::<T, I>::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok());
|
||||
assert!(Assets::<T, I>::mint(
|
||||
origin.clone().into(),
|
||||
Default::default(),
|
||||
target_lookup,
|
||||
100u32.into()
|
||||
)
|
||||
.is_ok());
|
||||
}
|
||||
swap_is_sufficient::<T, I>(&mut s);
|
||||
}
|
||||
@@ -91,7 +105,13 @@ fn add_sufficients<T: Config<I>, I: 'static>(minter: T::AccountId, n: u32) {
|
||||
for i in 0..n {
|
||||
let target = account("sufficient", i, SEED);
|
||||
let target_lookup = T::Lookup::unlookup(target);
|
||||
assert!(Assets::<T, I>::mint(origin.clone().into(), Default::default(), target_lookup, 100u32.into()).is_ok());
|
||||
assert!(Assets::<T, I>::mint(
|
||||
origin.clone().into(),
|
||||
Default::default(),
|
||||
target_lookup,
|
||||
100u32.into()
|
||||
)
|
||||
.is_ok());
|
||||
}
|
||||
swap_is_sufficient::<T, I>(&mut s);
|
||||
}
|
||||
@@ -105,7 +125,8 @@ fn add_approvals<T: Config<I>, I: 'static>(minter: T::AccountId, n: u32) {
|
||||
Default::default(),
|
||||
minter_lookup,
|
||||
(100 * (n + 1)).into(),
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
for i in 0..n {
|
||||
let target = account("approval", i, SEED);
|
||||
T::Currency::make_free_balance_be(&target, T::Currency::minimum_balance());
|
||||
@@ -115,7 +136,8 @@ fn add_approvals<T: Config<I>, I: 'static>(minter: T::AccountId, n: u32) {
|
||||
Default::default(),
|
||||
target_lookup,
|
||||
100u32.into(),
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -34,10 +34,7 @@ pub struct ExtraMutator<T: Config<I>, I: 'static = ()> {
|
||||
|
||||
impl<T: Config<I>, I: 'static> Drop for ExtraMutator<T, I> {
|
||||
fn drop(&mut self) {
|
||||
debug_assert!(
|
||||
self.commit().is_ok(),
|
||||
"attempt to write to non-existent asset account"
|
||||
);
|
||||
debug_assert!(self.commit().is_ok(), "attempt to write to non-existent asset account");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -38,9 +38,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
|
||||
/// Get the total supply of an asset `id`.
|
||||
pub fn total_supply(id: T::AssetId) -> T::Balance {
|
||||
Asset::<T, I>::get(id)
|
||||
.map(|x| x.supply)
|
||||
.unwrap_or_else(Zero::zero)
|
||||
Asset::<T, I>::get(id).map(|x| x.supply).unwrap_or_else(Zero::zero)
|
||||
}
|
||||
|
||||
pub(super) fn new_account(
|
||||
@@ -134,7 +132,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
match frozen.checked_add(&details.min_balance) {
|
||||
Some(required) if rest < required => return Frozen,
|
||||
None => return Overflow,
|
||||
_ => {}
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,9 +169,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
|
||||
let amount = if let Some(frozen) = T::Freezer::frozen_balance(id, who) {
|
||||
// Frozen balance: account CANNOT be deleted
|
||||
let required = frozen
|
||||
.checked_add(&details.min_balance)
|
||||
.ok_or(ArithmeticError::Overflow)?;
|
||||
let required =
|
||||
frozen.checked_add(&details.min_balance).ok_or(ArithmeticError::Overflow)?;
|
||||
account.balance.saturating_sub(required)
|
||||
} else {
|
||||
let is_provider = false;
|
||||
@@ -219,7 +216,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
Err(e) => {
|
||||
debug_assert!(false, "passed from reducible_balance; qed");
|
||||
return Err(e.into())
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Ok(actual)
|
||||
@@ -268,12 +265,12 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
) -> DispatchResult {
|
||||
Self::increase_balance(id, beneficiary, amount, |details| -> DispatchResult {
|
||||
if let Some(check_issuer) = maybe_check_issuer {
|
||||
ensure!(
|
||||
&check_issuer == &details.issuer,
|
||||
Error::<T, I>::NoPermission
|
||||
);
|
||||
ensure!(&check_issuer == &details.issuer, Error::<T, I>::NoPermission);
|
||||
}
|
||||
debug_assert!(T::Balance::max_value() - details.supply >= amount, "checked in prep; qed");
|
||||
debug_assert!(
|
||||
T::Balance::max_value() - details.supply >= amount,
|
||||
"checked in prep; qed"
|
||||
);
|
||||
details.supply = details.supply.saturating_add(amount);
|
||||
Ok(())
|
||||
})?;
|
||||
@@ -295,7 +292,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
&mut AssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T, I>>,
|
||||
) -> DispatchResult,
|
||||
) -> DispatchResult {
|
||||
if amount.is_zero() { return Ok(()) }
|
||||
if amount.is_zero() {
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
Self::can_increase(id, beneficiary, amount).into_result()?;
|
||||
Asset::<T, I>::try_mutate(id, |maybe_details| -> DispatchResult {
|
||||
@@ -364,7 +363,9 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
&mut AssetDetails<T::Balance, T::AccountId, DepositBalanceOf<T, I>>,
|
||||
) -> DispatchResult,
|
||||
) -> Result<T::Balance, DispatchError> {
|
||||
if amount.is_zero() { return Ok(amount) }
|
||||
if amount.is_zero() {
|
||||
return Ok(amount)
|
||||
}
|
||||
|
||||
let actual = Self::prep_debit(id, target, amount, f)?;
|
||||
|
||||
|
||||
@@ -24,15 +24,11 @@ impl<T: Config<I>, I: 'static> fungibles::Inspect<<T as SystemConfig>::AccountId
|
||||
type Balance = T::Balance;
|
||||
|
||||
fn total_issuance(asset: Self::AssetId) -> Self::Balance {
|
||||
Asset::<T, I>::get(asset)
|
||||
.map(|x| x.supply)
|
||||
.unwrap_or_else(Zero::zero)
|
||||
Asset::<T, I>::get(asset).map(|x| x.supply).unwrap_or_else(Zero::zero)
|
||||
}
|
||||
|
||||
fn minimum_balance(asset: Self::AssetId) -> Self::Balance {
|
||||
Asset::<T, I>::get(asset)
|
||||
.map(|x| x.min_balance)
|
||||
.unwrap_or_else(Zero::zero)
|
||||
Asset::<T, I>::get(asset).map(|x| x.min_balance).unwrap_or_else(Zero::zero)
|
||||
}
|
||||
|
||||
fn balance(asset: Self::AssetId, who: &<T as SystemConfig>::AccountId) -> Self::Balance {
|
||||
@@ -78,10 +74,7 @@ impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId>
|
||||
who: &<T as SystemConfig>::AccountId,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
let f = DebitFlags {
|
||||
keep_alive: false,
|
||||
best_effort: false,
|
||||
};
|
||||
let f = DebitFlags { keep_alive: false, best_effort: false };
|
||||
Self::do_burn(asset, who, amount, None, f)
|
||||
}
|
||||
|
||||
@@ -90,10 +83,7 @@ impl<T: Config<I>, I: 'static> fungibles::Mutate<<T as SystemConfig>::AccountId>
|
||||
who: &<T as SystemConfig>::AccountId,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
let f = DebitFlags {
|
||||
keep_alive: false,
|
||||
best_effort: true,
|
||||
};
|
||||
let f = DebitFlags { keep_alive: false, best_effort: true };
|
||||
Self::do_burn(asset, who, amount, None, f)
|
||||
}
|
||||
}
|
||||
@@ -106,11 +96,7 @@ impl<T: Config<I>, I: 'static> fungibles::Transfer<T::AccountId> for Pallet<T, I
|
||||
amount: T::Balance,
|
||||
keep_alive: bool,
|
||||
) -> Result<T::Balance, DispatchError> {
|
||||
let f = TransferFlags {
|
||||
keep_alive,
|
||||
best_effort: false,
|
||||
burn_dust: false
|
||||
};
|
||||
let f = TransferFlags { keep_alive, best_effort: false, burn_dust: false };
|
||||
Self::do_transfer(asset, source, dest, amount, None, f)
|
||||
}
|
||||
}
|
||||
@@ -126,28 +112,35 @@ impl<T: Config<I>, I: 'static> fungibles::Unbalanced<T::AccountId> for Pallet<T,
|
||||
}
|
||||
});
|
||||
}
|
||||
fn decrease_balance(asset: T::AssetId, who: &T::AccountId, amount: Self::Balance)
|
||||
-> Result<Self::Balance, DispatchError>
|
||||
{
|
||||
fn decrease_balance(
|
||||
asset: T::AssetId,
|
||||
who: &T::AccountId,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
let f = DebitFlags { keep_alive: false, best_effort: false };
|
||||
Self::decrease_balance(asset, who, amount, f, |_, _| Ok(()))
|
||||
}
|
||||
fn decrease_balance_at_most(asset: T::AssetId, who: &T::AccountId, amount: Self::Balance)
|
||||
-> Self::Balance
|
||||
{
|
||||
fn decrease_balance_at_most(
|
||||
asset: T::AssetId,
|
||||
who: &T::AccountId,
|
||||
amount: Self::Balance,
|
||||
) -> Self::Balance {
|
||||
let f = DebitFlags { keep_alive: false, best_effort: true };
|
||||
Self::decrease_balance(asset, who, amount, f, |_, _| Ok(()))
|
||||
.unwrap_or(Zero::zero())
|
||||
Self::decrease_balance(asset, who, amount, f, |_, _| Ok(())).unwrap_or(Zero::zero())
|
||||
}
|
||||
fn increase_balance(asset: T::AssetId, who: &T::AccountId, amount: Self::Balance)
|
||||
-> Result<Self::Balance, DispatchError>
|
||||
{
|
||||
fn increase_balance(
|
||||
asset: T::AssetId,
|
||||
who: &T::AccountId,
|
||||
amount: Self::Balance,
|
||||
) -> Result<Self::Balance, DispatchError> {
|
||||
Self::increase_balance(asset, who, amount, |_| Ok(()))?;
|
||||
Ok(amount)
|
||||
}
|
||||
fn increase_balance_at_most(asset: T::AssetId, who: &T::AccountId, amount: Self::Balance)
|
||||
-> Self::Balance
|
||||
{
|
||||
fn increase_balance_at_most(
|
||||
asset: T::AssetId,
|
||||
who: &T::AccountId,
|
||||
amount: Self::Balance,
|
||||
) -> Self::Balance {
|
||||
match Self::increase_balance(asset, who, amount, |_| Ok(())) {
|
||||
Ok(()) => amount,
|
||||
Err(_) => Zero::zero(),
|
||||
|
||||
+100
-120
@@ -122,40 +122,49 @@
|
||||
// Ensure we're `no_std` when compiling for Wasm.
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub mod weights;
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
mod benchmarking;
|
||||
#[cfg(test)]
|
||||
pub mod mock;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
pub mod weights;
|
||||
|
||||
mod extra_mutator;
|
||||
pub use extra_mutator::*;
|
||||
mod impl_stored_map;
|
||||
mod impl_fungibles;
|
||||
mod functions;
|
||||
mod impl_fungibles;
|
||||
mod impl_stored_map;
|
||||
mod types;
|
||||
pub use types::*;
|
||||
|
||||
use sp_std::{prelude::*, borrow::Borrow, convert::TryInto};
|
||||
use sp_runtime::{
|
||||
TokenError, ArithmeticError,
|
||||
traits::{AtLeast32BitUnsigned, Zero, StaticLookup, Saturating, CheckedSub, CheckedAdd, Bounded}
|
||||
};
|
||||
use codec::HasCompact;
|
||||
use frame_support::pallet_prelude::*;
|
||||
use frame_support::traits::{Currency, ReservableCurrency, BalanceStatus::Reserved, StoredMap};
|
||||
use frame_support::traits::tokens::{WithdrawConsequence, DepositConsequence, fungibles};
|
||||
use frame_support::{
|
||||
dispatch::{DispatchError, DispatchResult},
|
||||
ensure,
|
||||
traits::{
|
||||
tokens::{fungibles, DepositConsequence, WithdrawConsequence},
|
||||
BalanceStatus::Reserved,
|
||||
Currency, ReservableCurrency, StoredMap,
|
||||
},
|
||||
};
|
||||
use frame_system::Config as SystemConfig;
|
||||
use sp_runtime::{
|
||||
traits::{
|
||||
AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedSub, Saturating, StaticLookup, Zero,
|
||||
},
|
||||
ArithmeticError, TokenError,
|
||||
};
|
||||
use sp_std::{borrow::Borrow, convert::TryInto, prelude::*};
|
||||
|
||||
pub use weights::WeightInfo;
|
||||
pub use pallet::*;
|
||||
pub use weights::WeightInfo;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
use frame_system::pallet_prelude::*;
|
||||
use super::*;
|
||||
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
|
||||
use frame_system::pallet_prelude::*;
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::generate_store(pub(super) trait Store)]
|
||||
@@ -267,11 +276,7 @@ pub mod pallet {
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
#[pallet::metadata(
|
||||
T::AccountId = "AccountId",
|
||||
T::Balance = "Balance",
|
||||
T::AssetId = "AssetId"
|
||||
)]
|
||||
#[pallet::metadata(T::AccountId = "AccountId", T::Balance = "Balance", T::AssetId = "AssetId")]
|
||||
pub enum Event<T: Config<I>, I: 'static = ()> {
|
||||
/// Some asset class was created. \[asset_id, creator, owner\]
|
||||
Created(T::AssetId, T::AccountId, T::AccountId),
|
||||
@@ -514,13 +519,12 @@ pub mod pallet {
|
||||
}
|
||||
Self::deposit_event(Event::Destroyed(id));
|
||||
|
||||
Ok(
|
||||
Some(T::WeightInfo::destroy(
|
||||
details.accounts.saturating_sub(details.sufficients),
|
||||
details.sufficients,
|
||||
details.approvals,
|
||||
)).into()
|
||||
)
|
||||
Ok(Some(T::WeightInfo::destroy(
|
||||
details.accounts.saturating_sub(details.sufficients),
|
||||
details.sufficients,
|
||||
details.approvals,
|
||||
))
|
||||
.into())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -541,7 +545,7 @@ pub mod pallet {
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
beneficiary: <T::Lookup as StaticLookup>::Source,
|
||||
#[pallet::compact] amount: T::Balance
|
||||
#[pallet::compact] amount: T::Balance,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let beneficiary = T::Lookup::lookup(beneficiary)?;
|
||||
@@ -569,7 +573,7 @@ pub mod pallet {
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
who: <T::Lookup as StaticLookup>::Source,
|
||||
#[pallet::compact] amount: T::Balance
|
||||
#[pallet::compact] amount: T::Balance,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let who = T::Lookup::lookup(who)?;
|
||||
@@ -602,16 +606,12 @@ pub mod pallet {
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
target: <T::Lookup as StaticLookup>::Source,
|
||||
#[pallet::compact] amount: T::Balance
|
||||
#[pallet::compact] amount: T::Balance,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let dest = T::Lookup::lookup(target)?;
|
||||
|
||||
let f = TransferFlags {
|
||||
keep_alive: false,
|
||||
best_effort: false,
|
||||
burn_dust: false
|
||||
};
|
||||
let f = TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
|
||||
Self::do_transfer(id, &origin, &dest, amount, None, f).map(|_| ())
|
||||
}
|
||||
|
||||
@@ -638,16 +638,12 @@ pub mod pallet {
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
target: <T::Lookup as StaticLookup>::Source,
|
||||
#[pallet::compact] amount: T::Balance
|
||||
#[pallet::compact] amount: T::Balance,
|
||||
) -> DispatchResult {
|
||||
let source = ensure_signed(origin)?;
|
||||
let dest = T::Lookup::lookup(target)?;
|
||||
|
||||
let f = TransferFlags {
|
||||
keep_alive: true,
|
||||
best_effort: false,
|
||||
burn_dust: false
|
||||
};
|
||||
let f = TransferFlags { keep_alive: true, best_effort: false, burn_dust: false };
|
||||
Self::do_transfer(id, &source, &dest, amount, None, f).map(|_| ())
|
||||
}
|
||||
|
||||
@@ -682,11 +678,7 @@ pub mod pallet {
|
||||
let source = T::Lookup::lookup(source)?;
|
||||
let dest = T::Lookup::lookup(dest)?;
|
||||
|
||||
let f = TransferFlags {
|
||||
keep_alive: false,
|
||||
best_effort: false,
|
||||
burn_dust: false
|
||||
};
|
||||
let f = TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
|
||||
Self::do_transfer(id, &source, &dest, amount, Some(origin), f).map(|_| ())
|
||||
}
|
||||
|
||||
@@ -704,17 +696,14 @@ pub mod pallet {
|
||||
pub fn freeze(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
who: <T::Lookup as StaticLookup>::Source
|
||||
who: <T::Lookup as StaticLookup>::Source,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
|
||||
let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(&origin == &d.freezer, Error::<T, I>::NoPermission);
|
||||
let who = T::Lookup::lookup(who)?;
|
||||
ensure!(
|
||||
Account::<T, I>::contains_key(id, &who),
|
||||
Error::<T, I>::BalanceZero
|
||||
);
|
||||
ensure!(Account::<T, I>::contains_key(id, &who), Error::<T, I>::BalanceZero);
|
||||
|
||||
Account::<T, I>::mutate(id, &who, |a| a.is_frozen = true);
|
||||
|
||||
@@ -735,19 +724,15 @@ pub mod pallet {
|
||||
#[pallet::weight(T::WeightInfo::thaw())]
|
||||
pub fn thaw(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact]
|
||||
id: T::AssetId,
|
||||
who: <T::Lookup as StaticLookup>::Source
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
who: <T::Lookup as StaticLookup>::Source,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
|
||||
let details = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(&origin == &details.admin, Error::<T, I>::NoPermission);
|
||||
let who = T::Lookup::lookup(who)?;
|
||||
ensure!(
|
||||
Account::<T, I>::contains_key(id, &who),
|
||||
Error::<T, I>::BalanceZero
|
||||
);
|
||||
ensure!(Account::<T, I>::contains_key(id, &who), Error::<T, I>::BalanceZero);
|
||||
|
||||
Account::<T, I>::mutate(id, &who, |a| a.is_frozen = false);
|
||||
|
||||
@@ -767,7 +752,7 @@ pub mod pallet {
|
||||
#[pallet::weight(T::WeightInfo::freeze_asset())]
|
||||
pub fn freeze_asset(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
|
||||
@@ -794,7 +779,7 @@ pub mod pallet {
|
||||
#[pallet::weight(T::WeightInfo::thaw_asset())]
|
||||
pub fn thaw_asset(
|
||||
origin: OriginFor<T>,
|
||||
#[pallet::compact] id: T::AssetId
|
||||
#[pallet::compact] id: T::AssetId,
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
|
||||
@@ -832,7 +817,7 @@ pub mod pallet {
|
||||
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(&origin == &details.owner, Error::<T, I>::NoPermission);
|
||||
if details.owner == owner {
|
||||
return Ok(());
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
let metadata_deposit = Metadata::<T, I>::get(id).deposit;
|
||||
@@ -912,14 +897,10 @@ pub mod pallet {
|
||||
) -> DispatchResult {
|
||||
let origin = ensure_signed(origin)?;
|
||||
|
||||
let bounded_name: BoundedVec<u8, T::StringLimit> = name
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
let bounded_symbol: BoundedVec<u8, T::StringLimit> = symbol
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
let bounded_name: BoundedVec<u8, T::StringLimit> =
|
||||
name.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
let bounded_symbol: BoundedVec<u8, T::StringLimit> =
|
||||
symbol.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
|
||||
let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(&origin == &d.owner, Error::<T, I>::NoPermission);
|
||||
@@ -1008,15 +989,11 @@ pub mod pallet {
|
||||
) -> DispatchResult {
|
||||
T::ForceOrigin::ensure_origin(origin)?;
|
||||
|
||||
let bounded_name: BoundedVec<u8, T::StringLimit> = name
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
let bounded_name: BoundedVec<u8, T::StringLimit> =
|
||||
name.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
|
||||
let bounded_symbol: BoundedVec<u8, T::StringLimit> = symbol
|
||||
.clone()
|
||||
.try_into()
|
||||
.map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
let bounded_symbol: BoundedVec<u8, T::StringLimit> =
|
||||
symbol.clone().try_into().map_err(|_| Error::<T, I>::BadMetadata)?;
|
||||
|
||||
ensure!(Asset::<T, I>::contains_key(id), Error::<T, I>::Unknown);
|
||||
Metadata::<T, I>::try_mutate_exists(id, |metadata| {
|
||||
@@ -1145,25 +1122,28 @@ pub mod pallet {
|
||||
|
||||
let mut d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(!d.is_frozen, Error::<T, I>::Frozen);
|
||||
Approvals::<T, I>::try_mutate((id, &owner, &delegate), |maybe_approved| -> DispatchResult {
|
||||
let mut approved = match maybe_approved.take() {
|
||||
// an approval already exists and is being updated
|
||||
Some(a) => a,
|
||||
// a new approval is created
|
||||
None => {
|
||||
d.approvals.saturating_inc();
|
||||
Default::default()
|
||||
Approvals::<T, I>::try_mutate(
|
||||
(id, &owner, &delegate),
|
||||
|maybe_approved| -> DispatchResult {
|
||||
let mut approved = match maybe_approved.take() {
|
||||
// an approval already exists and is being updated
|
||||
Some(a) => a,
|
||||
// a new approval is created
|
||||
None => {
|
||||
d.approvals.saturating_inc();
|
||||
Default::default()
|
||||
},
|
||||
};
|
||||
let deposit_required = T::ApprovalDeposit::get();
|
||||
if approved.deposit < deposit_required {
|
||||
T::Currency::reserve(&owner, deposit_required - approved.deposit)?;
|
||||
approved.deposit = deposit_required;
|
||||
}
|
||||
};
|
||||
let deposit_required = T::ApprovalDeposit::get();
|
||||
if approved.deposit < deposit_required {
|
||||
T::Currency::reserve(&owner, deposit_required - approved.deposit)?;
|
||||
approved.deposit = deposit_required;
|
||||
}
|
||||
approved.amount = approved.amount.saturating_add(amount);
|
||||
*maybe_approved = Some(approved);
|
||||
Ok(())
|
||||
})?;
|
||||
approved.amount = approved.amount.saturating_add(amount);
|
||||
*maybe_approved = Some(approved);
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
Asset::<T, I>::insert(id, d);
|
||||
Self::deposit_event(Event::ApprovedTransfer(id, owner, delegate, amount));
|
||||
|
||||
@@ -1192,7 +1172,8 @@ pub mod pallet {
|
||||
let owner = ensure_signed(origin)?;
|
||||
let delegate = T::Lookup::lookup(delegate)?;
|
||||
let mut d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
|
||||
let approval = Approvals::<T, I>::take((id, &owner, &delegate)).ok_or(Error::<T, I>::Unknown)?;
|
||||
let approval =
|
||||
Approvals::<T, I>::take((id, &owner, &delegate)).ok_or(Error::<T, I>::Unknown)?;
|
||||
T::Currency::unreserve(&owner, approval.deposit);
|
||||
|
||||
d.approvals.saturating_dec();
|
||||
@@ -1234,7 +1215,8 @@ pub mod pallet {
|
||||
let owner = T::Lookup::lookup(owner)?;
|
||||
let delegate = T::Lookup::lookup(delegate)?;
|
||||
|
||||
let approval = Approvals::<T, I>::take((id, &owner, &delegate)).ok_or(Error::<T, I>::Unknown)?;
|
||||
let approval =
|
||||
Approvals::<T, I>::take((id, &owner, &delegate)).ok_or(Error::<T, I>::Unknown)?;
|
||||
T::Currency::unreserve(&owner, approval.deposit);
|
||||
d.approvals.saturating_dec();
|
||||
Asset::<T, I>::insert(id, d);
|
||||
@@ -1273,33 +1255,31 @@ pub mod pallet {
|
||||
let owner = T::Lookup::lookup(owner)?;
|
||||
let destination = T::Lookup::lookup(destination)?;
|
||||
|
||||
Approvals::<T, I>::try_mutate_exists((id, &owner, delegate), |maybe_approved| -> DispatchResult {
|
||||
let mut approved = maybe_approved.take().ok_or(Error::<T, I>::Unapproved)?;
|
||||
let remaining = approved
|
||||
.amount
|
||||
.checked_sub(&amount)
|
||||
.ok_or(Error::<T, I>::Unapproved)?;
|
||||
Approvals::<T, I>::try_mutate_exists(
|
||||
(id, &owner, delegate),
|
||||
|maybe_approved| -> DispatchResult {
|
||||
let mut approved = maybe_approved.take().ok_or(Error::<T, I>::Unapproved)?;
|
||||
let remaining =
|
||||
approved.amount.checked_sub(&amount).ok_or(Error::<T, I>::Unapproved)?;
|
||||
|
||||
let f = TransferFlags {
|
||||
keep_alive: false,
|
||||
best_effort: false,
|
||||
burn_dust: false
|
||||
};
|
||||
Self::do_transfer(id, &owner, &destination, amount, None, f)?;
|
||||
let f =
|
||||
TransferFlags { keep_alive: false, best_effort: false, burn_dust: false };
|
||||
Self::do_transfer(id, &owner, &destination, amount, None, f)?;
|
||||
|
||||
if remaining.is_zero() {
|
||||
T::Currency::unreserve(&owner, approved.deposit);
|
||||
Asset::<T, I>::mutate(id, |maybe_details| {
|
||||
if let Some(details) = maybe_details {
|
||||
details.approvals.saturating_dec();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
approved.amount = remaining;
|
||||
*maybe_approved = Some(approved);
|
||||
}
|
||||
Ok(())
|
||||
})?;
|
||||
if remaining.is_zero() {
|
||||
T::Currency::unreserve(&owner, approved.deposit);
|
||||
Asset::<T, I>::mutate(id, |maybe_details| {
|
||||
if let Some(details) = maybe_details {
|
||||
details.approvals.saturating_dec();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
approved.amount = remaining;
|
||||
*maybe_approved = Some(approved);
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,9 +20,12 @@
|
||||
use super::*;
|
||||
use crate as pallet_assets;
|
||||
|
||||
use frame_support::{construct_runtime, parameter_types};
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header};
|
||||
use frame_support::{parameter_types, construct_runtime};
|
||||
use sp_runtime::{
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
};
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
|
||||
type Block = frame_system::mocking::MockBlock<Test>;
|
||||
@@ -108,8 +111,7 @@ impl Config for Test {
|
||||
type Extra = ();
|
||||
}
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::{cell::RefCell, collections::HashMap};
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
pub(crate) enum Hook {
|
||||
|
||||
@@ -18,10 +18,10 @@
|
||||
//! Tests for Assets pallet.
|
||||
|
||||
use super::*;
|
||||
use crate::{Error, mock::*};
|
||||
use sp_runtime::{TokenError, traits::ConvertInto};
|
||||
use frame_support::{assert_ok, assert_noop, traits::Currency};
|
||||
use crate::{mock::*, Error};
|
||||
use frame_support::{assert_noop, assert_ok, traits::Currency};
|
||||
use pallet_balances::Error as BalancesError;
|
||||
use sp_runtime::{traits::ConvertInto, TokenError};
|
||||
|
||||
#[test]
|
||||
fn basic_minting_should_work() {
|
||||
@@ -151,13 +151,25 @@ fn force_cancel_approval_works() {
|
||||
assert_eq!(Asset::<Test>::get(0).unwrap().approvals, 1);
|
||||
let e = Error::<Test>::NoPermission;
|
||||
assert_noop!(Assets::force_cancel_approval(Origin::signed(2), 0, 1, 2), e);
|
||||
assert_noop!(Assets::force_cancel_approval(Origin::signed(1), 1, 1, 2), Error::<Test>::Unknown);
|
||||
assert_noop!(Assets::force_cancel_approval(Origin::signed(1), 0, 2, 2), Error::<Test>::Unknown);
|
||||
assert_noop!(Assets::force_cancel_approval(Origin::signed(1), 0, 1, 3), Error::<Test>::Unknown);
|
||||
assert_noop!(
|
||||
Assets::force_cancel_approval(Origin::signed(1), 1, 1, 2),
|
||||
Error::<Test>::Unknown
|
||||
);
|
||||
assert_noop!(
|
||||
Assets::force_cancel_approval(Origin::signed(1), 0, 2, 2),
|
||||
Error::<Test>::Unknown
|
||||
);
|
||||
assert_noop!(
|
||||
Assets::force_cancel_approval(Origin::signed(1), 0, 1, 3),
|
||||
Error::<Test>::Unknown
|
||||
);
|
||||
assert_eq!(Asset::<Test>::get(0).unwrap().approvals, 1);
|
||||
assert_ok!(Assets::force_cancel_approval(Origin::signed(1), 0, 1, 2));
|
||||
assert_eq!(Asset::<Test>::get(0).unwrap().approvals, 0);
|
||||
assert_noop!(Assets::force_cancel_approval(Origin::signed(1), 0, 1, 2), Error::<Test>::Unknown);
|
||||
assert_noop!(
|
||||
Assets::force_cancel_approval(Origin::signed(1), 0, 1, 2),
|
||||
Error::<Test>::Unknown
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -222,7 +234,6 @@ fn destroy_with_bad_witness_should_not_work() {
|
||||
w.accounts += 2;
|
||||
w.sufficients += 2;
|
||||
assert_ok!(Assets::destroy(Origin::signed(1), 0, w));
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@@ -259,7 +270,10 @@ fn non_providing_should_work() {
|
||||
// ...or transfer...
|
||||
assert_noop!(Assets::transfer(Origin::signed(0), 0, 1, 50), TokenError::CannotCreate);
|
||||
// ...or force-transfer
|
||||
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 0, 1, 50), TokenError::CannotCreate);
|
||||
assert_noop!(
|
||||
Assets::force_transfer(Origin::signed(1), 0, 0, 1, 50),
|
||||
TokenError::CannotCreate
|
||||
);
|
||||
|
||||
Balances::make_free_balance_be(&1, 100);
|
||||
Balances::make_free_balance_be(&2, 100);
|
||||
@@ -278,7 +292,10 @@ fn min_balance_should_work() {
|
||||
// Cannot create a new account with a balance that is below minimum...
|
||||
assert_noop!(Assets::mint(Origin::signed(1), 0, 2, 9), TokenError::BelowMinimum);
|
||||
assert_noop!(Assets::transfer(Origin::signed(1), 0, 2, 9), TokenError::BelowMinimum);
|
||||
assert_noop!(Assets::force_transfer(Origin::signed(1), 0, 1, 2, 9), TokenError::BelowMinimum);
|
||||
assert_noop!(
|
||||
Assets::force_transfer(Origin::signed(1), 0, 1, 2, 9),
|
||||
TokenError::BelowMinimum
|
||||
);
|
||||
|
||||
// When deducting from an account to below minimum, it should be reaped.
|
||||
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 91));
|
||||
@@ -333,7 +350,10 @@ fn transferring_enough_to_kill_source_when_keep_alive_should_fail() {
|
||||
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 10));
|
||||
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
|
||||
assert_eq!(Assets::balance(0, 1), 100);
|
||||
assert_noop!(Assets::transfer_keep_alive(Origin::signed(1), 0, 2, 91), Error::<Test>::BalanceLow);
|
||||
assert_noop!(
|
||||
Assets::transfer_keep_alive(Origin::signed(1), 0, 2, 91),
|
||||
Error::<Test>::BalanceLow
|
||||
);
|
||||
assert_ok!(Assets::transfer_keep_alive(Origin::signed(1), 0, 2, 90));
|
||||
assert_eq!(Assets::balance(0, 1), 10);
|
||||
assert_eq!(Assets::balance(0, 2), 90);
|
||||
@@ -385,13 +405,19 @@ fn origin_guards_should_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
|
||||
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
|
||||
assert_noop!(Assets::transfer_ownership(Origin::signed(2), 0, 2), Error::<Test>::NoPermission);
|
||||
assert_noop!(
|
||||
Assets::transfer_ownership(Origin::signed(2), 0, 2),
|
||||
Error::<Test>::NoPermission
|
||||
);
|
||||
assert_noop!(Assets::set_team(Origin::signed(2), 0, 2, 2, 2), Error::<Test>::NoPermission);
|
||||
assert_noop!(Assets::freeze(Origin::signed(2), 0, 1), Error::<Test>::NoPermission);
|
||||
assert_noop!(Assets::thaw(Origin::signed(2), 0, 2), Error::<Test>::NoPermission);
|
||||
assert_noop!(Assets::mint(Origin::signed(2), 0, 2, 100), Error::<Test>::NoPermission);
|
||||
assert_noop!(Assets::burn(Origin::signed(2), 0, 1, 100), Error::<Test>::NoPermission);
|
||||
assert_noop!(Assets::force_transfer(Origin::signed(2), 0, 1, 2, 100), Error::<Test>::NoPermission);
|
||||
assert_noop!(
|
||||
Assets::force_transfer(Origin::signed(2), 0, 1, 2, 100),
|
||||
Error::<Test>::NoPermission
|
||||
);
|
||||
let w = Asset::<Test>::get(0).unwrap().destroy_witness();
|
||||
assert_noop!(Assets::destroy(Origin::signed(2), 0, w), Error::<Test>::NoPermission);
|
||||
});
|
||||
@@ -410,7 +436,10 @@ fn transfer_owner_should_work() {
|
||||
assert_eq!(Balances::reserved_balance(&2), 1);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
|
||||
assert_noop!(Assets::transfer_ownership(Origin::signed(1), 0, 1), Error::<Test>::NoPermission);
|
||||
assert_noop!(
|
||||
Assets::transfer_ownership(Origin::signed(1), 0, 1),
|
||||
Error::<Test>::NoPermission
|
||||
);
|
||||
|
||||
// Set metadata now and make sure that deposit gets transferred back.
|
||||
assert_ok!(Assets::set_metadata(Origin::signed(2), 0, vec![0u8; 10], vec![0u8; 10], 12));
|
||||
@@ -513,25 +542,25 @@ fn set_metadata_should_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// Cannot add metadata to unknown asset
|
||||
assert_noop!(
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 10], vec![0u8; 10], 12),
|
||||
Error::<Test>::Unknown,
|
||||
);
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 10], vec![0u8; 10], 12),
|
||||
Error::<Test>::Unknown,
|
||||
);
|
||||
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
|
||||
// Cannot add metadata to unowned asset
|
||||
assert_noop!(
|
||||
Assets::set_metadata(Origin::signed(2), 0, vec![0u8; 10], vec![0u8; 10], 12),
|
||||
Error::<Test>::NoPermission,
|
||||
);
|
||||
Assets::set_metadata(Origin::signed(2), 0, vec![0u8; 10], vec![0u8; 10], 12),
|
||||
Error::<Test>::NoPermission,
|
||||
);
|
||||
|
||||
// Cannot add oversized metadata
|
||||
assert_noop!(
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 100], vec![0u8; 10], 12),
|
||||
Error::<Test>::BadMetadata,
|
||||
);
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 100], vec![0u8; 10], 12),
|
||||
Error::<Test>::BadMetadata,
|
||||
);
|
||||
assert_noop!(
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 10], vec![0u8; 100], 12),
|
||||
Error::<Test>::BadMetadata,
|
||||
);
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 10], vec![0u8; 100], 12),
|
||||
Error::<Test>::BadMetadata,
|
||||
);
|
||||
|
||||
// Successfully add metadata and take deposit
|
||||
Balances::make_free_balance_be(&1, 30);
|
||||
@@ -546,9 +575,9 @@ fn set_metadata_should_work() {
|
||||
|
||||
// Cannot over-reserve
|
||||
assert_noop!(
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 20], vec![0u8; 20], 12),
|
||||
BalancesError::<Test, _>::InsufficientBalance,
|
||||
);
|
||||
Assets::set_metadata(Origin::signed(1), 0, vec![0u8; 20], vec![0u8; 20], 12),
|
||||
BalancesError::<Test, _>::InsufficientBalance,
|
||||
);
|
||||
|
||||
// Clear Metadata
|
||||
assert!(Metadata::<Test>::contains_key(0));
|
||||
@@ -566,7 +595,6 @@ fn freezer_should_work() {
|
||||
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 100));
|
||||
assert_eq!(Assets::balance(0, 1), 100);
|
||||
|
||||
|
||||
// freeze 50 of it.
|
||||
set_frozen_balance(0, 1, 50);
|
||||
|
||||
@@ -624,45 +652,73 @@ fn imbalances_should_work() {
|
||||
#[test]
|
||||
fn force_metadata_should_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
//force set metadata works
|
||||
// force set metadata works
|
||||
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
|
||||
assert_ok!(Assets::force_set_metadata(Origin::root(), 0, vec![0u8; 10], vec![0u8; 10], 8, false));
|
||||
assert_ok!(Assets::force_set_metadata(
|
||||
Origin::root(),
|
||||
0,
|
||||
vec![0u8; 10],
|
||||
vec![0u8; 10],
|
||||
8,
|
||||
false
|
||||
));
|
||||
assert!(Metadata::<Test>::contains_key(0));
|
||||
|
||||
//overwrites existing metadata
|
||||
// overwrites existing metadata
|
||||
let asset_original_metadata = Metadata::<Test>::get(0);
|
||||
assert_ok!(Assets::force_set_metadata(Origin::root(), 0, vec![1u8; 10], vec![1u8; 10], 8, false));
|
||||
assert_ok!(Assets::force_set_metadata(
|
||||
Origin::root(),
|
||||
0,
|
||||
vec![1u8; 10],
|
||||
vec![1u8; 10],
|
||||
8,
|
||||
false
|
||||
));
|
||||
assert_ne!(Metadata::<Test>::get(0), asset_original_metadata);
|
||||
|
||||
//attempt to set metadata for non-existent asset class
|
||||
// attempt to set metadata for non-existent asset class
|
||||
assert_noop!(
|
||||
Assets::force_set_metadata(Origin::root(), 1, vec![0u8; 10], vec![0u8; 10], 8, false),
|
||||
Error::<Test>::Unknown
|
||||
);
|
||||
|
||||
//string length limit check
|
||||
// string length limit check
|
||||
let limit = StringLimit::get() as usize;
|
||||
assert_noop!(
|
||||
Assets::force_set_metadata(Origin::root(), 0, vec![0u8; limit + 1], vec![0u8; 10], 8, false),
|
||||
Assets::force_set_metadata(
|
||||
Origin::root(),
|
||||
0,
|
||||
vec![0u8; limit + 1],
|
||||
vec![0u8; 10],
|
||||
8,
|
||||
false
|
||||
),
|
||||
Error::<Test>::BadMetadata
|
||||
);
|
||||
assert_noop!(
|
||||
Assets::force_set_metadata(Origin::root(), 0, vec![0u8; 10], vec![0u8; limit + 1], 8, false),
|
||||
Assets::force_set_metadata(
|
||||
Origin::root(),
|
||||
0,
|
||||
vec![0u8; 10],
|
||||
vec![0u8; limit + 1],
|
||||
8,
|
||||
false
|
||||
),
|
||||
Error::<Test>::BadMetadata
|
||||
);
|
||||
|
||||
//force clear metadata works
|
||||
// force clear metadata works
|
||||
assert!(Metadata::<Test>::contains_key(0));
|
||||
assert_ok!(Assets::force_clear_metadata(Origin::root(), 0));
|
||||
assert!(!Metadata::<Test>::contains_key(0));
|
||||
|
||||
//Error handles clearing non-existent asset class
|
||||
// Error handles clearing non-existent asset class
|
||||
assert_noop!(Assets::force_clear_metadata(Origin::root(), 1), Error::<Test>::Unknown);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_asset_status_should_work(){
|
||||
fn force_asset_status_should_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&1, 10);
|
||||
Balances::make_free_balance_be(&2, 10);
|
||||
@@ -670,28 +726,28 @@ fn force_asset_status_should_work(){
|
||||
assert_ok!(Assets::mint(Origin::signed(1), 0, 1, 50));
|
||||
assert_ok!(Assets::mint(Origin::signed(1), 0, 2, 150));
|
||||
|
||||
//force asset status to change min_balance > balance
|
||||
// force asset status to change min_balance > balance
|
||||
assert_ok!(Assets::force_asset_status(Origin::root(), 0, 1, 1, 1, 1, 100, true, false));
|
||||
assert_eq!(Assets::balance(0, 1), 50);
|
||||
|
||||
//account can recieve assets for balance < min_balance
|
||||
// account can recieve assets for balance < min_balance
|
||||
assert_ok!(Assets::transfer(Origin::signed(2), 0, 1, 1));
|
||||
assert_eq!(Assets::balance(0, 1), 51);
|
||||
|
||||
//account on outbound transfer will cleanup for balance < min_balance
|
||||
// account on outbound transfer will cleanup for balance < min_balance
|
||||
assert_ok!(Assets::transfer(Origin::signed(1), 0, 2, 1));
|
||||
assert_eq!(Assets::balance(0,1), 0);
|
||||
assert_eq!(Assets::balance(0, 1), 0);
|
||||
|
||||
//won't create new account with balance below min_balance
|
||||
// won't create new account with balance below min_balance
|
||||
assert_noop!(Assets::transfer(Origin::signed(2), 0, 3, 50), TokenError::BelowMinimum);
|
||||
|
||||
//force asset status will not execute for non-existent class
|
||||
// force asset status will not execute for non-existent class
|
||||
assert_noop!(
|
||||
Assets::force_asset_status(Origin::root(), 1, 1, 1, 1, 1, 90, true, false),
|
||||
Error::<Test>::Unknown
|
||||
);
|
||||
|
||||
//account drains to completion when funds dip below min_balance
|
||||
// account drains to completion when funds dip below min_balance
|
||||
assert_ok!(Assets::force_asset_status(Origin::root(), 0, 1, 1, 1, 1, 110, true, false));
|
||||
assert_ok!(Assets::transfer(Origin::signed(2), 0, 1, 110));
|
||||
assert_eq!(Assets::balance(0, 1), 200);
|
||||
@@ -715,7 +771,10 @@ fn balance_conversion_should_work() {
|
||||
Err(ConversionError::AssetMissing)
|
||||
);
|
||||
assert_eq!(
|
||||
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(100, not_sufficient),
|
||||
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(
|
||||
100,
|
||||
not_sufficient
|
||||
),
|
||||
Err(ConversionError::AssetNotSufficient)
|
||||
);
|
||||
// 10 / 1 == 10 -> the conversion should 10x the value
|
||||
|
||||
@@ -21,18 +21,13 @@ use super::*;
|
||||
use frame_support::pallet_prelude::*;
|
||||
|
||||
use frame_support::traits::{fungible, tokens::BalanceConversion};
|
||||
use sp_runtime::{FixedPointNumber, FixedPointOperand, FixedU128};
|
||||
use sp_runtime::traits::Convert;
|
||||
use sp_runtime::{traits::Convert, FixedPointNumber, FixedPointOperand, FixedU128};
|
||||
|
||||
pub(super) type DepositBalanceOf<T, I = ()> =
|
||||
<<T as Config<I>>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
|
||||
|
||||
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, MaxEncodedLen)]
|
||||
pub struct AssetDetails<
|
||||
Balance,
|
||||
AccountId,
|
||||
DepositBalance,
|
||||
> {
|
||||
pub struct AssetDetails<Balance, AccountId, DepositBalance> {
|
||||
/// Can change `owner`, `issuer`, `freezer` and `admin` accounts.
|
||||
pub(super) owner: AccountId,
|
||||
/// Can mint tokens.
|
||||
@@ -144,7 +139,9 @@ pub trait FrozenBalance<AssetId, AccountId, Balance> {
|
||||
}
|
||||
|
||||
impl<AssetId, AccountId, Balance> FrozenBalance<AssetId, AccountId, Balance> for () {
|
||||
fn frozen_balance(_: AssetId, _: &AccountId) -> Option<Balance> { None }
|
||||
fn frozen_balance(_: AssetId, _: &AccountId) -> Option<Balance> {
|
||||
None
|
||||
}
|
||||
fn died(_: AssetId, _: &AccountId) {}
|
||||
}
|
||||
|
||||
@@ -175,10 +172,7 @@ pub(super) struct DebitFlags {
|
||||
|
||||
impl From<TransferFlags> for DebitFlags {
|
||||
fn from(f: TransferFlags) -> Self {
|
||||
Self {
|
||||
keep_alive: f.keep_alive,
|
||||
best_effort: f.best_effort,
|
||||
}
|
||||
Self { keep_alive: f.keep_alive, best_effort: f.best_effort }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,7 +199,7 @@ type BalanceOf<F, T> = <F as fungible::Inspect<AccountIdOf<T>>>::Balance;
|
||||
/// minimum balance and the minimum asset balance.
|
||||
pub struct BalanceToAssetBalance<F, T, CON, I = ()>(PhantomData<(F, T, CON, I)>);
|
||||
impl<F, T, CON, I> BalanceConversion<BalanceOf<F, T>, AssetIdOf<T, I>, AssetBalanceOf<T, I>>
|
||||
for BalanceToAssetBalance<F, T, CON, I>
|
||||
for BalanceToAssetBalance<F, T, CON, I>
|
||||
where
|
||||
F: fungible::Inspect<AccountIdOf<T>>,
|
||||
T: Config<I>,
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
// --template=./.maintain/frame-weight-template.hbs
|
||||
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user