Skip to main content

erpc_sdk/
monad.rs

1use std::sync::Arc;
2
3use serde::Serialize;
4use serde_json::Value;
5use url::Url;
6
7use crate::{
8    PendingRpcRequest, Result, RpcNamespace,
9    rpc::{BatchPolicy, HttpJsonRpcTransport},
10};
11
12/// Monad mainnet read-only JSON-RPC methods represented by this release.
13pub const MONAD_RPC_METHODS: &[&str] = &["eth_call", "eth_chainId", "eth_getBalance"];
14
15/// Monad mainnet read-only service client.
16#[derive(Clone)]
17pub struct MonadClient {
18    /// Monad mainnet read-only JSON-RPC namespace.
19    pub rpc: MonadRpcClient,
20}
21
22impl MonadClient {
23    pub(crate) const fn new(transport: Arc<HttpJsonRpcTransport>) -> Self {
24        Self {
25            rpc: MonadRpcClient::new(transport),
26        }
27    }
28}
29
30/// Monad mainnet read-only JSON-RPC namespace.
31///
32/// The deliberately narrow surface has compile-fail coverage for forbidden
33/// escape hatches and state-changing operations:
34///
35/// ```compile_fail
36/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
37/// # async fn forbidden() -> erpc_sdk::Result<()> {
38/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.monad.rpc;
39/// let _ = rpc.raw::<String>("eth_chainId", None);
40/// # Ok(())
41/// # }
42/// ```
43///
44/// ```compile_fail
45/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
46/// # async fn forbidden() -> erpc_sdk::Result<()> {
47/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.monad.rpc;
48/// let _ = rpc.request::<String>("eth_chainId", Vec::<serde_json::Value>::new());
49/// # Ok(())
50/// # }
51/// ```
52///
53/// ```compile_fail
54/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
55/// # async fn forbidden() -> erpc_sdk::Result<()> {
56/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.monad.rpc;
57/// let _ = rpc.batch(Vec::new());
58/// # Ok(())
59/// # }
60/// ```
61///
62/// ```compile_fail
63/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
64/// # async fn forbidden() -> erpc_sdk::Result<()> {
65/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.monad.rpc;
66/// let _ = rpc.subscribe("newHeads", Vec::new());
67/// # Ok(())
68/// # }
69/// ```
70///
71/// ```compile_fail
72/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
73/// # async fn forbidden() -> erpc_sdk::Result<()> {
74/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.monad.rpc;
75/// let _ = rpc.eth_send_raw_transaction(["0xdeadbeef"]);
76/// # Ok(())
77/// # }
78/// ```
79#[derive(Clone)]
80pub struct MonadRpcClient {
81    namespace: RpcNamespace,
82}
83
84impl MonadRpcClient {
85    pub(crate) const fn new(transport: Arc<HttpJsonRpcTransport>) -> Self {
86        Self {
87            namespace: RpcNamespace::new(transport, BatchPolicy::Unsupported),
88        }
89    }
90
91    /// Returns the credential-free Monad mainnet origin and root.
92    #[must_use]
93    pub fn endpoint(&self) -> String {
94        let endpoint = self.namespace.endpoint();
95        if endpoint.is_empty() {
96            return endpoint;
97        }
98        let Ok(mut url) = Url::parse(&endpoint) else {
99            return String::new();
100        };
101        url.set_path("/");
102        url.set_query(None);
103        url.set_fragment(None);
104        let _ = url.set_username("");
105        let _ = url.set_password(None);
106        url.to_string()
107    }
108
109    /// Creates a pending `eth_chainId` read.
110    ///
111    /// # Panics
112    ///
113    /// The empty parameter array is infallible to serialize.
114    #[must_use]
115    pub fn eth_chain_id(&self) -> PendingRpcRequest<String> {
116        self.namespace
117            .request("eth_chainId", Vec::<Value>::new())
118            .expect("empty parameters serialize")
119    }
120
121    /// Creates a pending positional `eth_getBalance` read.
122    pub fn eth_get_balance<P: Serialize>(&self, params: P) -> Result<PendingRpcRequest<String>> {
123        self.namespace.request("eth_getBalance", params)
124    }
125
126    /// Creates a pending positional `eth_call` read simulation.
127    pub fn eth_call<P: Serialize>(&self, params: P) -> Result<PendingRpcRequest<String>> {
128        self.namespace.request("eth_call", params)
129    }
130}