|
| 1 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 2 | + |
| 3 | +pub use pallet::*; |
| 4 | + |
| 5 | +#[cfg(feature = "runtime-benchmarks")] |
| 6 | +mod benchmarking; |
| 7 | + |
| 8 | +#[cfg(test)] |
| 9 | +pub(crate) mod mock; |
| 10 | +#[cfg(test)] |
| 11 | +mod tests; |
| 12 | + |
| 13 | +pub mod weights; |
| 14 | +pub use weights::*; |
| 15 | + |
| 16 | +use fc_traits_tracks::MutateTracks; |
| 17 | +use frame_support::{ |
| 18 | + pallet_prelude::*, |
| 19 | + traits::{nonfungibles_v2::Create, OriginTrait, RankedMembers}, |
| 20 | +}; |
| 21 | +use frame_system::pallet_prelude::{BlockNumberFor, OriginFor}; |
| 22 | +use pallet_communities::{ |
| 23 | + types::{ |
| 24 | + AccountIdLookupOf, AccountIdOf, CommunityIdOf, DecisionMethodFor, NativeBalanceOf, PalletsOriginOf, |
| 25 | + RuntimeOriginFor, |
| 26 | + }, |
| 27 | + Origin as CommunityOrigin, |
| 28 | +}; |
| 29 | +use pallet_nfts::CollectionConfig; |
| 30 | +use pallet_referenda::{TrackInfo, TracksInfo}; |
| 31 | + |
| 32 | +type TrackInfoOf<T> = TrackInfo<NativeBalanceOf<T>, BlockNumberFor<T>>; |
| 33 | + |
| 34 | +#[frame_support::pallet] |
| 35 | +pub mod pallet { |
| 36 | + use sp_runtime::str_array; |
| 37 | + |
| 38 | + use super::*; |
| 39 | + |
| 40 | + type CommunityName = BoundedVec<u8, ConstU32<25>>; |
| 41 | + |
| 42 | + /// Configure the pallet by specifying the parameters and types on which it |
| 43 | + /// depends. |
| 44 | + #[pallet::config] |
| 45 | + pub trait Config: frame_system::Config + pallet_communities::Config { |
| 46 | + /// Because this pallet emits events, it depends on the runtime's |
| 47 | + /// definition of an event. |
| 48 | + type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; |
| 49 | + |
| 50 | + type CreateCollection: Create< |
| 51 | + AccountIdOf<Self>, |
| 52 | + CollectionConfig<NativeBalanceOf<Self>, BlockNumberFor<Self>, CommunityIdOf<Self>>, |
| 53 | + CollectionId = CommunityIdOf<Self>, |
| 54 | + >; |
| 55 | + |
| 56 | + type Tracks: TracksInfo<NativeBalanceOf<Self>, BlockNumberFor<Self>> |
| 57 | + + MutateTracks< |
| 58 | + NativeBalanceOf<Self>, |
| 59 | + BlockNumberFor<Self>, |
| 60 | + Id = CommunityIdOf<Self>, |
| 61 | + RuntimeOrigin = PalletsOriginOf<Self>, |
| 62 | + >; |
| 63 | + |
| 64 | + type RankedCollective: RankedMembers<AccountId = AccountIdOf<Self>>; |
| 65 | + |
| 66 | + /// Type representing the weight of this pallet |
| 67 | + type WeightInfo: WeightInfo; |
| 68 | + |
| 69 | + // #[cfg(feature = "runtime-benchmarks")] |
| 70 | + // type BenchmarkHelper: BenchmarkHelper<Self>; |
| 71 | + } |
| 72 | + |
| 73 | + #[pallet::pallet] |
| 74 | + pub struct Pallet<T>(_); |
| 75 | + |
| 76 | + // Pallets use events to inform users when important changes are made. |
| 77 | + // https://docs.substrate.io/main-docs/build/events-errors/ |
| 78 | + #[pallet::event] |
| 79 | + #[pallet::generate_deposit(pub(super) fn deposit_event)] |
| 80 | + pub enum Event<T: Config> { |
| 81 | + /// The community with [`CommmunityId`](pallet_communities::CommunityId) |
| 82 | + /// has been created. |
| 83 | + CommunityRegistered { id: T::CommunityId }, |
| 84 | + } |
| 85 | + |
| 86 | + // Errors inform users that something worked or went wrong. |
| 87 | + #[pallet::error] |
| 88 | + pub enum Error<T> { |
| 89 | + /// Community name didn't contain valid utf8 characters |
| 90 | + InvalidCommunityName, |
| 91 | + /// It was not possible to register the community |
| 92 | + CannotRegister, |
| 93 | + } |
| 94 | + |
| 95 | + // Dispatchable functions allows users to interact with the pallet and invoke |
| 96 | + // state changes. These functions materialize as "extrinsics", which are often |
| 97 | + // compared to transactions. Dispatchable functions must be annotated with a |
| 98 | + // weight and must return a DispatchResult. |
| 99 | + #[pallet::call(weight(<T as Config>::WeightInfo))] |
| 100 | + impl<T: Config> Pallet<T> { |
| 101 | + #[pallet::call_index(0)] |
| 102 | + pub fn register( |
| 103 | + origin: OriginFor<T>, |
| 104 | + community_id: CommunityIdOf<T>, |
| 105 | + name: CommunityName, |
| 106 | + maybe_admin_origin: Option<PalletsOriginOf<T>>, |
| 107 | + maybe_decision_method: Option<DecisionMethodFor<T>>, |
| 108 | + _maybe_first_member: Option<AccountIdLookupOf<T>>, |
| 109 | + ) -> DispatchResult { |
| 110 | + let maybe_deposit = T::CreateOrigin::ensure_origin(origin)?; |
| 111 | + |
| 112 | + let community_name = core::str::from_utf8(&name).map_err(|_| Error::<T>::InvalidCommunityName)?; |
| 113 | + let community_origin: RuntimeOriginFor<T> = CommunityOrigin::<T>::new(community_id).into(); |
| 114 | + let admin_origin = maybe_admin_origin.unwrap_or(community_origin.clone().into_caller()); |
| 115 | + // Register first to check if community exists |
| 116 | + pallet_communities::Pallet::<T>::register(&admin_origin, &community_id, maybe_deposit)?; |
| 117 | + |
| 118 | + if let Some(decision_method) = maybe_decision_method { |
| 119 | + pallet_communities::Pallet::<T>::set_decision_method( |
| 120 | + admin_origin.clone().into(), |
| 121 | + community_id, |
| 122 | + decision_method, |
| 123 | + )?; |
| 124 | + } |
| 125 | + |
| 126 | + let community_account = pallet_communities::Pallet::<T>::community_account(&community_id); |
| 127 | + |
| 128 | + // Create memberships collection for community |
| 129 | + T::CreateCollection::create_collection_with_id( |
| 130 | + community_id, |
| 131 | + &community_account, |
| 132 | + &community_account, |
| 133 | + &CollectionConfig { |
| 134 | + settings: Default::default(), |
| 135 | + max_supply: None, |
| 136 | + mint_settings: Default::default(), |
| 137 | + }, |
| 138 | + )?; |
| 139 | + |
| 140 | + // Create governance track for community |
| 141 | + T::Tracks::insert( |
| 142 | + community_id, |
| 143 | + Self::default_tack(community_name), |
| 144 | + community_origin.into_caller(), |
| 145 | + )?; |
| 146 | + // Induct community at Kreivo Governance with rank 1 |
| 147 | + T::RankedCollective::induct(&community_account)?; |
| 148 | + |
| 149 | + Self::deposit_event(Event::<T>::CommunityRegistered { id: community_id }); |
| 150 | + Ok(()) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + impl<T: Config> Pallet<T> { |
| 155 | + fn default_tack(name: &str) -> TrackInfoOf<T> { |
| 156 | + use sp_runtime::Perbill; |
| 157 | + TrackInfo { |
| 158 | + name: str_array(name), |
| 159 | + max_deciding: 1, |
| 160 | + decision_deposit: 0u8.into(), |
| 161 | + prepare_period: 1u8.into(), |
| 162 | + decision_period: u8::MAX.into(), |
| 163 | + confirm_period: 1u8.into(), |
| 164 | + min_enactment_period: 1u8.into(), |
| 165 | + min_approval: pallet_referenda::Curve::LinearDecreasing { |
| 166 | + length: Perbill::from_percent(100), |
| 167 | + floor: Perbill::from_percent(50), |
| 168 | + ceil: Perbill::from_percent(100), |
| 169 | + }, |
| 170 | + min_support: pallet_referenda::Curve::LinearDecreasing { |
| 171 | + length: Perbill::from_percent(100), |
| 172 | + floor: Perbill::from_percent(0), |
| 173 | + ceil: Perbill::from_percent(50), |
| 174 | + }, |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments