1#![feature(inherent_associated_types)]
2#![allow(clippy::type_complexity)]
3#![warn(missing_docs)]
4#![deny(unsafe_code)]
5#![cfg_attr(docsrs, feature(doc_cfg))]
6
7use lazy_static::lazy_static;
78
79use crate::config::NeoConstants;
80pub use api_trait::*;
81pub use cache::{Cache, CacheConfig, CacheStats, RpcCache};
82pub use circuit_breaker::{
83 CircuitBreaker, CircuitBreakerConfig, CircuitBreakerStats, CircuitState,
84};
85pub use connection_pool::{ConnectionPool, PoolConfig, PoolStats};
86pub use errors::ProviderError;
87pub use ext::*;
88pub use mock_client::MockClient;
89pub use production_client::{ProductionClientConfig, ProductionClientStats, ProductionRpcClient};
90pub use rpc::*;
91#[allow(deprecated)]
92pub use test_provider::{MAINNET, TESTNET};
93pub use utils::*;
94
95mod api_trait;
96mod cache;
97mod circuit_breaker;
98mod connection_pool;
99mod errors;
101mod ext;
102mod mock_blocks;
103mod mock_client;
104mod production_client;
105mod rpc;
106mod rx;
107mod utils;
109
110lazy_static! {
111 pub static ref HTTP_PROVIDER: RpcClient<Http> = {
112 let url_str =
113 std::env::var("ENDPOINT").unwrap_or_else(|_| NeoConstants::SEED_1.to_string());
114 let url = url::Url::parse(&url_str).expect("Failed to parse URL");
115 let http_provider = Http::new(url).expect("Failed to create HTTP provider");
116 RpcClient::new(http_provider)
117 };
118}
119
120#[allow(missing_docs)]
121mod test_provider {
124 use std::{convert::TryFrom, iter::Cycle, slice::Iter, sync::Mutex};
125
126 use once_cell::sync::Lazy;
127
128 use super::*;
129
130 const INFURA_KEYS: &[&str] = &["15e8aaed6f894d63a0f6a0206c006cdd"];
132
133 pub static MAINNET: Lazy<TestProvider> =
134 Lazy::new(|| TestProvider::new(INFURA_KEYS, "mainnet"));
135
136 pub static TESTNET: Lazy<TestProvider> =
137 Lazy::new(|| TestProvider::new(INFURA_KEYS, "testnet"));
138
139 #[derive(Debug)]
140 pub struct TestProvider {
141 network: String,
142 keys: Mutex<Cycle<Iter<'static, &'static str>>>,
143 }
144
145 impl TestProvider {
146 pub fn new(keys: &'static [&'static str], network: impl Into<String>) -> Self {
147 Self { keys: keys.iter().cycle().into(), network: network.into() }
148 }
149
150 pub fn url(&self) -> String {
151 let Self { network, keys } = self;
152 let key = keys.lock().unwrap().next().unwrap();
153 format!("https://{network}.infura.io/v3/{key}")
154 }
155
156 pub fn provider(&self) -> RpcClient<Http> {
157 let url_str = self.url();
158 let url = url::Url::parse(&url_str).expect("Failed to parse URL");
159 let http_provider = Http::new(url).expect("Failed to create HTTP provider");
160 RpcClient::new(http_provider)
161 }
162
163 #[cfg(feature = "ws")]
164 pub async fn ws(&self) -> RpcClient<crate::Ws> {
165 let url = format!(
166 "wss://{}.infura.neo.io/ws/v3/{}",
167 self.network,
168 self.keys.lock().unwrap().next().unwrap()
169 );
170 RpcClient::connect(url.as_str()).await.unwrap()
171 }
172 }
173}