Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pallets/market/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ std = [
"polkadot-sdk/std",
]
try-runtime = [ "polkadot-sdk/try-runtime" ]
dynamic-pricing = []
21 changes: 13 additions & 8 deletions pallets/market/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ mod benchmarks {
RegionRecord { end: 8, owner: Some(caller.clone()), paid: None };
T::Regions::create_region(region_id, record, caller.clone())?;

let timeslice_price: BalanceOf<T> = 1_000u32.into();
let price_data = 1_000u32;
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id, timeslice_price, None);
_(RawOrigin::Signed(caller.clone()), region_id, price_data.into(), None);

assert_last_event::<T>(
Event::Listed {
region_id,
timeslice_price,
price_data: price_data.into(),
seller: caller.clone(),
sale_recipient: caller,
}
Expand All @@ -69,11 +69,11 @@ mod benchmarks {
RegionRecord { end: 8, owner: Some(caller.clone()), paid: None };
T::Regions::create_region(region_id, record, caller.clone())?;

let timeslice_price: BalanceOf<T> = 1_000u32.into();
let price_data: <T::MarketImpl as crate::MarketT<T>>::PriceData = 1_000u32.into();
crate::Pallet::<T>::list_region(
RawOrigin::Signed(caller.clone()).into(),
region_id,
timeslice_price,
price_data,
None,
)?;

Expand Down Expand Up @@ -101,11 +101,13 @@ mod benchmarks {
None,
)?;

let new_timeslice_price = 2_000u32.into();
let new_price_data: <T::MarketImpl as crate::MarketT<T>>::PriceData = 2_000u32.into();
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id, new_timeslice_price);
_(RawOrigin::Signed(caller.clone()), region_id, new_price_data.clone());

assert_last_event::<T>(Event::PriceUpdated { region_id, new_timeslice_price }.into());
assert_last_event::<T>(
Event::PriceUpdated { region_id, price_data: new_price_data }.into(),
);

Ok(())
}
Expand All @@ -129,7 +131,10 @@ mod benchmarks {
)?;

<T as crate::Config>::Currency::set_balance(&caller.clone(), u32::MAX.into());
#[cfg(feature = "dynamic-pricing")]
let max_price = 8000u32.into();
#[cfg(not(feature = "dynamic-pricing"))]
let max_price = 1000u32.into();

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id, max_price);
Expand Down
142 changes: 142 additions & 0 deletions pallets/market/src/dynamic_pricing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// This file is part of RegionX.
//
// RegionX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RegionX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use pallet_broker::Timeslice;
use polkadot_sdk::frame_support::traits::{fungible::Mutate, nonfungible::Transfer};
use sp_runtime::traits::{SaturatedConversion, Saturating};

pub struct DynamicPricing<T: Config>(PhantomData<T>);

impl<T: Config> MarketT<T> for DynamicPricing<T> {
type PriceData = BalanceOf<T>;

fn list_region(
who: T::AccountId,
region_id: RegionId,
timeslice_price: Self::PriceData,
sale_recipient: Option<T::AccountId>,
) -> DispatchResult {
ensure!(Listings::<T>::get(region_id).is_none(), Error::<T>::AlreadyListed);

let region = T::Regions::region(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;
ensure!(!region.locked, Error::<T>::RegionLocked);
let record = region.record.get().ok_or(Error::<T>::RecordUnavailable)?;

// It doesn't make sense to list a region that expired.
let current_timeslice = Self::current_timeslice();
ensure!(record.end > current_timeslice, Error::<T>::RegionExpired);

T::Regions::lock(&region_id.into(), Some(who.clone()))?;

let sale_recipient = sale_recipient.unwrap_or(who.clone());
Listings::<T>::insert(
region_id,
Listing {
seller: who.clone(),
price_data: timeslice_price,
sale_recipient: sale_recipient.clone(),
},
);

Ok(())
}

fn unlist_region(who: T::AccountId, region_id: RegionId) -> DispatchResult {
let listing = Listings::<T>::get(region_id).ok_or(Error::<T>::NotListed)?;
let record = T::Regions::record(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;

// If the region expired anyone can remove it from the market.
let current_timeslice = Self::current_timeslice();
if current_timeslice <= record.end {
ensure!(who == listing.seller, Error::<T>::NotAllowed);
};

Listings::<T>::remove(region_id);
T::Regions::unlock(&region_id.into(), None)?;

Ok(())
}

fn update_region_price(
who: T::AccountId,
region_id: RegionId,
new_timeslice_price: Self::PriceData,
) -> DispatchResult {
let mut listing = Listings::<T>::get(region_id).ok_or(Error::<T>::NotListed)?;
let record = T::Regions::record(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;

// Only the seller can update the price
ensure!(who == listing.seller, Error::<T>::NotAllowed);

let current_timeslice = Self::current_timeslice();
ensure!(current_timeslice < record.end, Error::<T>::RegionExpired);

listing.price_data = new_timeslice_price;
Listings::<T>::insert(region_id, listing);

Ok(())
}

fn purchase_region(
who: T::AccountId,
region_id: RegionId,
max_price: BalanceOf<T>,
) -> Result<BalanceOf<T>, DispatchError> {
let listing = Listings::<T>::get(region_id).ok_or(Error::<T>::NotListed)?;
let record = T::Regions::record(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;

ensure!(who != listing.seller && who != listing.sale_recipient, Error::<T>::NotAllowed);

let price = Self::calculate_region_price(region_id, record, listing.price_data);
ensure!(price <= max_price, Error::<T>::PriceTooHigh);
T::Currency::transfer(&who, &listing.sale_recipient, price, Preservation::Preserve)?;

// Remove the region from sale:
Listings::<T>::remove(region_id);
T::Regions::unlock(&region_id.into(), None)?;

T::Regions::transfer(&region_id.into(), &who)?;

Ok(price)
}
}

impl<T: Config> DynamicPricing<T> {
pub(crate) fn calculate_region_price(
region_id: RegionId,
record: RegionRecordOf<T>,
timeslice_price: BalanceOf<T>,
) -> BalanceOf<T> {
let current_timeslice = Self::current_timeslice();
let duration = record.end.saturating_sub(region_id.begin);

if current_timeslice < region_id.begin {
// The region didn't start yet, so there is no value lost.
let price = timeslice_price.saturating_mul(duration.into());

return price;
}

let remaining_timeslices = record.end.saturating_sub(current_timeslice);
timeslice_price.saturating_mul(remaining_timeslices.into())
}

pub(crate) fn current_timeslice() -> Timeslice {
let latest_rc_block = T::RCBlockNumberProvider::current_block_number();
let timeslice_period = T::TimeslicePeriod::get();
(latest_rc_block / timeslice_period).saturated_into()
}
}
101 changes: 101 additions & 0 deletions pallets/market/src/fixed_pricing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// This file is part of RegionX.
//
// RegionX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RegionX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

use crate::*;
use polkadot_sdk::frame_support::traits::{fungible::Mutate, nonfungible::Transfer};

pub struct FixedPricing<T: Config>(PhantomData<T>);

impl<T: Config> MarketT<T> for FixedPricing<T> {
type PriceData = BalanceOf<T>;

fn list_region(
who: T::AccountId,
region_id: RegionId,
price: Self::PriceData,
sale_recipient: Option<T::AccountId>,
) -> DispatchResult {
ensure!(Listings::<T>::get(region_id).is_none(), Error::<T>::AlreadyListed);

let _region = T::Regions::region(&region_id.into()).ok_or(Error::<T>::UnknownRegion)?;

T::Regions::lock(&region_id.into(), Some(who.clone()))?;

let sale_recipient = sale_recipient.unwrap_or(who.clone());
Listings::<T>::insert(
region_id,
Listing {
seller: who.clone(),
price_data: price,
sale_recipient: sale_recipient.clone(),
},
);

Ok(())
}

fn unlist_region(who: T::AccountId, region_id: RegionId) -> DispatchResult {
let listing = Listings::<T>::get(region_id).ok_or(Error::<T>::NotListed)?;

ensure!(who == listing.seller, Error::<T>::NotAllowed);

Listings::<T>::remove(region_id);
T::Regions::unlock(&region_id.into(), None)?;

Ok(())
}

fn update_region_price(
who: T::AccountId,
region_id: RegionId,
new_price: Self::PriceData,
) -> DispatchResult {
let mut listing = Listings::<T>::get(region_id).ok_or(Error::<T>::NotListed)?;

// Only the seller can update the price
ensure!(who == listing.seller, Error::<T>::NotAllowed);

listing.price_data = new_price;
Listings::<T>::insert(region_id, listing);

Ok(())
}

fn purchase_region(
who: T::AccountId,
region_id: RegionId,
max_price: BalanceOf<T>,
) -> Result<BalanceOf<T>, DispatchError> {
let listing = Listings::<T>::get(region_id).ok_or(Error::<T>::NotListed)?;

ensure!(who != listing.seller && who != listing.sale_recipient, Error::<T>::NotAllowed);

ensure!(listing.price_data <= max_price, Error::<T>::PriceTooHigh);
T::Currency::transfer(
&who,
&listing.sale_recipient,
listing.price_data,
Preservation::Preserve,
)?;

// Remove the region from sale:
Listings::<T>::remove(region_id);
T::Regions::unlock(&region_id.into(), None)?;

T::Regions::transfer(&region_id.into(), &who)?;

Ok(listing.price_data)
}
}
Loading
Loading