11use super :: * ;
22use frame_support:: { pallet_prelude:: * } ;
3- //use frame_system::pallet_prelude::*;
43use frame_support:: sp_io:: hashing:: blake2_256;
54use sp_runtime:: sp_std:: vec:: Vec ;
65use crate :: types:: * ;
76use frame_support:: traits:: Time ;
8- // use frame_support::traits::{Currency};
9- // use frame_support::traits::ExistenceRequirement::KeepAlive;
7+ use frame_support:: traits:: { Currency } ;
8+ use frame_support:: traits:: ExistenceRequirement :: KeepAlive ;
109
1110impl < T : Config > Pallet < T > {
1211
@@ -47,7 +46,7 @@ impl<T: Config> Pallet<T> {
4746 Ok ( ( ) )
4847 }
4948
50- pub fn do_enroll ( authority : T :: AccountId , marketplace_id : [ u8 ; 32 ] , account_or_application : AccountOrApplication < T > , approved : bool , feedback : BoundedVec < u8 , T :: MaxFeedbackLen > , ) ->DispatchResult {
49+ pub fn do_enroll ( authority : T :: AccountId , marketplace_id : [ u8 ; 32 ] , account_or_application : AccountOrApplication < T > , approved : bool , feedback : BoundedVec < u8 , T :: MaxFeedbackLen > , ) ->DispatchResult {
5150 // ensure the origin is owner or admin
5251 Self :: can_enroll ( authority, marketplace_id) ?;
5352 let next_status = match approved{
@@ -152,7 +151,7 @@ impl<T: Config> Pallet<T> {
152151 Ok ( ( ) )
153152 }
154153
155- pub fn do_enlist_sell_offer ( authority : T :: AccountId , marketplace_id : [ u8 ; 32 ] , collection_id : T :: CollectionId , item_id : T :: ItemId , price : u128 , ) -> DispatchResult {
154+ pub fn do_enlist_sell_offer ( authority : T :: AccountId , marketplace_id : [ u8 ; 32 ] , collection_id : T :: CollectionId , item_id : T :: ItemId , price : BalanceOf < T > , ) -> DispatchResult {
156155 //This function is only called by the owner of the marketplace
157156 //ensure the marketplace exists
158157 ensure ! ( <Marketplaces <T >>:: contains_key( marketplace_id) , Error :: <T >:: MarketplaceNotFound ) ;
@@ -165,13 +164,12 @@ impl<T: Config> Pallet<T> {
165164 }
166165
167166 //ensure the price is valid
168- // Self::is_the_price_valid(price)?;
167+ Self :: is_the_price_valid ( price) ?;
169168
170169 //Add timestamp to the offer
171170 let ( timestamp, timestamp2) = Self :: get_timestamp_in_milliseconds ( ) . ok_or ( Error :: < T > :: TimestampError ) ?;
172171
173172 //create an offer_id
174- //TODO: create an offer id generator, used in cases where the offer_id generated is not unique
175173 let offer_id = ( marketplace_id, authority. clone ( ) , collection_id, timestamp, timestamp2) . using_encoded ( blake2_256) ;
176174
177175 //create offer structure
@@ -215,15 +213,15 @@ impl<T: Config> Pallet<T> {
215213 Ok ( ( ) )
216214 }
217215
218- pub fn do_enlist_buy_offer ( authority : T :: AccountId , marketplace_id : [ u8 ; 32 ] , collection_id : T :: CollectionId , item_id : T :: ItemId , price : u128 , ) -> DispatchResult {
216+ 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 {
219217 //ensure the item is for sale, if not, return error
220218 Self :: can_this_item_receive_buy_orders ( collection_id, item_id, marketplace_id) ?;
221219
222220 //ensure the marketplace exists
223221 ensure ! ( <Marketplaces <T >>:: contains_key( marketplace_id) , Error :: <T >:: MarketplaceNotFound ) ;
224222
225223 //ensure the collection exists
226- //For this case user doesn't have to be the owner of the collection
224+ //For this case user doesn't need to be the owner of the collection
227225 //but the owner of the item cannot create a buy offer for their own collection
228226 if let Some ( a) = pallet_uniques:: Pallet :: < T > :: owner ( collection_id, item_id) {
229227 ensure ! ( a != authority, Error :: <T >:: CannotCreateOffer ) ;
@@ -232,11 +230,11 @@ impl<T: Config> Pallet<T> {
232230 }
233231
234232 //ensure user has enough balance to create the offer
235- // let total_user_balance = T::LocalCurrency ::total_balance(&authority);
236- // ensure!(total_user_balance >= price, Error::<T>::NotEnoughBalance);
233+ let total_user_balance = T :: Currency :: total_balance ( & authority) ;
234+ ensure ! ( total_user_balance >= price, Error :: <T >:: NotEnoughBalance ) ;
237235
238236 //ensure the price is valid
239- // Self::is_the_price_valid(price)?;
237+ Self :: is_the_price_valid ( price) ?;
240238
241239 //Add timestamp to the offer
242240 let ( timestamp, timestamp2) = Self :: get_timestamp_in_milliseconds ( ) . ok_or ( Error :: < T > :: TimestampError ) ?;
@@ -283,20 +281,11 @@ impl<T: Config> Pallet<T> {
283281 Ok ( ( ) )
284282 }
285283
286- pub fn do_take_sell_offer ( buyer : T :: AccountId , offer_id : [ u8 ; 32 ] , marketplace_id : [ u8 ; 32 ] ) -> DispatchResult {
284+ pub fn do_take_sell_offer ( buyer : T :: AccountId , offer_id : [ u8 ; 32 ] ) -> DispatchResult {
287285 //This extrisicn is called by the user who wants to buy the item
288- //ensure offer exists
289- ensure ! ( <OffersInfo <T >>:: contains_key( offer_id) , Error :: <T >:: OfferNotFound ) ;
290-
291- //ensure the marketplace exists
292- ensure ! ( <Marketplaces <T >>:: contains_key( marketplace_id) , Error :: <T >:: MarketplaceNotFound ) ;
293-
294286 //get offer data
295287 let offer_data = <OffersInfo < T > >:: get ( offer_id) . ok_or ( Error :: < T > :: OfferNotFound ) ?;
296288
297- //ensure the marketplace_id where the extrinsic is being called is the same as the one in the offer
298- ensure ! ( offer_data. marketplace_id == marketplace_id, Error :: <T >:: MarketplaceNotFound ) ;
299-
300289 //ensure the collection & owner exists
301290 let owner_item = pallet_uniques:: Pallet :: < T > :: owner ( offer_data. collection_id , offer_data. item_id ) . ok_or ( Error :: < T > :: OwnerNotFound ) ?;
302291
@@ -311,19 +300,19 @@ impl<T: Config> Pallet<T> {
311300
312301 //TODO: Use free_balance instead of total_balance
313302 //Get the buyer's balance
314- // let total_amount_buyer = T::LocalCurrency ::total_balance(&buyer);
303+ let total_amount_buyer = T :: Currency :: total_balance ( & buyer) ;
315304 //ensure the buyer has enough balance to buy the item
316- // ensure!(total_amount_buyer > offer_data.price, Error::<T>::NotEnoughBalance);
305+ ensure ! ( total_amount_buyer > offer_data. price, Error :: <T >:: NotEnoughBalance ) ;
317306 //Transfer the balance
318- // T::LocalCurrency ::transfer(&buyer, &owner_item, offer_data.price, KeepAlive)?;
307+ T :: Currency :: transfer ( & buyer, & owner_item, offer_data. price , KeepAlive ) ?;
319308
320309 //Use uniques transfer function to transfer the item to the buyer
321310 pallet_uniques:: Pallet :: < T > :: do_transfer ( offer_data. collection_id , offer_data. item_id , buyer. clone ( ) , |_, _|{
322311 Ok ( ( ) )
323312 } ) ?;
324313
325314 //update offer status from all marketplaces
326- Self :: update_offers_status ( buyer. clone ( ) , offer_data. collection_id , offer_data. item_id , marketplace_id) ?;
315+ Self :: update_offers_status ( buyer. clone ( ) , offer_data. collection_id , offer_data. item_id , offer_data . marketplace_id ) ?;
327316
328317 //remove all the offers associated with the item
329318 Self :: delete_all_offers_for_this_item ( offer_data. collection_id , offer_data. item_id ) ?;
@@ -332,20 +321,12 @@ impl<T: Config> Pallet<T> {
332321 Ok ( ( ) )
333322 }
334323
335- pub fn do_take_buy_offer ( authority : T :: AccountId , offer_id : [ u8 ; 32 ] , marketplace_id : [ u8 ; 32 ] ) -> DispatchResult {
324+ pub fn do_take_buy_offer ( authority : T :: AccountId , offer_id : [ u8 ; 32 ] ) -> DispatchResult {
336325 //This extrinsic is called by the owner of the item who accepts the buy offer created by a marketparticipant
337- //Ensure offer exists. This offer_id corresponds to the buy order
338- ensure ! ( <OffersInfo <T >>:: contains_key( offer_id) , Error :: <T >:: OfferNotFound ) ;
339-
340- //ensure the marketplace exists
341- ensure ! ( <Marketplaces <T >>:: contains_key( marketplace_id) , Error :: <T >:: MarketplaceNotFound ) ;
342326
343327 //get offer data
344328 let offer_data = <OffersInfo < T > >:: get ( offer_id) . ok_or ( Error :: < T > :: OfferNotFound ) ?;
345329
346- //ensure the marketplace_id where the extrinsic is being called is the same as the one in the offer
347- ensure ! ( offer_data. marketplace_id == marketplace_id, Error :: <T >:: MarketplaceNotFound ) ;
348-
349330 //ensure the collection & owner exists
350331 let owner_item = pallet_uniques:: Pallet :: < T > :: owner ( offer_data. collection_id , offer_data. item_id ) . ok_or ( Error :: < T > :: OwnerNotFound ) ?;
351332
@@ -363,19 +344,18 @@ impl<T: Config> Pallet<T> {
363344
364345 //TODO: Use free_balance instead of total_balance
365346 //Get the buyer's balance
366- // let total_amount_buyer = T::LocalCurrency ::total_balance(&offer_data.creator);
347+ let total_amount_buyer = T :: Currency :: total_balance ( & offer_data. creator ) ;
367348 //ensure the buy_offer_creator has enough balance to buy the item
368- // ensure!(total_amount_buyer > offer_data.price, Error::<T>::NotEnoughBalance);
349+ ensure ! ( total_amount_buyer > offer_data. price, Error :: <T >:: NotEnoughBalance ) ;
369350 //Transfer the balance to the owner of the item
370- // T::LocalCurrency::transfer(&offer_data.creator, &owner_item, offer_data.price, KeepAlive)?;
371-
351+ T :: Currency :: transfer ( & offer_data. creator , & owner_item, offer_data. price , KeepAlive ) ?;
372352 //Use uniques transfer function to transfer the item to the market_participant
373353 pallet_uniques:: Pallet :: < T > :: do_transfer ( offer_data. collection_id , offer_data. item_id , offer_data. creator . clone ( ) , |_, _|{
374354 Ok ( ( ) )
375355 } ) ?;
376356
377357 //update offer status from all marketplaces
378- Self :: update_offers_status ( offer_data. creator . clone ( ) , offer_data. collection_id , offer_data. item_id , marketplace_id) ?;
358+ Self :: update_offers_status ( offer_data. creator . clone ( ) , offer_data. collection_id , offer_data. item_id , offer_data . marketplace_id ) ?;
379359
380360 //remove all the offers associated with the item
381361 Self :: delete_all_offers_for_this_item ( offer_data. collection_id , offer_data. item_id ) ?;
@@ -384,53 +364,6 @@ impl<T: Config> Pallet<T> {
384364 Ok ( ( ) )
385365 }
386366
387- //TODO: Under development
388- // pub fn do_duplicate_offer(authority: T::AccountId, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId, modified_price: BalanceOf<T>) -> DispatchResult{
389- // //ensure new marketplace_id exits
390- // ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);
391-
392- // //ensure that the offer_id exists
393- // ensure!(<OffersInfo<T>>::contains_key(offer_id), Error::<T>::OfferNotFound);
394-
395- // //ensure the offer_id exists in OffersByItem
396- // Self::does_exist_offer_id_for_this_item(collection_id, item_id, offer_id)?;
397-
398- // //get the offer data
399- // let mut copy_offer_data = <OffersInfo<T>>::get(offer_id).ok_or(Error::<T>::OfferNotFound)?;
400-
401- // //modify the offer data
402- // //by know we only allow to modify its price by the user
403- // //we modify its marketplace_id because the offer is duplicated to another marketplace
404- // copy_offer_data.price = modified_price;
405- // copy_offer_data.marketplace_id = marketplace_id;
406-
407- // //generate a new offer_id
408- // let new_offer_id = (marketplace_id, authority.clone(), collection_id, copy_offer_data.creation_date, copy_offer_data.expiration_date).using_encoded(blake2_256);
409-
410- // //insert in OffersInfo
411- // // validate new offer_id does not exists
412- // ensure!(!<OffersInfo<T>>::contains_key(new_offer_id), Error::<T>::OfferAlreadyExists);
413- // <OffersInfo<T>>::insert(new_offer_id, copy_offer_data);
414-
415- // //insert in OffersByMarketplace
416- // <OffersByMarketplace<T>>::try_mutate(marketplace_id, |offer| {
417- // offer.try_push(new_offer_id)
418- // }).map_err(|_| Error::<T>::OfferStorageError)?;
419-
420- // //insert in OffersByAccount
421- // <OffersByAccount<T>>::try_mutate(authority.clone(), |offer| {
422- // offer.try_push(new_offer_id)
423- // }).map_err(|_| Error::<T>::OfferStorageError)?;
424-
425- // //add the new offer_id to OffersByItem
426- // <OffersByItem<T>>::try_mutate(collection_id, item_id, |offers| {
427- // offers.try_push(new_offer_id)
428- // }).map_err(|_| Error::<T>::OfferStorageError)?;
429-
430- // Self::deposit_event(Event::OfferDuplicated(new_offer_id, marketplace_id));
431-
432- // Ok(())
433- // }
434367
435368 pub fn do_remove_offer ( authority : T :: AccountId , offer_id : [ u8 ; 32 ] ) -> DispatchResult {
436369
@@ -483,7 +416,7 @@ impl<T: Config> Pallet<T> {
483416 /*---- Helper functions ----*/
484417
485418 pub fn set_up_application (
486- fields : BoundedVec < ( BoundedVec < u8 , ConstU32 < 100 > > , BoundedVec < u8 , ConstU32 < 100 > > ) , T :: MaxFiles > ,
419+ fields : Fields < T > ,
487420 custodian_fields : Option < ( T :: AccountId , BoundedVec < BoundedVec < u8 , ConstU32 < 100 > > , T :: MaxFiles > ) >
488421 ) -> ( Option < T :: AccountId > , BoundedVec < ApplicationField , T :: MaxFiles > ) {
489422 let mut f: Vec < ApplicationField > = fields. iter ( ) . map ( |tuple|{
@@ -647,7 +580,8 @@ impl<T: Config> Pallet<T> {
647580
648581 //First we need to get the list of all the authorities for the marketplace.
649582 let _users = <AuthoritiesByMarketplace < T > >:: iter_prefix ( marketplace_id)
650- . map ( |( _authority, users) | users) . flatten ( ) . collect :: < Vec < _ > > ( ) ;
583+ . flat_map ( |( _authority, users) | users) . collect :: < Vec < _ > > ( ) ;
584+
651585
652586 //1. remove from MarketplacesByAuthority
653587 _users. iter ( ) . for_each ( |user|{
@@ -725,9 +659,9 @@ impl<T: Config> Pallet<T> {
725659 fn _is_offer_status ( offer_id : [ u8 ; 32 ] , offer_status : OfferStatus , ) -> bool {
726660 //we already know that the offer exists, so we don't need to check it here.
727661 if let Some ( offer) = <OffersInfo < T > >:: get ( offer_id) {
728- return offer. status == offer_status;
662+ offer. status == offer_status
729663 } else {
730- return false ;
664+ false
731665 }
732666 }
733667
@@ -740,14 +674,6 @@ impl<T: Config> Pallet<T> {
740674 }
741675
742676
743- fn _get_offer_creator ( offer_id : [ u8 ; 32 ] , ) -> Result < T :: AccountId , DispatchError > {
744- //we already know that the offer exists, so we don't need to check it here.
745- if let Some ( offer) = <OffersInfo < T > >:: get ( offer_id) {
746- return Ok ( offer. creator ) ;
747- } else {
748- return Err ( Error :: < T > :: OfferNotFound ) ?;
749- }
750- }
751677
752678 //sell orders here...
753679
@@ -766,6 +692,14 @@ impl<T: Config> Pallet<T> {
766692 Ok ( ( ) )
767693 }
768694
695+ fn is_the_price_valid ( price : BalanceOf < T > , ) -> DispatchResult {
696+ let minimun_amount: BalanceOf < T > = 1000u32 . into ( ) ;
697+ if price > minimun_amount {
698+ Ok ( ( ) )
699+ } else {
700+ Err ( Error :: < T > :: PriceMustBeGreaterThanZero ) ?
701+ }
702+ }
769703
770704 fn can_this_item_receive_sell_orders ( collection_id : T :: CollectionId , item_id : T :: ItemId , marketplace_id : [ u8 ; 32 ] ) -> DispatchResult {
771705 let offers = <OffersByItem < T > >:: get ( collection_id, item_id) ;
@@ -798,11 +732,11 @@ impl<T: Config> Pallet<T> {
798732 let offer_info = <OffersInfo < T > >:: get ( offer) . ok_or ( Error :: < T > :: OfferNotFound ) ?;
799733 //ensure the offer_type is SellOrder, because this vector also contains buy offers.
800734 if offer_info. marketplace_id == marketplace_id && offer_info. offer_type == OfferType :: SellOrder {
801- return Ok ( ( ) ) ;
735+ return Ok ( ( ) )
802736 }
803737 }
804738
805- return Err ( Error :: < T > :: ItemNotForSale ) ?;
739+ Err ( Error :: < T > :: ItemNotForSale ) ?
806740 }
807741
808742
0 commit comments