Skip to content

Commit 6c1e122

Browse files
committed
adds marketplace optimizations
1 parent 0e24fbf commit 6c1e122

File tree

4 files changed

+34
-41
lines changed

4 files changed

+34
-41
lines changed

pallets/gated-marketplace/src/functions.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
use super::*;
33
use frame_support::{pallet_prelude::*};
44
use frame_support::sp_io::hashing::blake2_256;
5-
use sp_runtime::sp_std::vec::Vec;
5+
use sp_runtime::sp_std::vec::Vec; // vec primitive
6+
use scale_info::prelude::vec; // vec![] macro
67
use crate::types::*;
78
use pallet_rbac::types::*;
89

@@ -14,9 +15,7 @@ impl<T: Config> Pallet<T> {
1415

1516
pub fn do_initial_setup()->DispatchResult{
1617
let pallet_id = Self::pallet_id();
17-
let mut super_roles = Vec::<Vec<u8>>::new();
18-
super_roles.push(MarketplaceRole::Owner.to_vec());
19-
super_roles.push(MarketplaceRole::Admin.to_vec());
18+
let super_roles = vec![MarketplaceRole::Owner.to_vec(), MarketplaceRole::Admin.to_vec()];
2019
let super_role_ids = T::Rbac::create_and_set_roles(pallet_id.clone(), super_roles)?;
2120
for super_role in super_role_ids{
2221
T::Rbac::create_and_set_permissions(pallet_id.clone(), super_role, Permission::admin_permissions())?;
@@ -27,7 +26,7 @@ impl<T: Config> Pallet<T> {
2726
// appraiser role and permissions
2827
let _appraiser_role_id = T::Rbac::create_and_set_roles(pallet_id.clone(), [MarketplaceRole::Appraiser.to_vec()].to_vec())?;
2928
// redemption specialist role and permissions
30-
let _redemption_role_id = T::Rbac::create_and_set_roles(pallet_id.clone(), [MarketplaceRole::RedemptionSpecialist.to_vec()].to_vec())?;
29+
let _redemption_role_id = T::Rbac::create_and_set_roles(pallet_id, [MarketplaceRole::RedemptionSpecialist.to_vec()].to_vec())?;
3130
Ok(())
3231
}
3332

@@ -37,7 +36,7 @@ impl<T: Config> Pallet<T> {
3736
// ensure the generated id is unique
3837
ensure!(!<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceAlreadyExists);
3938
//Insert on marketplaces and marketplaces by auth
40-
T::Rbac::create_scope(Self::pallet_id(),marketplace_id.clone())?;
39+
T::Rbac::create_scope(Self::pallet_id(),marketplace_id)?;
4140
Self::insert_in_auth_market_lists(owner.clone(), MarketplaceRole::Owner, marketplace_id)?;
4241
Self::insert_in_auth_market_lists(admin.clone(), MarketplaceRole::Admin, marketplace_id)?;
4342
<Marketplaces<T>>::insert(marketplace_id, marketplace);
@@ -72,7 +71,7 @@ impl<T: Config> Pallet<T> {
7271
pub fn do_enroll(authority: T::AccountId, marketplace_id: [u8;32], account_or_application: AccountOrApplication<T>, approved: bool, feedback: BoundedVec<u8, T::MaxFeedbackLen>,)->DispatchResult{
7372
// ensure the origin is owner or admin
7473
//Self::can_enroll(authority, marketplace_id)?;
75-
Self::is_authorized(authority.clone(), &marketplace_id,Permission::Enroll)?;
74+
Self::is_authorized(authority, &marketplace_id,Permission::Enroll)?;
7675
let next_status = match approved{
7776
true => ApplicationStatus::Approved,
7877
false => ApplicationStatus::Rejected,
@@ -99,7 +98,7 @@ impl<T: Config> Pallet<T> {
9998
//ensure the origin is owner or admin
10099
//TODO: implement copy trait for MarketplaceAuthority & T::AccountId
101100
//Self::can_enroll(authority, marketplace_id)?;
102-
Self::is_authorized(authority.clone(), &marketplace_id,Permission::AddAuth)?;
101+
Self::is_authorized(authority, &marketplace_id,Permission::AddAuth)?;
103102
//ensure the account is not already an authority
104103
// handled by T::Rbac::assign_role_to_user
105104
//ensure!(!Self::does_exist_authority(account.clone(), marketplace_id, authority_type), Error::<T>::AlreadyApplied);
@@ -130,7 +129,7 @@ impl<T: Config> Pallet<T> {
130129
match authority_type{
131130
MarketplaceRole::Owner => {
132131
ensure!(Self::owner_exist(marketplace_id), Error::<T>::OwnerNotFound);
133-
Err(Error::<T>::CantRemoveOwner)?;
132+
return Err(Error::<T>::CantRemoveOwner.into());
134133
},
135134
MarketplaceRole::Admin => {
136135
// Admins can not delete themselves
@@ -187,7 +186,7 @@ impl<T: Config> Pallet<T> {
187186
if let Some(a) = pallet_uniques::Pallet::<T>::owner(collection_id, item_id) {
188187
ensure!(a == authority, Error::<T>::NotOwner);
189188
} else {
190-
Err(Error::<T>::CollectionNotFound)?;
189+
return Err(Error::<T>::CollectionNotFound.into());
191190
}
192191

193192
//ensure the price is valid
@@ -254,7 +253,7 @@ impl<T: Config> Pallet<T> {
254253
if let Some(a) = pallet_uniques::Pallet::<T>::owner(collection_id, item_id) {
255254
ensure!(a != authority, Error::<T>::CannotCreateOffer);
256255
} else {
257-
Err(Error::<T>::CollectionNotFound)?;
256+
return Err(Error::<T>::CollectionNotFound.into());
258257
}
259258

260259
//ensure user has enough balance to create the offer
@@ -457,8 +456,8 @@ impl<T: Config> Pallet<T> {
457456
}).collect();
458457
let custodian = match custodian_fields{
459458
Some(c_fields)=>{
460-
for i in 0..f.len(){
461-
f[i].custodian_cid = Some(c_fields.1[i].clone());
459+
for (i, field) in f.iter_mut().enumerate(){
460+
field.custodian_cid = Some(c_fields.1[i].clone());
462461
}
463462

464463
Some(c_fields.0)
@@ -470,7 +469,7 @@ impl<T: Config> Pallet<T> {
470469

471470
fn insert_in_auth_market_lists(authority: T::AccountId, role: MarketplaceRole, marketplace_id: [u8;32])->DispatchResult{
472471

473-
T::Rbac::assign_role_to_user(authority.clone(), Self::pallet_id(),
472+
T::Rbac::assign_role_to_user(authority, Self::pallet_id(),
474473
&marketplace_id, role.id())?;
475474

476475
Ok(())
@@ -546,7 +545,7 @@ impl<T: Config> Pallet<T> {
546545
T::Rbac::is_authorized(
547546
authority,
548547
Self::pallet_id(),
549-
&marketplace_id,
548+
marketplace_id,
550549
&permission.id(),
551550
)
552551
}
@@ -651,8 +650,8 @@ impl<T: Config> Pallet<T> {
651650
.map_err(|_| Error::<T>::ApplicationNotFound)?;
652651

653652
match application.status {
654-
ApplicationStatus::Pending => Err(Error::<T>::ApplicationStatusStillPending)?,
655-
ApplicationStatus::Approved => Err(Error::<T>::ApplicationHasAlreadyBeenApproved)?,
653+
ApplicationStatus::Pending => return Err(Error::<T>::ApplicationStatusStillPending.into()),
654+
ApplicationStatus::Approved => return Err(Error::<T>::ApplicationHasAlreadyBeenApproved.into()),
656655
ApplicationStatus::Rejected => {
657656
//If status is Rejected, we need to delete the previous application from all the storage sources.
658657
<Applications<T>>::remove(application_id);
@@ -709,34 +708,28 @@ impl<T: Config> Pallet<T> {
709708

710709
fn is_the_price_valid(price: BalanceOf<T>,) -> DispatchResult {
711710
let minimun_amount: BalanceOf<T> = 1000u32.into();
712-
if price > minimun_amount {
713-
Ok(())
714-
} else {
715-
Err(Error::<T>::PriceMustBeGreaterThanZero)?
716-
}
711+
ensure!(price > minimun_amount, Error::<T>::PriceMustBeGreaterThanZero);
712+
Ok(())
717713
}
718714

719715
fn can_this_item_receive_sell_orders(collection_id: T::CollectionId, item_id: T::ItemId, marketplace_id: [u8;32]) -> DispatchResult {
720716
let offers = <OffersByItem<T>>::get(collection_id, item_id);
721717

722718
//if len is == 0, it means that there is no offers for this item, maybe it's the first entry
723-
if offers.len() == 0 {
724-
return Ok(());
725-
} else if offers.len() > 0 {
719+
if offers.len() > 0 {
726720
for offer in offers {
727721
let offer_info = <OffersInfo<T>>::get(offer).ok_or(Error::<T>::OfferNotFound)?;
728722
//ensure the offer_type is SellOrder, because this vector also contains buy offers.
729723
if offer_info.marketplace_id == marketplace_id && offer_info.offer_type == OfferType::SellOrder {
730-
return Err(Error::<T>::OfferAlreadyExists)?;
724+
return Err(Error::<T>::OfferAlreadyExists.into());
731725
}
732726
}
733-
}
734-
727+
}
728+
735729
Ok(())
736730
}
737731

738732
fn can_this_item_receive_buy_orders(collection_id: T::CollectionId, item_id: T::ItemId, marketplace_id: [u8;32]) -> DispatchResult {
739-
//TODO: optimize this function, when rust-analyzer pluggin is fixed, it will be possible to use the .iter().find()
740733
//First we check if the item has is for sale, if not, return error
741734
ensure!(<OffersByItem<T>>::contains_key(collection_id, item_id), Error::<T>::ItemNotForSale);
742735

@@ -751,7 +744,7 @@ impl<T: Config> Pallet<T> {
751744
}
752745
}
753746

754-
Err(Error::<T>::ItemNotForSale)?
747+
Err(Error::<T>::ItemNotForSale.into())
755748
}
756749

757750

pallets/gated-marketplace/src/types.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl Default for MarketplaceRole{
4141
}
4242

4343
impl MarketplaceRole{
44-
pub fn to_vec(&self) -> Vec<u8>{
44+
pub fn to_vec(self) -> Vec<u8>{
4545
match self{
4646
Self::Owner => "Owner".as_bytes().to_vec(),
4747
Self::Admin => "Admin".as_bytes().to_vec(),
@@ -78,19 +78,19 @@ pub enum Permission{
7878
}
7979

8080
impl Permission{
81-
pub fn to_vec(&self) -> Vec<u8>{
81+
pub fn to_vec(self) -> Vec<u8>{
8282
match self{
8383
Self::Enroll => "Enroll".as_bytes().to_vec(),
8484
Self::AddAuth => "AddAuth".as_bytes().to_vec(),
8585
Self::RemoveAuth => "RemoveAuth".as_bytes().to_vec(),
8686
Self::UpdateLabel => "UpdateLabel".as_bytes().to_vec(),
8787
Self::RemoveMarketplace => "RemoveMarketplace".as_bytes().to_vec(),
88-
&Self::EnlistSellOffer=>"EnlistSellOffer".as_bytes().to_vec(),
89-
&Self::TakeSellOffer=>"TakeSellOffer".as_bytes().to_vec(),
90-
&Self::DuplicateOffer=>"DuplicateOffer".as_bytes().to_vec(),
91-
&Self::RemoveOffer=>"RemoveOffer".as_bytes().to_vec(),
92-
&Self::EnlistBuyOffer=>"EnlistBuyOffer".as_bytes().to_vec(),
93-
&Self::TakeBuyOffer=>"TakeBuyOffer".as_bytes().to_vec(),
88+
Self::EnlistSellOffer=>"EnlistSellOffer".as_bytes().to_vec(),
89+
Self::TakeSellOffer=>"TakeSellOffer".as_bytes().to_vec(),
90+
Self::DuplicateOffer=>"DuplicateOffer".as_bytes().to_vec(),
91+
Self::RemoveOffer=>"RemoveOffer".as_bytes().to_vec(),
92+
Self::EnlistBuyOffer=>"EnlistBuyOffer".as_bytes().to_vec(),
93+
Self::TakeBuyOffer=>"TakeBuyOffer".as_bytes().to_vec(),
9494
}
9595
}
9696

pallets/rbac/src/functions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<T: Config> RoleBasedAccessControl<T::AccountId> for Pallet<T>{
104104
}
105105
Self::set_multiple_pallet_roles(pallet.to_id_enum(), role_ids.clone())?;
106106
let bounded_ids = Self::bound(role_ids, Error::<T>::ExceedMaxRolesPerPallet)?;
107+
Self::deposit_event(Event::RolesStored(pallet.to_id()));
107108
Ok(bounded_ids)
108109
}
109110

pallets/rbac/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ pub mod pallet {
108108
_,
109109
(
110110
NMapKey<Blake2_128Concat, T::AccountId>,// user
111-
// getting "the trait bound `usize: scale_info::TypeInfo` is not satisfied" errors
112111
NMapKey<Identity, PalletId>, // pallet_id
113112
NMapKey<Identity, ScopeId>, // scope_id
114113
),
@@ -137,8 +136,8 @@ pub mod pallet {
137136
#[pallet::event]
138137
#[pallet::generate_deposit(pub(super) fn deposit_event)]
139138
pub enum Event<T: Config> {
140-
/// Event documentation should end with an array that provides descriptive names for event
141-
SomethingStored(u32, T::AccountId),
139+
/// An initial roles config was stored [pallet_id]
140+
RolesStored(PalletId),
142141
}
143142

144143
// Errors inform users that something went wrong.

0 commit comments

Comments
 (0)