Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Use provided usd-per-eth value if an endpoint is specified (#11209)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rakanalh authored and niklasad1 committed Nov 5, 2019
1 parent e612977 commit 6d25b09
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,7 @@ mod tests {
},
fetch,
p,
"fake_endpoint".to_owned()
)
)
}
Expand Down
7 changes: 3 additions & 4 deletions miner/price-info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ impl<F> cmp::PartialEq for Client<F> {

impl<F: Fetch> Client<F> {
/// Creates a new instance of the `Client` given a `fetch::Client`.
pub fn new(fetch: F, pool: Executor) -> Client<F> {
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<F> {
Client { pool, api_endpoint, fetch }
}

Expand Down Expand Up @@ -105,11 +104,11 @@ mod test {
use super::Client;

fn price_info_ok(response: &str, executor: Executor) -> Client<FakeFetch<String>> {
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<FakeFetch<String>> {
Client::new(FakeFetch::new(None::<String>), executor)
Client::new(FakeFetch::new(None::<String>), executor, "fake_endpoint".to_owned())
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions miner/src/gas_price_calibrator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
34 changes: 21 additions & 13 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 {
Expand Down Expand Up @@ -667,23 +668,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())?,
});
}

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);
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)),
Colour::Yellow.bold().paint(format!("{}", wei_per_gas))
);
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))
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(),
})
}
}

fn extra_data(&self) -> Result<Bytes, String> {
Expand Down
2 changes: 1 addition & 1 deletion parity/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn to_addresses(s: &Option<String>) -> Result<Vec<Address>, String> {

/// Tries to parse string as a price.
pub fn to_price(s: &str) -> Result<f32, String> {
s.parse::<f32>().map_err(|_| format!("Invalid transaciton price 's' given. Must be a decimal number."))
s.parse::<f32>().map_err(|_| format!("Invalid transaction price {:?} given. Must be a decimal number.", s))
}

pub fn join_set(set: Option<&HashSet<String>>) -> Option<String> {
Expand Down
7 changes: 6 additions & 1 deletion parity/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use miner::gas_price_calibrator::{GasPriceCalibratorOptions, GasPriceCalibrator}
use parity_version::version_data;
use user_defaults::UserDefaults;

use crate::configuration;

#[derive(Debug, PartialEq)]
pub enum SpecType {
Foundation,
Expand Down Expand Up @@ -258,6 +260,7 @@ pub enum GasPricerConfig {
Calibrated {
usd_per_tx: f32,
recalibration_period: Duration,
api_endpoint: String
}
}

Expand All @@ -266,6 +269,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(),
}
}
}
Expand All @@ -274,7 +278,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 {
Expand All @@ -283,6 +287,7 @@ impl GasPricerConfig {
},
fetch,
p,
api_endpoint.clone(),
)
)
}
Expand Down

0 comments on commit 6d25b09

Please sign in to comment.