diff --git a/driver/src/main.rs b/driver/src/main.rs index 0c5df7cac..76cdbd01a 100644 --- a/driver/src/main.rs +++ b/driver/src/main.rs @@ -51,11 +51,6 @@ struct Options { #[structopt(short, long, env = "ETHEREUM_NODE_URL")] node_url: Url, - /// The network ID used for signing transactions (e.g. 1 for mainnet, 4 for - /// rinkeby, 5777 for ganache). - #[structopt(short = "i", long, env = "NETWORK_ID")] - network_id: u64, - /// Which style of solver to use. Can be one of: /// 'NaiveSolver' for the naive solver; /// 'StandardSolver' for mixed integer programming solver; @@ -256,11 +251,11 @@ fn main() { // Set up shared HTTP client and HTTP services. let http_factory = HttpFactory::new(options.http_timeout, http_metrics); - let (web3, gas_station) = setup_http_services(&http_factory, &options); + let (web3, gas_station) = setup_http_services(&http_factory, &options).wait(); // Set up connection to exchange contract let contract = Arc::new( - StableXContractImpl::new(&web3, options.private_key.clone(), options.network_id) + StableXContractImpl::new(&web3, options.private_key.clone()) .wait() .unwrap(), ); @@ -362,13 +357,14 @@ fn setup_monitoring() -> ( (stablex_metrics, http_metrics, solver_metrics, health) } -fn setup_http_services( +async fn setup_http_services( http_factory: &HttpFactory, options: &Options, ) -> (Web3, Arc) { let web3 = web3_provider(http_factory, options.node_url.as_str(), options.rpc_timeout).unwrap(); - let gas_station = - gas_price::create_estimator(options.network_id, &http_factory, &web3).unwrap(); + let gas_station = gas_price::create_estimator(&http_factory, &web3) + .await + .unwrap(); (web3, gas_station) } diff --git a/price-estimator/src/main.rs b/price-estimator/src/main.rs index 795a78e83..eb1957565 100644 --- a/price-estimator/src/main.rs +++ b/price-estimator/src/main.rs @@ -55,11 +55,6 @@ struct Options { #[structopt(long, env = "ETHEREUM_NODE_URL")] node_url: Url, - /// The network ID used for gas estimations. - /// For example 1 for mainnet, 4 for rinkeby. - #[structopt(long, env = "NETWORK_ID", default_value = "1")] - network_id: u64, - /// The timeout in seconds of web3 JSON RPC calls. #[structopt( long, @@ -154,13 +149,10 @@ fn main() { let web3 = web3_provider(&http_factory, options.node_url.as_str(), options.timeout).unwrap(); // The private key is not actually used but StableXContractImpl requires it. let private_key = PrivateKey::from_raw([1u8; 32]).unwrap(); - let contract = Arc::new( - StableXContractImpl::new(&web3, private_key, 0) - .wait() - .unwrap(), - ); - let gas_station = - gas_price::create_estimator(options.network_id, &http_factory, &web3).unwrap(); + let contract = Arc::new(StableXContractImpl::new(&web3, private_key).wait().unwrap()); + let gas_station = gas_price::create_estimator(&http_factory, &web3) + .wait() + .unwrap(); let cache: HashMap<_, _> = options.token_data.clone().into(); let token_info = TokenInfoCache::with_cache(contract.clone(), cache); diff --git a/services-core/src/contracts.rs b/services-core/src/contracts.rs index 10ca12790..18b80cf95 100644 --- a/services-core/src/contracts.rs +++ b/services-core/src/contracts.rs @@ -17,8 +17,8 @@ pub fn web3_provider(http_factory: &HttpFactory, url: &str, timeout: Duration) - Ok(web3) } -fn method_defaults(key: PrivateKey, network_id: u64) -> Result { - let account = Account::Offline(key, Some(network_id)); +fn method_defaults(key: PrivateKey, chain_id: u64) -> Result { + let account = Account::Offline(key, Some(chain_id)); let defaults = MethodDefaults { from: Some(account), gas: None, diff --git a/services-core/src/contracts/stablex_contract.rs b/services-core/src/contracts/stablex_contract.rs index 21e2d769f..71b61c62e 100644 --- a/services-core/src/contracts/stablex_contract.rs +++ b/services-core/src/contracts/stablex_contract.rs @@ -15,7 +15,11 @@ use ethcontract::{ transaction::{confirm::ConfirmParams, Account, GasPrice, ResolveCondition, TransactionResult}, Address, BlockNumber, PrivateKey, U256, }; -use futures::future::{BoxFuture, FutureExt as _}; +use futures::{ + compat::Future01CompatExt as _, + future::{BoxFuture, FutureExt as _}, +}; + use lazy_static::lazy_static; use std::collections::HashMap; use std::time::Duration; @@ -33,8 +37,9 @@ pub struct StableXContractImpl { } impl StableXContractImpl { - pub async fn new(web3: &contracts::Web3, key: PrivateKey, network_id: u64) -> Result { - let defaults = contracts::method_defaults(key, network_id)?; + pub async fn new(web3: &contracts::Web3, key: PrivateKey) -> Result { + let chain_id = web3.eth().chain_id().compat().await?.as_u64(); + let defaults = contracts::method_defaults(key, chain_id)?; let viewer = BatchExchangeViewer::deployed(&web3).await?; let mut instance = BatchExchange::deployed(&web3).await?; @@ -324,8 +329,6 @@ impl StableXContract for StableXContractImpl { } async fn get_transaction_count(&self) -> Result { - use futures::compat::Future01CompatExt as _; - let web3 = self.instance.raw_instance().web3(); let account = self.account().ok_or_else(|| anyhow!("no account"))?; let address = account.address(); diff --git a/services-core/src/gas_price.rs b/services-core/src/gas_price.rs index 0aa671653..621603a73 100644 --- a/services-core/src/gas_price.rs +++ b/services-core/src/gas_price.rs @@ -5,6 +5,7 @@ pub use self::gas_station::GnosisSafeGasStation; use crate::{contracts::Web3, http::HttpFactory}; use anyhow::Result; use ethcontract::U256; +use futures::compat::Future01CompatExt as _; use std::sync::Arc; #[cfg_attr(test, mockall::automock)] @@ -15,12 +16,12 @@ pub trait GasPriceEstimating { } /// Creates the default gas price estimator for the given network. -pub fn create_estimator( - network_id: u64, +pub async fn create_estimator( http_factory: &HttpFactory, web3: &Web3, ) -> Result> { - Ok(match gas_station::api_url_from_network_id(network_id) { + let network_id = web3.net().version().compat().await?; + Ok(match gas_station::api_url_from_network_id(&network_id) { Some(url) => Arc::new(GnosisSafeGasStation::new(http_factory, url)?), None => Arc::new(web3.clone()), }) diff --git a/services-core/src/gas_price/gas_station.rs b/services-core/src/gas_price/gas_station.rs index 5a9559059..1f23f67b3 100644 --- a/services-core/src/gas_price/gas_station.rs +++ b/services-core/src/gas_price/gas_station.rs @@ -12,10 +12,10 @@ use uint::FromDecStrErr; const DEFAULT_MAINNET_URI: &str = "https://safe-relay.gnosis.io/api/v1/gas-station/"; const DEFAULT_RINKEBY_URI: &str = "https://safe-relay.rinkeby.gnosis.io/api/v1/gas-station/"; -pub fn api_url_from_network_id(network_id: u64) -> Option<&'static str> { +pub fn api_url_from_network_id(network_id: &str) -> Option<&'static str> { match network_id { - 1 => Some(DEFAULT_MAINNET_URI), - 4 => Some(DEFAULT_RINKEBY_URI), + "1" => Some(DEFAULT_MAINNET_URI), + "4" => Some(DEFAULT_RINKEBY_URI), _ => None, } } diff --git a/services-core/src/token_info/onchain.rs b/services-core/src/token_info/onchain.rs index f99a9d676..c4c417e69 100644 --- a/services-core/src/token_info/onchain.rs +++ b/services-core/src/token_info/onchain.rs @@ -44,7 +44,6 @@ mod tests { "0x0102030405060708091011121314151617181920212223242526272829303132", ) .expect("Invalid private key"), - 1, ) .wait() .expect("Error creating contract")