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

Use provided usd-per-eth value if an endpoint is specified #11209

Merged
merged 6 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ethcore/src/miner/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,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 @@ -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 {
Expand Down Expand Up @@ -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())?,
});
}

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 @@ -146,7 +146,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 @@ -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,
Expand Down Expand Up @@ -248,6 +250,7 @@ pub enum GasPricerConfig {
Calibrated {
usd_per_tx: f32,
recalibration_period: Duration,
api_endpoint: String
}
}

Expand All @@ -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(),
}
}
}
Expand All @@ -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 } => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api_endpointis needed here because it is used to create PriceInfoClient

GasPricer::new_calibrated(
GasPriceCalibrator::new(
GasPriceCalibratorOptions {
Expand All @@ -273,6 +277,7 @@ impl GasPricerConfig {
},
fetch,
p,
api_endpoint.clone(),
)
)
}
Expand Down