Skip to content

Commit 7ec0bdf

Browse files
authored
update roles assignment for afloat (#17)
* 🔧 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
1 parent d806af2 commit 7ec0bdf

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

pallets/afloat/src/lib.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ pub mod types;
1515
pub mod pallet {
1616
use frame_support::{
1717
pallet_prelude::*,
18-
sp_io::hashing::blake2_256,
1918
traits::{Currency, UnixTime},
2019
};
2120
use frame_system::{pallet_prelude::*, RawOrigin};
2221
use pallet_fruniques::types::{Attributes, CollectionDescription, FruniqueRole, ParentInfo};
2322
use pallet_gated_marketplace::types::*;
24-
use sp_runtime::{traits::StaticLookup, Permill};
2523
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
2624

2725
use crate::types::*;
@@ -56,16 +54,26 @@ pub mod pallet {
5654
#[pallet::event]
5755
#[pallet::generate_deposit(pub(super) fn deposit_event)]
5856
pub enum Event<T: Config> {
59-
SomethingStored(u32, T::AccountId),
57+
// New user created
6058
NewUser(T::AccountId),
59+
// User edited
6160
UserEdited(T::AccountId),
61+
// User deleted
6262
UserDeleted(T::AccountId),
63+
// A sell created taken by an user
6364
SellOrderCreated(T::AccountId),
65+
// A buy created taken by an user
6466
BuyOrderCreated(T::AccountId),
67+
// A ell order taken by an user
6568
SellOrderTaken(T::AccountId),
69+
// A buy order taken by an user
6670
BuyOrderTaken(T::AccountId),
71+
// Updated balance to an account (who updated the balance, the account, the balance)
6772
AfloatBalanceSet(T::AccountId, T::AccountId, T::Balance),
73+
// A new admin is added (who added the admin, the admin)
6874
AdminAdded(T::AccountId, T::AccountId),
75+
// A user is assigned to a role (who assigned the role, the user, the role)
76+
UserAssignedToRoleAdded(T::AccountId, T::AccountId, AfloatRole),
6977
}
7078

7179
// Errors inform users that something went wrong.
@@ -399,5 +407,21 @@ pub mod pallet {
399407
let who = ensure_signed(origin.clone())?;
400408
Self::do_cancel_offer(who, order_id)
401409
}
410+
411+
#[pallet::call_index(12)]
412+
#[pallet::weight(Weight::from_parts(10_000,0) + T::DbWeight::get().reads_writes(1,1))]
413+
pub fn assign_user_to_role(
414+
origin: OriginFor<T>,
415+
user_address: T::AccountId,
416+
role: AfloatRole,
417+
) -> DispatchResult {
418+
let who = ensure_signed(origin.clone())?;
419+
let is_admin_or_owner = Self::is_admin_or_owner(who.clone())?;
420+
ensure!(is_admin_or_owner, Error::<T>::Unauthorized);
421+
ensure!(UserInfo::<T>::contains_key(user_address.clone()), Error::<T>::UserNotFound);
422+
Self::give_role_to_user(user_address.clone(), role.clone())?;
423+
Self::deposit_event(Event::UserAssignedToRoleAdded(who, user_address, role));
424+
Ok(())
425+
}
402426
}
403427
}

pallets/gated-marketplace/src/functions.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ impl<T: Config> Pallet<T> {
4545
Ok(())
4646
}
4747

48+
/// Creates a new marketplace
49+
/// The owner and admin are added to the marketplace as authorities
50+
/// The asset_id is the currency of the marketplace and it's assumed to exist
4851
pub fn do_create_marketplace(
4952
origin: OriginFor<T>,
5053
admin: T::AccountId,
@@ -53,22 +56,12 @@ impl<T: Config> Pallet<T> {
5356
let owner = ensure_signed(origin.clone())?;
5457
// Gen market id
5558
let marketplace_id = marketplace.using_encoded(blake2_256);
56-
//Get asset id
57-
let asset_id = marketplace.asset_id;
58-
let min_balance: T::Balance = T::Balance::from(1u32);
5959

6060
// ensure the generated id is unique
6161
ensure!(
6262
!<Marketplaces<T>>::contains_key(marketplace_id),
6363
Error::<T>::MarketplaceAlreadyExists
6464
);
65-
// Create asset
66-
// pallet_mapped_assets::Pallet::<T>::create(
67-
// origin,
68-
// asset_id,
69-
// T::Lookup::unlookup(owner.clone()),
70-
// min_balance,
71-
// )?;
7265
//Insert on marketplaces and marketplaces by auth
7366
<T as pallet::Config>::Rbac::create_scope(Self::pallet_id(), marketplace_id)?;
7467
Self::insert_in_auth_market_lists(owner.clone(), MarketplaceRole::Owner, marketplace_id)?;

0 commit comments

Comments
 (0)