Skip to content

Commit

Permalink
update roles assignment for afloat (#17)
Browse files Browse the repository at this point in the history
* 🔧 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
  • Loading branch information
tlacloc authored Sep 27, 2023
1 parent d806af2 commit 7ec0bdf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
30 changes: 27 additions & 3 deletions pallets/afloat/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -56,16 +54,26 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
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.
Expand Down Expand Up @@ -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<T>,
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::<T>::Unauthorized);
ensure!(UserInfo::<T>::contains_key(user_address.clone()), Error::<T>::UserNotFound);
Self::give_role_to_user(user_address.clone(), role.clone())?;
Self::deposit_event(Event::UserAssignedToRoleAdded(who, user_address, role));
Ok(())
}
}
}
13 changes: 3 additions & 10 deletions pallets/gated-marketplace/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ impl<T: Config> Pallet<T> {
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<T>,
admin: T::AccountId,
Expand All @@ -53,22 +56,12 @@ impl<T: Config> Pallet<T> {
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!(
!<Marketplaces<T>>::contains_key(marketplace_id),
Error::<T>::MarketplaceAlreadyExists
);
// Create asset
// pallet_mapped_assets::Pallet::<T>::create(
// origin,
// asset_id,
// T::Lookup::unlookup(owner.clone()),
// min_balance,
// )?;
//Insert on marketplaces and marketplaces by auth
<T as pallet::Config>::Rbac::create_scope(Self::pallet_id(), marketplace_id)?;
Self::insert_in_auth_market_lists(owner.clone(), MarketplaceRole::Owner, marketplace_id)?;
Expand Down

0 comments on commit 7ec0bdf

Please sign in to comment.