Skip to content

Draft: Feature/mapped assets in gm #424

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

Open
wants to merge 4 commits into
base: feature/afloat-pallet
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
49 changes: 31 additions & 18 deletions pallets/gated-marketplace/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use super::*;
use crate::types::*;
use frame_support::pallet_prelude::*;
use frame_support::sp_io::hashing::blake2_256;
use frame_support::traits::tokens::AssetId;
use pallet_rbac::types::*;
use scale_info::prelude::vec; // vec![] macro
use sp_runtime::sp_std::vec::Vec; // vec primitive
use frame_system::pallet_prelude::*;

use frame_support::traits::fungibles::Transfer;
use frame_support::traits::Currency;
use frame_support::traits::ExistenceRequirement::KeepAlive;
use frame_support::traits::Time;
use sp_runtime::Permill;
use sp_runtime::traits::StaticLookup;
Expand Down Expand Up @@ -239,15 +243,9 @@ impl<T: Config> Pallet<T> {
Ok(())
}


pub fn self_enroll(
account: T::AccountId,
marketplace_id: [u8; 32],
) -> DispatchResult {

//since users can self-enroll, the caller of this function must validate
pub fn self_enroll(account: T::AccountId, marketplace_id: [u8; 32]) -> DispatchResult {
//since users can self-enroll, the caller of this function must validate
//that the user is indeed the owner of the address by using ensure_signed


//ensure the account is not already in the marketplace
ensure!(
Expand All @@ -261,10 +259,13 @@ impl<T: Config> Pallet<T> {
// ensure the marketplace exist
ensure!(<Marketplaces<T>>::contains_key(marketplace_id), Error::<T>::MarketplaceNotFound);


Self::insert_in_auth_market_lists(account.clone(), MarketplaceRole::Participant, marketplace_id)?;
Self::insert_in_auth_market_lists(
account.clone(),
MarketplaceRole::Participant,
marketplace_id,
)?;
Self::deposit_event(Event::AuthorityAdded(account, MarketplaceRole::Participant));

Ok(())
}

Expand Down Expand Up @@ -375,6 +376,7 @@ impl<T: Config> Pallet<T> {
collection_id,
item_id,
creator: authority.clone(),
// might require a storage migration
price,
fee: price * Permill::deconstruct(marketplace.sell_fee).into() / 1_000_000u32.into(),
percentage: Permill::from_percent(percentage),
Expand Down Expand Up @@ -439,7 +441,7 @@ impl<T: Config> Pallet<T> {
&marketplace_id,
authority.clone(),
&collection_id,
&item_id
&item_id,
)?;

//Get asset id
Expand Down Expand Up @@ -469,6 +471,7 @@ impl<T: Config> Pallet<T> {
collection_id,
item_id,
creator: authority.clone(),
// TODO: evaluate if the change will require a storage migration
price,
fee: price * Permill::deconstruct(marketplace.buy_fee).into() / 1_000_000u32.into(),
percentage: Permill::from_percent(percentage),
Expand Down Expand Up @@ -537,7 +540,7 @@ impl<T: Config> Pallet<T> {
//ensure user has enough balance to create the offer
let total_amount_buyer = pallet_mapped_assets::Pallet::<T>::balance(asset_id.clone(), buyer.clone());
//ensure the buyer has enough balance to buy the item
ensure!(total_amount_buyer > offer_data.price, Error::<T>::NotEnoughBalance);
//ensure!(total_amount_buyer > offer_data.price, Error::<T>::NotEnoughBalance);

let marketplace =
<Marketplaces<T>>::get(offer_data.marketplace_id).ok_or(Error::<T>::OfferNotFound)?;
Expand Down Expand Up @@ -646,7 +649,7 @@ impl<T: Config> Pallet<T> {
//ensure user has enough balance to create the offer
let total_amount_buyer = pallet_mapped_assets::Pallet::<T>::balance(asset_id.clone(), offer_data.creator.clone());
//ensure the buy_offer_creator has enough balance to buy the item
ensure!(total_amount_buyer > offer_data.price, Error::<T>::NotEnoughBalance);
//ensure!(total_amount_buyer > offer_data.price, Error::<T>::NotEnoughBalance);

let marketplace =
<Marketplaces<T>>::get(offer_data.marketplace_id).ok_or(Error::<T>::OfferNotFound)?;
Expand Down Expand Up @@ -941,7 +944,10 @@ impl<T: Config> Pallet<T> {
// ensure the origin is authorized to block users
Self::is_authorized(authority.clone(), &marketplace_id, Permission::BlockUser)?;
// ensure the user is not already a participant of the marketplace
ensure!(!Self::has_any_role(user.clone(), &marketplace_id), Error::<T>::UserAlreadyParticipant);
ensure!(
!Self::has_any_role(user.clone(), &marketplace_id),
Error::<T>::UserAlreadyParticipant
);
// ensure the user is not already blocked
ensure!(
!Self::is_user_blocked(user.clone(), marketplace_id),
Expand All @@ -968,7 +974,10 @@ impl<T: Config> Pallet<T> {
// ensure the origin is authorized to block users
Self::is_authorized(authority.clone(), &marketplace_id, Permission::BlockUser)?;
// ensure the user is not already a participant of the marketplace
ensure!(!Self::has_any_role(user.clone(), &marketplace_id), Error::<T>::UserAlreadyParticipant);
ensure!(
!Self::has_any_role(user.clone(), &marketplace_id),
Error::<T>::UserAlreadyParticipant
);
// ensure the user is blocked
ensure!(Self::is_user_blocked(user.clone(), marketplace_id), Error::<T>::UserIsNotBlocked);

Expand Down Expand Up @@ -1008,7 +1017,11 @@ impl<T: Config> Pallet<T> {
/// Let us know if the selected account has at least one role in the marketplace.
fn has_any_role(account: T::AccountId, marketplace_id: &[u8; 32]) -> bool {
let pallet_id = Self::pallet_id();
<T as pallet::Config>::Rbac::does_user_have_any_role_in_scope(account, pallet_id, marketplace_id)
<T as pallet::Config>::Rbac::does_user_have_any_role_in_scope(
account,
pallet_id,
marketplace_id,
)
}

///Lets us know if the selected user is an admin.
Expand Down
17 changes: 12 additions & 5 deletions pallets/gated-marketplace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ pub mod types;
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_support::traits::{Currency, Time};
use frame_support::traits::{
tokens::fungibles::{Inspect, Transfer},
Currency, Time,
};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::Scale;
use sp_runtime::Permill;
Expand All @@ -23,10 +26,12 @@ pub mod pallet {

use crate::types::*;
use pallet_rbac::types::RoleBasedAccessControl;

pub type BalanceOf<T> = <<T as pallet_uniques::Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;
//pub type BalanceOf<T> = <<T as pallet_uniques::Config>::Currency as Currency<
// <T as frame_system::Config>::AccountId,
//>>::Balance;
// TODO: replace BalanceOf with the mapped assets one
pub type BalanceOf<T> =
<<T as Config>::MappedAssets as Inspect<<T as frame_system::Config>::AccountId>>::Balance;

#[pallet::config]
pub trait Config: frame_system::Config + pallet_fruniques::Config + pallet_mapped_assets::Config {
Expand Down Expand Up @@ -69,6 +74,8 @@ pub mod pallet {
type MaxBlockedUsersPerMarket: Get<u32>;

type Rbac: RoleBasedAccessControl<Self::AccountId>;

type MappedAssets: Transfer<Self::AccountId> + Inspect<Self::AccountId>;
}

#[pallet::pallet]
Expand Down
12 changes: 5 additions & 7 deletions parachain-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ frame-system-benchmarking = { git = "https://github.com/paritytech/substrate", d
frame-system-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false }
frame-try-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.38" }
# Local Pallets
pallet-template = { default-features = false, path = "../pallets/template" }
pallet-fruniques = { version = "0.1.0-dev", default-features = false, path = "../pallets/fruniques" }
pallet-bitcoin-vaults = { default-features = false, path = "../pallets/bitcoin-vaults" }
pallet-gated-marketplace = { default-features = false, path = "../pallets/gated-marketplace" }
pallet-rbac = { default-features = false, path = "../pallets/rbac" }
pallet-confidential-docs = { default-features = false, path = "../pallets/confidential-docs" }
pallet-fund-admin = { default-features = false, path = "../pallets/fund-admin" }
pallet-bitcoin-vaults = { path = "../pallets/bitcoin-vaults", default-features = false }
pallet-fruniques = { path = "../pallets/fruniques", default-features = false }
pallet-gated-marketplace = { path = "../pallets/gated-marketplace", default-features = false }
pallet-rbac = { path = "../pallets/rbac", default-features = false }
pallet-confidential-docs = { path = "../pallets/confidential-docs", default-features = false }
pallet-mapped-assets = { path = "../pallets/mapped-assets",default-features = false }
# Prebuilt Pallets
pallet-alliance = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false }
Expand Down
29 changes: 29 additions & 0 deletions parachain-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use sp_runtime::{
ApplyExtrinsicResult, MultiSignature,
};

use pallet_mapped_assets::DefaultCallback;
use sp_std::prelude::*;
#[cfg(feature = "std")]
use sp_version::NativeVersion;
Expand Down Expand Up @@ -819,6 +820,33 @@ impl pallet_confidential_docs::Config for Runtime {
type MaxMemberGroups = MaxMemberGroups;
}

parameter_types! {
pub const MappedMaxReserves: u32 = 200;
}

impl pallet_mapped_assets::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = u128;
type AssetId = u32;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSigned<AccountId>>;
type ForceOrigin = EnsureRoot<AccountId>;
type AssetDeposit = AssetDeposit;
type AssetAccountDeposit = ConstU128<DOLLARS>;
type MetadataDepositBase = MetadataDepositBase;
type MetadataDepositPerByte = MetadataDepositPerByte;
type ApprovalDeposit = ApprovalDeposit;
type StringLimit = StringLimit;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
type MaxReserves = MappedMaxReserves;
type ReserveIdentifier = u32;
type RemoveItemsLimit = RemoveItemsLimit;
type AssetIdParameter = u32;
type CallbackHandle = DefaultCallback;
}

impl pallet_remark::Config for Runtime {
type WeightInfo = pallet_remark::weights::SubstrateWeight<Self>;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -1042,6 +1070,7 @@ impl pallet_gated_marketplace::Config for Runtime {
type Timestamp = Timestamp;
type Moment = Moment;
type Rbac = RBAC;
type MappedAssets = MappedAssets;
}

impl pallet_mapped_assets::Config for Runtime {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ impl pallet_gated_marketplace::Config for Runtime {
type Timestamp = Timestamp;
type Moment = Moment;
type Rbac = RBAC;
type MappedAssets = MappedAssets;
}

parameter_types! {
Expand Down Expand Up @@ -748,7 +749,6 @@ impl pallet_mapped_assets::Config for Runtime {
type CallbackHandle = DefaultCallback;
}


parameter_types! {
pub const MaxScopesPerPallet: u32 = 1000;
pub const MaxRolesPerPallet: u32 = 50;
Expand Down