Skip to content

Commit d04f6ca

Browse files
committed
Add validations to prevent users to make offers for items thar are not for sale.
1 parent df66e3a commit d04f6ca

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

pallets/gated-marketplace/src/functions.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ impl<T: Config> Pallet<T> {
219219
pub fn do_enlist_buy_offer(authority: T::AccountId, marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId, price: BalanceOf<T>,) -> DispatchResult {
220220
//TODO: ensure the user is a Marketparticipant
221221

222+
//ensure the item is for sale, if not, return error
223+
ensure!(<OffersByItem<T>>::contains_key(collection_id, item_id), Error::<T>::ItemNotForSale);
224+
222225
//ensure the marketplace exists
223226
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
224227

@@ -284,6 +287,7 @@ impl<T: Config> Pallet<T> {
284287
}
285288

286289
pub fn do_take_sell_offer(buyer: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
290+
//This extrisicn is called by the user who wants to buy the item
287291
//ensure the collection & owner exists
288292
let owner_item = pallet_uniques::Pallet::<T>::owner(collection_id, item_id).ok_or(Error::<T>::OwnerNotFound)?;
289293

@@ -325,10 +329,14 @@ impl<T: Config> Pallet<T> {
325329
Ok(())
326330
}
327331

328-
pub fn do_take_buy_offer(offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
332+
pub fn do_take_buy_offer(authority: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
333+
//This extrinsic is called by the owner of the item who accepts the buy offer from the interested user.
329334
//ensure the collection & owner exists
330335
let owner_item = pallet_uniques::Pallet::<T>::owner(collection_id, item_id).ok_or(Error::<T>::OwnerNotFound)?;
331336

337+
//ensure only owner of the item can call the extrinic
338+
ensure!(owner_item == authority.clone(), Error::<T>::NotOwner);
339+
332340
// Get the account_id of the offer creator (the buyer)
333341
let buy_offer_creator = Self::get_offer_creator(offer_id).map_err(|_| Error::<T>::OfferNotFound)?;
334342

@@ -697,7 +705,8 @@ impl<T: Config> Pallet<T> {
697705
}
698706
Ok(())
699707
}
700-
708+
709+
//TODO: merge the timestamp function to convert from moment to milliseconds.
701710
fn convert_moment_to_u64_in_milliseconds(date: T::Moment) -> Result<u64, DispatchError> {
702711
let date_as_u64_millis;
703712
if let Some(_date_as_u64) = TryInto::<u64>::try_into(date).ok() {
@@ -811,12 +820,11 @@ impl<T: Config> Pallet<T> {
811820
}
812821
}
813822
}
814-
815823
Ok(())
816824
}
817825

818826

819-
fn delete_all_sell_orders_for_this_item(collection_id: T::CollectionId, item_id: T::ItemId) -> DispatchResult {
827+
fn _delete_all_sell_orders_for_this_item(collection_id: T::CollectionId, item_id: T::ItemId) -> DispatchResult {
820828
//ensure the item has offers associated with it.
821829
ensure!(<OffersByItem<T>>::contains_key(collection_id, item_id), Error::<T>::OfferNotFound);
822830

pallets/gated-marketplace/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ pub mod pallet {
291291
PriceMustBeGreaterThanZero,
292292
/// User cannot create buy offers for their own items
293293
CannotCreateOffer,
294+
/// This items is not available for sale
295+
ItemNotForSale,
294296
}
295297

296298
#[pallet::call]
@@ -559,7 +561,7 @@ pub mod pallet {
559561
pub fn take_buy_offer(origin: OriginFor<T>, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
560562
let who = ensure_signed(origin.clone())?;
561563

562-
Self::do_take_buy_offer(offer_id, marketplace_id, collection_id, item_id)
564+
Self::do_take_buy_offer(who, offer_id, marketplace_id, collection_id, item_id)
563565
}
564566

565567

0 commit comments

Comments
 (0)