ethers-rs/ethers-providers/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.8 KiB
Rust
Raw Normal View History

#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(unsafe_code)]
#![deny(rustdoc::broken_intra_doc_links)]
#![allow(clippy::type_complexity)]
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
mod ext;
pub use ext::*;
2020-06-10 08:58:27 +00:00
mod rpc;
pub use rpc::*;
2020-05-26 09:52:15 +00:00
mod toolbox;
pub use toolbox::*;
feat(providers): add a subset of admin namespace (#1880) * tell Geth to expose the admin namespace * wip: add admin namespace support * add networking and peer related structs * add rest of chain config fields * add datadir to geth * fix data dir ref * add dev flag to geth * set dev only if block_time is not set * put mutually exclusive options in an enum * make block_time use the devmode enum * add p2p port to geth * add basic impls for admin endpoints * move from_int_or_hex to ethers-core utils * fix nodeinfo protocol field * the type is better represented by a struct which can have either eth or snap * add chain id and discovery toggle for Geth * remove PeerEvent * should re-add when peer event endpoints are implemented * simplify serde options for admin responses * change signature for peer modification apis * these admin apis accept an enode, which _may_ be an enr, but could also be a legacy enode "v4" url. * add note on where `ChainConfig` fields come from * add note on PeerInfo about the source of fields * add admin namespace support to CHANGELOG * update pr number in changelog * cargo fmt * move chainconfig to genesis in utils * accept genesis file in geth * add genesis writing to geth spawn * finally get geth nodes to connect * import io::Read in provider tests * fix PeerInfo and use enode id for provider test * make clippy happy * improve documentation for genesis module * remove not(wasm) attributes on genesis module * remove debugging printlns * remove io::Read from provider tests * add failing post merge test case * add full mainnet nodeinfo for testing * support deserializing json bignums to U256 * the serde_json arbitrary_precision feature is necessary so the serde_json::Number variant of `IntOrHexOrBigNum` can be converted into a string and fed into U256::from_dec_string * fix from_int_or_hex_opt doc string * remove third variant from IntOrHex * unnecessary since serde_json::Number handles smaller ints as well * add comments for ids * fix enode id type in admin_peers provider test * fix admin typo in Cargo.toml Co-authored-by: Georgios Konstantopoulos <me@gakonst.com> * add method to wait on a gethinstance adding a peer * fix dial loop and wait_to_add_peer doc comments * update geth * the build can be updated by changing the GETH_BUILD env var * wait for geth to exit on drop * remove unnecessary wait * fix mid-handshake PeerInfo deserialization * remove println * make tests less flaky * handle discovery with the rest of the non dev opts * dump geth stderr to debug failing ci test * add method which dumps the unread stderr of the geth instance into a string * remove call_raw debug println * bug is due to authrpc endpoint being in use * use unused port when authrpc port is not specified * remove dump_stderr from GethInstance * did not work properly anyways Co-authored-by: Georgios Konstantopoulos <me@gakonst.com>
2022-11-30 21:22:44 +00:00
/// Crate utilities and type aliases
mod utils;
pub use utils::{interval, maybe, EscalationPolicy};
2020-05-27 11:32:44 +00:00
/// Errors
mod errors;
pub use errors::{MiddlewareError, ProviderError, RpcError};
mod stream;
pub use futures_util::StreamExt;
2022-08-03 16:21:55 +00:00
pub use stream::{
tx_stream::TransactionStream, FilterWatcher, DEFAULT_LOCAL_POLL_INTERVAL, DEFAULT_POLL_INTERVAL,
2022-08-03 16:21:55 +00:00
};
mod middleware;
#[cfg(feature = "celo")]
pub use middleware::CeloMiddleware;
pub use middleware::Middleware;
#[allow(deprecated)]
pub use test_provider::{GOERLI, MAINNET, ROPSTEN, SEPOLIA};
#[allow(missing_docs)]
/// Pre-instantiated Infura HTTP clients which rotate through multiple API keys
/// to prevent rate limits
pub mod test_provider {
use super::*;
2022-03-13 18:29:09 +00:00
use crate::Http;
use once_cell::sync::Lazy;
use std::{convert::TryFrom, iter::Cycle, slice::Iter, sync::Mutex};
// List of infura keys to rotate through so we don't get rate limited
2023-02-02 19:12:15 +00:00
const INFURA_KEYS: &[&str] = &["15e8aaed6f894d63a0f6a0206c006cdd"];
pub static MAINNET: Lazy<TestProvider> =
Lazy::new(|| TestProvider::new(INFURA_KEYS, "mainnet"));
pub static GOERLI: Lazy<TestProvider> = Lazy::new(|| TestProvider::new(INFURA_KEYS, "goerli"));
pub static SEPOLIA: Lazy<TestProvider> =
Lazy::new(|| TestProvider::new(INFURA_KEYS, "sepolia"));
#[deprecated = "Ropsten testnet has been deprecated in favor of Goerli or Sepolia."]
pub static ROPSTEN: Lazy<TestProvider> =
Lazy::new(|| TestProvider::new(INFURA_KEYS, "ropsten"));
#[derive(Debug)]
pub struct TestProvider {
network: String,
keys: Mutex<Cycle<Iter<'static, &'static str>>>,
}
impl TestProvider {
pub fn new(keys: &'static [&'static str], network: impl Into<String>) -> Self {
Self { keys: keys.iter().cycle().into(), network: network.into() }
}
2022-05-18 20:40:56 +00:00
pub fn url(&self) -> String {
let Self { network, keys } = self;
let key = keys.lock().unwrap().next().unwrap();
format!("https://{network}.infura.io/v3/{key}")
2022-05-18 20:40:56 +00:00
}
pub fn provider(&self) -> Provider<Http> {
Provider::try_from(self.url().as_str()).unwrap()
}
2022-03-13 17:11:27 +00:00
2022-03-13 18:29:09 +00:00
#[cfg(feature = "ws")]
pub async fn ws(&self) -> Provider<crate::Ws> {
2022-03-13 17:11:27 +00:00
let url = format!(
"wss://{}.infura.io/ws/v3/{}",
self.network,
self.keys.lock().unwrap().next().unwrap()
);
Provider::connect(url.as_str()).await.unwrap()
}
}
}