From 7ec0bdff6a6ad68317ec593bb464c9df31455b89 Mon Sep 17 00:00:00 2001 From: Tlalocman Date: Wed, 27 Sep 2023 16:36:57 -0600 Subject: [PATCH] update roles assignment for afloat (#17) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🔧 chore(functions.rs): remove commented out code for creating asset in do_create_marketplace function ✨ feat(functions.rs): add documentation for do_create_marketplace function to explain its purpose and behavior * 🔧 chore(lib.rs): remove unused imports to improve code cleanliness and reduce clutter ✨ feat(lib.rs): add new events to track user creation, editing, deletion, sell and buy order creation, order taking, balance updates, admin addition, and user role assignment 🐛 fix(lib.rs): fix typo in event comment 🚀 feat(lib.rs): add new extrinsic `assign_user_to_role` to allow assigning a user to a role --- pallets/afloat/src/lib.rs | 30 +++++++++++++++++++--- pallets/gated-marketplace/src/functions.rs | 13 +++------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/pallets/afloat/src/lib.rs b/pallets/afloat/src/lib.rs index 93b8885e..6a74f517 100644 --- a/pallets/afloat/src/lib.rs +++ b/pallets/afloat/src/lib.rs @@ -15,13 +15,11 @@ pub mod types; pub mod pallet { use frame_support::{ pallet_prelude::*, - sp_io::hashing::blake2_256, traits::{Currency, UnixTime}, }; use frame_system::{pallet_prelude::*, RawOrigin}; use pallet_fruniques::types::{Attributes, CollectionDescription, FruniqueRole, ParentInfo}; use pallet_gated_marketplace::types::*; - use sp_runtime::{traits::StaticLookup, Permill}; const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); use crate::types::*; @@ -56,16 +54,26 @@ pub mod pallet { #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event { - SomethingStored(u32, T::AccountId), + // New user created NewUser(T::AccountId), + // User edited UserEdited(T::AccountId), + // User deleted UserDeleted(T::AccountId), + // A sell created taken by an user SellOrderCreated(T::AccountId), + // A buy created taken by an user BuyOrderCreated(T::AccountId), + // A ell order taken by an user SellOrderTaken(T::AccountId), + // A buy order taken by an user BuyOrderTaken(T::AccountId), + // Updated balance to an account (who updated the balance, the account, the balance) AfloatBalanceSet(T::AccountId, T::AccountId, T::Balance), + // A new admin is added (who added the admin, the admin) AdminAdded(T::AccountId, T::AccountId), + // A user is assigned to a role (who assigned the role, the user, the role) + UserAssignedToRoleAdded(T::AccountId, T::AccountId, AfloatRole), } // Errors inform users that something went wrong. @@ -399,5 +407,21 @@ pub mod pallet { let who = ensure_signed(origin.clone())?; Self::do_cancel_offer(who, order_id) } + + #[pallet::call_index(12)] + #[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().reads_writes(1,1))] + pub fn assign_user_to_role( + origin: OriginFor, + user_address: T::AccountId, + role: AfloatRole, + ) -> DispatchResult { + let who = ensure_signed(origin.clone())?; + let is_admin_or_owner = Self::is_admin_or_owner(who.clone())?; + ensure!(is_admin_or_owner, Error::::Unauthorized); + ensure!(UserInfo::::contains_key(user_address.clone()), Error::::UserNotFound); + Self::give_role_to_user(user_address.clone(), role.clone())?; + Self::deposit_event(Event::UserAssignedToRoleAdded(who, user_address, role)); + Ok(()) + } } } diff --git a/pallets/gated-marketplace/src/functions.rs b/pallets/gated-marketplace/src/functions.rs index 977293f2..f6924d9e 100644 --- a/pallets/gated-marketplace/src/functions.rs +++ b/pallets/gated-marketplace/src/functions.rs @@ -45,6 +45,9 @@ impl Pallet { Ok(()) } + /// Creates a new marketplace + /// The owner and admin are added to the marketplace as authorities + /// The asset_id is the currency of the marketplace and it's assumed to exist pub fn do_create_marketplace( origin: OriginFor, admin: T::AccountId, @@ -53,22 +56,12 @@ impl Pallet { let owner = ensure_signed(origin.clone())?; // Gen market id let marketplace_id = marketplace.using_encoded(blake2_256); - //Get asset id - let asset_id = marketplace.asset_id; - let min_balance: T::Balance = T::Balance::from(1u32); // ensure the generated id is unique ensure!( !>::contains_key(marketplace_id), Error::::MarketplaceAlreadyExists ); - // Create asset - // pallet_mapped_assets::Pallet::::create( - // origin, - // asset_id, - // T::Lookup::unlookup(owner.clone()), - // min_balance, - // )?; //Insert on marketplaces and marketplaces by auth ::Rbac::create_scope(Self::pallet_id(), marketplace_id)?; Self::insert_in_auth_market_lists(owner.clone(), MarketplaceRole::Owner, marketplace_id)?;