Skip to main content

erpc_sdk/
arc.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/// Arc read-only JSON-RPC methods represented by this release.
13pub const ARC_RPC_METHODS: &[&str] = &["eth_call", "eth_chainId", "eth_getBalance"];
14
15/// Arc read-only service client.
16#[derive(Clone)]
17pub struct ArcClient {
18    /// Arc read-only JSON-RPC namespace.
19    pub rpc: ArcRpcClient,
20}
21
22impl ArcClient {
23    pub(crate) const fn new(transport: Arc<HttpJsonRpcTransport>) -> Self {
24        Self {
25            rpc: ArcRpcClient::new(transport),
26        }
27    }
28}
29
30/// Arc read-only JSON-RPC namespace.
31///
32/// Arc native USDC balances are returned in 18-decimal base units. The
33/// official ERC-20 view uses 6 decimals over the same underlying balance.
34/// This namespace returns both wire values unchanged and does not expose a
35/// token, wrapper, or native-sentinel registry.
36///
37/// Arc Mainnet's documented chain ID is `5042` (`0x13b2`). A configured
38/// endpoint's response is passed through unchanged, so a testnet response
39/// such as `0x4cef52` is never relabeled as Mainnet.
40///
41/// The intentionally narrow surface has compile-fail coverage for forbidden
42/// escape hatches and state-changing operations:
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"))?.arc.rpc;
48/// let _ = rpc.raw::<String>("eth_chainId", None);
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"))?.arc.rpc;
57/// let _ = rpc.request::<String>("eth_chainId", Vec::<serde_json::Value>::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"))?.arc.rpc;
66/// let _ = rpc.batch(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"))?.arc.rpc;
75/// let _ = rpc.subscribe("newHeads", Vec::new());
76/// # Ok(())
77/// # }
78/// ```
79///
80/// ```compile_fail
81/// # use erpc_sdk::{ErpcClient, ErpcClientConfig};
82/// # async fn forbidden() -> erpc_sdk::Result<()> {
83/// let rpc = ErpcClient::new(ErpcClientConfig::new("key"))?.arc.rpc;
84/// let _ = rpc.eth_send_raw_transaction(["0xdeadbeef"]);
85/// # Ok(())
86/// # }
87/// ```
88#[derive(Clone)]
89pub struct ArcRpcClient {
90    namespace: RpcNamespace,
91}
92
93impl ArcRpcClient {
94    pub(crate) const fn new(transport: Arc<HttpJsonRpcTransport>) -> Self {
95        Self {
96            namespace: RpcNamespace::new(transport, BatchPolicy::Unsupported),
97        }
98    }
99
100    /// Returns the credential-free Arc origin and root.
101    #[must_use]
102    pub fn endpoint(&self) -> String {
103        let endpoint = self.namespace.endpoint();
104        if endpoint.is_empty() {
105            return endpoint;
106        }
107        let Ok(mut url) = Url::parse(&endpoint) else {
108            return String::new();
109        };
110        url.set_path("/");
111        url.set_query(None);
112        url.set_fragment(None);
113        let _ = url.set_username("");
114        let _ = url.set_password(None);
115        url.to_string()
116    }
117
118    /// Creates a pending `eth_chainId` read.
119    ///
120    /// # Panics
121    ///
122    /// The empty parameter array is infallible to serialize.
123    #[must_use]
124    pub fn eth_chain_id(&self) -> PendingRpcRequest<String> {
125        self.namespace
126            .request("eth_chainId", Vec::<Value>::new())
127            .expect("empty parameters serialize")
128    }
129
130    /// Creates a pending positional `eth_getBalance` read.
131    pub fn eth_get_balance<P: Serialize>(&self, params: P) -> Result<PendingRpcRequest<String>> {
132        self.namespace.request("eth_getBalance", params)
133    }
134
135    /// Creates a pending positional `eth_call` read simulation.
136    pub fn eth_call<P: Serialize>(&self, params: P) -> Result<PendingRpcRequest<String>> {
137        self.namespace.request("eth_call", params)
138    }
139}