Skip to main content

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