Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Feb 24, 2024
1 parent fde5009 commit 02b650c
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ license = "GPL-3.0-only"
[workspace]
members = [
"contracts/xc_regions",
"contracts/order_book",
"contracts/coretime_market",
"environment",
"primitives",
Expand Down
45 changes: 45 additions & 0 deletions contracts/order_book/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "order_book"
authors = ["RegionX <support@regionx.tech>"]
version = "0.1.0"
description = "Secondary Coretime marketpalce contract."
edition = "2021"

[dependencies]
ink = { version = "4.2.1", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
sp-arithmetic = { version = "23.0.0", default-features = false }

# OpenBrush dependency
openbrush = { git = "https://github.com/Brushfam/openbrush-contracts", branch = "develop", default-features = false, features=["psp34"] }

environment = { path = "../../environment", default-features = false, features = ["ink"] }
primitives = { path = "../../primitives", default-features = false }
block-number-extension = { path = "../../extension/block-number-extension", default-features = false, features = ["ink"]}
xc_regions = { path = "../xc_regions", default-features = false, features = ["ink-as-dependency"] }

[dev-dependencies]
ink_e2e = "4.2.1"

[lib]
path = "src/lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"primitives/std",
"scale/std",
"scale-info/std",
"sp-arithmetic/std",
"environment/ink-std",
"openbrush/std",
"xc_regions/std",
]
ink-as-dependency = []
e2e-tests = []

[profile.release]
overflow-checks = false
82 changes: 82 additions & 0 deletions contracts/order_book/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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/>.

//! Coretime market
//!
//! This is the contract implementation of a Coretime marketplace working on top of the `XcRegions`
//! contract.
//!
//! The contract employs a timeslice-based pricing model that determines the price of regions on
//! sale, based on the value of a single timeslice. This approach is useful as it allows us to
//! emulate the expiring nature of Coretime.
//!
//! ## Terminology:
//!
//! - Expired region: A region that can no longer be assigned to any particular task.
//! - Active region: A region which is currently able to perform a task. I.e. current timeslice >
//! region.begin
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#![feature(min_specialization)]

mod types;

#[openbrush::contract(env = environment::ExtendedEnvironment)]
pub mod coretime_market {
use crate::types::MarketError;
use block_number_extension::BlockNumberProviderExtension;
use environment::ExtendedEnvironment;
use ink::{
codegen::{EmitEvent, Env},
prelude::vec::Vec,
reflect::ContractEventBase,
EnvAccess,
};
use openbrush::{contracts::traits::psp34::Id, storage::Mapping, traits::Storage};
use primitives::Version;
use xc_regions::{traits::RegionMetadataRef, PSP34Ref};

#[ink(storage)]
#[derive(Storage)]
pub struct CoretimeMarket {
xc_regions_contract: AccountId,
}

impl CoretimeMarket {
#[ink(constructor)]
pub fn new(xc_regions_contract: AccountId) -> Self {
Self { xc_regions_contract }
}

#[ink(message)]
pub fn xc_regions_contract(&self) -> AccountId {
self.xc_regions_contract
}

#[ink(message, payable)]
pub fn list_region(&mut self, id: Id) -> Result<Version, MarketError> {
let caller = self.env().caller();
let market = self.env().account_id();

let Id::U128(region_id) = id else { return Err(MarketError::InvalidRegionId) };

// Ensure that the region exists and its metadata is set.
let metadata = RegionMetadataRef::get_metadata(&self.xc_regions_contract, id.clone())
.map_err(MarketError::XcRegionsMetadataError)?;

Ok(metadata.version)
}
}
}
43 changes: 43 additions & 0 deletions contracts/order_book/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use xc_regions::types::XcRegionsError;

#[derive(scale::Decode, scale::Encode, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
pub enum MarketError {
/// An arithmetic error occured.
ArithmeticError,
/// The provided identifier is not a valid region id.
InvalidRegionId,
/// The specified region is expired.
RegionExpired,
/// The caller made the call without sending the required deposit amount.
MissingDeposit,
/// Caller tried to perform an action on a region that is not listed.
RegionNotListed,
/// The caller tried to purchase a region without sending enough tokens.
InsufficientFunds,
/// The metadata of the region doesn't match with what the caller expected.
MetadataNotMatching,
/// Failed to transfer the tokens to the seller.
TransferFailed,
/// The caller tried to perform an operation that they have no permission for.
NotAllowed,
/// An error occured when calling the xc-regions contract through the metadata interface.
XcRegionsMetadataError(XcRegionsError),
}

impl core::fmt::Display for MarketError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
MarketError::ArithmeticError => write!(f, "ArithmeticError"),
MarketError::InvalidRegionId => write!(f, "InvalidRegionId"),
MarketError::RegionExpired => write!(f, "RegionExpired"),
MarketError::MissingDeposit => write!(f, "MissingDeposit"),
MarketError::RegionNotListed => write!(f, "RegionNotListed"),
MarketError::InsufficientFunds => write!(f, "InsufficientFunds"),
MarketError::MetadataNotMatching => write!(f, "MetadataNotMatching"),
MarketError::TransferFailed => write!(f, "TransferFailed"),
MarketError::NotAllowed => write!(f, "NotAllowed"),
MarketError::XcRegionsMetadataError(e) => write!(f, "{}", e),
}
}
}

0 comments on commit 02b650c

Please sign in to comment.