df_hyperliquid_core/client/evm/rpc/
client.rs1use 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#[derive(Debug, Clone)]
12pub struct Rpc<H> {
13 http: H,
14 url: String,
15}
16
17impl<H: Http> Rpc<H> {
18 pub fn new(http: H, url: impl Into<String>) -> Self {
20 Rpc { http, url: url.into() }
21 }
22
23 pub fn hyperevm(http: H, network: Network) -> Self {
25 Rpc::new(http, network.evm_rpc_url())
26 }
27
28 #[must_use]
30 pub fn url(&self) -> &str {
31 &self.url
32 }
33
34 #[must_use]
36 pub const fn http(&self) -> &H {
37 &self.http
38 }
39
40 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 pub async fn chain_id(&self) -> Result<u64, RpcError> {
56 self.run(eth::chain_id()).await
57 }
58
59 pub async fn block_number(&self) -> Result<u64, RpcError> {
61 self.run(eth::block_number()).await
62 }
63
64 pub async fn balance(&self, owner: Address, block: BlockTag) -> Result<U256, RpcError> {
66 self.run(eth::balance(owner, block)).await
67 }
68
69 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 pub async fn gas_price(&self) -> Result<U256, RpcError> {
76 self.run(eth::gas_price()).await
77 }
78
79 pub async fn max_priority_fee(&self) -> Result<U256, RpcError> {
81 self.run(eth::max_priority_fee()).await
82 }
83
84 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 pub async fn estimate_gas(&self, call: &CallRequest) -> Result<u64, RpcError> {
91 self.run(eth::estimate_gas(call)).await
92 }
93
94 pub async fn call(&self, call: &CallRequest, block: BlockTag) -> Result<Vec<u8>, RpcError> {
96 self.run(eth::call(call, block)).await
97 }
98
99 pub async fn send_raw_transaction(&self, raw: &[u8]) -> Result<B256, RpcError> {
102 self.run(eth::send_raw_transaction(raw)).await
103 }
104
105 pub async fn receipt(&self, hash: B256) -> Result<Option<Receipt>, RpcError> {
107 self.run(eth::receipt(hash)).await
108 }
109
110 pub async fn transaction(&self, hash: B256) -> Result<Option<Transaction>, RpcError> {
112 self.run(eth::transaction(hash)).await
113 }
114
115 pub async fn block(&self, block: BlockTag, full: bool) -> Result<Option<Block>, RpcError> {
117 self.run(eth::block(block, full)).await
118 }
119
120 pub async fn code(&self, at: Address, block: BlockTag) -> Result<Vec<u8>, RpcError> {
122 self.run(eth::code(at, block)).await
123 }
124
125 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 pub async fn logs(&self, filter: &LogFilter) -> Result<Vec<Log>, RpcError> {
132 self.run(eth::logs(filter)).await
133 }
134
135 pub async fn big_block_gas_price(&self) -> Result<U256, RpcError> {
137 self.run(eth::big_block_gas_price()).await
138 }
139
140 pub async fn using_big_blocks(&self, owner: Address) -> Result<bool, RpcError> {
142 self.run(eth::using_big_blocks(owner)).await
143 }
144
145 pub async fn net_version(&self) -> Result<u64, RpcError> {
147 self.run(eth::net_version()).await
148 }
149
150 pub async fn client_version(&self) -> Result<String, RpcError> {
152 self.run(eth::client_version()).await
153 }
154
155 pub async fn syncing(&self) -> Result<bool, RpcError> {
157 self.run(eth::syncing()).await
158 }
159
160 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 pub async fn block_receipts(&self, block: BlockTag) -> Result<Option<Vec<Receipt>>, RpcError> {
167 self.run(eth::block_receipts(block)).await
168 }
169
170 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 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 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 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 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 pub async fn system_transactions(&self, block: BlockTag) -> Result<Option<Vec<Transaction>>, RpcError> {
205 self.run(eth::system_transactions(block)).await
206 }
207
208 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}