From d53ca0ea56f3afdec3579879101f8aa42dd8a2ff Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 14 Nov 2021 13:27:05 +0100 Subject: [PATCH] feat: make reqwest optional but enabled by default (#580) * feat: make reqwest optional but enabled by default * update changelog --- CHANGELOG.md | 1 + Cargo.toml | 2 ++ ethers-contract/Cargo.toml | 5 +++-- ethers-contract/ethers-contract-abigen/Cargo.toml | 5 ++++- ethers-contract/ethers-contract-abigen/src/util.rs | 12 +++++++++--- 5 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f244878..fd8f8609 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Unreleased +- Provide a way to opt out of networking support in abigen proc macro with `abigen-offline` feature [#580](https://github.com/gakonst/ethers-rs/pull/580) - Add `.call()` method to `Deployer` for performing dry runs of contract deployments. [#554](https://github.com/gakonst/ethers-rs/pull/554) - Improve error message from failure in `ethers_contract_abigen::Source::parse` [#552](https://github.com/gakonst/ethers-rs/pull/552) - use enumerated aliases for overloaded functions [#545](https://github.com/gakonst/ethers-rs/pull/545) diff --git a/Cargo.toml b/Cargo.toml index 2b28b5c0..39ab8b0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,8 @@ ledger = ["ethers-signers/ledger"] yubi = ["ethers-signers/yubi"] ## contracts abigen = ["ethers-contract/abigen"] +### abigen without reqwest +abigen-offline = ["ethers-contract/abigen-offline"] ## solc solc-async = ["ethers-solc/async"] solc-full = ["ethers-solc/full"] diff --git a/ethers-contract/Cargo.toml b/ethers-contract/Cargo.toml index 7c68a899..36126885 100644 --- a/ethers-contract/Cargo.toml +++ b/ethers-contract/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["ethereum", "web3", "celo", "ethers"] [dependencies] ethers-providers = { version = "^0.5.0", path = "../ethers-providers", default-features = false } ethers-core = { version = "^0.5.0", path = "../ethers-core", default-features = false } -ethers-contract-abigen = { version = "^0.5.0", path = "ethers-contract-abigen", optional = true } +ethers-contract-abigen = { version = "^0.5.0", path = "ethers-contract-abigen", default-features = false, optional = true } ethers-contract-derive = { version = "^0.5.0", path = "ethers-contract-derive", optional = true } ethers-derive-eip712 = { version = "0.1.0", path = "../ethers-core/ethers-derive-eip712", optional = true } @@ -39,7 +39,8 @@ tokio = { version = "1.5", default-features = false, features = ["macros"] } [features] eip712 = ["ethers-derive-eip712", "ethers-core/eip712"] -abigen = ["ethers-contract-abigen", "ethers-contract-derive"] +abigen = ["ethers-contract-abigen/reqwest", "ethers-contract-derive"] +abigen-offline = ["ethers-contract-abigen", "ethers-contract-derive"] celo = ["legacy", "ethers-core/celo", "ethers-core/celo", "ethers-providers/celo"] legacy = [] diff --git a/ethers-contract/ethers-contract-abigen/Cargo.toml b/ethers-contract/ethers-contract-abigen/Cargo.toml index 34eddb66..46c06759 100644 --- a/ethers-contract/ethers-contract-abigen/Cargo.toml +++ b/ethers-contract/ethers-contract-abigen/Cargo.toml @@ -21,7 +21,7 @@ url = "2.1" serde_json = "1.0.61" serde = { version = "1.0.124", features = ["derive"] } hex = { version = "0.4.2", default-features = false, features = ["std"] } -reqwest = { version = "0.11.3", features = ["blocking"] } +reqwest = { version = "0.11.3", features = ["blocking"] , optional = true } once_cell = "1.8.0" cfg-if = "1.0.0" @@ -29,6 +29,9 @@ cfg-if = "1.0.0" # NOTE: this enables wasm compatibility for getrandom indirectly getrandom = { version = "0.2", features = ["js"] } +[features] +default = ["reqwest"] + [package.metadata.docs.rs] all-features = true rustdoc-args = ["--cfg", "docsrs"] diff --git a/ethers-contract/ethers-contract-abigen/src/util.rs b/ethers-contract/ethers-contract-abigen/src/util.rs index 2706352a..d7b5fc60 100644 --- a/ethers-contract/ethers-contract-abigen/src/util.rs +++ b/ethers-contract/ethers-contract-abigen/src/util.rs @@ -1,6 +1,7 @@ use ethers_core::types::Address; use anyhow::{anyhow, Result}; +use cfg_if::cfg_if; use inflector::Inflector; use proc_macro2::{Ident, Literal, Span, TokenStream}; use quote::quote; @@ -69,10 +70,15 @@ where Ok(address_str[2..].parse()?) } -#[cfg(not(target_arch = "wasm32"))] /// Perform an HTTP GET request and return the contents of the response. -pub fn http_get(url: &str) -> Result { - Ok(reqwest::blocking::get(url)?.text()?) +pub fn http_get(_url: &str) -> Result { + cfg_if! { + if #[cfg(any(not(target_arch = "wasm32"), not(features = "reqwest")))]{ + Err(anyhow!("HTTP is unsupported")) + } else { + Ok(reqwest::blocking::get(_url)?.text()?) + } + } } #[cfg(test)]