From 3e6b31ecb974c4742ab1079bd4ef6d9b377090d7 Mon Sep 17 00:00:00 2001 From: danielsanchezq Date: Fri, 30 Apr 2021 15:23:07 +0200 Subject: [PATCH] Request fees through rest API --- jcli/src/jcli_lib/rest/v0/mod.rs | 2 +- jcli/src/jcli_lib/rest/v0/settings/mod.rs | 11 +++++- jcli/src/jcli_lib/transaction/simplified.rs | 43 +++++++++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/jcli/src/jcli_lib/rest/v0/mod.rs b/jcli/src/jcli_lib/rest/v0/mod.rs index 298af2f562..ee9286e014 100644 --- a/jcli/src/jcli_lib/rest/v0/mod.rs +++ b/jcli/src/jcli_lib/rest/v0/mod.rs @@ -6,7 +6,7 @@ pub mod message; mod network; mod node; mod rewards; -mod settings; +pub mod settings; mod shutdown; mod stake; mod stake_pool; diff --git a/jcli/src/jcli_lib/rest/v0/settings/mod.rs b/jcli/src/jcli_lib/rest/v0/settings/mod.rs index 3f2f751a02..442cc70b7d 100644 --- a/jcli/src/jcli_lib/rest/v0/settings/mod.rs +++ b/jcli/src/jcli_lib/rest/v0/settings/mod.rs @@ -1,5 +1,7 @@ use crate::jcli_lib::rest::{Error, RestArgs}; use crate::jcli_lib::utils::OutputFormat; +use jormungandr_lib::interfaces::SettingsDto; + use structopt::StructOpt; #[derive(StructOpt)] @@ -20,9 +22,14 @@ impl Settings { args, output_format, } = self; - let response = args.client()?.get(&["v0", "settings"]).execute()?.json()?; - let formatted = output_format.format_json(response)?; + let settings = request_settings(args)?; + let formatted = output_format.format_json(serde_json::to_value(&settings)?)?; println!("{}", formatted); Ok(()) } } + +pub fn request_settings(args: RestArgs) -> Result { + serde_json::from_str(&(args.client()?.get(&["v0", "settings"]).execute()?.text()?)) + .map_err(Error::SerdeError) +} diff --git a/jcli/src/jcli_lib/transaction/simplified.rs b/jcli/src/jcli_lib/transaction/simplified.rs index 2ed70b9600..98296ce2de 100644 --- a/jcli/src/jcli_lib/transaction/simplified.rs +++ b/jcli/src/jcli_lib/transaction/simplified.rs @@ -1,7 +1,5 @@ use crate::jcli_lib::rest::RestArgs; use crate::jcli_lib::transaction::{common, Error}; -use chain_crypto::{Ed25519, Ed25519Extended, PublicKey, SecretKey}; - use crate::transaction::mk_witness::WitnessType; use crate::transaction::staging::Staging; use crate::utils::key_parser::read_ed25519_secret_key_from_file; @@ -9,10 +7,14 @@ use crate::utils::AccountId; use crate::{rest, transaction}; use chain_addr::Kind; use chain_core::property::FromStr; +use chain_crypto::{Ed25519, Ed25519Extended, PublicKey, SecretKey}; use chain_impl_mockchain::account::SpendingCounter; use chain_impl_mockchain::key::EitherEd25519SecretKey; use chain_impl_mockchain::transaction::Output; use jormungandr_lib::interfaces; + +use crate::transaction::common::CommonFees; +use jormungandr_lib::interfaces::SettingsDto; use rand::rngs::OsRng; use rand::SeedableRng; use rand_chacha::ChaChaRng; @@ -42,9 +44,6 @@ pub struct MakeTransaction { #[structopt(flatten)] pub common: common::CommonTransaction, - #[structopt(flatten)] - pub fee: common::CommonFees, - #[structopt(flatten)] rest_args: RestArgs, } @@ -58,7 +57,6 @@ impl MakeTransaction { receiver_address, secret_key, self.value, - self.fee, &self.block0_hash, self.rest_args.clone(), self.change, @@ -90,13 +88,41 @@ fn create_receiver_secret_key_and_address( Ok((sk, address)) } +fn common_fee_from_settings(settings: &SettingsDto) -> CommonFees { + let fees = settings.fees; + CommonFees { + constant: fees.constant, + coefficient: fees.coefficient, + certificate: fees.certificate, + certificate_pool_registration: fees + .per_certificate_fees + .certificate_pool_registration + .map(Into::into), + certificate_stake_delegation: fees + .per_certificate_fees + .certificate_owner_stake_delegation + .map(Into::into), + certificate_owner_stake_delegation: fees + .per_certificate_fees + .certificate_owner_stake_delegation + .map(Into::into), + certificate_vote_plan: fees + .per_vote_certificate_fees + .certificate_vote_plan + .map(Into::into), + certificate_vote_cast: fees + .per_vote_certificate_fees + .certificate_vote_cast + .map(Into::into), + } +} + #[allow(clippy::too_many_arguments)] pub fn make_transaction( sender_account: interfaces::Address, receiver_address: interfaces::Address, secret_key: EitherEd25519SecretKey, value: interfaces::Value, - fee: common::CommonFees, block0_hash: &str, rest_args: RestArgs, change: Option, @@ -112,6 +138,9 @@ pub fn make_transaction( value: value.into(), })?; + let settings = rest::v0::settings::request_settings(rest_args.clone())?; + let fee = common_fee_from_settings(&settings); + // finalize transaction::finalize::finalize(fee, change, &mut transaction)?;