Skip to content

Commit

Permalink
feat(kreivo-runtime): configure hooks on CommunitiesMemberships to …
Browse files Browse the repository at this point in the history
…copy certain fields (such as gas and expirations) when a membership is assigned.
  • Loading branch information
pandres95 committed Jan 5, 2025
1 parent 6ea51d5 commit 25ae3e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 71 deletions.
80 changes: 11 additions & 69 deletions runtime/kreivo/src/communities/kreivo_memberships.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,23 @@
use super::*;

use core::marker::PhantomData;
use fc_traits_memberships::{Inspect, Manager, Rank};
use frame_support::traits::{
nonfungible_v2::Inspect as NonFunInspect,
nonfungibles_v2::{Inspect as NonFunsInspect, Mutate},
};
use sp_runtime::DispatchError;

pub struct KreivoMemberships<F, M>(PhantomData<(F, M)>);

impl<F, M, AccountId> Inspect<AccountId> for KreivoMemberships<F, M>
where
F: Inspect<AccountId>,
{
type Group = F::Group;
type Membership = F::Membership;

fn user_memberships(
who: &AccountId,
maybe_group: Option<Self::Group>,
) -> Box<dyn Iterator<Item = (Self::Group, Self::Membership)>> {
F::user_memberships(who, maybe_group)
}

fn check_membership(who: &AccountId, m: &Self::Membership) -> Option<Self::Group> {
F::check_membership(who, m)
}

fn members_total(group: &Self::Group) -> u32 {
F::members_total(group)
}
}
use fc_traits_memberships::OnMembershipAssigned;
use frame_support::traits::nonfungibles_v2::{Inspect as NonFunsInspect, Mutate};

const WELL_KNOWN_ATTR_KEYS: [&[u8]; 3] = [
&*b"membership_member_rank",
&*b"membership_gas",
&*b"membership_expiration",
];

impl<F, M, AccountId> Manager<AccountId, pallet_nfts::ItemConfig> for KreivoMemberships<F, M>
where
F: Inspect<AccountId>
+ Manager<AccountId, pallet_nfts::ItemConfig>
+ NonFunsInspect<AccountId>
+ Mutate<AccountId, pallet_nfts::ItemConfig, CollectionId = Self::Group, ItemId = Self::Membership>,
M: NonFunInspect<AccountId, ItemId = Self::Membership>,
{
fn assign(group: &Self::Group, m: &Self::Membership, who: &AccountId) -> Result<(), DispatchError> {
F::assign(group, m, who)?;
for key in WELL_KNOWN_ATTR_KEYS.into_iter() {
if let Some(value) = M::system_attribute(m, key) {
F::set_attribute(group, m, key, &value)?;
parameter_types! {
pub TemplatePropertiesOnAssignMembership: Box<dyn OnMembershipAssigned<AccountId, CommunityId, MembershipId>> =
Box::new(|_, group, m| {
for key in WELL_KNOWN_ATTR_KEYS.into_iter() {
if let Some(value) = CommunityMemberships::system_attribute(&group, Some(&m), key) {
<CommunityMemberships as Mutate<_, _>>::set_attribute(&group, &m, key, &value)?;
}
}
}
Ok(())
}

fn release(group: &Self::Group, m: &Self::Membership) -> Result<(), DispatchError> {
F::release(group, m)
}
}

impl<F, M, AccountId, R> Rank<AccountId, pallet_nfts::ItemConfig, R> for KreivoMemberships<F, M>
where
F: Inspect<AccountId> + Rank<AccountId, pallet_nfts::ItemConfig, R>,
R: Eq + Ord,
{
fn rank_of(group: &Self::Group, m: &Self::Membership) -> Option<R> {
F::rank_of(group, m)
}

fn set_rank(group: &Self::Group, m: &Self::Membership, rank: impl Into<R>) -> Result<(), DispatchError> {
F::set_rank(group, m, rank)
}

fn ranks_total(group: &Self::Group) -> u32 {
F::ranks_total(group)
}
Ok(())
});
}
8 changes: 6 additions & 2 deletions runtime/kreivo/src/communities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use pallet_communities::origin::{EnsureCommunity, EnsureSignedPays};
use sp_runtime::{morph_types, traits::AccountIdConversion};
use virto_common::{CommunityId, MembershipId};

use fc_traits_memberships::{NonFungiblesMemberships, WithHooks};

pub mod governance;
mod kreivo_memberships;
pub mod memberships;
Expand All @@ -19,7 +21,6 @@ use self::{
};
use pallet_custom_origins::CreateMemberships;

use crate::communities::kreivo_memberships::KreivoMemberships;
#[cfg(feature = "runtime-benchmarks")]
use {
frame_benchmarking::BenchmarkError,
Expand Down Expand Up @@ -67,7 +68,10 @@ impl pallet_communities::Config for Runtime {
type CreateOrigin = RootCreatesCommunitiesForFree;
type AdminOrigin = EitherOf<EnsureCommunity<Self>, EnsureCommunityAccount>;
type MemberMgmtOrigin = EitherOf<EnsureCommunity<Self>, EnsureCommunityAccount>;
type MemberMgmt = KreivoMemberships<CommunityMemberships, MembershipsManagementCollection>;
type MemberMgmt = WithHooks<
NonFungiblesMemberships<CommunityMemberships>,
kreivo_memberships::TemplatePropertiesOnAssignMembership,
>;
type MembershipId = MembershipId;

type Polls = CommunityReferenda;
Expand Down

0 comments on commit 25ae3e1

Please sign in to comment.