From 6993ec95312f3ee446345ecb2591f0c66a9b4fcb Mon Sep 17 00:00:00 2001 From: Rakan Alhneiti Date: Tue, 29 Oct 2019 12:18:30 +0100 Subject: [PATCH] Use provided usd-per-eth value if an endpoint is specified (#11209) * Fix `invalid transaction price` error message * Setup Calibrated GasPriceConfig when usd-per-eth is an endpoint The change will try to check if the specified value is an endpoint. If the value of `auto` is specified, the default endpoint URL will be used otherwise, the user-provided value will be taken as-is for an endpoint. * Use if-let and check for usd-per-eth arg: 1. auto = use etherscan 2. value = use fixed pricer 3. endpoint = use the provided endpoint as-is * Fix typo in to_pricce error message * Correct whitespace indentation * Use arg_usd_per_eth directly --- ethcore/src/miner/miner.rs | 1 + miner/price-info/src/lib.rs | 7 +++--- miner/src/gas_price_calibrator.rs | 4 ++-- parity/configuration.rs | 36 +++++++++++++++++++------------ parity/helpers.rs | 2 +- parity/params.rs | 7 +++++- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/ethcore/src/miner/miner.rs b/ethcore/src/miner/miner.rs index 96e5d760b..1671fbffa 100644 --- a/ethcore/src/miner/miner.rs +++ b/ethcore/src/miner/miner.rs @@ -1910,6 +1910,7 @@ mod tests { }, fetch, p, + "fake_endpoint".to_owned() ) ) } diff --git a/miner/price-info/src/lib.rs b/miner/price-info/src/lib.rs index d249704b5..397c2f94f 100644 --- a/miner/price-info/src/lib.rs +++ b/miner/price-info/src/lib.rs @@ -57,8 +57,7 @@ impl cmp::PartialEq for Client { impl Client { /// Creates a new instance of the `Client` given a `fetch::Client`. - pub fn new(fetch: F, pool: Executor) -> Client { - let api_endpoint = "https://api.etherscan.io/api?module=stats&action=ethprice".to_owned(); + pub fn new(fetch: F, pool: Executor, api_endpoint: String) -> Client { Client { pool, api_endpoint, fetch } } @@ -105,11 +104,11 @@ mod test { use super::Client; fn price_info_ok(response: &str, executor: Executor) -> Client> { - Client::new(FakeFetch::new(Some(response.to_owned())), executor) + Client::new(FakeFetch::new(Some(response.to_owned())), executor, "fake_endpoint".to_owned()) } fn price_info_not_found(executor: Executor) -> Client> { - Client::new(FakeFetch::new(None::), executor) + Client::new(FakeFetch::new(None::), executor, "fake_endpoint".to_owned()) } #[test] diff --git a/miner/src/gas_price_calibrator.rs b/miner/src/gas_price_calibrator.rs index 7a0943640..e91b880fa 100644 --- a/miner/src/gas_price_calibrator.rs +++ b/miner/src/gas_price_calibrator.rs @@ -43,11 +43,11 @@ pub struct GasPriceCalibrator { impl GasPriceCalibrator { /// Create a new gas price calibrator. - pub fn new(options: GasPriceCalibratorOptions, fetch: FetchClient, p: Executor) -> GasPriceCalibrator { + pub fn new(options: GasPriceCalibratorOptions, fetch: FetchClient, p: Executor, api_endpoint: String) -> GasPriceCalibrator { GasPriceCalibrator { options: options, next_calibration: Instant::now(), - price_info: PriceInfoClient::new(fetch, p), + price_info: PriceInfoClient::new(fetch, p, api_endpoint), } } diff --git a/parity/configuration.rs b/parity/configuration.rs index 4c99e51c3..0ca62f072 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -58,6 +58,7 @@ use network::{IpFilter}; const DEFAULT_MAX_PEERS: u16 = 50; const DEFAULT_MIN_PEERS: u16 = 25; +pub const ETHERSCAN_ETH_PRICE_ENDPOINT: &str = "https://api.etherscan.io/api?module=stats&action=ethprice"; #[derive(Debug, PartialEq)] pub enum Cmd { @@ -666,23 +667,30 @@ impl Configuration { } let usd_per_tx = to_price(&self.args.arg_usd_per_tx)?; - if "auto" == self.args.arg_usd_per_eth.as_str() { - return Ok(GasPricerConfig::Calibrated { + + if "auto" == self.args.arg_usd_per_eth { + Ok(GasPricerConfig::Calibrated { usd_per_tx: usd_per_tx, recalibration_period: to_duration(self.args.arg_price_update_period.as_str())?, - }); + api_endpoint: ETHERSCAN_ETH_PRICE_ENDPOINT.to_string(), + }) + } else if let Ok(usd_per_eth_parsed) = to_price(&self.args.arg_usd_per_eth) { + let wei_per_gas = wei_per_gas(usd_per_tx, usd_per_eth_parsed); + + info!( + "Using a fixed conversion rate of Ξ1 = {} ({} wei/gas)", + Colour::White.bold().paint(format!("US${:.2}", usd_per_eth_parsed)), + Colour::Yellow.bold().paint(format!("{}", wei_per_gas)) + ); + + Ok(GasPricerConfig::Fixed(wei_per_gas)) + } else { + Ok(GasPricerConfig::Calibrated { + usd_per_tx: usd_per_tx, + recalibration_period: to_duration(self.args.arg_price_update_period.as_str())?, + api_endpoint: self.args.arg_usd_per_eth.clone(), + }) } - - let usd_per_eth = to_price(&self.args.arg_usd_per_eth)?; - let wei_per_gas = wei_per_gas(usd_per_tx, usd_per_eth); - - info!( - "Using a fixed conversion rate of Ξ1 = {} ({} wei/gas)", - Colour::White.bold().paint(format!("US${:.2}", usd_per_eth)), - Colour::Yellow.bold().paint(format!("{}", wei_per_gas)) - ); - - Ok(GasPricerConfig::Fixed(wei_per_gas)) } fn extra_data(&self) -> Result { diff --git a/parity/helpers.rs b/parity/helpers.rs index 2404630cc..a0a973b94 100644 --- a/parity/helpers.rs +++ b/parity/helpers.rs @@ -146,7 +146,7 @@ pub fn to_addresses(s: &Option) -> Result, String> { /// Tries to parse string as a price. pub fn to_price(s: &str) -> Result { - s.parse::().map_err(|_| format!("Invalid transaciton price 's' given. Must be a decimal number.")) + s.parse::().map_err(|_| format!("Invalid transaction price {:?} given. Must be a decimal number.", s)) } pub fn join_set(set: Option<&HashSet>) -> Option { diff --git a/parity/params.rs b/parity/params.rs index fafc94d80..45547b46b 100644 --- a/parity/params.rs +++ b/parity/params.rs @@ -29,6 +29,8 @@ use parity_version::version_data; use user_defaults::UserDefaults; use types::client_types::Mode; +use crate::configuration; + #[derive(Debug, PartialEq)] pub enum SpecType { Foundation, @@ -248,6 +250,7 @@ pub enum GasPricerConfig { Calibrated { usd_per_tx: f32, recalibration_period: Duration, + api_endpoint: String } } @@ -256,6 +259,7 @@ impl Default for GasPricerConfig { GasPricerConfig::Calibrated { usd_per_tx: 0.0001f32, recalibration_period: Duration::from_secs(3600), + api_endpoint: configuration::ETHERSCAN_ETH_PRICE_ENDPOINT.to_string(), } } } @@ -264,7 +268,7 @@ impl GasPricerConfig { pub fn to_gas_pricer(&self, fetch: FetchClient, p: Executor) -> GasPricer { match *self { GasPricerConfig::Fixed(u) => GasPricer::Fixed(u), - GasPricerConfig::Calibrated { usd_per_tx, recalibration_period, .. } => { + GasPricerConfig::Calibrated { usd_per_tx, recalibration_period, ref api_endpoint } => { GasPricer::new_calibrated( GasPriceCalibrator::new( GasPriceCalibratorOptions { @@ -273,6 +277,7 @@ impl GasPricerConfig { }, fetch, p, + api_endpoint.clone(), ) ) }