From d9db40402b5cf1a0a7406dab3ced747c2dd287ff Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Fri, 18 Dec 2020 13:15:19 +0200 Subject: [PATCH] feat: derives &, Arc and Box for Middleware (#109) * test: can stack Middlewares with Arcs * feat: derive &, Box and Arc impls for Middleware * chore: fix spacing --- Cargo.lock | 37 +++++++++++++++++++++++++++++++ ethers-contract/tests/contract.rs | 5 +---- ethers-middleware/tests/stack.rs | 5 +++-- ethers-providers/Cargo.toml | 1 + ethers-providers/src/lib.rs | 3 +++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 36dddd17..db209756 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,6 +266,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" +[[package]] +name = "auto_impl" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42cbf586c80ada5e5ccdecae80d3ef0854f224e2dd74435f8d87e6831b8d0a38" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -874,6 +886,7 @@ dependencies = [ "async-tls", "async-trait", "async-tungstenite", + "auto_impl", "ethers", "ethers-core", "futures-channel", @@ -1884,6 +1897,30 @@ dependencies = [ "uint", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro-hack" version = "0.5.19" diff --git a/ethers-contract/tests/contract.rs b/ethers-contract/tests/contract.rs index 74c954e7..675dec4c 100644 --- a/ethers-contract/tests/contract.rs +++ b/ethers-contract/tests/contract.rs @@ -387,10 +387,7 @@ mod celo_tests { let call = contract .method::<_, H256>("setValue", "hi".to_owned()) .unwrap(); - let pending_tx = call - .send() - .await - .unwrap(); + let pending_tx = call.send().await.unwrap(); let _receipt = pending_tx.await.unwrap(); let value: String = contract diff --git a/ethers-middleware/tests/stack.rs b/ethers-middleware/tests/stack.rs index f6d2f5bd..abd1a589 100644 --- a/ethers-middleware/tests/stack.rs +++ b/ethers-middleware/tests/stack.rs @@ -57,7 +57,7 @@ mod tests { let address = signer.address(); // the base provider - let provider = Provider::::try_from(ganache.endpoint()).unwrap(); + let provider = Arc::new(Provider::::try_from(ganache.endpoint()).unwrap()); // the Gas Price escalator middleware is the first middleware above the provider, // so that it receives the transaction last, after all the other middleware @@ -69,7 +69,8 @@ mod tests { let provider = GasOracleMiddleware::new(provider, gas_oracle); // The signing middleware signs txs - let provider = SignerMiddleware::new(provider, signer); + use std::sync::Arc; + let provider = Arc::new(SignerMiddleware::new(provider, signer)); // The nonce manager middleware MUST be above the signing middleware so that it overrides // the nonce and the signer does not make any eth_getTransaction count calls diff --git a/ethers-providers/Cargo.toml b/ethers-providers/Cargo.toml index 0bd2dc46..965e98bf 100644 --- a/ethers-providers/Cargo.toml +++ b/ethers-providers/Cargo.toml @@ -41,6 +41,7 @@ async-tls = { version = "0.7.0", optional = true } # needed for parsing while deserialization in gas oracles serde-aux = "0.6.1" +auto_impl = "0.4.1" [dev-dependencies] ethers = { version = "0.1.3", path = "../ethers" } diff --git a/ethers-providers/src/lib.rs b/ethers-providers/src/lib.rs index 33217c86..f00895d9 100644 --- a/ethers-providers/src/lib.rs +++ b/ethers-providers/src/lib.rs @@ -118,6 +118,7 @@ mod pubsub; pub use pubsub::{PubsubClient, SubscriptionStream}; use async_trait::async_trait; +use auto_impl::auto_impl; use serde::{de::DeserializeOwned, Serialize}; use std::{error::Error, fmt::Debug, future::Future, pin::Pin}; @@ -128,6 +129,7 @@ pub(crate) type PinBoxFut<'a, T> = Pin> + Send + 'a>>; #[async_trait] +#[auto_impl(&, Box, Arc)] /// Trait which must be implemented by data transports to be used with the Ethereum /// JSON-RPC provider. pub trait JsonRpcClient: Debug + Send + Sync { @@ -147,6 +149,7 @@ pub trait FromErr { } #[async_trait] +#[auto_impl(&, Box, Arc)] pub trait Middleware: Sync + Send + Debug { type Error: Sync + Send + Error + FromErr<::Error>; type Provider: JsonRpcClient;