use alloc::vec::Vec;
use core::fmt::Debug;
use codec::{Codec, MaxEncodedLen};
use frame_support::{weights::WeightMeter, Parameter};
use scale_info::TypeInfo;
use sp_runtime::DispatchError;
use crate::{CoreIndex, PotentialRenewalId, RegionId, Timeslice};
pub trait Market<RelayBlockNumber, Balance, AccountId> {
type Error: Into<DispatchError>;
type BidId: Copy + Debug + Codec + MaxEncodedLen + TypeInfo + Eq;
type InitData: Parameter;
type Configuration: Parameter;
type CoreRangeProvider: CoreRangeProvider;
type TimesliceProvider: TimesliceProvider;
fn configure(configuration: Self::Configuration) -> Result<(), Self::Error>;
fn start_sales(
block_number: RelayBlockNumber,
init_data: Self::InitData,
) -> Result<SalesStarted<RelayBlockNumber>, Self::Error>;
fn place_order(
block_number: RelayBlockNumber,
who: &AccountId,
price_limit: Balance,
) -> Result<OrderResult<Balance, Self::BidId>, Self::Error>;
fn place_renewal_order(
block_number: RelayBlockNumber,
who: &AccountId,
renewal: PotentialRenewalId,
) -> Result<RenewalOrderResult<Balance, Self::BidId>, Self::Error>;
fn adjust_bid(
block_number: RelayBlockNumber,
id: Self::BidId,
who: &AccountId,
new_price: Option<Balance>,
) -> Result<AdjustBidResult<Balance>, Self::Error>;
fn tick(
now: RelayBlockNumber,
weight_meter: &mut WeightMeter,
) -> Vec<TickAction<AccountId, Balance, RelayBlockNumber>>;
}
pub trait CoreRangeProvider {
fn core_range() -> Option<SoldCoresRange>;
}
pub struct SoldCoresRange {
pub from: CoreIndex,
pub to: CoreIndex,
}
pub trait TimesliceProvider {
fn next_timeslice_to_commit() -> Option<Timeslice>;
fn latest_timeslice_ready_to_commit() -> Option<Timeslice>;
}
pub struct MarketSaleInfo<RelayBlockNumber> {
pub sale_start: RelayBlockNumber,
pub region_begin: Timeslice,
pub region_end: Timeslice,
pub cores_offered: CoreIndex,
pub first_core: CoreIndex,
pub cores_sold: CoreIndex,
}
pub struct SalesStarted<RelayBlockNumber> {
pub sale: MarketSaleInfo<RelayBlockNumber>,
}
pub enum OrderResult<Balance, BidId> {
BidPlaced {
id: BidId,
bid_price: Balance,
},
Sold {
price: Balance,
region_id: RegionId,
region_end: Timeslice,
},
}
pub enum RenewalOrderResult<Balance, BidId> {
BidPlaced {
id: BidId,
bid_price: Balance,
},
Renewed {
price: Balance,
region_id: RegionId,
effective_to: Timeslice,
},
}
pub enum AdjustBidResult<Balance> {
Lock {
amount: Balance,
},
Refund {
amount: Balance,
},
}
pub enum TickAction<AccountId, Balance, RelayBlockNumber> {
SellRegion {
owner: AccountId,
paid: Balance,
region_id: RegionId,
region_end: Timeslice,
},
RenewRegion {
owner: AccountId,
renewal_id: PotentialRenewalId,
},
Refund {
amount: Balance,
who: AccountId,
},
ProcessAutoRenewals {
after_timeslice: Timeslice,
next_renewal_at: Timeslice,
},
SaleRotated {
old_sale: MarketSaleInfo<RelayBlockNumber>,
new_sale: MarketSaleInfo<RelayBlockNumber>,
},
}