Skip to content

Commit c469b1d

Browse files
committed
I replaced the tight coupling for the timestamp pallet, with a loosely coupling instead.
1 parent fd994bc commit c469b1d

File tree

4 files changed

+31
-41
lines changed

4 files changed

+31
-41
lines changed

pallets/gated-marketplace/src/functions.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use frame_support::{pallet_prelude::*};
44
use frame_support::sp_io::hashing::blake2_256;
55
use sp_runtime::sp_std::vec::Vec;
66
use crate::types::*;
7+
use frame_support::traits::Time;
78
//use frame_support::traits::{Currency};
89
//use frame_support::traits::ExistenceRequirement::KeepAlive;
910

@@ -712,26 +713,13 @@ impl<T: Config> Pallet<T> {
712713
}
713714
Ok(())
714715
}
715-
716-
//TODO: merge the timestamp function to convert from moment to milliseconds.
717-
fn convert_moment_to_u64_in_milliseconds(date: T::Moment) -> Result<u64, DispatchError> {
718-
let date_as_u64_millis;
719-
if let Some(_date_as_u64) = TryInto::<u64>::try_into(date).ok() {
720-
date_as_u64_millis = _date_as_u64;
721-
} else {
722-
return Err(Error::<T>::TimestampError)?;
723-
}
724-
725-
Ok(date_as_u64_millis)
726-
}
716+
727717

728718
fn get_timestamp_in_milliseconds() -> Option<(u64, u64)> {
729-
let timestamp: <T as pallet_timestamp::Config>::Moment = <pallet_timestamp::Pallet<T>>::get();
730-
731-
let timestamp2 = Self::convert_moment_to_u64_in_milliseconds(timestamp).unwrap_or(0);
732-
let timestamp3 = timestamp2 + (7 * 24 * 60 * 60 * 1000);
719+
let timestamp:u64 = T::Timestamp::now().into();
720+
let timestamp2 = timestamp + (7 * 24 * 60 * 60 * 1000);
733721

734-
Some((timestamp2, timestamp3))
722+
Some((timestamp, timestamp2))
735723
}
736724

737725
fn _is_offer_status(offer_id: [u8;32], offer_status: OfferStatus,) -> bool{
@@ -743,14 +731,6 @@ impl<T: Config> Pallet<T> {
743731
}
744732
}
745733

746-
// fn _get_offer_price(offer_id: [u8;32],) -> Result<BalanceOf<T>, DispatchError> {
747-
// //we already know that the offer exists, so we don't need to check it here.
748-
// if let Some(offer) = <OffersInfo<T>>::get(offer_id) {
749-
// return Ok(offer.price);
750-
// } else {
751-
// return Err(Error::<T>::OfferNotFound)?;
752-
// }
753-
// }
754734

755735
fn does_exist_offer_id_for_this_item(collection_id: T::CollectionId, item_id: T::ItemId, offer_id: [u8;32]) -> DispatchResult {
756736
let offers = <OffersByItem<T>>::try_get(collection_id, item_id).map_err(|_| Error::<T>::OfferNotFound)?;
@@ -759,14 +739,6 @@ impl<T: Config> Pallet<T> {
759739
Ok(())
760740
}
761741

762-
// fn is_the_price_valid(price: BalanceOf<T>,) -> DispatchResult {
763-
// let minimun_amount: BalanceOf<T> = 1000u32.into();
764-
// if price > minimun_amount {
765-
// return Ok(());
766-
// } else {
767-
// return Err(Error::<T>::PriceMustBeGreaterThanZero)?;
768-
// }
769-
// }
770742

771743
fn _get_offer_creator(offer_id: [u8;32],) -> Result<T::AccountId, DispatchError> {
772744
//we already know that the offer exists, so we don't need to check it here.

pallets/gated-marketplace/src/lib.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,32 @@ mod types;
1818

1919
#[frame_support::pallet]
2020
pub mod pallet {
21-
use frame_support::{pallet_prelude::{*, OptionQuery}, transactional};
21+
use frame_support::pallet_prelude::*;
22+
use frame_support::transactional;
2223
use frame_system::pallet_prelude::*;
24+
use sp_runtime::traits::Scale;
25+
use frame_support::traits::Time;
2326
//use frame_support::traits::Currency;
24-
//use sp_runtime::sp_std::vec::Vec;
27+
28+
2529
use crate::types::*;
26-
//use frame_support::traits::tokens::Balance;
27-
//use std::fmt::Debug;
2830

2931
#[pallet::config]
30-
pub trait Config: frame_system::Config + pallet_fruniques::Config + pallet_uniques::Config + pallet_timestamp::Config{
32+
pub trait Config: frame_system::Config + pallet_fruniques::Config + pallet_uniques::Config{
3133
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
34+
3235
//type LocalCurrency: Currency<Self::AccountId>;
33-
//type Balance: Balance + MaybeSerializeDeserialize + Debug + MaxEncodedLen;
36+
37+
type Moment: Parameter
38+
+ Default
39+
+ Scale<Self::BlockNumber, Output = Self::Moment>
40+
+ Copy
41+
+ MaxEncodedLen
42+
+ scale_info::StaticTypeInfo
43+
+ Into<u64>;
44+
45+
type Timestamp: Time<Moment = Self::Moment>;
46+
3447

3548
type RemoveOrigin: EnsureOrigin<Self::Origin>;
3649
#[pallet::constant]

pallets/gated-marketplace/src/mock.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ parameter_types! {
7070
pub const MaxFiles: u32 = 10;
7171
pub const MaxApplicationsPerCustodian: u32 = 2;
7272
pub const MaxMarketsPerItem: u32 = 10;
73-
7473
pub const MaxOffersPerMarket: u32 = 100;
7574
}
7675

@@ -88,6 +87,8 @@ impl pallet_gated_marketplace::Config for Test {
8887
type MaxApplicationsPerCustodian = MaxApplicationsPerCustodian;
8988
type MaxOffersPerMarket = MaxOffersPerMarket;
9089
type MaxMarketsPerItem = MaxMarketsPerItem;
90+
type Timestamp = Timestamp;
91+
type Moment = u64;
9192
//type LocalCurrency = Balances;
9293
}
9394

runtime/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ pub type Index = u32;
6868
/// A hash of some data used by the chain.
6969
pub type Hash = sp_core::H256;
7070

71+
pub type Moment = u64;
72+
7173
/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
7274
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
7375
/// of data like extrinsics, allowing for them to continue syncing the network through upgrades
@@ -572,7 +574,9 @@ impl pallet_gated_marketplace::Config for Runtime {
572574
type MaxApplicationsPerCustodian = MaxApplicationsPerCustodian;
573575
type MaxMarketsPerItem = MaxMarketsPerItem;
574576
type MaxOffersPerMarket = MaxOffersPerMarket;
575-
//type LocalCurrency = Balances;
577+
type Timestamp = Timestamp;
578+
type Moment = Moment;
579+
576580
}
577581

578582
parameter_types! {

0 commit comments

Comments
 (0)