zilliqa_rs/providers/
http.rs

1use async_trait::async_trait;
2use jsonrpsee::{
3    core::{client::ClientT, traits::ToRpcParams},
4    http_client::{HttpClient, HttpClientBuilder},
5};
6use serde::de::DeserializeOwned;
7use url::Url;
8
9use crate::Error;
10
11use super::JsonRpcClient;
12
13/// HTTP Provider
14#[derive(Debug)]
15pub struct Http {
16    client: HttpClient,
17}
18
19impl Http {
20    /// Creates a new HTTP provider.
21    pub fn new(url: impl Into<Url>) -> Result<Self, Error> {
22        Ok(Self {
23            client: HttpClientBuilder::default().build(url.into())?,
24        })
25    }
26}
27
28#[async_trait]
29impl JsonRpcClient for Http {
30    async fn request<T: Send + Sync + ToRpcParams, R: DeserializeOwned>(&self, method: &str, params: T) -> Result<R, Error> {
31        self.client.request(method, params).await.map_err(Error::JsonRpcError)
32    }
33}