From 51ccf10e02fd9decb670d28a84f4ae142723fc76 Mon Sep 17 00:00:00 2001 From: Alain Brenzikofer Date: Mon, 16 Dec 2024 11:29:49 +0100 Subject: [PATCH] add cli for demurrage proposal --- client/src/cli_args.rs | 16 ++++++++- client/src/commands/encointer_democracy.rs | 41 ++++++++++++++++++++-- client/src/community_spec.rs | 17 +++++---- client/src/main.rs | 10 ++++++ 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/client/src/cli_args.rs b/client/src/cli_args.rs index 473add6c..32c96761 100644 --- a/client/src/cli_args.rs +++ b/client/src/cli_args.rs @@ -34,7 +34,7 @@ const VOTE_ARG: &str = "vote"; const REPUTATION_VEC_ARG: &str = "reputation-vec"; const INACTIVITY_TIMEOUT_ARG: &str = "inactivity-timeout"; const NOMINAL_INCOME_ARG: &str = "nominal-income"; - +const DEMURRAGE_HALVING_BLOCKS_ARG: &str = "demurage-halving-blocks"; const PURPOSE_ID_ARG: &str = "purpose-id"; pub trait EncointerArgs<'b> { @@ -72,6 +72,7 @@ pub trait EncointerArgs<'b> { fn reputation_vec_arg(self) -> Self; fn inactivity_timeout_arg(self) -> Self; fn nominal_income_arg(self) -> Self; + fn demurrage_halving_blocks_arg(self) -> Self; fn purpose_id_arg(self) -> Self; } @@ -109,6 +110,7 @@ pub trait EncointerArgsExtractor { fn reputation_vec_arg(&self) -> Option>; fn inactivity_timeout_arg(&self) -> Option; fn nominal_income_arg(&self) -> Option; + fn demurrage_halving_blocks_arg(&self) -> Option; fn purpose_id_arg(&self) -> Option; } @@ -436,6 +438,15 @@ impl<'a, 'b> EncointerArgs<'b> for App<'a, 'b> { .help("nominal income"), ) } + fn demurrage_halving_blocks_arg(self) -> Self { + self.arg( + Arg::with_name(DEMURRAGE_HALVING_BLOCKS_ARG) + .takes_value(true) + .required(true) + .value_name("DEMURRAGE_HALVING_BLOCKS") + .help("demurrage halving blocks"), + ) + } fn purpose_id_arg(self) -> Self { self.arg( Arg::with_name(PURPOSE_ID_ARG) @@ -566,6 +577,9 @@ impl<'a> EncointerArgsExtractor for ArgMatches<'a> { self.value_of(NOMINAL_INCOME_ARG) .map(|v| BalanceType::from_num(v.parse::().unwrap())) } + fn demurrage_halving_blocks_arg(&self) -> Option { + self.value_of(DEMURRAGE_HALVING_BLOCKS_ARG).map(|v| v.parse().unwrap()) + } fn purpose_id_arg(&self) -> Option { self.value_of(PURPOSE_ID_ARG).map(|v| v.parse().unwrap()) } diff --git a/client/src/commands/encointer_democracy.rs b/client/src/commands/encointer_democracy.rs index aa7e8923..c7abdba6 100644 --- a/client/src/commands/encointer_democracy.rs +++ b/client/src/commands/encointer_democracy.rs @@ -1,8 +1,11 @@ use crate::cli_args::EncointerArgsExtractor; -use crate::utils::{ - ensure_payment, get_chain_api, - keys::{get_accountid_from_str, get_pair_from_str}, +use crate::{ + community_spec::demurrage_per_block_from_halving_blocks, + utils::{ + ensure_payment, get_chain_api, + keys::{get_accountid_from_str, get_pair_from_str}, + }, }; use chrono::{prelude::*, Utc}; use clap::ArgMatches; @@ -86,6 +89,38 @@ pub fn submit_update_nominal_income_proposal( .into() } +pub fn submit_update_demurrage_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 cid = api + .verify_cid(matches.cid_arg().expect("please supply argument --cid"), None) + .await; + let new_demurrage_halving_blocks = matches.demurrage_halving_blocks_arg().unwrap(); + let new_demurrage_per_block = + demurrage_per_block_from_halving_blocks(new_demurrage_halving_blocks); + 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::::UpdateDemurrage(cid, new_demurrage_per_block) + ) + .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: Update demurrage for cid {cid} to {new_demurrage_per_block}"); + Ok(()) + }) + .into() +} pub fn submit_petition(_args: &str, matches: &ArgMatches<'_>) -> Result<(), clap::Error> { let rt = tokio::runtime::Runtime::new().unwrap(); rt.block_on(async { diff --git a/client/src/community_spec.rs b/client/src/community_spec.rs index 823a89a2..9cef8a5e 100644 --- a/client/src/community_spec.rs +++ b/client/src/community_spec.rs @@ -119,12 +119,8 @@ impl CommunitySpec for serde_json::Value { fn demurrage(&self) -> Option { match serde_json::from_value::(self["community"]["demurrage_halving_blocks"].clone()) { Ok(demurrage_halving_blocks) => { - let demurrage_rate = ln::(Demurrage::from_num(0.5)) - .unwrap() - .checked_mul(Demurrage::from_num(-1)) - .unwrap() - .checked_div(Demurrage::from_num(demurrage_halving_blocks)) - .unwrap(); + let demurrage_rate = + demurrage_per_block_from_halving_blocks(demurrage_halving_blocks); log::info!( "demurrage halving blocks: {} which translates to a rate of {} ", @@ -198,3 +194,12 @@ pub fn add_location_call( ) -> AddLocationCall { compose_call!(metadata, "EncointerCommunities", "add_location", cid, loc).unwrap() } + +pub fn demurrage_per_block_from_halving_blocks(halving_blocks: u64) -> Demurrage { + ln::(Demurrage::from_num(0.5)) + .unwrap() + .checked_mul(Demurrage::from_num(-1)) + .unwrap() + .checked_div(Demurrage::from_num(halving_blocks)) + .unwrap() +} diff --git a/client/src/main.rs b/client/src/main.rs index 877cb953..77e80a23 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -611,6 +611,16 @@ fn main() { }) .runner(commands::encointer_democracy::submit_update_nominal_income_proposal), ) + .add_cmd( + Command::new("submit-update-demurrage-proposal") + .description("Submit update demurrage proposal for specified community") + .options(|app| { + app.setting(AppSettings::ColoredHelp) + .account_arg() + .demurrage_halving_blocks_arg() + }) + .runner(commands::encointer_democracy::submit_update_demurrage_proposal), + ) .add_cmd( Command::new("submit-petition") .description("Submit a petition for specified community (if --cid specified) or global")