|
| 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