Skip to content

Commit

Permalink
Request fees through rest API
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsanchezq committed Apr 30, 2021
1 parent 68d9804 commit 541c731
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion jcli/src/jcli_lib/rest/v0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions jcli/src/jcli_lib/rest/v0/settings/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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<SettingsDto, Error> {
serde_json::from_str(&(args.client()?.get(&["v0", "settings"]).execute()?.text()?))
.map_err(Error::SerdeError)
}
43 changes: 36 additions & 7 deletions jcli/src/jcli_lib/transaction/simplified.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
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;
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;
Expand Down Expand Up @@ -42,9 +44,6 @@ pub struct MakeTransaction {
#[structopt(flatten)]
pub common: common::CommonTransaction,

#[structopt(flatten)]
pub fee: common::CommonFees,

#[structopt(flatten)]
rest_args: RestArgs,
}
Expand All @@ -58,7 +57,6 @@ impl MakeTransaction {
receiver_address,
secret_key,
self.value,
self.fee,
&self.block0_hash,
self.rest_args.clone(),
self.change,
Expand Down Expand Up @@ -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<interfaces::Address>,
Expand All @@ -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)?;

Expand Down

0 comments on commit 541c731

Please sign in to comment.