Skip to content

Commit

Permalink
change(pallet-communities-manager): use first_admin account instead o…
Browse files Browse the repository at this point in the history
…f allowing arbitrary origins as community's first admin (#400)
  • Loading branch information
pandres95 authored May 11, 2024
1 parent 529eeb1 commit d4a3a79
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 2 additions & 3 deletions pallets/communities-manager/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ mod benchmarks {
setup_account::<T>(&first_member)?;

let community_id: CommunityIdOf<T> = 1.into();
let admin_origin: RuntimeOriginFor<T> = frame_system::Origin::<T>::Signed(first_member.clone()).into();
let admin_origin_caller: PalletsOriginOf<T> = admin_origin.into_caller();
let first_admin = T::Lookup::unlookup(first_member.clone());

#[extrinsic_call]
_(
RawOrigin::Root,
community_id,
BoundedVec::truncate_from(b"Test Community".into()),
Some(admin_origin_caller.clone()),
first_admin,
None,
None,
);
Expand Down
18 changes: 12 additions & 6 deletions pallets/communities-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ use pallet_communities::{
use pallet_nfts::{CollectionConfig, ItemConfig};
use pallet_referenda::{TrackInfo, TracksInfo};
use parity_scale_codec::Decode;
use sp_runtime::{str_array, traits::Get};
use sp_runtime::{
str_array,
traits::{Get, StaticLookup},
};

type TrackInfoOf<T> = TrackInfo<NativeBalanceOf<T>, BlockNumberFor<T>>;

Expand Down Expand Up @@ -135,22 +138,24 @@ pub mod pallet {
origin: OriginFor<T>,
community_id: CommunityIdOf<T>,
name: CommunityName,
maybe_admin_origin: Option<PalletsOriginOf<T>>,
first_admin: pallet_communities::AccountIdLookupOf<T>,
maybe_decision_method: Option<DecisionMethodFor<T>>,
maybe_track_info: Option<TrackInfoOf<T>>,
// _maybe_first_member: Option<AccountIdLookupOf<T>>,
) -> DispatchResult {
let maybe_deposit = T::RegisterOrigin::ensure_origin(origin)?;

let community_name = core::str::from_utf8(&name).map_err(|_| Error::<T>::InvalidCommunityName)?;
let community_origin: RuntimeOriginFor<T> = CommunityOrigin::<T>::new(community_id).into();
let admin_origin = maybe_admin_origin.unwrap_or(community_origin.clone().into_caller());

let first_admin_account_id = T::Lookup::lookup(first_admin)?;
let admin_origin = frame_system::Origin::<T>::Signed(first_admin_account_id);

// Register first to check if community exists
pallet_communities::Pallet::<T>::register(&admin_origin, &community_id, maybe_deposit)?;
pallet_communities::Pallet::<T>::register(&admin_origin.clone().into(), &community_id, maybe_deposit)?;

if let Some(decision_method) = maybe_decision_method {
pallet_communities::Pallet::<T>::set_decision_method(
admin_origin.clone().into(),
admin_origin.into(),
community_id,
decision_method,
)?;
Expand All @@ -171,6 +176,7 @@ pub mod pallet {
)?;

// Create governance track for community
let community_origin: RuntimeOriginFor<T> = CommunityOrigin::<T>::new(community_id).into();
T::Tracks::insert(
community_id,
maybe_track_info.unwrap_or_else(|| Self::default_tack(community_name)),
Expand Down

0 comments on commit d4a3a79

Please sign in to comment.