|
| 1 | +use { |
| 2 | + super::{Provider, ProviderKind, RateLimited, RpcProvider, RpcProviderFactory}, |
| 3 | + crate::{ |
| 4 | + env::WemixConfig, |
| 5 | + error::{RpcError, RpcResult}, |
| 6 | + }, |
| 7 | + async_trait::async_trait, |
| 8 | + axum::{ |
| 9 | + http::HeaderValue, |
| 10 | + response::{IntoResponse, Response}, |
| 11 | + }, |
| 12 | + hyper::{client::HttpConnector, http, Client, Method}, |
| 13 | + hyper_tls::HttpsConnector, |
| 14 | + std::collections::HashMap, |
| 15 | + tracing::debug, |
| 16 | +}; |
| 17 | + |
| 18 | +#[derive(Debug)] |
| 19 | +pub struct WemixProvider { |
| 20 | + pub client: Client<HttpsConnector<HttpConnector>>, |
| 21 | + pub supported_chains: HashMap<String, String>, |
| 22 | +} |
| 23 | + |
| 24 | +impl Provider for WemixProvider { |
| 25 | + fn supports_caip_chainid(&self, chain_id: &str) -> bool { |
| 26 | + self.supported_chains.contains_key(chain_id) |
| 27 | + } |
| 28 | + |
| 29 | + fn supported_caip_chains(&self) -> Vec<String> { |
| 30 | + self.supported_chains.keys().cloned().collect() |
| 31 | + } |
| 32 | + |
| 33 | + fn provider_kind(&self) -> ProviderKind { |
| 34 | + ProviderKind::Wemix |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[async_trait] |
| 39 | +impl RateLimited for WemixProvider { |
| 40 | + async fn is_rate_limited(&self, response: &mut Response) -> bool { |
| 41 | + response.status() == http::StatusCode::TOO_MANY_REQUESTS |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[async_trait] |
| 46 | +impl RpcProvider for WemixProvider { |
| 47 | + #[tracing::instrument(skip(self, body), fields(provider = %self.provider_kind()), level = "debug")] |
| 48 | + async fn proxy(&self, chain_id: &str, body: hyper::body::Bytes) -> RpcResult<Response> { |
| 49 | + let uri = self |
| 50 | + .supported_chains |
| 51 | + .get(chain_id) |
| 52 | + .ok_or(RpcError::ChainNotFound)?; |
| 53 | + |
| 54 | + let hyper_request = hyper::http::Request::builder() |
| 55 | + .method(Method::POST) |
| 56 | + .uri(uri) |
| 57 | + .header("Content-Type", "application/json") |
| 58 | + .body(hyper::body::Body::from(body))?; |
| 59 | + |
| 60 | + let response = self.client.request(hyper_request).await?; |
| 61 | + let status = response.status(); |
| 62 | + let body = hyper::body::to_bytes(response.into_body()).await?; |
| 63 | + |
| 64 | + if let Ok(response) = serde_json::from_slice::<jsonrpc::Response>(&body) { |
| 65 | + if response.error.is_some() && status.is_success() { |
| 66 | + debug!( |
| 67 | + "Strange: provider returned JSON RPC error, but status {status} is success: \ |
| 68 | + Wemix: {response:?}" |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + let mut response = (status, body).into_response(); |
| 74 | + response |
| 75 | + .headers_mut() |
| 76 | + .insert("Content-Type", HeaderValue::from_static("application/json")); |
| 77 | + Ok(response) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl RpcProviderFactory<WemixConfig> for WemixProvider { |
| 82 | + #[tracing::instrument(level = "debug")] |
| 83 | + fn new(provider_config: &WemixConfig) -> Self { |
| 84 | + let forward_proxy_client = Client::builder().build::<_, hyper::Body>(HttpsConnector::new()); |
| 85 | + let supported_chains: HashMap<String, String> = provider_config |
| 86 | + .supported_chains |
| 87 | + .iter() |
| 88 | + .map(|(k, v)| (k.clone(), v.0.clone())) |
| 89 | + .collect(); |
| 90 | + |
| 91 | + WemixProvider { |
| 92 | + client: forward_proxy_client, |
| 93 | + supported_chains, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments