Skip to content

Commit

Permalink
add treasuries pallet (#375)
Browse files Browse the repository at this point in the history
* add treasuries pallet

* add cli for treasuries rpc

* tested successfully

* lift patches and bump crate versions

* comment fix
  • Loading branch information
brenzi authored Sep 10, 2024
1 parent 3f739e3 commit 1184e62
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 23 deletions.
79 changes: 71 additions & 8 deletions Cargo.lock

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

10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ encointer-balances-tx-payment = { default-features = false, version = "~13.1.0"
encointer-balances-tx-payment-rpc = { version = "~13.1.0" }
encointer-balances-tx-payment-rpc-runtime-api = { default-features = false, version = "~13.1.0" }
encointer-ceremonies-assignment = { version = "~13.1.0" }
encointer-primitives = { default-features = false, version = "~13.2.0" }
encointer-primitives = { default-features = false, version = "~13.3.0" }
pallet-encointer-balances = { default-features = false, version = "~13.1.0" }
pallet-encointer-bazaar = { default-features = false, version = "~13.1.0" }
pallet-encointer-bazaar-rpc = { version = "~13.1.0" }
Expand All @@ -42,10 +42,13 @@ pallet-encointer-ceremonies-rpc-runtime-api = { default-features = false, versio
pallet-encointer-communities = { default-features = false, version = "~13.1.0" }
pallet-encointer-communities-rpc = { version = "~13.1.0" }
pallet-encointer-communities-rpc-runtime-api = { default-features = false, version = "~13.1.0" }
pallet-encointer-democracy = { default-features = false, version = "~13.2.0" }
pallet-encointer-democracy = { default-features = false, version = "~13.3.0" }
pallet-encointer-faucet = { default-features = false, version = "~13.2.0" }
pallet-encointer-reputation-commitments = { default-features = false, version = "~13.1.0" }
pallet-encointer-scheduler = { default-features = false, version = "~13.1.0" }
pallet-encointer-treasuries = { default-features = false, version = "~13.3.0" }
pallet-encointer-treasuries-rpc = { version = "~13.3.0" }
pallet-encointer-treasuries-rpc-runtime-api = { default-features = false, version = "~13.3.0" }


# substrate deps
Expand Down Expand Up @@ -159,6 +162,9 @@ substrate-api-client = { git = "https://github.com/encointer/substrate-api-clien
#pallet-encointer-faucet = { path = "../pallets/faucet" }
#pallet-encointer-reputation-commitments = { path = "../pallets/reputation-commitments" }
#pallet-encointer-scheduler = { path = "../pallets/scheduler" }
#pallet-encointer-treasuries = { path = "../pallets/treasuries" }
#pallet-encointer-treasuries-rpc = { path = "../pallets/treasuries/rpc" }
#pallet-encointer-treasuries-rpc-runtime-api = { path = "../pallets/treasuries/rpc/runtime-api" }

# [patch."https://github.com/encointer/substrate-fixed"]
# substrate-fixed = { path = "../substrate-fixed" }
2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "encointer-client-notee"
authors = ["encointer.org <[email protected]>"]
edition = "2021"
#keep with node version. major, minor and patch
version = "1.14.0"
version = "1.14.1"

[dependencies]
# todo migrate to clap >=3 https://github.com/encointer/encointer-node/issues/107
Expand Down
3 changes: 2 additions & 1 deletion client/encointer-api-client-extension/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
name = "encointer-api-client-extension"
version = "0.8.2"
# align with encointer pallet crates versions
version = "13.3.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 2 additions & 0 deletions client/encointer-api-client-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub use democracy::*;
pub use extrinsic_params::*;
pub use reputation_commitments::*;
pub use scheduler::*;
pub use treasuries::*;

mod bazaar;
mod ceremonies;
Expand All @@ -31,3 +32,4 @@ mod democracy;
mod extrinsic_params;
mod reputation_commitments;
mod scheduler;
mod treasuries;
26 changes: 26 additions & 0 deletions client/encointer-api-client-extension/src/treasuries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::Api;
use encointer_node_notee_runtime::AccountId;
use encointer_primitives::communities::CommunityIdentifier;

use substrate_api_client::{ac_compose_macros::rpc_params, rpc::Request};

#[maybe_async::maybe_async(?Send)]
pub trait TreasuriesApi {
async fn get_community_treasury_account_unchecked(
&self,
maybecid: Option<CommunityIdentifier>,
) -> Option<AccountId>;
}

#[maybe_async::maybe_async(?Send)]
impl TreasuriesApi for Api {
async fn get_community_treasury_account_unchecked(
&self,
maybecid: Option<CommunityIdentifier>,
) -> Option<AccountId> {
self.client()
.request("encointer_getCommunityTreasuryAccountUnchecked", rpc_params![maybecid])
.await
.expect("Could not get treasury address...")
}
}
53 changes: 47 additions & 6 deletions client/src/commands/encointer_democracy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::cli_args::EncointerArgsExtractor;

use crate::utils::{ensure_payment, get_chain_api, keys::get_pair_from_str};
use crate::utils::{
ensure_payment, get_chain_api,
keys::{get_accountid_from_str, get_pair_from_str},
};
use chrono::{prelude::*, Utc};
use clap::ArgMatches;
use encointer_api_client_extension::{
set_api_extrisic_params_builder, Api, CeremoniesApi, CommunitiesApi, DemocracyApi, EncointerXt,
Moment, ParentchainExtrinsicSigner, SchedulerApi,
};
use encointer_node_notee_runtime::Hash;
use encointer_node_notee_runtime::{AccountId, Balance, Hash};
use encointer_primitives::{
ceremonies::{CeremonyIndexType, CommunityCeremony, ReputationCountType},
democracy::{
Expand Down Expand Up @@ -39,7 +42,7 @@ pub fn submit_set_inactivity_timeout_proposal(
api,
"EncointerDemocracy",
"submit_proposal",
ProposalAction::SetInactivityTimeout(inactivity_timeout)
ProposalAction::<AccountId, Balance>::SetInactivityTimeout(inactivity_timeout)
)
.unwrap();
ensure_payment(&api, &xt.encode().into(), tx_payment_cid_arg).await;
Expand Down Expand Up @@ -70,7 +73,7 @@ pub fn submit_update_nominal_income_proposal(
api,
"EncointerDemocracy",
"submit_proposal",
ProposalAction::UpdateNominalIncome(cid, new_income)
ProposalAction::<AccountId, Balance>::UpdateNominalIncome(cid, new_income)
)
.unwrap();
ensure_payment(&api, &xt.encode().into(), tx_payment_cid_arg).await;
Expand All @@ -81,6 +84,44 @@ pub fn submit_update_nominal_income_proposal(
.into()
}

pub fn submit_spend_native_proposal(
_args: &str,
matches: &ArgMatches<'_>,
) -> Result<(), clap::Error> {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let who = matches.account_arg().map(get_pair_from_str).unwrap();
let mut api = get_chain_api(matches).await;
api.set_signer(ParentchainExtrinsicSigner::new(sr25519_core::Pair::from(who.clone())));
let maybecid = if let Some(cid) = matches.cid_arg() {
Some(api.verify_cid(cid, None).await)
} else {
None
};
let arg_to = matches.value_of("to").unwrap();
let to = get_accountid_from_str(arg_to);
let amount = matches
.value_of("amount")
.unwrap()
.parse::<u128>()
.expect("amount can be converted to u128");
let tx_payment_cid_arg = matches.tx_payment_cid_arg();
set_api_extrisic_params_builder(&mut api, tx_payment_cid_arg).await;

let xt: EncointerXt<_> = compose_extrinsic!(
api,
"EncointerDemocracy",
"submit_proposal",
ProposalAction::<AccountId, Balance>::SpendNative(maybecid, to.clone(), amount)
)
.unwrap();
ensure_payment(&api, &xt.encode().into(), tx_payment_cid_arg).await;
let _result = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock).await;
println!("Proposal Submitted: Spend Native for cid {maybecid:?} to {to}, amount {amount}");
Ok(())
})
.into()
}
pub fn list_proposals(_args: &str, matches: &ArgMatches<'_>) -> Result<(), clap::Error> {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
Expand All @@ -100,13 +141,13 @@ pub fn list_proposals(_args: &str, matches: &ArgMatches<'_>) -> Result<(), clap:
let proposal_lifetime = api.get_proposal_lifetime().await.unwrap();
let min_turnout_permill = api.get_min_turnout().await.unwrap();
println!("📜 Number of proposals: {}, global config: proposal lifetime: {:?}, confirmation period: {:?}, min turnout: {:.3}%", storage_keys.len(), proposal_lifetime, confirmation_period, min_turnout_permill as f64 / 10f64);
let mut proposals: Vec<(ProposalIdType, Proposal<Moment>)> = Vec::new();
let mut proposals: Vec<(ProposalIdType, Proposal<Moment, AccountId, Balance>)> = Vec::new();
for storage_key in storage_keys.iter() {
let key_postfix = storage_key.as_ref();
let proposal_id =
ProposalIdType::decode(&mut key_postfix[key_postfix.len() - 16..].as_ref())
.unwrap();
let proposal: Proposal<Moment> =
let proposal: Proposal<Moment, AccountId, Balance> =
api.get_storage_by_key(storage_key.clone(), maybe_at).await.unwrap().unwrap();
if !matches.all_flag() && proposal.state.has_failed() {
continue
Expand Down
20 changes: 20 additions & 0 deletions client/src/commands/encointer_treasuries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{cli_args::EncointerArgsExtractor, utils::get_chain_api};
use clap::ArgMatches;
use encointer_api_client_extension::{CommunitiesApi, TreasuriesApi};

pub fn get_treasury_account(_args: &str, matches: &ArgMatches<'_>) -> Result<(), clap::Error> {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let api = get_chain_api(matches).await;

let maybecid = if let Some(cid) = matches.cid_arg() {
Some(api.verify_cid(cid, None).await)
} else {
None
};
let treasury = api.get_community_treasury_account_unchecked(maybecid).await.unwrap();
println!("{treasury}");
Ok(())
})
.into()
}
1 change: 1 addition & 0 deletions client/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ pub mod encointer_democracy;
pub mod encointer_faucet;
pub mod encointer_reputation_commitments;
pub mod encointer_scheduler;
pub mod encointer_treasuries;
pub mod frame;
pub mod keystore;
Loading

0 comments on commit 1184e62

Please sign in to comment.