Skip to content

Commit a28c73a

Browse files
committed
Market interface
1 parent 2da3987 commit a28c73a

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

substrate/frame/broker/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod benchmarking;
2525
mod core_mask;
2626
mod coretime_interface;
2727
mod dispatchable_impls;
28+
mod market;
2829
#[cfg(test)]
2930
mod mock;
3031
mod nonfungible_impl;
@@ -45,6 +46,7 @@ pub use weights::WeightInfo;
4546
pub use adapt_price::*;
4647
pub use core_mask::*;
4748
pub use coretime_interface::*;
49+
pub use market::*;
4850
pub use types::*;
4951

5052
extern crate alloc;
@@ -97,6 +99,9 @@ pub mod pallet {
9799
/// The algorithm to determine the next price on the basis of market performance.
98100
type PriceAdapter: AdaptPrice<BalanceOf<Self>>;
99101

102+
/// The bulk coretime market algorithm implementation.
103+
type Market: Market<BalanceOf<Self>, RelayBlockNumberOf<Self>, Self::AccountId>;
104+
100105
/// Reversible conversion from local balance to Relay-chain balance. This will typically be
101106
/// the `Identity`, but provided just in case the chains use different representations.
102107
type ConvertBalance: Convert<BalanceOf<Self>, RelayBalanceOf<Self>>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
use crate::PotentialRenewalId;
19+
20+
/// Trait representig generic market logic.
21+
///
22+
/// The assumptions for this generic market are:
23+
/// - Every order will either create a bid or will be resolved immediately.
24+
/// - There're two types of orders: bulk coretime purchase and bulk coretime renewal.
25+
/// - Coretime regions are fungible.
26+
pub trait Market<Balance, BlockNumber, AccountId> {
27+
type Error;
28+
/// Internal market state that must be preserved between the method calls. If the market logic
29+
/// allows creating bids they should be stored there as well as the bid structure depends on the
30+
/// market implementation.
31+
type State;
32+
/// Unique ID assigned to every bid.
33+
type BidId;
34+
35+
/// Place an order for bulk coretime purchase.
36+
///
37+
/// This method may or may not create a bid, according to the market rules.
38+
///
39+
/// - `since_timeslice_start` - amount of blocks passed since the current timeslice start
40+
/// - `amount` - maximum price which the buyer is willing to pay (or None if it's defined by the
41+
/// market itself)
42+
/// - `state` - market state, the caller is responsible for storing it
43+
fn place_order(
44+
since_timeslice_start: BlockNumber,
45+
who: AccountId,
46+
amount: Option<Balance>,
47+
state: &mut Self::State,
48+
) -> Result<PlaceOrderOutcome<Balance, Self::BidId>, Self::Error>;
49+
50+
/// Place an order for bulk coretime renewal.
51+
///
52+
/// This method may or may not create a bid, according to the market rules.
53+
///
54+
/// - `since_timeslice_start` - amount of blocks passed since the current timeslice start
55+
/// - `buying_price` - price which was paid for this region the last time it was sold
56+
/// - `state` - market state, the caller is responsible for storing it
57+
fn place_renewal_order(
58+
since_timeslice_start: BlockNumber,
59+
who: AccountId,
60+
renewal: PotentialRenewalId,
61+
buying_price: Balance,
62+
state: &mut Self::State,
63+
) -> Result<PlaceRenewalOrderOutcome<Balance, Self::BidId>, Self::Error>;
64+
65+
/// Close the bid given its `BidId`.
66+
///
67+
/// If the market logic allows creating the bids this method allows to close any bids (either
68+
/// forcefully if `maybe_check_owner` is `None` or checking the bid owner if it's `Some`).
69+
fn close_bid(
70+
id: Self::BidId,
71+
maybe_check_owner: Option<AccountId>,
72+
state: &mut Self::State,
73+
) -> Result<(), Self::Error>;
74+
75+
/// Logic that gets called in `on_initialize` hook.
76+
fn tick(
77+
since_timeslice_start: BlockNumber,
78+
state: &mut Self::State,
79+
) -> Result<Vec<TickAction<AccountId, Balance, Self::BidId>>, Self::Error>;
80+
}
81+
82+
enum PlaceOrderOutcome<Balance, BidId> {
83+
BidPlaced { id: BidId, bid_amount: Balance },
84+
Sold { price: Balance },
85+
}
86+
87+
enum PlaceRenewalOrderOutcome<Balance, BidId> {
88+
BidPlaced { id: BidId, bid_amount: Balance },
89+
Sold { price: Balance },
90+
}
91+
92+
enum TickAction<AccountId, Balance, BidId> {
93+
SellRegion { who: AccountId, refund: Balance },
94+
RenewRegion { who: AccountId, renewal_id: PotentialRenewalId, refund: Balance },
95+
CloseBid { id: BidId, amount: Balance },
96+
}

0 commit comments

Comments
 (0)