Skip to content

Commit 2d271a3

Browse files
committed
Merge branch 'feature/offers' into feature/role-base-access-control
2 parents 06b9ea0 + 2a39951 commit 2d271a3

File tree

9 files changed

+184
-256
lines changed

9 files changed

+184
-256
lines changed

.devcontainer/devcontainer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"lldb.executable": "/usr/bin/lldb"
77
},
88
"extensions": [
9-
"rust-lang.rust",
10-
"bungcip.better-toml",
11-
"vadimcn.vscode-lldb"
12-
],
9+
"rust-lang.rust",
10+
"bungcip.better-toml",
11+
"vadimcn.vscode-lldb",
12+
"rust-lang.rust-analyzer"
13+
],
1314
"forwardPorts": [
1415
3000,
1516
9944

pallets/fruniques/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{mock::*, Error};
22

3-
use frame_support::{assert_err, assert_noop, assert_ok};
3+
use frame_support::{assert_noop, assert_ok};
44
use sp_runtime::Permill;
55

66
pub struct ExtBuilder;

pallets/gated-marketplace/src/functions.rs

Lines changed: 109 additions & 177 deletions
Large diffs are not rendered by default.

pallets/gated-marketplace/src/lib.rs

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,31 @@ mod types;
1515

1616
#[frame_support::pallet]
1717
pub mod pallet {
18-
use frame_support::{pallet_prelude::{*, OptionQuery}, transactional};
18+
use frame_support::pallet_prelude::*;
19+
use frame_support::transactional;
1920
use frame_system::pallet_prelude::*;
20-
use frame_support::traits::Currency;
21-
//use sp_runtime::sp_std::vec::Vec;
21+
use sp_runtime::traits::Scale;
22+
use frame_support::traits::{Currency, Time};
23+
2224
use crate::types::*;
23-
//use frame_support::traits::tokens::Balance;
2425
use pallet_rbac::types::RoleBasedAccessControl;
2526

27+
pub type BalanceOf<T> = <<T as pallet_uniques::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
28+
2629
#[pallet::config]
27-
pub trait Config: frame_system::Config + pallet_fruniques::Config + pallet_uniques::Config + pallet_timestamp::Config{
30+
pub trait Config: frame_system::Config + pallet_fruniques::Config{
31+
2832
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
29-
type LocalCurrency: Currency<Self::AccountId>;
30-
//type Balance: Balance + MaybeSerializeDeserialize + Debug + MaxEncodedLen;
33+
34+
type Moment: Parameter
35+
+ Default
36+
+ Scale<Self::BlockNumber, Output = Self::Moment>
37+
+ Copy
38+
+ MaxEncodedLen
39+
+ scale_info::StaticTypeInfo
40+
+ Into<u64>;
41+
42+
type Timestamp: Time<Moment = Self::Moment>;
3143

3244
type RemoveOrigin: EnsureOrigin<Self::Origin>;
3345
#[pallet::constant]
@@ -88,7 +100,7 @@ pub mod pallet {
88100
_,
89101
Blake2_128Concat,
90102
T::AccountId, // K1: account_id
91-
Blake2_128Concat,
103+
Identity,
92104
[u8;32], // k2: marketplace_id
93105
[u8;32], //application_id
94106
OptionQuery
@@ -113,7 +125,7 @@ pub mod pallet {
113125
_,
114126
Blake2_128Concat,
115127
T::AccountId, //custodians
116-
Blake2_128Concat,
128+
Identity,
117129
[u8;32], //marketplace_id
118130
BoundedVec<T::AccountId,T::MaxApplicationsPerCustodian>, //applicants
119131
ValueQuery
@@ -135,7 +147,7 @@ pub mod pallet {
135147
#[pallet::getter(fn offers_by_account)]
136148
pub(super) type OffersByAccount<T: Config> = StorageMap<
137149
_,
138-
Identity,
150+
Blake2_128Concat,
139151
T::AccountId, // account_id
140152
BoundedVec<[u8;32], T::MaxOffersPerMarket>, // offer_id's
141153
ValueQuery,
@@ -187,6 +199,8 @@ pub mod pallet {
187199
OfferWasAccepted([u8;32], T::AccountId),
188200
/// Offer was duplicated. [new_offer_id, new_marketplace_id]
189201
OfferDuplicated([u8;32], [u8;32]),
202+
/// Offer was removed. [offer_id], [marketplace_id]
203+
OfferRemoved([u8;32], [u8;32]),
190204
}
191205

192206
// Errors inform users that something went wrong.
@@ -321,7 +335,7 @@ pub mod pallet {
321335
origin: OriginFor<T>,
322336
marketplace_id: [u8;32],
323337
// Getting encoding errors from polkadotjs if an object vector have optional fields
324-
fields : BoundedVec<(BoundedVec<u8,ConstU32<100> >,BoundedVec<u8,ConstU32<100>> ), T::MaxFiles>,
338+
fields : Fields<T>,
325339
custodian_fields: Option<(T::AccountId, BoundedVec<BoundedVec<u8,ConstU32<100>>, T::MaxFiles> )>
326340
) -> DispatchResult {
327341
let who = ensure_signed(origin)?;
@@ -359,7 +373,7 @@ pub mod pallet {
359373
origin: OriginFor<T>,
360374
marketplace_id: [u8;32],
361375
// Getting encoding errors from polkadotjs if an object vector have optional fields
362-
fields : BoundedVec<(BoundedVec<u8,ConstU32<100> >,BoundedVec<u8,ConstU32<100>> ), T::MaxFiles>,
376+
fields : Fields<T>,
363377
custodian_fields: Option<(T::AccountId, BoundedVec<BoundedVec<u8,ConstU32<100>>, T::MaxFiles> )>
364378
) -> DispatchResult {
365379
let who = ensure_signed(origin)?;
@@ -520,58 +534,33 @@ pub mod pallet {
520534

521535
/// Accepts a sell order.
522536
///
523-
/// This extrinsic accepts a sell order in the selected marketplace.
537+
/// This extrisicn is called by the user who wants to buy the item.
538+
/// Aaccepts a sell order in the selected marketplace.
524539
///
525540
/// ### Parameters:
526541
/// - `origin`: The user who performs the action.
542+
/// - 'offer_id`: The id of the sell order to be accepted.
527543
/// - `marketplace_id`: The id of the marketplace where we want to accept the sell order.
528-
/// - `collection_id`: The id of the collection.
529-
/// - `item_id`: The id of the item inside the collection.
530544
///
531545
/// ### Considerations:
532546
/// - You don't need to be the owner of the item to accept the sell order.
533547
/// - Once the sell order is accepted, the ownership of the item is transferred to the buyer.
534548
/// - If you don't have the enough balance to accept the sell order, it will throw an error.
535549
#[transactional]
536550
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
537-
pub fn take_sell_offer(origin: OriginFor<T>, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
551+
pub fn take_sell_offer(origin: OriginFor<T>, offer_id: [u8;32]) -> DispatchResult {
538552
let who = ensure_signed(origin.clone())?;
539553

540-
Self::do_take_sell_offer(who, offer_id, marketplace_id, collection_id, item_id)
554+
Self::do_take_sell_offer(who, offer_id)
541555
}
542-
543-
/// Allows a user to duplicate a sell order.
544-
///
545-
/// This extrinsic allows a user to duplicate a sell order in any marketplace.
546-
///
547-
/// ### Parameters:
548-
/// - `origin`: The user who performs the action.
549-
/// - `marketplace_id`: The id of the marketplace where we want to duplicate the sell order.
550-
/// - `collection_id`: The id of the collection.
551-
/// - `item_id`: The id of the item inside the collection.
552-
///
553-
/// ### Considerations:
554-
/// - You can only duplicate a sell order if you are the owner of the item.
555-
/// - The expiration date of the sell order is the same as the original sell order.
556-
/// - You can update the price of the sell order.
557-
#[transactional]
558-
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
559-
pub fn duplicate_offer(origin: OriginFor<T>, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId, modified_price: BalanceOf<T>) -> DispatchResult {
560-
let who = ensure_signed(origin.clone())?;
561-
562-
Self::do_duplicate_offer(who, offer_id, marketplace_id, collection_id, item_id, modified_price)
563-
}
564-
565-
556+
566557
/// Delete an offer.
567558
///
568559
/// This extrinsic deletes an offer in the selected marketplace.
569560
///
570561
/// ### Parameters:
571562
/// - `origin`: The user who performs the action.
572-
/// - `marketplace_id`: The id of the marketplace where we want to delete the offer.
573-
/// - `collection_id`: The id of the collection.
574-
/// - `item_id`: The id of the item inside the collection.
563+
/// - `offer_id`: The id of the offer to be deleted.
575564
///
576565
/// ### Considerations:
577566
/// - You can delete sell orders or buy orders.
@@ -581,12 +570,12 @@ pub mod pallet {
581570
/// delete them one by one.
582571
#[transactional]
583572
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
584-
pub fn remove_offer(origin: OriginFor<T>, offer_id: [u8;32], marketplace_id: [u8;32], collection_id: T::CollectionId, item_id: T::ItemId,) -> DispatchResult {
573+
pub fn remove_offer(origin: OriginFor<T>, offer_id: [u8;32]) -> DispatchResult {
585574
//Currently, we can only remove one offer at a time.
586575
//TODO: Add support for removing multiple offers at a time.
587576
let who = ensure_signed(origin.clone())?;
588577

589-
Self::do_remove_offer(who, offer_id, marketplace_id, collection_id, item_id)
578+
Self::do_remove_offer(who, offer_id)
590579
}
591580

592581
/// Enlist a buy order.
@@ -615,13 +604,13 @@ pub mod pallet {
615604

616605
/// Accepts a buy order.
617606
///
618-
/// This extrinsic accepts a buy order in the selected marketplace.
607+
/// This extrinsic is called by the owner of the item who accepts the buy offer created by a marketparticipant.
608+
/// Accepts a buy order in the selected marketplace.
619609
///
620610
/// ### Parameters:
621611
/// - `origin`: The user who performs the action.
612+
/// - `offer_id`: The id of the buy order to be accepted.
622613
/// - `marketplace_id`: The id of the marketplace where we accept the buy order.
623-
/// - `collection_id`: The id of the collection.
624-
/// - `item_id`: The id of the item inside the collection.
625614
///
626615
/// ### Considerations:
627616
/// - You need to be the owner of the item to accept a buy order.
@@ -631,10 +620,10 @@ pub mod pallet {
631620
/// - Once the buy order is accepted, the ownership of the item is transferred to the buyer.
632621
#[transactional]
633622
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
634-
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 {
623+
pub fn take_buy_offer(origin: OriginFor<T>, offer_id: [u8;32]) -> DispatchResult {
635624
let who = ensure_signed(origin.clone())?;
636625

637-
Self::do_take_buy_offer(who, offer_id, marketplace_id, collection_id, item_id)
626+
Self::do_take_buy_offer(who, offer_id)
638627
}
639628

640629

pallets/gated-marketplace/src/mock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ parameter_types! {
7171
pub const MaxFiles: u32 = 10;
7272
pub const MaxApplicationsPerCustodian: u32 = 2;
7373
pub const MaxMarketsPerItem: u32 = 10;
74-
7574
pub const MaxOffersPerMarket: u32 = 100;
7675
}
7776

@@ -89,7 +88,9 @@ impl pallet_gated_marketplace::Config for Test {
8988
type MaxApplicationsPerCustodian = MaxApplicationsPerCustodian;
9089
type MaxOffersPerMarket = MaxOffersPerMarket;
9190
type MaxMarketsPerItem = MaxMarketsPerItem;
92-
type LocalCurrency = Balances;
91+
type Timestamp = Timestamp;
92+
type Moment = u64;
93+
//type LocalCurrency = Balances;
9394
type Rbac = RBAC;
9495
}
9596

pallets/gated-marketplace/src/tests.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn _create_file(name: &str, cid: &str, create_custodian_file: bool) -> Applicati
5353

5454
// due to encoding problems with polkadot-js, the custodians_cid generation will be done in another function
5555
fn create_application_fields( n_files: u32) ->
56-
BoundedVec<(BoundedVec<u8,ConstU32<100> >,BoundedVec<u8,ConstU32<100>> ), MaxFiles> {
56+
BoundedVec<(BoundedVec<u8,ConstU32<100> >,BoundedVec<u8,ConstU32<100>> ), MaxFiles> {
5757
let mut files = Vec::<(BoundedVec<u8,ConstU32<100> >,BoundedVec<u8,ConstU32<100>> )>::default();
5858
for i in 0..n_files{
5959
let file_name = format!("file{}",i.to_string());
@@ -1118,7 +1118,7 @@ fn take_sell_offer_works(){
11181118
assert_ok!(GatedMarketplace::enlist_sell_offer(Origin::signed(1), m_id, 0, 0, 1200));
11191119
let offer_id = GatedMarketplace::offers_by_item(0, 0).iter().next().unwrap().clone();
11201120

1121-
assert_ok!(GatedMarketplace::take_sell_offer(Origin::signed(2), offer_id, m_id, 0, 0));
1121+
assert_ok!(GatedMarketplace::take_sell_offer(Origin::signed(2), offer_id));
11221122
assert_eq!(GatedMarketplace::offers_by_item(0, 0).len(), 0);
11231123
assert_eq!(GatedMarketplace::offers_info(offer_id).unwrap().status, OfferStatus::Closed);
11241124

@@ -1141,7 +1141,7 @@ fn take_sell_offer_owner_cannnot_be_the_buyer_shouldnt_work() {
11411141
assert_ok!(GatedMarketplace::enlist_sell_offer(Origin::signed(1), m_id, 0, 0, 1200));
11421142
let offer_id = GatedMarketplace::offers_by_item(0, 0).iter().next().unwrap().clone();
11431143

1144-
assert_noop!(GatedMarketplace::take_sell_offer(Origin::signed(1), offer_id, m_id, 0, 0), Error::<Test>::CannotTakeOffer);
1144+
assert_noop!(GatedMarketplace::take_sell_offer(Origin::signed(1), offer_id), Error::<Test>::CannotTakeOffer);
11451145
});
11461146
}
11471147

@@ -1162,7 +1162,7 @@ fn take_sell_offer_id_does_not_exist_shouldnt_work(){
11621162
let offer_id = GatedMarketplace::offers_by_item(0, 0).iter().next().unwrap().clone();
11631163
let offer_id2 = offer_id.using_encoded(blake2_256);
11641164

1165-
assert_noop!(GatedMarketplace::take_sell_offer(Origin::signed(2), offer_id2, m_id, 0, 0), Error::<Test>::OfferNotFound);
1165+
assert_noop!(GatedMarketplace::take_sell_offer(Origin::signed(2), offer_id2), Error::<Test>::OfferNotFound);
11661166
});
11671167
}
11681168

@@ -1183,7 +1183,7 @@ fn take_sell_offer_buyer_does_not_have_enough_balance_shouldnt_work(){
11831183
assert_ok!(GatedMarketplace::enlist_sell_offer(Origin::signed(1), m_id, 0, 0, 1200));
11841184
let offer_id = GatedMarketplace::offers_by_item(0, 0).iter().next().unwrap().clone();
11851185

1186-
assert_noop!(GatedMarketplace::take_sell_offer(Origin::signed(2), offer_id, m_id, 0, 0), Error::<Test>::NotEnoughBalance);
1186+
assert_noop!(GatedMarketplace::take_sell_offer(Origin::signed(2), offer_id), Error::<Test>::NotEnoughBalance);
11871187
});
11881188
}
11891189

@@ -1208,7 +1208,7 @@ fn take_buy_offer_works(){
12081208
let offer_id2 = GatedMarketplace::offers_by_account(2).iter().next().unwrap().clone();
12091209
assert_eq!(GatedMarketplace::offers_info(offer_id2).unwrap().offer_type, OfferType::BuyOrder);
12101210

1211-
assert_ok!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id2, m_id, 0, 0));
1211+
assert_ok!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id2));
12121212
assert_eq!(GatedMarketplace::offers_by_item(0, 0).len(), 0);
12131213
assert_eq!(GatedMarketplace::offers_info(offer_id).unwrap().status, OfferStatus::Closed);
12141214
});
@@ -1236,7 +1236,7 @@ fn take_buy_offer_only_owner_can_accept_buy_offers_shouldnt_work(){
12361236
let offer_id2 = GatedMarketplace::offers_by_account(2).iter().next().unwrap().clone();
12371237
assert_eq!(GatedMarketplace::offers_info(offer_id2).unwrap().offer_type, OfferType::BuyOrder);
12381238

1239-
assert_noop!(GatedMarketplace::take_buy_offer(Origin::signed(2), offer_id2, m_id, 0, 0), Error::<Test>::NotOwner);
1239+
assert_noop!(GatedMarketplace::take_buy_offer(Origin::signed(2), offer_id2), Error::<Test>::NotOwner);
12401240
});
12411241
}
12421242

@@ -1262,7 +1262,7 @@ fn take_buy_offer_id_does_not_exist_shouldnt_work(){
12621262
assert_eq!(GatedMarketplace::offers_info(offer_id2).unwrap().offer_type, OfferType::BuyOrder);
12631263

12641264
let offer_id3 = offer_id2.using_encoded(blake2_256);
1265-
assert_noop!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id3, m_id, 0, 0), Error::<Test>::OfferNotFound);
1265+
assert_noop!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id3), Error::<Test>::OfferNotFound);
12661266

12671267
});
12681268
}
@@ -1289,7 +1289,7 @@ fn take_buy_offer_user_does_not_have_enough_balance_shouldnt_work(){
12891289
assert_eq!(GatedMarketplace::offers_info(offer_id2).unwrap().offer_type, OfferType::BuyOrder);
12901290

12911291
Balances::make_free_balance_be(&2, 0);
1292-
assert_noop!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id2, m_id, 0, 0), Error::<Test>::NotEnoughBalance);
1292+
assert_noop!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id2), Error::<Test>::NotEnoughBalance);
12931293
});
12941294
}
12951295

@@ -1310,7 +1310,7 @@ fn remove_sell_offer_works(){
13101310
let offer_id = GatedMarketplace::offers_by_account(1).iter().next().unwrap().clone();
13111311
assert!(GatedMarketplace::offers_info(offer_id).is_some());
13121312

1313-
assert_ok!(GatedMarketplace::remove_offer(Origin::signed(1), offer_id, m_id, 0, 0));
1313+
assert_ok!(GatedMarketplace::remove_offer(Origin::signed(1), offer_id));
13141314
assert_eq!(GatedMarketplace::offers_by_account(1).len(), 0);
13151315
assert!(GatedMarketplace::offers_info(offer_id).is_none());
13161316
});
@@ -1337,7 +1337,7 @@ fn remove_buy_offer_works(){
13371337
let offer_id2 = GatedMarketplace::offers_by_account(2).iter().next().unwrap().clone();
13381338
assert!(GatedMarketplace::offers_info(offer_id2).is_some());
13391339

1340-
assert_ok!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id2, m_id, 0, 0));
1340+
assert_ok!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id2));
13411341
assert_eq!(GatedMarketplace::offers_by_account(2).len(), 0);
13421342
assert!(GatedMarketplace::offers_info(offer_id2).is_none());
13431343
});
@@ -1361,7 +1361,7 @@ fn remove_offer_id_does_not_exist_sholdnt_work(){
13611361
assert!(GatedMarketplace::offers_info(offer_id).is_some());
13621362

13631363
let offer_id2 = offer_id.using_encoded(blake2_256);
1364-
assert_noop!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id2, m_id, 0, 0), Error::<Test>::OfferNotFound);
1364+
assert_noop!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id2), Error::<Test>::OfferNotFound);
13651365
});
13661366
}
13671367

@@ -1382,7 +1382,7 @@ fn remove_offer_creator_doesnt_match_sholdnt_work(){
13821382
let offer_id = GatedMarketplace::offers_by_account(1).iter().next().unwrap().clone();
13831383
assert!(GatedMarketplace::offers_info(offer_id).is_some());
13841384

1385-
assert_noop!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id, m_id, 0, 0), Error::<Test>::CannotRemoveOffer);
1385+
assert_noop!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id), Error::<Test>::CannotRemoveOffer);
13861386
});
13871387
}
13881388

@@ -1407,11 +1407,11 @@ fn remove_offer_status_is_closed_shouldnt_work(){
14071407
let offer_id2 = GatedMarketplace::offers_by_account(2).iter().next().unwrap().clone();
14081408
assert_eq!(GatedMarketplace::offers_info(offer_id2).unwrap().offer_type, OfferType::BuyOrder);
14091409

1410-
assert_ok!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id2, m_id, 0, 0));
1410+
assert_ok!(GatedMarketplace::take_buy_offer(Origin::signed(1), offer_id2));
14111411
assert_eq!(GatedMarketplace::offers_by_item(0, 0).len(), 0);
14121412
assert_eq!(GatedMarketplace::offers_info(offer_id).unwrap().status, OfferStatus::Closed);
14131413

1414-
assert_noop!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id2, m_id, 0, 0), Error::<Test>::CannotDeleteOffer);
1414+
assert_noop!(GatedMarketplace::remove_offer(Origin::signed(2), offer_id2), Error::<Test>::CannotDeleteOffer);
14151415
});
14161416

14171417
}

0 commit comments

Comments
 (0)