use crate::common::error::AppError;
use alloy::network::Ethereum;
use alloy::providers::RootProvider;
use url::Url;
pub type HttpProvider = RootProvider<Ethereum>;
pub type WsProvider = RootProvider<Ethereum>;
pub struct ConnectionFactory;
impl ConnectionFactory {
pub fn http(rpc_url: &str) -> Result<HttpProvider, AppError> {
let url =
Url::parse(rpc_url).map_err(|e| AppError::Config(format!("Invalid RPC URL: {}", e)))?;
let provider = RootProvider::new_http(url);
Ok(provider)
}
pub async fn ws(ws_url: &str) -> Result<WsProvider, AppError> {
let provider = RootProvider::connect(ws_url)
.await
.map_err(|e| AppError::Connection(format!("WS Connection failed: {}", e)))?;
Ok(provider)
}
}