Merge pull request #843 from ethcore/usdgaspricing

USD-based pricing of gas.
This commit is contained in:
Gav Wood 2016-03-28 10:53:38 +02:00
commit c20ca0fa4e
4 changed files with 82 additions and 8 deletions

1
Cargo.lock generated
View File

@ -14,6 +14,7 @@ dependencies = [
"ethminer 1.1.0", "ethminer 1.1.0",
"ethsync 1.1.0", "ethsync 1.1.0",
"fdlimit 0.1.0", "fdlimit 0.1.0",
"hyper 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "number_prefix 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -29,6 +29,10 @@ ethminer = { path = "miner" }
ethcore-devtools = { path = "devtools" } ethcore-devtools = { path = "devtools" }
ethcore-rpc = { path = "rpc", optional = true } ethcore-rpc = { path = "rpc", optional = true }
[dependencies.hyper]
version = "0.8"
default-features = false
[features] [features]
default = ["rpc"] default = ["rpc"]
rpc = ["ethcore-rpc"] rpc = ["ethcore-rpc"]

View File

@ -36,6 +36,9 @@ extern crate time;
extern crate number_prefix; extern crate number_prefix;
extern crate rpassword; extern crate rpassword;
// for price_info.rs
#[macro_use] extern crate hyper;
#[cfg(feature = "rpc")] #[cfg(feature = "rpc")]
extern crate ethcore_rpc as rpc; extern crate ethcore_rpc as rpc;
@ -60,6 +63,8 @@ use docopt::Docopt;
use daemonize::Daemonize; use daemonize::Daemonize;
use number_prefix::{binary_prefix, Standalone, Prefixed}; use number_prefix::{binary_prefix, Standalone, Prefixed};
mod price_info;
fn die_with_message(msg: &str) -> ! { fn die_with_message(msg: &str) -> ! {
println!("ERROR: {}", msg); println!("ERROR: {}", msg);
exit(1); exit(1);
@ -126,8 +131,11 @@ API and Console Options:
[default: web3,eth,net,personal]. [default: web3,eth,net,personal].
Sealing/Mining Options: Sealing/Mining Options:
--gas-price WEI Minimum amount of Wei to be paid for a transaction --usd-per-tx USD Amount of USD to be paid for a basic transaction
to be accepted for mining [default: 20000000000]. [default: 0.005]. The minimum gas price is set
accordingly.
--usd-per-eth SOURCE USD value of a single ETH. SOURCE may be either an
amount in USD or a web service [default: etherscan].
--gas-floor-target GAS Amount of gas per block to target when sealing a new --gas-floor-target GAS Amount of gas per block to target when sealing a new
block [default: 4712388]. block [default: 4712388].
--author ADDRESS Specify the block author (aka "coinbase") address --author ADDRESS Specify the block author (aka "coinbase") address
@ -162,7 +170,9 @@ Geth-compatibility Options:
--rpcport PORT Equivalent to --jsonrpc-port PORT. --rpcport PORT Equivalent to --jsonrpc-port PORT.
--rpcapi APIS Equivalent to --jsonrpc-apis APIS. --rpcapi APIS Equivalent to --jsonrpc-apis APIS.
--rpccorsdomain URL Equivalent to --jsonrpc-cors URL. --rpccorsdomain URL Equivalent to --jsonrpc-cors URL.
--gasprice WEI Equivalent to --gas-price WEI. --gasprice WEI Minimum amount of Wei per GAS to be paid for a
transaction to be accepted for mining. Overrides
--basic-tx-usd.
--etherbase ADDRESS Equivalent to --author ADDRESS. --etherbase ADDRESS Equivalent to --author ADDRESS.
--extradata STRING Equivalent to --extra-data STRING. --extradata STRING Equivalent to --extra-data STRING.
@ -204,7 +214,8 @@ struct Args {
flag_jsonrpc_cors: String, flag_jsonrpc_cors: String,
flag_jsonrpc_apis: String, flag_jsonrpc_apis: String,
flag_author: String, flag_author: String,
flag_gas_price: String, flag_usd_per_tx: String,
flag_usd_per_eth: String,
flag_gas_floor_target: String, flag_gas_floor_target: String,
flag_extra_data: Option<String>, flag_extra_data: Option<String>,
flag_logging: Option<String>, flag_logging: Option<String>,
@ -339,10 +350,29 @@ impl Configuration {
} }
fn gas_price(&self) -> U256 { fn gas_price(&self) -> U256 {
let d = self.args.flag_gasprice.as_ref().unwrap_or(&self.args.flag_gas_price); match self.args.flag_gasprice.as_ref() {
U256::from_dec_str(d).unwrap_or_else(|_| { Some(d) => {
die!("{}: Invalid gas price given. Must be a decimal unsigned 256-bit number.", d) U256::from_dec_str(d).unwrap_or_else(|_| {
}) die!("{}: Invalid gas price given. Must be a decimal unsigned 256-bit number.", d)
})
}
_ => {
let usd_per_tx: f32 = FromStr::from_str(&self.args.flag_usd_per_tx).unwrap_or_else(|_| {
die!("{}: Invalid basic transaction price given in USD. Must be a decimal number.", self.args.flag_usd_per_tx)
});
let usd_per_eth = match self.args.flag_usd_per_eth.as_str() {
"etherscan" => price_info::PriceInfo::get().map(|x| x.ethusd).unwrap_or_else(|| {
die!("Unable to retrieve USD value of ETH from etherscan. Rerun with a different value for --usd-per-eth.")
}),
x => FromStr::from_str(x).unwrap_or_else(|_| die!("{}: Invalid ether price given in USD. Must be a decimal number.", x))
};
let wei_per_usd: f32 = 1.0e18 / usd_per_eth;
let gas_per_tx: f32 = 21000.0;
let wei_per_gas: f32 = wei_per_usd * usd_per_tx / gas_per_tx;
info!("Using a conversion rate of Ξ1 = US${} ({} wei/gas)", usd_per_eth, wei_per_gas);
U256::from_dec_str(&format!("{:.0}", wei_per_gas)).unwrap()
}
}
} }
fn extra_data(&self) -> Bytes { fn extra_data(&self) -> Bytes {

39
parity/price_info.rs Normal file
View File

@ -0,0 +1,39 @@
use rustc_serialize::json::Json;
use std::io::Read;
use hyper::Client;
use hyper::header::Connection;
use std::str::FromStr;
pub struct PriceInfo {
pub ethusd: f32,
}
impl PriceInfo {
pub fn get() -> Option<PriceInfo> {
let mut body = String::new();
// TODO: actually bother dicking around with the errors and make it proper.
match match match Client::new()
.get("http://api.etherscan.io/api?module=stats&action=ethprice")
.header(Connection::close())
.send() {
Err(_) => { return None; },
Ok(mut s) => s.read_to_string(&mut body),
} {
Err(_) => { return None; },
_ => { Json::from_str(&body) }
} {
Err(_) => { return None; },
Ok(json) => {
let ethusd: f32 = if let Some(&Json::String(ref s)) = json.find_path(&["result", "ethusd"]) {
FromStr::from_str(&s).unwrap()
} else {
return None;
};
Some(PriceInfo {
ethusd: ethusd,
})
}
}
}
}