Skip to main content

df_hyperliquid_core/client/evm/rpc/
client.rs

1//! [`Rpc`], one chain's JSON-RPC: a URL and the wire to it, each method sent
2//! once and its answer read.
3
4use crate::client::evm::records::{Block, FeeHistory, Log, Receipt, Transaction};
5use crate::client::evm::rpc::{BlockTag, CallRequest, LogFilter, Query, RpcError, eth};
6use crate::client::evm::u256::{B256, U256};
7use crate::client::{Http, Network, Request, TransportError};
8use crate::utils::Address;
9
10/// One chain's JSON-RPC: a URL and the wire to it.
11#[derive(Debug, Clone)]
12pub struct Rpc<H> {
13    http: H,
14    url: String,
15}
16
17impl<H: Http> Rpc<H> {
18    /// Any node: an Arbitrum RPC, or HyperEVM through a provider.
19    pub fn new(http: H, url: impl Into<String>) -> Self {
20        Rpc { http, url: url.into() }
21    }
22
23    /// HyperEVM's own RPC on `network`.
24    pub fn hyperevm(http: H, network: Network) -> Self {
25        Rpc::new(http, network.evm_rpc_url())
26    }
27
28    /// The node's URL.
29    #[must_use]
30    pub fn url(&self) -> &str {
31        &self.url
32    }
33
34    /// The transport queries are sent over.
35    #[must_use]
36    pub const fn http(&self) -> &H {
37        &self.http
38    }
39
40    /// Posts the query once and reads the answer. Nothing is retried.
41    pub async fn run<T>(&self, query: Query<T>) -> Result<T, RpcError> {
42        let response = self.http.send(Request::post_json(self.url.clone(), query.body())).await?;
43        if response.status == 429 {
44            return Err(TransportError::RateLimited { retry_after_ms: response.retry_after_ms() }.into());
45        }
46        let text = response.text();
47        match (response.is_success(), query.answer(&text)) {
48            (_, Err(refused @ RpcError::Node { .. })) => Err(refused),
49            (true, answer) => answer,
50            (false, _) => Err(TransportError::Status { status: response.status, body: text }.into()),
51        }
52    }
53
54    /// Runs [`eth::chain_id`]: the chain id.
55    pub async fn chain_id(&self) -> Result<u64, RpcError> {
56        self.run(eth::chain_id()).await
57    }
58
59    /// Runs [`eth::block_number`]: the newest block's number.
60    pub async fn block_number(&self) -> Result<u64, RpcError> {
61        self.run(eth::block_number()).await
62    }
63
64    /// Runs [`eth::balance`]: wei of the chain's coin `owner` holds at `block`.
65    pub async fn balance(&self, owner: Address, block: BlockTag) -> Result<U256, RpcError> {
66        self.run(eth::balance(owner, block)).await
67    }
68
69    /// Runs [`eth::transaction_count`]: the next nonce of `owner` at `block`.
70    pub async fn transaction_count(&self, owner: Address, block: BlockTag) -> Result<u64, RpcError> {
71        self.run(eth::transaction_count(owner, block)).await
72    }
73
74    /// Runs [`eth::gas_price`]: on HyperEVM, the next small block's base fee, in wei.
75    pub async fn gas_price(&self) -> Result<U256, RpcError> {
76        self.run(eth::gas_price()).await
77    }
78
79    /// Runs [`eth::max_priority_fee`]: the suggested tip in wei; on HyperEVM, zero for now.
80    pub async fn max_priority_fee(&self) -> Result<U256, RpcError> {
81        self.run(eth::max_priority_fee()).await
82    }
83
84    /// Runs [`eth::fee_history`]: base fees and tips over `blocks` blocks up to `newest`.
85    pub async fn fee_history(&self, blocks: u64, newest: BlockTag, percentiles: &[u8]) -> Result<FeeHistory, RpcError> {
86        self.run(eth::fee_history(blocks, newest, percentiles)).await
87    }
88
89    /// Runs [`eth::estimate_gas`]: the gas the call or creation would use.
90    pub async fn estimate_gas(&self, call: &CallRequest) -> Result<u64, RpcError> {
91        self.run(eth::estimate_gas(call)).await
92    }
93
94    /// Runs [`eth::call`]: the bytes the call returns at `block`, without sending a transaction.
95    pub async fn call(&self, call: &CallRequest, block: BlockTag) -> Result<Vec<u8>, RpcError> {
96        self.run(eth::call(call, block)).await
97    }
98
99    /// Sent once. With no answer the transaction may still have landed: look
100    /// for its receipt by [`crate::client::evm::tx::tx_hash`] before sending it again.
101    pub async fn send_raw_transaction(&self, raw: &[u8]) -> Result<B256, RpcError> {
102        self.run(eth::send_raw_transaction(raw)).await
103    }
104
105    /// Runs [`eth::receipt`]: `None` until the transaction is mined.
106    pub async fn receipt(&self, hash: B256) -> Result<Option<Receipt>, RpcError> {
107        self.run(eth::receipt(hash)).await
108    }
109
110    /// Runs [`eth::transaction`]: `None` for a hash the node does not know.
111    pub async fn transaction(&self, hash: B256) -> Result<Option<Transaction>, RpcError> {
112        self.run(eth::transaction(hash)).await
113    }
114
115    /// Runs [`eth::block`], with whole transactions when `full`.
116    pub async fn block(&self, block: BlockTag, full: bool) -> Result<Option<Block>, RpcError> {
117        self.run(eth::block(block, full)).await
118    }
119
120    /// Runs [`eth::code`]: the code at `at`, empty for an account that is not a contract.
121    pub async fn code(&self, at: Address, block: BlockTag) -> Result<Vec<u8>, RpcError> {
122        self.run(eth::code(at, block)).await
123    }
124
125    /// Runs [`eth::storage_at`]: the word in storage `slot` of `at`.
126    pub async fn storage_at(&self, at: Address, slot: U256, block: BlockTag) -> Result<B256, RpcError> {
127        self.run(eth::storage_at(at, slot, block)).await
128    }
129
130    /// Runs [`eth::logs`]: every log the filter matches.
131    pub async fn logs(&self, filter: &LogFilter) -> Result<Vec<Log>, RpcError> {
132        self.run(eth::logs(filter)).await
133    }
134
135    /// Runs [`eth::big_block_gas_price`]: on HyperEVM, the next big block's base fee, in wei.
136    pub async fn big_block_gas_price(&self) -> Result<U256, RpcError> {
137        self.run(eth::big_block_gas_price()).await
138    }
139
140    /// Runs [`eth::using_big_blocks`]: whether `owner`'s transactions go to HyperEVM's big blocks.
141    pub async fn using_big_blocks(&self, owner: Address) -> Result<bool, RpcError> {
142        self.run(eth::using_big_blocks(owner)).await
143    }
144
145    /// [`eth::net_version`].
146    pub async fn net_version(&self) -> Result<u64, RpcError> {
147        self.run(eth::net_version()).await
148    }
149
150    /// [`eth::client_version`].
151    pub async fn client_version(&self) -> Result<String, RpcError> {
152        self.run(eth::client_version()).await
153    }
154
155    /// [`eth::syncing`].
156    pub async fn syncing(&self) -> Result<bool, RpcError> {
157        self.run(eth::syncing()).await
158    }
159
160    /// [`eth::block_by_hash`].
161    pub async fn block_by_hash(&self, block: B256, full: bool) -> Result<Option<Block>, RpcError> {
162        self.run(eth::block_by_hash(block, full)).await
163    }
164
165    /// [`eth::block_receipts`].
166    pub async fn block_receipts(&self, block: BlockTag) -> Result<Option<Vec<Receipt>>, RpcError> {
167        self.run(eth::block_receipts(block)).await
168    }
169
170    /// [`eth::block_receipts_by_hash`].
171    pub async fn block_receipts_by_hash(&self, block: B256) -> Result<Option<Vec<Receipt>>, RpcError> {
172        self.run(eth::block_receipts_by_hash(block)).await
173    }
174
175    /// [`eth::block_transaction_count`].
176    pub async fn block_transaction_count(&self, block: BlockTag) -> Result<Option<u64>, RpcError> {
177        self.run(eth::block_transaction_count(block)).await
178    }
179
180    /// [`eth::block_transaction_count_by_hash`].
181    pub async fn block_transaction_count_by_hash(&self, block: B256) -> Result<Option<u64>, RpcError> {
182        self.run(eth::block_transaction_count_by_hash(block)).await
183    }
184
185    /// [`eth::transaction_by_block_and_index`].
186    pub async fn transaction_by_block_and_index(
187        &self,
188        block: BlockTag,
189        position: u64,
190    ) -> Result<Option<Transaction>, RpcError> {
191        self.run(eth::transaction_by_block_and_index(block, position)).await
192    }
193
194    /// [`eth::transaction_by_block_hash_and_index`].
195    pub async fn transaction_by_block_hash_and_index(
196        &self,
197        block: B256,
198        position: u64,
199    ) -> Result<Option<Transaction>, RpcError> {
200        self.run(eth::transaction_by_block_hash_and_index(block, position)).await
201    }
202
203    /// [`eth::system_transactions`].
204    pub async fn system_transactions(&self, block: BlockTag) -> Result<Option<Vec<Transaction>>, RpcError> {
205        self.run(eth::system_transactions(block)).await
206    }
207
208    /// [`eth::system_transactions_by_hash`].
209    pub async fn system_transactions_by_hash(&self, block: B256) -> Result<Option<Vec<Transaction>>, RpcError> {
210        self.run(eth::system_transactions_by_hash(block)).await
211    }
212}