openzeppelin_monitor/services/blockchain/transports/
mod.rs1mod evm {
8 pub mod http;
9}
10mod stellar {
11 pub mod http;
12}
13
14mod endpoint_manager;
15mod error;
16mod http;
17
18pub use endpoint_manager::EndpointManager;
19pub use error::TransportError;
20pub use evm::http::EVMTransportClient;
21pub use http::HttpTransportClient;
22pub use stellar::http::StellarTransportClient;
23
24use reqwest_middleware::ClientWithMiddleware;
25use reqwest_retry::{
26 default_on_request_failure, default_on_request_success, Retryable, RetryableStrategy,
27};
28use serde::Serialize;
29use serde_json::{json, Value};
30
31pub const ROTATE_ON_ERROR_CODES: [u16; 1] = [429];
34
35#[async_trait::async_trait]
37pub trait BlockchainTransport: Send + Sync {
38 async fn get_current_url(&self) -> String;
40
41 async fn send_raw_request<P>(
43 &self,
44 method: &str,
45 params: Option<P>,
46 ) -> Result<Value, TransportError>
47 where
48 P: Into<Value> + Send + Clone + Serialize;
49
50 async fn customize_request<P>(&self, method: &str, params: Option<P>) -> Value
52 where
53 P: Into<Value> + Send + Clone + Serialize,
54 {
55 json!({
57 "jsonrpc": "2.0",
58 "id": 1,
59 "method": method,
60 "params": params.map(|p| p.into())
61 })
62 }
63
64 fn update_endpoint_manager_client(
66 &mut self,
67 client: ClientWithMiddleware,
68 ) -> Result<(), anyhow::Error>;
69}
70
71#[async_trait::async_trait]
73pub trait RotatingTransport: BlockchainTransport {
74 async fn try_connect(&self, url: &str) -> Result<(), anyhow::Error>;
76
77 async fn update_client(&self, url: &str) -> Result<(), anyhow::Error>;
79}
80
81pub struct TransientErrorRetryStrategy;
84impl RetryableStrategy for TransientErrorRetryStrategy {
85 fn handle(
86 &self,
87 res: &Result<reqwest::Response, reqwest_middleware::Error>,
88 ) -> Option<Retryable> {
89 match res {
90 Ok(success) => default_on_request_success(success),
91 Err(error) => default_on_request_failure(error),
92 }
93 }
94}