Skip to content

Commit

Permalink
introduce community treasuries MVP (#401)
Browse files Browse the repository at this point in the history
* plain treasuries pallet

* add rpc

* fmt

* refactor to allow global community by not supplying cid

* intermediate imple of democracy SpendNative

* builds

* fmt

* add unit test for spend_local

* fmt

* taplo

* revert bogus edit

* bump crate versions to 13.3

* bump crate versions to 13.3...

* fix benchmarking trait bounds

* fmt

* remove wrong comment

* fix crate naming

* fix features

* add test

* fmt
  • Loading branch information
brenzi authored Sep 10, 2024
1 parent 557aea1 commit 86d3e05
Show file tree
Hide file tree
Showing 19 changed files with 620 additions and 28 deletions.
61 changes: 58 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,32 @@ members = [
'rpc',
'scheduler',
'test-utils',
'treasuries',
'treasuries/rpc',
'treasuries/rpc/runtime-api',
'vouches',
]

[workspace.dependencies]
# local pin
encointer-ceremonies-assignment = { path = "ceremonies/assignment", default-features = false, version = "13.1.0" }
encointer-meetup-validation = { path = "ceremonies/meetup-validation", default-features = false, version = "13.1.0" }
encointer-primitives = { path = "primitives", default-features = false, features = ["serde_derive"], version = "13.1.0" }
encointer-primitives = { path = "primitives", default-features = false, features = ["serde_derive"], version = "13.3.0" }
encointer-rpc = { path = "rpc", version = "13.1.0" }
ep-core = { path = "primitives/core", default-features = false, version = "13.0.0" }
pallet-encointer-balances = { path = "balances", default-features = false, version = "13.1.0" }
pallet-encointer-ceremonies = { path = "ceremonies", default-features = false, version = "13.1.0" }
pallet-encointer-communities = { path = "communities", default-features = false, version = "13.1.0" }
pallet-encointer-reputation-commitments = { path = "reputation-commitments", default-features = false, version = "13.1.0" }
pallet-encointer-scheduler = { path = "scheduler", default-features = false, version = "13.1.0" }
pallet-encointer-treasuries = { path = "treasuries", default-features = false, version = "13.3.0" }
test-utils = { path = "test-utils" }
# rpc apis
encointer-balances-tx-payment-rpc-runtime-api = { path = "balances-tx-payment/rpc/runtime-api", version = "13.1.0" }
pallet-encointer-bazaar-rpc-runtime-api = { path = "bazaar/rpc/runtime-api", version = "13.1.0" }
pallet-encointer-ceremonies-rpc-runtime-api = { path = "ceremonies/rpc/runtime-api", version = "13.1.0" }
pallet-encointer-communities-rpc-runtime-api = { path = "communities/rpc/runtime-api", version = "13.1.0" }
pallet-encointer-treasuries-rpc-runtime-api = { path = "treasuries/rpc/runtime-api", version = "13.3.0" }

# various
array-bytes = "6.1.0"
Expand Down
7 changes: 4 additions & 3 deletions democracy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-encointer-democracy"
version = "13.2.0"
version = "13.3.0"
authors = ["Encointer Association <[email protected]>"]
edition = "2021"
description = "Democracy pallet for the Encointer blockchain runtime"
Expand All @@ -19,19 +19,21 @@ pallet-encointer-ceremonies = { workspace = true }
pallet-encointer-communities = { workspace = true }
pallet-encointer-reputation-commitments = { workspace = true }
pallet-encointer-scheduler = { workspace = true }
pallet-encointer-treasuries = { workspace = true }

# substrate deps
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-timestamp = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

# benchmarking
frame-benchmarking = { workspace = true, optional = true }
sp-application-crypto = { workspace = true, optional = true }
sp-core = { workspace = true, optional = true }


[dev-dependencies]
approx = { workspace = true }
Expand Down Expand Up @@ -71,7 +73,6 @@ runtime-benchmarks = [
"pallet-encointer-communities/runtime-benchmarks",
"pallet-encointer-scheduler/runtime-benchmarks",
"sp-application-crypto",
"sp-core",
]
try-runtime = [
"frame-system/try-runtime",
Expand Down
6 changes: 5 additions & 1 deletion democracy/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ fn advance_timestamp_equivalent_to_n_blocks<T: Config>(n: u64) {
}

benchmarks! {

where_clause {
where
sp_core::H256: From<<T as frame_system::Config>::Hash>,
T::AccountId: AsRef<[u8; 32]>,
}
submit_proposal {
let zoran = account("zoran", 1, 1);
let cid = CommunityIdentifier::default();
Expand Down
43 changes: 34 additions & 9 deletions democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extern crate alloc;
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;

use frame_support::traits::Currency;
// Logger target
//const LOG: &str = "encointer";

Expand All @@ -59,9 +59,13 @@ mod benchmarking;
pub use pallet::*;

type ReputationVecOf<T> = ReputationVec<<T as Config>::MaxReputationCount>;
pub type BalanceOf<T> = <<T as pallet_encointer_treasuries::Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::Balance;

use pallet_encointer_ceremonies::Pallet as CeremoniesPallet;
use pallet_encointer_communities::Pallet as CommunitiesPallet;
use pallet_encointer_treasuries::Pallet as TreasuriesPallet;

#[allow(clippy::unused_unit)]
#[frame_support::pallet]
Expand All @@ -88,6 +92,7 @@ pub mod pallet {
+ pallet_encointer_ceremonies::Config
+ pallet_encointer_communities::Config
+ pallet_encointer_reputation_commitments::Config
+ pallet_encointer_treasuries::Config
{
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

Expand Down Expand Up @@ -125,7 +130,7 @@ pub mod pallet {
},
ProposalSubmitted {
proposal_id: ProposalIdType,
proposal_action: ProposalAction,
proposal_action: ProposalAction<T::AccountId, BalanceOf<T>>,
},
VotePlaced {
proposal_id: ProposalIdType,
Expand Down Expand Up @@ -184,8 +189,13 @@ pub mod pallet {
/// All proposals that have ever been proposed including the past ones.
#[pallet::storage]
#[pallet::getter(fn proposals)]
pub(super) type Proposals<T: Config> =
StorageMap<_, Blake2_128Concat, ProposalIdType, Proposal<T::Moment>, OptionQuery>;
pub(super) type Proposals<T: Config> = StorageMap<
_,
Blake2_128Concat,
ProposalIdType,
Proposal<T::Moment, T::AccountId, BalanceOf<T>>,
OptionQuery,
>;

/// Proposal count of all proposals to date.
#[pallet::storage]
Expand Down Expand Up @@ -229,13 +239,17 @@ pub mod pallet {
}

#[pallet::call]
impl<T: Config> Pallet<T> {
impl<T: Config> Pallet<T>
where
sp_core::H256: From<<T as frame_system::Config>::Hash>,
T::AccountId: AsRef<[u8; 32]>,
{
#[pallet::call_index(0)]
#[pallet::weight((<T as Config>::WeightInfo::submit_proposal(), DispatchClass::Normal, Pays::Yes)
)]
pub fn submit_proposal(
origin: OriginFor<T>,
proposal_action: ProposalAction,
proposal_action: ProposalAction<T::AccountId, BalanceOf<T>>,
) -> DispatchResultWithPostInfo {
if Self::enactment_queue(proposal_action.clone().get_identifier()).is_some() {
return Err(Error::<T>::ProposalWaitingForEnactment.into());
Expand Down Expand Up @@ -330,7 +344,11 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T> {
impl<T: Config> Pallet<T>
where
sp_core::H256: From<<T as frame_system::Config>::Hash>,
T::AccountId: AsRef<[u8; 32]>,
{
/// Returns the cindexes eligible for voting on a proposal with `proposal_start`.
///
/// It is essentially the range of:
Expand Down Expand Up @@ -481,7 +499,7 @@ pub mod pallet {

pub fn get_electorate(
start_cindex: CeremonyIndexType,
proposal_action: ProposalAction,
proposal_action: ProposalAction<T::AccountId, BalanceOf<T>>,
) -> Result<ReputationCountType, Error<T>> {
let voting_cindexes = Self::voting_cindexes(start_cindex)?;

Expand Down Expand Up @@ -578,6 +596,9 @@ pub mod pallet {
text: petition.clone(),
});
},
ProposalAction::SpendNative(maybe_cid, ref beneficiary, amount) => {
TreasuriesPallet::<T>::do_spend_native(maybe_cid, beneficiary.clone(), amount)?;
},
};

proposal.state = ProposalState::Enacted;
Expand All @@ -588,7 +609,11 @@ pub mod pallet {
}
}

impl<T: Config> OnCeremonyPhaseChange for Pallet<T> {
impl<T: Config> OnCeremonyPhaseChange for Pallet<T>
where
sp_core::H256: From<<T as frame_system::Config>::Hash>,
T::AccountId: AsRef<[u8; 32]>,
{
fn on_ceremony_phase_change(new_phase: CeremonyPhaseType) {
match new_phase {
CeremonyPhaseType::Assigning => {
Expand Down
4 changes: 4 additions & 0 deletions democracy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ frame_support::construct_runtime!(
{
System: frame_system::{Pallet, Call, Config<T>, Storage, Event<T>},
Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
EncointerScheduler: pallet_encointer_scheduler::{Pallet, Call, Storage, Config<T>, Event},
EncointerCommunities: pallet_encointer_communities::{Pallet, Call, Storage, Event<T>},
EncointerCeremonies: pallet_encointer_ceremonies::{Pallet, Call, Storage, Event<T>},
EncointerBalances: pallet_encointer_balances::{Pallet, Call, Storage, Event<T>},
EncointerDemocracy: dut::{Pallet, Call, Storage, Config<T>, Event<T>},
EncointerReputationCommitments:pallet_encointer_reputation_commitments::{Pallet, Call, Storage, Event<T>},
EncointerTreasuries: pallet_encointer_treasuries::{Pallet, Event<T>},
}
);

Expand All @@ -56,11 +58,13 @@ impl dut::Config for TestRuntime {
// boilerplate
impl_frame_system!(TestRuntime);
impl_timestamp!(TestRuntime, EncointerScheduler);
impl_balances!(TestRuntime, System);
impl_encointer_balances!(TestRuntime);
impl_encointer_communities!(TestRuntime);
impl_encointer_scheduler!(TestRuntime, EncointerDemocracy);
impl_encointer_ceremonies!(TestRuntime);
impl_encointer_reputation_commitments!(TestRuntime);
impl_encointer_treasuries!(TestRuntime);

// genesis values
pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
Loading

0 comments on commit 86d3e05

Please sign in to comment.