This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Uniques V2] Refactor roles (#12437)
* Basics * WIP: change the data format * Refactor * Remove redundant new() method * Rename settings * Enable tests * Chore * Change params order * Delete the config on collection removal * Chore * Remove redundant system features * Rename force_item_status to force_collection_status * Update node runtime * Chore * Remove thaw_collection * Chore * Connect collection.is_frozen to config * Allow to lock the collection in a new way * Move free_holding into settings * Connect collection's metadata locker to feature flags * DRY * Chore * Connect pallet level feature flags * Prepare tests for the new changes * Implement Item settings * Allow to lock the metadata or attributes of an item * Common -> Settings * Extract settings related code to a separate file * Move feature flag checks inside the do_* methods * Split settings.rs into parts * Extract repeated code into macro * Extract macros into their own file * Chore * Fix traits * Fix traits * Test SystemFeatures * Fix benchmarks * Add missing benchmark * Fix node/runtime/lib.rs * ".git/.scripts/bench-bot.sh" pallet dev pallet_nfts * Keep item's config on burn if it's not empty * Fix the merge artifacts * Fmt * Add SystemFeature::NoSwaps check * Refactor roles structure * Rename SystemFeatures to PalletFeatures * Rename errors * Add docs * Change error message * Rework pallet features * Move macros * Change comments * Fmt * Refactor Incrementable * Use pub(crate) for do_* functions * Update comments * Refactor freeze and lock functions * Rework Collection config and Item confg api * Chore * Make clippy happy * Chore * Fix artifacts * Address comments * Further refactoring * Add comments * Add tests for group_roles_by_account() * Update frame/nfts/src/impl_nonfungibles.rs * Add test * Replace Itertools group_by with a custom implementation * ItemsNotTransferable => ItemsNonTransferable * Update frame/nfts/src/features/roles.rs Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com> * Address PR comments * Add missed comment Co-authored-by: command-bot <> Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>
- Loading branch information
1 parent
ef16fd2
commit 6763dd6
Showing
9 changed files
with
291 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ | |
pub mod atomic_swap; | ||
pub mod buy_sell; | ||
pub mod lock; | ||
pub mod roles; | ||
pub mod settings; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2022 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use crate::*; | ||
use frame_support::pallet_prelude::*; | ||
use sp_std::collections::btree_map::BTreeMap; | ||
|
||
impl<T: Config<I>, I: 'static> Pallet<T, I> { | ||
/// Clears all the roles in a specified collection. | ||
/// | ||
/// - `collection_id`: A collection to clear the roles in. | ||
/// | ||
/// Throws an error if some of the roles were left in storage. | ||
/// This means the `CollectionRoles::max_roles()` needs to be adjusted. | ||
pub(crate) fn clear_roles(collection_id: &T::CollectionId) -> Result<(), DispatchError> { | ||
let res = CollectionRoleOf::<T, I>::clear_prefix( | ||
&collection_id, | ||
CollectionRoles::max_roles() as u32, | ||
None, | ||
); | ||
ensure!(res.maybe_cursor.is_none(), Error::<T, I>::RolesNotCleared); | ||
Ok(()) | ||
} | ||
|
||
/// Returns true if a specified account has a provided role within that collection. | ||
/// | ||
/// - `collection_id`: A collection to check the role in. | ||
/// - `account_id`: An account to check the role for. | ||
/// - `role`: A role to validate. | ||
/// | ||
/// Returns boolean. | ||
pub(crate) fn has_role( | ||
collection_id: &T::CollectionId, | ||
account_id: &T::AccountId, | ||
role: CollectionRole, | ||
) -> bool { | ||
CollectionRoleOf::<T, I>::get(&collection_id, &account_id) | ||
.map_or(false, |roles| roles.has_role(role)) | ||
} | ||
|
||
/// Groups provided roles by account, given one account could have multiple roles. | ||
/// | ||
/// - `input`: A vector of (Account, Role) tuples. | ||
/// | ||
/// Returns a grouped vector. | ||
pub fn group_roles_by_account( | ||
input: Vec<(T::AccountId, CollectionRole)>, | ||
) -> Vec<(T::AccountId, CollectionRoles)> { | ||
let mut result = BTreeMap::new(); | ||
for (account, role) in input.into_iter() { | ||
result.entry(account).or_insert(CollectionRoles::none()).add_role(role); | ||
} | ||
result.into_iter().collect() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.