Skip to main content

zombienet_orchestrator/tx_helper/
client.rs

1#![allow(clippy::result_large_err)]
2use subxt::{backend::rpc::RpcClient, OnlineClient};
3
4#[async_trait::async_trait]
5pub trait ClientFromUrl: Sized {
6    async fn from_secure_url(url: &str) -> Result<Self, subxt::Error>;
7    async fn from_insecure_url(url: &str) -> Result<Self, subxt::Error>;
8}
9
10#[async_trait::async_trait]
11impl<Config: subxt::Config + Send + Sync> ClientFromUrl for OnlineClient<Config> {
12    async fn from_secure_url(url: &str) -> Result<Self, subxt::Error> {
13        Self::from_url(url).await
14    }
15
16    async fn from_insecure_url(url: &str) -> Result<Self, subxt::Error> {
17        Self::from_insecure_url(url).await
18    }
19}
20
21#[async_trait::async_trait]
22impl ClientFromUrl for RpcClient {
23    async fn from_secure_url(url: &str) -> Result<Self, subxt::Error> {
24        Self::from_url(url).await.map_err(subxt::Error::from)
25    }
26
27    async fn from_insecure_url(url: &str) -> Result<Self, subxt::Error> {
28        Self::from_insecure_url(url)
29            .await
30            .map_err(subxt::Error::from)
31    }
32}
33
34pub async fn get_client_from_url<T: ClientFromUrl + Send>(url: &str) -> Result<T, subxt::Error> {
35    if subxt::utils::url_is_secure(url)? {
36        T::from_secure_url(url).await
37    } else {
38        T::from_insecure_url(url).await
39    }
40}