mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-15 23:05:45 +00:00
9e403629d5
The Ambassador Program on Polkadot Collectives Parachain The Polkadot Ambassador Program has existed for a while; more information can be found [here](https://wiki.polkadot.network/docs/ambassadors). In this PR, the program is being brought on chain. ### On Chain Structure The on-chain program consists of nine ranks, divided into four categories ([full list](https://github.com/paritytech/cumulus/blob/c238fb26b75569a11abb57437fd14acd26e05f18/parachains/runtimes/collectives/collectives-polkadot/src/ambassador/mod.rs#L52)): - Ambassadors (1-2 tiers) - Senior Ambassadors (3-4 tiers) - Head Ambassadors (5-7 tiers) - Master Ambassadors (8-9 tiers) Each rank has a corresponding `Origin` (e.g., `HeadAmbassadorsTier5` - [full list](https://github.com/paritytech/cumulus/blob/c238fb26b75569a11abb57437fd14acd26e05f18/parachains/runtimes/collectives/collectives-polkadot/src/ambassador/origins.rs#L35)), which represents the collective voice of members of that rank and above. ### Referendum The `AmbassadorReferenda` instance of [referenda pallet](https://docs.rs/pallet-referenda/latest/pallet_referenda/) consists of [nine tracks](https://github.com/paritytech/cumulus/blob/c238fb26b75569a11abb57437fd14acd26e05f18/parachains/runtimes/collectives/collectives-polkadot/src/ambassador/tracks.rs#L51), each corresponding to an `Origin`. A referendum taken on `senior ambassador tier 4` track invites all members from rank 4 or above to vote and commands `SeniorAmbassadors` `Origin`. Every member gets one vote plus an additional vote for every excess rank. The referendum proposal can be submitted by any member of a senior rank or above. ### Membership Management Initial members will be brought on chain via migration, with subsequent member management handled through the `AmbassadorCollective` instance of [ranked collective pallet](https://docs.rs/pallet-ranked-collective/latest/pallet_ranked_collective/). Both `Root` and `FellowshipAdmin` `Origins`, commanded via public Polkadot referendum, can promote or demote members to and from any rank. Members themselves also have the power to promote or demote via its referendum, with a senior member vote by the rank two above the new / current rank - [full configuration](https://github.com/paritytech/cumulus/blob/9ab6aa47063d7e8b67ddc10d9c136037f99c03a3/parachains/runtimes/collectives/collectives-polkadot/src/ambassador/mod.rs#L67). ### Content Management The program's on-chain content is managed via the collectives content pallet, allowing for setting its charter and making announcements. The voice of head ambassadors have the authority to set the charter, while announcements can be made by any senior rank member or through a referendum among all members. ### Additional Functionality The `AmbassadorCore` instance of [core fellowship pallet](https://docs.rs/pallet-core-fellowship/latest/pallet_core_fellowship/) decorates the ranked collectives pallet with features like salary determination, activity/passivity registration, and the handling of promotion and demotion periods. While the usage of this pallet is optional in the first version, future updates will make it the exclusive method for induction/promotion. Periodic salaries in USDt, payable on Asset Hub, are introduced through the [salary pallet](https://docs.rs/pallet-salary/latest/pallet_salary/). This requires induction into the ambassador core pallet. Please for more information on the pallets' functionality refer to their documentations. ### Next Steps: - Migrate to seed the program members - Mint ambassador NFT badges on Asset Hub when promoting - Treasury pallet instance for the Ambassador Program --------- Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com>
207 lines
6.9 KiB
Rust
207 lines
6.9 KiB
Rust
// Copyright (C) 2023 Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Managed Collective Content Pallet
|
|
//!
|
|
//! The pallet provides the functionality to store different types of content. This would typically
|
|
//! be used by an on-chain collective, such as the Polkadot Alliance or Ambassador Program.
|
|
//!
|
|
//! The pallet stores content as an [OpaqueCid], which should correspond to some off-chain hosting
|
|
//! service, such as IPFS, and contain any type of data. Each type of content has its own origin
|
|
//! from which it can be managed. The origins are configurable in the runtime. Storing content does
|
|
//! not require a deposit, as it is expected to be managed by a trusted collective.
|
|
//!
|
|
//! Content types:
|
|
//!
|
|
//! - Collective [charter](pallet::Charter): A single document (`OpaqueCid`) managed by
|
|
//! [CharterOrigin](pallet::Config::CharterOrigin).
|
|
//! - Collective [announcements](pallet::Announcements): A list of announcements managed by
|
|
//! [AnnouncementOrigin](pallet::Config::AnnouncementOrigin).
|
|
|
|
#![cfg_attr(not(feature = "std"), no_std)]
|
|
|
|
#[cfg(test)]
|
|
mod mock;
|
|
#[cfg(test)]
|
|
mod tests;
|
|
|
|
#[cfg(feature = "runtime-benchmarks")]
|
|
mod benchmarking;
|
|
pub mod weights;
|
|
|
|
pub use pallet::*;
|
|
pub use weights::WeightInfo;
|
|
|
|
use frame_support::{traits::schedule::DispatchTime, BoundedVec};
|
|
use sp_core::ConstU32;
|
|
use sp_std::prelude::*;
|
|
|
|
/// IPFS compatible CID.
|
|
// Worst case 2 bytes base and codec, 2 bytes hash type and size, 64 bytes hash digest.
|
|
pub type OpaqueCid = BoundedVec<u8, ConstU32<68>>;
|
|
|
|
#[frame_support::pallet]
|
|
pub mod pallet {
|
|
use super::*;
|
|
use frame_support::{ensure, pallet_prelude::*};
|
|
use frame_system::pallet_prelude::*;
|
|
use sp_runtime::{traits::BadOrigin, Saturating};
|
|
|
|
/// The current storage version.
|
|
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
|
|
|
|
#[pallet::pallet]
|
|
#[pallet::storage_version(STORAGE_VERSION)]
|
|
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
|
|
|
|
/// The module configuration trait.
|
|
#[pallet::config]
|
|
pub trait Config<I: 'static = ()>: frame_system::Config {
|
|
/// The overarching event type.
|
|
type RuntimeEvent: From<Event<Self, I>>
|
|
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
|
|
|
/// Default lifetime for an announcement before it expires.
|
|
type AnnouncementLifetime: Get<BlockNumberFor<Self>>;
|
|
|
|
/// The origin to control the collective announcements.
|
|
type AnnouncementOrigin: EnsureOrigin<Self::RuntimeOrigin>;
|
|
|
|
/// Maximum number of announcements in the storage.
|
|
#[pallet::constant]
|
|
type MaxAnnouncements: Get<u32>;
|
|
|
|
/// The origin to control the collective charter.
|
|
type CharterOrigin: EnsureOrigin<Self::RuntimeOrigin>;
|
|
|
|
/// Weight information needed for the pallet.
|
|
type WeightInfo: WeightInfo;
|
|
}
|
|
|
|
#[pallet::error]
|
|
pub enum Error<T, I = ()> {
|
|
/// The announcement is not found.
|
|
MissingAnnouncement,
|
|
/// Number of announcements exceeds `MaxAnnouncementsCount`.
|
|
TooManyAnnouncements,
|
|
/// Cannot expire in the past.
|
|
InvalidExpiration,
|
|
}
|
|
|
|
#[pallet::event]
|
|
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
|
pub enum Event<T: Config<I>, I: 'static = ()> {
|
|
/// A new charter has been set.
|
|
NewCharterSet { cid: OpaqueCid },
|
|
/// A new announcement has been made.
|
|
AnnouncementAnnounced { cid: OpaqueCid, expire_at: BlockNumberFor<T> },
|
|
/// An on-chain announcement has been removed.
|
|
AnnouncementRemoved { cid: OpaqueCid },
|
|
}
|
|
|
|
/// The collective charter.
|
|
#[pallet::storage]
|
|
pub type Charter<T: Config<I>, I: 'static = ()> = StorageValue<_, OpaqueCid, OptionQuery>;
|
|
|
|
/// The collective announcements.
|
|
#[pallet::storage]
|
|
pub type Announcements<T: Config<I>, I: 'static = ()> =
|
|
CountedStorageMap<_, Blake2_128Concat, OpaqueCid, BlockNumberFor<T>, OptionQuery>;
|
|
|
|
#[pallet::call]
|
|
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
|
/// Set the collective charter.
|
|
///
|
|
/// Parameters:
|
|
/// - `origin`: Must be the [Config::CharterOrigin].
|
|
/// - `cid`: [CID](super::OpaqueCid) of the IPFS document of the collective charter.
|
|
#[pallet::call_index(0)]
|
|
#[pallet::weight(T::WeightInfo::set_charter())]
|
|
pub fn set_charter(origin: OriginFor<T>, cid: OpaqueCid) -> DispatchResult {
|
|
T::CharterOrigin::ensure_origin(origin)?;
|
|
|
|
Charter::<T, I>::put(&cid);
|
|
|
|
Self::deposit_event(Event::<T, I>::NewCharterSet { cid });
|
|
Ok(())
|
|
}
|
|
|
|
/// Publish an announcement.
|
|
///
|
|
/// Parameters:
|
|
/// - `origin`: Must be the [Config::AnnouncementOrigin].
|
|
/// - `cid`: [CID](super::OpaqueCid) of the IPFS document to announce.
|
|
/// - `maybe_expire`: Expiration block of the announcement. If `None`
|
|
/// [`Config::AnnouncementLifetime`]
|
|
/// used as a default.
|
|
#[pallet::call_index(1)]
|
|
#[pallet::weight(T::WeightInfo::announce())]
|
|
pub fn announce(
|
|
origin: OriginFor<T>,
|
|
cid: OpaqueCid,
|
|
maybe_expire: Option<DispatchTime<BlockNumberFor<T>>>,
|
|
) -> DispatchResult {
|
|
T::AnnouncementOrigin::ensure_origin(origin)?;
|
|
|
|
let now = frame_system::Pallet::<T>::block_number();
|
|
let expire_at = maybe_expire
|
|
.map_or(now.saturating_add(T::AnnouncementLifetime::get()), |e| e.evaluate(now));
|
|
ensure!(expire_at > now, Error::<T, I>::InvalidExpiration);
|
|
ensure!(
|
|
T::MaxAnnouncements::get() > <Announcements<T, I>>::count(),
|
|
Error::<T, I>::TooManyAnnouncements
|
|
);
|
|
|
|
<Announcements<T, I>>::insert(cid.clone(), expire_at);
|
|
|
|
Self::deposit_event(Event::<T, I>::AnnouncementAnnounced { cid, expire_at });
|
|
Ok(())
|
|
}
|
|
|
|
/// Remove an announcement.
|
|
///
|
|
/// Transaction fee refunded for expired announcements.
|
|
///
|
|
/// Parameters:
|
|
/// - `origin`: Must be the [Config::AnnouncementOrigin] or signed for expired
|
|
/// announcements.
|
|
/// - `cid`: [CID](super::OpaqueCid) of the IPFS document to remove.
|
|
#[pallet::call_index(2)]
|
|
#[pallet::weight(T::WeightInfo::remove_announcement())]
|
|
pub fn remove_announcement(
|
|
origin: OriginFor<T>,
|
|
cid: OpaqueCid,
|
|
) -> DispatchResultWithPostInfo {
|
|
let maybe_who = match T::AnnouncementOrigin::try_origin(origin) {
|
|
Ok(_) => None,
|
|
Err(origin) => Some(ensure_signed(origin)?),
|
|
};
|
|
let expire_at = <Announcements<T, I>>::get(cid.clone())
|
|
.ok_or(Error::<T, I>::MissingAnnouncement)?;
|
|
let now = frame_system::Pallet::<T>::block_number();
|
|
ensure!(maybe_who.is_none() || now >= expire_at, BadOrigin);
|
|
|
|
<Announcements<T, I>>::remove(cid.clone());
|
|
|
|
Self::deposit_event(Event::<T, I>::AnnouncementRemoved { cid });
|
|
|
|
if now >= expire_at {
|
|
return Ok(Pays::No.into())
|
|
}
|
|
Ok(Pays::Yes.into())
|
|
}
|
|
}
|
|
}
|