Skip to content

Commit

Permalink
add cli for demurrage proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
brenzi committed Dec 16, 2024
1 parent c821d8a commit 51ccf10
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 10 deletions.
16 changes: 15 additions & 1 deletion client/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -109,6 +110,7 @@ pub trait EncointerArgsExtractor {
fn reputation_vec_arg(&self) -> Option<Vec<&str>>;
fn inactivity_timeout_arg(&self) -> Option<u32>;
fn nominal_income_arg(&self) -> Option<BalanceType>;
fn demurrage_halving_blocks_arg(&self) -> Option<u64>;
fn purpose_id_arg(&self) -> Option<PurposeIdType>;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -566,6 +577,9 @@ impl<'a> EncointerArgsExtractor for ArgMatches<'a> {
self.value_of(NOMINAL_INCOME_ARG)
.map(|v| BalanceType::from_num(v.parse::<f64>().unwrap()))
}
fn demurrage_halving_blocks_arg(&self) -> Option<u64> {
self.value_of(DEMURRAGE_HALVING_BLOCKS_ARG).map(|v| v.parse().unwrap())
}
fn purpose_id_arg(&self) -> Option<PurposeIdType> {
self.value_of(PURPOSE_ID_ARG).map(|v| v.parse().unwrap())
}
Expand Down
41 changes: 38 additions & 3 deletions client/src/commands/encointer_democracy.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<AccountId, Balance>::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 {
Expand Down
17 changes: 11 additions & 6 deletions client/src/community_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,8 @@ impl CommunitySpec for serde_json::Value {
fn demurrage(&self) -> Option<Demurrage> {
match serde_json::from_value::<u64>(self["community"]["demurrage_halving_blocks"].clone()) {
Ok(demurrage_halving_blocks) => {
let demurrage_rate = ln::<Demurrage, Demurrage>(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 {} ",
Expand Down Expand Up @@ -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, Demurrage>(Demurrage::from_num(0.5))
.unwrap()
.checked_mul(Demurrage::from_num(-1))
.unwrap()
.checked_div(Demurrage::from_num(halving_blocks))
.unwrap()
}
10 changes: 10 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 51ccf10

Please sign in to comment.