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

Add market pallet #1119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 25 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ members = [
"pallets/band",
"pallets/qa-tools",
"pallets/extended-assets",
"pallets/market",
"node/",
"utils/parse",
"utils/generate-bags",
Expand Down
1 change: 1 addition & 0 deletions common/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,7 @@ pub enum AssetType {
NFT,
Soulbound,
Regulated,
Product,
}

/// Presents information about an asset.
Expand Down
14 changes: 14 additions & 0 deletions common/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,12 @@ pub trait AssetManager<
amount: Balance,
) -> DispatchResult;

fn burn_unchecked(
asset_id: &Self::AssetId,
from: &T::AccountId,
amount: Balance,
) -> DispatchResult;

fn burn(
origin: OriginFor<T>,
asset_id: Self::AssetId,
Expand Down Expand Up @@ -1373,6 +1379,14 @@ impl<
unimplemented!()
}

fn burn_unchecked(
_asset_id: &Self::AssetId,
_from: &T::AccountId,
_amount: Balance,
) -> DispatchResult {
unimplemented!()
}

fn burn(
_origin: OriginFor<T>,
_asset_id: Self::AssetId,
Expand Down
8 changes: 8 additions & 0 deletions pallets/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,14 @@ impl<T: Config>
Self::mint_unchecked(asset_id, to, amount)
}

fn burn_unchecked(
asset_id: &Self::AssetId,
from: &T::AccountId,
amount: Balance,
) -> DispatchResult {
Self::burn_unchecked(asset_id, from, amount)
}

fn burn(
origin: OriginFor<T>,
asset_id: Self::AssetId,
Expand Down
60 changes: 60 additions & 0 deletions pallets/market/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[package]
edition = '2021'
authors = ['Polka Biome Ltd. <jihoon@tutanota.de>']
license = "BSD-4-Clause"
homepage = 'https://sora.org'
repository = 'https://github.com/sora-xor/sora2-network'
name = 'market'
version = '1.0.1'

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

[dependencies]
codec = { package = "parity-scale-codec", version = "3", default-features = false, features = [
"derive",
] }
scale-info = { version = "2", default-features = false, features = ["derive"] }
frame-support = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
frame-system = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
frame-benchmarking = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false, optional = true }
sp-core = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
sp-runtime = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
sp-std = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
common = { path = "../../common", default-features = false }
hex-literal = "0.4.1"
traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", package = "orml-traits", default-features = false }

[dev-dependencies]
sp-core = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
sp-io = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
sp-runtime = { version = "7.0.0", git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38", default-features = false }
pallet-balances = { git = "https://github.com/sora-xor/substrate.git", branch = "polkadot-v0.9.38" }
currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", package = "orml-currencies" }
tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", package = "orml-tokens", default-features = false }
hex-literal = { version = "0.4.1"}
common = { path = "../../common", features = ['test']}
assets = { path = "../assets" }
permissions = { path = "../permissions" }
technical = { path = "../technical" }

[features]
default = ['std']
std = [
"codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-core/std",
"sp-std/std",
"sp-runtime/std",
"traits/std",
"common/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
]

try-runtime = ["frame-support/try-runtime"]
193 changes: 193 additions & 0 deletions pallets/market/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// This file is part of the SORA network and Polkaswap app.

// Copyright (c) 2020, 2021, Polka Biome Ltd. All rights reserved.
// SPDX-License-Identifier: BSD-4-Clause

// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:

// Redistributions of source code must retain the above copyright notice, this list
// of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// All advertising materials mentioning features or use of this software must display
// the following acknowledgement: This product includes software developed by Polka Biome
// Ltd., SORA, and Polkaswap.
//
// Neither the name of the Polka Biome Ltd. nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY Polka Biome Ltd. AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Polka Biome Ltd. BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

//! Market module benchmarking.

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

use super::*;

use frame_benchmarking::benchmarks;
use frame_system::RawOrigin;

use common::{AssetManager, AssetName, AssetSymbol, DEFAULT_BALANCE_PRECISION};

fn asset_owner<T: Config>() -> T::AccountId {
frame_benchmarking::account("owner", 0, 0)
}

fn buyer<T: Config>(i: u32) -> T::AccountId {
frame_benchmarking::account("buyer", i, 0)
}

fn add_product<T: Config>(price_asset: AssetIdOf<T>) -> AssetIdOf<T> {
let owner = asset_owner::<T>();

Pallet::<T>::create_new_product(
owner,
common::AssetName(b"PRODUCT".to_vec()),
common::AssetSymbol(b"PRODUCT".to_vec()),
common::Description(b"PRODUCT".to_vec()),
common::ContentSource(b"PRODUCT".to_vec()),
Product {
price_asset,
price: 100,
extensions: vec![],
},
)
.expect("Failed to register product")
}

fn add_asset<T: Config>() -> AssetIdOf<T> {
let owner = asset_owner::<T>();
frame_system::Pallet::<T>::inc_providers(&owner);

T::AssetManager::register_from(
&owner,
AssetSymbol(b"TOKEN".to_vec()),
AssetName(b"TOKEN".to_vec()),
DEFAULT_BALANCE_PRECISION,
10000,
true,
AssetType::Regular,
None,
None,
)
.expect("Failed to register asset")
}

benchmarks! {
create_product {
let owner = asset_owner::<T>();
let owner_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(owner).into();
let price_asset = add_asset::<T>();
let required_product = add_product::<T>(price_asset);
let disallowed_product = add_product::<T>(price_asset);
}: {
Pallet::<T>::create_product(owner_origin,
common::AssetName(b"PRODUCT".to_vec()),
common::AssetSymbol(b"PRODUCT".to_vec()),
common::Description(b"PRODUCT".to_vec()),
common::ContentSource(b"PRODUCT".to_vec()),
Product {
price_asset,
price: 100,
extensions: vec![
Extension::Expirable(10u32.into()),
Extension::MaxAmount(10),
Extension::RequiredProduct(required_product, 1),
Extension::DisallowedProduct(disallowed_product)
],
}
).unwrap();
}

buy {
let owner = asset_owner::<T>();
let owner_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(owner.clone()).into();
let price_asset = add_asset::<T>();
let required_product = add_product::<T>(price_asset);
let disallowed_product = add_product::<T>(price_asset);
let product = Pallet::<T>::create_new_product(owner.clone(),
common::AssetName(b"PRODUCT".to_vec()),
common::AssetSymbol(b"PRODUCT".to_vec()),
common::Description(b"PRODUCT".to_vec()),
common::ContentSource(b"PRODUCT".to_vec()),
Product {
price_asset,
price: 100,
extensions: vec![
Extension::Expirable(10u32.into()),
Extension::MaxAmount(10),
Extension::RequiredProduct(required_product, 1),
Extension::DisallowedProduct(disallowed_product)
],
}
).unwrap();
Pallet::<T>::buy(owner_origin.clone(), required_product, 1).unwrap();
}: {
Pallet::<T>::buy(owner_origin, product, 1).unwrap();
}
verify {
assert_eq!(
T::AssetInfoProvider::total_balance(&product, &owner).unwrap(),
1
);
}

on_initialize {
let a in 1..50;
let owner = asset_owner::<T>();
let owner_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(owner.clone()).into();
let price_asset = add_asset::<T>();
let product = Pallet::<T>::create_new_product(owner.clone(),

Check failure

Code scanning / clippy

redundant clone Error

redundant clone
common::AssetName(b"PRODUCT".to_vec()),
common::AssetSymbol(b"PRODUCT".to_vec()),
common::Description(b"PRODUCT".to_vec()),
common::ContentSource(b"PRODUCT".to_vec()),
Product {
price_asset,
price: 100,
extensions: vec![
Extension::Expirable(10u32.into()),
],
}
).unwrap();
for i in 0..a {
let buyer = buyer::<T>(i);
let buyer_origin: <T as frame_system::Config>::RuntimeOrigin = RawOrigin::Signed(buyer.clone()).into();
T::AssetManager::mint_unchecked(&price_asset, &buyer, 1000).unwrap();
Pallet::<T>::buy(buyer_origin, product, 1).unwrap();
assert_eq!(
T::AssetInfoProvider::total_balance(&product, &buyer).unwrap(),
1
);
}
frame_system::Pallet::<T>::set_block_number(frame_system::Pallet::<T>::block_number() + 10u32.into());
}: {
Pallet::<T>::on_initialize(frame_system::Pallet::<T>::block_number());
}
verify {
assert_eq!(Expirations::<T>::iter().collect::<Vec<_>>(), vec![]);
for i in 0..a {
let buyer = buyer::<T>(i);
assert_eq!(
T::AssetInfoProvider::total_balance(&product, &buyer).unwrap(),
0
);
}
}

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