Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coretime market pallet #136

Merged
merged 46 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
3254008
Coretime market pallet
Szegoo May 19, 2024
93554ed
progress
Szegoo May 19, 2024
1a9a1a2
lockable trait
Szegoo May 19, 2024
ecade40
region locking implementation
Szegoo May 19, 2024
32fb94d
..
Szegoo May 19, 2024
07ad9d7
listing
Szegoo May 20, 2024
8e19c88
check owner
Szegoo May 20, 2024
997a023
region primitives
Szegoo May 20, 2024
1611d9d
merge
Szegoo May 20, 2024
673c386
implement inspect
Szegoo May 20, 2024
c2727cb
unlist
Szegoo May 20, 2024
7df3faa
purchase
Szegoo May 20, 2024
465bbfb
init tests
Szegoo May 20, 2024
891f5c9
region price test
Szegoo May 20, 2024
95d7e61
docs
Szegoo May 21, 2024
90c71b0
feat: update_region_price
cuteolaf May 23, 2024
3347bb6
WIP: tests
cuteolaf May 23, 2024
2b82918
update `set_record`
cuteolaf May 23, 2024
5175de2
test for list_region
cuteolaf May 23, 2024
7e4f557
cargo fmt
cuteolaf May 23, 2024
44aefb1
add test for unknown region
cuteolaf May 23, 2024
d5f648c
test unlist_region
cuteolaf May 23, 2024
1e137ef
more checks for update_price
cuteolaf May 23, 2024
2f22115
test update_region_price
cuteolaf May 23, 2024
fada48f
cargo clippy
cuteolaf May 23, 2024
96d1014
update purchase_region
cuteolaf May 23, 2024
2d453aa
Update pallets/market/src/lib.rs
cuteolaf May 23, 2024
8932342
Update pallets/market/src/lib.rs
cuteolaf May 23, 2024
0f381a7
update event
cuteolaf May 23, 2024
6bd932e
test purchase_region
cuteolaf May 24, 2024
9e64e69
remove unused import
cuteolaf May 24, 2024
43a12d3
use token error
cuteolaf May 24, 2024
fea3128
benchmark setup
Szegoo May 25, 2024
866f64e
bench unlist
Szegoo May 25, 2024
97f4d23
bench update price
Szegoo May 25, 2024
1b329f7
bench purchase_region
Szegoo May 25, 2024
6c396ab
add market pallet to runtime
Szegoo May 25, 2024
ed1392c
fix build
Szegoo May 26, 2024
5abb7f3
fix benchmark build
Szegoo May 26, 2024
c185887
merge
Szegoo May 26, 2024
6e1eda5
fmt
Szegoo May 26, 2024
1d6d125
generate weights
Szegoo May 26, 2024
46f05e8
use weights
Szegoo May 26, 2024
6a20c6c
improve tests
Szegoo May 26, 2024
723ea02
Update pallets/market/src/benchmarking.rs
Szegoo May 26, 2024
9e4a5a8
Update pallets/market/src/lib.rs
Szegoo May 26, 2024
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
4 changes: 4 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ spaces_around_ranges = false
trailing_comma = "Vertical"
trailing_semicolon = false
use_field_init_shorthand = true

# Format comments
comment_width = 100
wrap_comments = true
49 changes: 49 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"node",
"pallets/*",
"primitives/*",
"runtime/*",
]

Expand All @@ -11,7 +12,7 @@ name = "regionx"
version = "0.1.0"
authors = ["Sergej Sakac", "Oliver Lim"]
description = "RegionX Coretime market Kusama runtime"
license = "Apache-2.0"
license = "GPLv3"
homepage = "https://regionx.tech/"
repository = "https://github.com/RegionX-Labs/RegionX-Node"
edition = "2021"
Expand Down Expand Up @@ -160,6 +161,9 @@ ismp-parachain-runtime-api = { version = "1.6.2", default-features = false }
ismp-testsuite = { git="https://github.com/polytope-labs/hyperbridge.git", branch="main", default-features = false }

# Local
region-primitives = { path = "./primitives/region", default-features = false }
nonfungible-primitives= { path = "./primitives/nonfungible", default-features = false }
regionx-primitives = { path = "./runtime/primitives", default-features = false }
regionx-runtime = { path = "./runtime/regionx", default-features = false }
pallet-regions = { path = "./pallets/regions", default-features = false }
pallet-market = { path = "./pallets/market", default-features = false }
59 changes: 59 additions & 0 deletions pallets/market/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[package]
name = "pallet-market"
authors = ["Anonymous"]
description = "Pallet containing the Coretime marketplace logic"
version = "0.1.0"
license = "GPLv3"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
edition = "2021"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
log = { workspace = true }
codec = { workspace = true, default-features = false, features = ["derive"] }
scale-info = { workspace = true, default-features = false, features = ["derive"] }

# Substrate
frame-benchmarking = { workspace = true, default-features = false, optional = true }
frame-support = { workspace = true, default-features = false }
frame-system = { workspace = true, default-features = false }
sp-io = { workspace = true, default-features = false }
sp-core = { workspace = true, default-features = false }
sp-runtime = { workspace = true, default-features = false }
pallet-broker = { workspace = true, default-features = false }

# Local
nonfungible-primitives = { workspace = true, default-features = false }
region-primitives = { workspace = true, default-features = false }

[dev-dependencies]
serde = { workspace = true }
pallet-regions = { workspace = true, default-features = false }
pallet-balances = { workspace = true, default-features = false }
ismp = { workspace = true, default-features = false }
ismp-testsuite = { workspace = true }

[features]
default = ["std"]
runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
std = [
"log/std",
"codec/std",
"nonfungible-primitives/std",
"region-primitives/std",
"scale-info/std",
"sp-io/std",
"sp-core/std",
"sp-runtime/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"pallet-broker/std",
"pallet-balances/std",
"pallet-regions/std",
"ismp/std",
]
try-runtime = [ "frame-support/try-runtime" ]
141 changes: 141 additions & 0 deletions pallets/market/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// 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/>.

//! Benchmarks for pallet-market

#![cfg(feature = "runtime-benchmarks")]

use super::*;

use frame_benchmarking::v2::*;
use frame_support::traits::fungible::Mutate;
use frame_system::RawOrigin;
use pallet_broker::{CoreMask, RegionId, RegionRecord};

const SEED: u32 = 0;

fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn list_region() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: caller.clone(), paid: None };
T::BenchmarkHelper::create_region(region_id, record, caller.clone())?;

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

assert_last_event::<T>(
Event::Listed {
region_id,
timeslice_price,
seller: caller.clone(),
sale_recipient: caller,
}
.into(),
);

Ok(())
}

#[benchmark]
fn unlist_region() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: caller.clone(), paid: None };
T::BenchmarkHelper::create_region(region_id, record, caller.clone())?;

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

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id);

assert_last_event::<T>(Event::Unlisted { region_id }.into());

Ok(())
}

#[benchmark]
fn update_region_price() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: caller.clone(), paid: None };
T::BenchmarkHelper::create_region(region_id, record, caller.clone())?;

crate::Pallet::<T>::list_region(
RawOrigin::Signed(caller.clone()).into(),
region_id,
1_000u32.into(),
None,
)?;

let new_timeslice_price = 2_000u32.into();
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id, new_timeslice_price);

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

Ok(())
}

#[benchmark]
fn purchase_region() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let alice: T::AccountId = account("alice", 0, SEED);

let region_id = RegionId { begin: 0, core: 0, mask: CoreMask::complete() };
let record: RegionRecordOf<T> = RegionRecord { end: 8, owner: alice.clone(), paid: None };

T::Currency::set_balance(&alice.clone(), u32::MAX.into());
T::BenchmarkHelper::create_region(region_id, record, alice.clone())?;

crate::Pallet::<T>::list_region(
RawOrigin::Signed(alice).into(),
region_id,
1_000u32.into(),
None,
)?;

T::Currency::set_balance(&caller.clone(), u32::MAX.into());
let max_price = 8000u32.into();
#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), region_id, max_price);

assert_last_event::<T>(
Event::Purchased { region_id, total_price: max_price, buyer: caller }.into(),
);

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading
Loading