alloy_rpc_client/
builder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use crate::RpcClient;
use alloy_transport::{
    BoxTransport, BoxTransportConnect, Transport, TransportConnect, TransportResult,
};
use tower::{
    layer::util::{Identity, Stack},
    Layer, ServiceBuilder,
};

/// A builder for the transport  [`RpcClient`].
///
/// This is a wrapper around [`tower::ServiceBuilder`]. It allows you to
/// configure middleware layers that will be applied to the transport, and has
/// some shortcuts for common layers and transports.
///
/// A builder accumulates Layers, and then is finished via the
/// [`ClientBuilder::connect`] method, which produces an RPC client.
#[derive(Debug)]
pub struct ClientBuilder<L> {
    pub(crate) builder: ServiceBuilder<L>,
}

impl Default for ClientBuilder<Identity> {
    fn default() -> Self {
        Self { builder: ServiceBuilder::new() }
    }
}

impl<L> ClientBuilder<L> {
    /// Add a middleware layer to the stack.
    ///
    /// This is a wrapper around [`tower::ServiceBuilder::layer`]. Layers that
    /// are added first will be called with the request first.
    pub fn layer<M>(self, layer: M) -> ClientBuilder<Stack<M, L>> {
        ClientBuilder { builder: self.builder.layer(layer) }
    }

    /// Create a new [`RpcClient`] with the given transport and the configured
    /// layers.
    pub fn transport<T>(self, transport: T, is_local: bool) -> RpcClient<L::Service>
    where
        L: Layer<T>,
        T: Transport,
        L::Service: Transport,
    {
        RpcClient::new(self.builder.service(transport), is_local)
    }

    /// Convenience function to create a new [`RpcClient`] with a [`reqwest`]
    /// HTTP transport.
    #[cfg(feature = "reqwest")]
    pub fn http(self, url: url::Url) -> RpcClient<L::Service>
    where
        L: Layer<alloy_transport_http::Http<reqwest::Client>>,
        L::Service: Transport,
    {
        let transport = alloy_transport_http::Http::new(url);
        let is_local = transport.guess_local();

        self.transport(transport, is_local)
    }

    /// Convenience function to create a new [`RpcClient`] with a `hyper` HTTP transport.
    #[cfg(all(not(target_arch = "wasm32"), feature = "hyper"))]
    pub fn hyper_http(self, url: url::Url) -> RpcClient<L::Service>
    where
        L: Layer<alloy_transport_http::HyperTransport>,
        L::Service: Transport,
    {
        let transport = alloy_transport_http::HyperTransport::new_hyper(url);
        let is_local = transport.guess_local();

        self.transport(transport, is_local)
    }

    /// Connect a pubsub transport, producing an [`RpcClient`] with the provided
    /// connection.
    #[cfg(feature = "pubsub")]
    pub async fn pubsub<C>(self, pubsub_connect: C) -> TransportResult<RpcClient<L::Service>>
    where
        C: alloy_pubsub::PubSubConnect,
        L: Layer<alloy_pubsub::PubSubFrontend>,
        L::Service: Transport,
    {
        let is_local = pubsub_connect.is_local();
        let transport = pubsub_connect.into_service().await?;
        Ok(self.transport(transport, is_local))
    }

    /// Connect a WS transport, producing an [`RpcClient`] with the provided
    /// connection
    #[cfg(feature = "ws")]
    pub async fn ws(
        self,
        ws_connect: alloy_transport_ws::WsConnect,
    ) -> TransportResult<RpcClient<L::Service>>
    where
        L: Layer<alloy_pubsub::PubSubFrontend>,
        L::Service: Transport,
    {
        self.pubsub(ws_connect).await
    }

    /// Connect an IPC transport, producing an [`RpcClient`] with the provided
    /// connection.
    #[cfg(feature = "ipc")]
    pub async fn ipc<T>(
        self,
        ipc_connect: alloy_transport_ipc::IpcConnect<T>,
    ) -> TransportResult<RpcClient<L::Service>>
    where
        alloy_transport_ipc::IpcConnect<T>: alloy_pubsub::PubSubConnect,
        L: Layer<alloy_pubsub::PubSubFrontend>,
        L::Service: Transport,
    {
        self.pubsub(ipc_connect).await
    }

    /// Connect a transport, producing an [`RpcClient`] with the provided
    /// connection.
    pub async fn connect<C>(self, connect: C) -> TransportResult<RpcClient<L::Service>>
    where
        C: TransportConnect,
        L: Layer<C::Transport>,
        L::Service: Transport,
    {
        let transport = connect.get_transport().await?;
        Ok(self.transport(transport, connect.is_local()))
    }

    /// Connect a transport, producing an [`RpcClient`] with a [`BoxTransport`]
    /// connection.
    pub async fn connect_boxed<C>(self, connect: C) -> TransportResult<RpcClient<L::Service>>
    where
        C: BoxTransportConnect,
        L: Layer<BoxTransport>,
        L::Service: Transport,
    {
        let transport = connect.get_boxed_transport().await?;
        Ok(self.transport(transport, connect.is_local()))
    }
}