Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Make Assets Pallet's Privileged Roles Option #12579

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c3cb0b4
make account fields in struct option
dharjeezy Oct 28, 2022
83189f6
ensure test works
dharjeezy Oct 28, 2022
935fa9d
started working on migration
dharjeezy Nov 1, 2022
e35b40f
use translate on the storage map for migration
dharjeezy Nov 3, 2022
789aa6e
cargo fmt
dharjeezy Nov 3, 2022
f9799f4
lock
dharjeezy Nov 23, 2022
9466b5d
Merge branch 'master' of https://github.com/dharjeezy/substrate into …
dharjeezy Nov 23, 2022
2eb1537
fix merge conflict
dharjeezy Nov 24, 2022
2e0b828
checks before repatriate_reserved and unreserve
dharjeezy Nov 24, 2022
57cb27c
and_then instead of map
dharjeezy Nov 26, 2022
b26bd66
Merge branch 'master' of https://github.com/dharjeezy/substrate into …
dharjeezy Dec 19, 2022
446e256
remove unnecessary unwrap()
dharjeezy Dec 19, 2022
43eabc4
lock
dharjeezy Dec 19, 2022
ae72aae
refractor and fix typo
dharjeezy Dec 19, 2022
0de20ab
removal of unneccessary unwrap
dharjeezy Dec 19, 2022
32c1059
remove redundancy
dharjeezy Dec 19, 2022
d8a301a
refractor check admin rights
dharjeezy Dec 29, 2022
c86a853
Merge remote-tracking branch 'origin/master' into dharjeezy/pallet-as…
Feb 9, 2023
8bcedb0
Merge remote-tracking branch 'origin/master' into dharjeezy/pallet-as…
Mar 22, 2023
bdbbcdd
fix migration
dharjeezy Apr 2, 2023
a785a08
Merge remote-tracking branch 'origin/dharjeezy/pallet-assets-details-…
dharjeezy Apr 2, 2023
e155cda
fix cargo build issues
dharjeezy Apr 4, 2023
2248192
Merge remote-tracking branch 'origin/master' into dharjeezy/pallet-as…
May 5, 2023
f2580be
Update frame/assets/src/migration.rs
jsidorenko May 5, 2023
1244d20
Refactoring
jsidorenko May 5, 2023
aad17d7
Fix benches
jsidorenko May 5, 2023
9bb7354
Fix tests
jsidorenko May 5, 2023
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
31 changes: 16 additions & 15 deletions frame/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn create_default_asset<T: Config<I>, I: 'static>(
assert!(Assets::<T, I>::force_create(
root,
asset_id,
caller_lookup.clone(),
Some(caller_lookup.clone()),
is_sufficient,
1u32.into(),
)
Expand Down Expand Up @@ -144,18 +144,18 @@ benchmarks_instance_pallet! {
let caller = T::CreateOrigin::ensure_origin(origin, &asset_id.into()).unwrap();
let caller_lookup = T::Lookup::unlookup(caller.clone());
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
}: _(SystemOrigin::Signed(caller.clone()), asset_id, caller_lookup, 1u32.into())
}: _(SystemOrigin::Signed(caller.clone()), asset_id, Some(caller_lookup), 1u32.into())
verify {
assert_last_event::<T, I>(Event::Created { asset_id: asset_id.into(), creator: caller.clone(), owner: caller }.into());
assert_last_event::<T, I>(Event::Created { asset_id: asset_id.into(), creator: caller.clone(), owner: caller.clone(), admin: Some(caller) }.into());
}

force_create {
let asset_id = default_asset_id::<T, I>();
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
}: _(SystemOrigin::Root, asset_id, caller_lookup, true, 1u32.into())
}: _(SystemOrigin::Root, asset_id, Some(caller_lookup), true, 1u32.into())
verify {
assert_last_event::<T, I>(Event::ForceCreated { asset_id: asset_id.into(), owner: caller }.into());
assert_last_event::<T, I>(Event::ForceCreated { asset_id: asset_id.into(), owner: Some(caller) }.into());
}

start_destroy {
Expand Down Expand Up @@ -311,23 +311,24 @@ benchmarks_instance_pallet! {
let (asset_id, caller, _) = create_default_asset::<T, I>(true);
let target: T::AccountId = account("target", 0, SEED);
let target_lookup = T::Lookup::unlookup(target.clone());
}: _(SystemOrigin::Signed(caller), asset_id, target_lookup)
T::Currency::make_free_balance_be(&target, DepositBalanceOf::<T, I>::max_value());
}: _(SystemOrigin::Signed(caller), asset_id, Some(target_lookup))
verify {
assert_last_event::<T, I>(Event::OwnerChanged { asset_id: asset_id.into(), owner: target }.into());
assert_last_event::<T, I>(Event::OwnerChanged { asset_id: asset_id.into(), owner: Some(target) }.into());
}

set_team {
let (asset_id, caller, _) = create_default_asset::<T, I>(true);
let target0 = T::Lookup::unlookup(account("target", 0, SEED));
let target1 = T::Lookup::unlookup(account("target", 1, SEED));
let target2 = T::Lookup::unlookup(account("target", 2, SEED));
}: _(SystemOrigin::Signed(caller), asset_id, target0, target1, target2)
}: _(SystemOrigin::Signed(caller), asset_id, Some(target0), Some(target1), Some(target2))
verify {
assert_last_event::<T, I>(Event::TeamChanged {
asset_id: asset_id.into(),
issuer: account("target", 0, SEED),
admin: account("target", 1, SEED),
freezer: account("target", 2, SEED),
issuer: Some(account("target", 0, SEED)),
admin: Some(account("target", 1, SEED)),
freezer: Some(account("target", 2, SEED)),
}.into());
}

Expand Down Expand Up @@ -403,10 +404,10 @@ benchmarks_instance_pallet! {
T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let call = Call::<T, I>::force_asset_status {
id: asset_id,
owner: caller_lookup.clone(),
issuer: caller_lookup.clone(),
admin: caller_lookup.clone(),
freezer: caller_lookup,
owner: Some(caller_lookup.clone()),
issuer: Some(caller_lookup.clone()),
admin: Some(caller_lookup.clone()),
freezer: Some(caller_lookup),
min_balance: 100u32.into(),
is_sufficient: true,
is_frozen: false,
Expand Down
29 changes: 15 additions & 14 deletions frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
maybe_check_issuer: Option<T::AccountId>,
) -> 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);
if maybe_check_issuer.is_some() {
ensure!(maybe_check_issuer == details.issuer, Error::<T, I>::NoPermission);
}
debug_assert!(details.supply.checked_add(&amount).is_some(), "checked in prep; qed");

Expand Down Expand Up @@ -442,8 +442,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

let actual = Self::decrease_balance(id, target, amount, f, |actual, details| {
// Check admin rights.
if let Some(check_admin) = maybe_check_admin {
ensure!(check_admin == details.admin, Error::<T, I>::NoPermission);
if maybe_check_admin.is_some() {
ensure!(maybe_check_admin == details.admin, Error::<T, I>::NoPermission);
}

debug_assert!(details.supply >= actual, "checked in prep; qed");
Expand Down Expand Up @@ -567,8 +567,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;

// Check admin rights.
if let Some(need_admin) = maybe_need_admin {
ensure!(need_admin == details.admin, Error::<T, I>::NoPermission);
if maybe_need_admin.is_some() {
ensure!(maybe_need_admin == details.admin, Error::<T, I>::NoPermission);
}

// Skip if source == dest
Expand Down Expand Up @@ -644,7 +644,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// considered dust and cleaned up.
pub(super) fn do_force_create(
id: T::AssetId,
owner: T::AccountId,
owner: Option<T::AccountId>,
is_sufficient: bool,
min_balance: T::Balance,
) -> DispatchResult {
Expand Down Expand Up @@ -681,8 +681,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
) -> DispatchResult {
Asset::<T, I>::try_mutate_exists(id, |maybe_details| -> Result<(), DispatchError> {
let mut details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
if let Some(check_owner) = maybe_check_owner {
ensure!(details.owner == check_owner, Error::<T, I>::NoPermission);
if maybe_check_owner.is_some() {
ensure!(maybe_check_owner == details.owner, Error::<T, I>::NoPermission);
}
details.status = AssetStatus::Destroying;

Expand Down Expand Up @@ -776,10 +776,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
ensure!(T::CallbackHandle::destroyed(&id).is_ok(), Error::<T, I>::CallbackFailed);

let metadata = Metadata::<T, I>::take(&id);
T::Currency::unreserve(
&details.owner,
details.deposit.saturating_add(metadata.deposit),
);

if let Some(owner) = details.owner {
T::Currency::unreserve(&owner, details.deposit.saturating_add(metadata.deposit));
}

Self::deposit_event(Event::Destroyed { asset_id: id });

Ok(())
Expand Down Expand Up @@ -897,7 +898,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

let d = Asset::<T, I>::get(id).ok_or(Error::<T, I>::Unknown)?;
ensure!(d.status == AssetStatus::Live, Error::<T, I>::AssetNotLive);
ensure!(from == &d.owner, Error::<T, I>::NoPermission);
ensure!(Some(from) == d.owner.as_ref(), Error::<T, I>::NoPermission);

Metadata::<T, I>::try_mutate_exists(id, |metadata| {
ensure!(metadata.as_ref().map_or(true, |m| !m.is_frozen), Error::<T, I>::NoPermission);
Expand Down
10 changes: 5 additions & 5 deletions frame/assets/src/impl_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<T: Config<I>, I: 'static> fungibles::Unbalanced<T::AccountId> for Pallet<T,
impl<T: Config<I>, I: 'static> fungibles::Create<T::AccountId> for Pallet<T, I> {
fn create(
id: T::AssetId,
admin: T::AccountId,
admin: Option<T::AccountId>,
is_sufficient: bool,
min_balance: Self::Balance,
) -> DispatchResult {
Expand Down Expand Up @@ -238,19 +238,19 @@ impl<T: Config<I>, I: 'static> fungibles::roles::Inspect<<T as SystemConfig>::Ac
for Pallet<T, I>
{
fn owner(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
Asset::<T, I>::get(asset).map(|x| x.owner)
Asset::<T, I>::get(asset).and_then(|x| x.owner)
}

fn issuer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
Asset::<T, I>::get(asset).map(|x| x.issuer)
Asset::<T, I>::get(asset).and_then(|x| x.issuer)
}

fn admin(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
Asset::<T, I>::get(asset).map(|x| x.admin)
Asset::<T, I>::get(asset).and_then(|x| x.admin)
}

fn freezer(asset: T::AssetId) -> Option<<T as SystemConfig>::AccountId> {
Asset::<T, I>::get(asset).map(|x| x.freezer)
Asset::<T, I>::get(asset).and_then(|x| x.freezer)
}
}

Expand Down
Loading