From a870dfa63afb2f96183275ab08d9c192dfc68c62 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 26 Apr 2022 16:50:02 +0000 Subject: [PATCH] derive more --- Cargo.lock | 22 +++++++++++++++++++++- Cargo.toml | 1 + src/main.rs | 20 +++++--------------- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e0f332ad..313c58a6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -546,6 +546,12 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e4c78c047431fee22c1a7bb92e00ad095a02a983affe4d8a72e2a2c62c1b94f3" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "convert_case" version = "0.5.0" @@ -699,6 +705,19 @@ dependencies = [ "const-oid", ] +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case 0.4.0", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + [[package]] name = "dialoguer" version = "0.8.0" @@ -987,7 +1006,7 @@ dependencies = [ "arrayvec", "bytes", "cargo_metadata", - "convert_case", + "convert_case 0.5.0", "elliptic-curve", "ethabi", "generic-array 0.14.5", @@ -3734,6 +3753,7 @@ dependencies = [ "argh", "atomic-counter", "dashmap", + "derive_more", "ethers", "futures", "governor", diff --git a/Cargo.toml b/Cargo.toml index dab05462..2411adc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ argh = "0.1.7" anyhow = "1.0.57" atomic-counter = "1.0.1" dashmap = "5.2.0" +derive_more = "0.99" ethers = { git = "https://github.com/gakonst/ethers-rs", features = ["rustls", "ws"] } futures = { version = "0.3.21", features = ["thread-pool"] } governor = { version = "0.4.2", features = ["dashmap", "std"] } diff --git a/src/main.rs b/src/main.rs index 98d2cf85..4bdbad58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ // TODO: don't use RwLock. i think we need a concurrent hashmap or we will hit all sorts of deadlocks +use derive_more::From; use ethers::prelude::{Block, TxHash}; use ethers::providers::Middleware; use futures::future; @@ -25,27 +26,15 @@ static APP_USER_AGENT: &str = concat!( env!("CARGO_PKG_VERSION"), ); -// TODO: i'm not sure we need this. i think we can use dyn +// TODO: i tried to use Box, but hit https://github.com/gakonst/ethers-rs/issues/592 +#[derive(From)] enum EthersProvider { Http(ethers::providers::Provider), Ws(ethers::providers::Provider), } -// TODO: seems like this should be derivable -impl From> for EthersProvider { - fn from(item: ethers::providers::Provider) -> Self { - EthersProvider::Http(item) - } -} - -// TODO: seems like this should be derivable -impl From> for EthersProvider { - fn from(item: ethers::providers::Provider) -> Self { - EthersProvider::Ws(item) - } -} - impl EthersProvider { + /// Send a web3 request pub async fn request( &self, method: &str, @@ -57,6 +46,7 @@ impl EthersProvider { } } + /// Subscribe to new block heads pub async fn new_heads(&self, url: String, blocks: Arc) -> anyhow::Result<()> { // TODO: automatically reconnect match &self {