anvil_zksync_api_server/impls/
eth.rs1use anvil_zksync_api_decl::EthNamespaceServer;
2use anvil_zksync_core::node::InMemoryNode;
3use function_name::named;
4use jsonrpsee::core::{async_trait, RpcResult};
5use zksync_types::api::state_override::StateOverride;
6use zksync_types::api::{
7 Block, BlockIdVariant, BlockNumber, FeeHistory, Log, Transaction, TransactionReceipt,
8 TransactionVariant,
9};
10use zksync_types::transaction_request::CallRequest;
11use zksync_types::web3::{Bytes, Index, SyncState, U64Number};
12use zksync_types::{api, Address, H256, U256, U64};
13use zksync_web3_decl::types::{Filter, FilterChanges};
14
15use crate::error::{rpc_unsupported, RpcErrorAdapter};
16
17pub struct EthNamespace {
18 node: InMemoryNode,
19}
20
21impl EthNamespace {
22 pub fn new(node: InMemoryNode) -> Self {
23 Self { node }
24 }
25}
26
27#[async_trait]
28impl EthNamespaceServer for EthNamespace {
29 async fn get_block_number(&self) -> RpcResult<U64> {
30 self.node
31 .get_block_number_impl()
32 .await
33 .map_err(RpcErrorAdapter::into)
34 }
35
36 async fn chain_id(&self) -> RpcResult<U64> {
37 self.node
38 .get_chain_id()
39 .await
40 .map(U64::from)
41 .map_err(RpcErrorAdapter::into)
42 }
43
44 async fn call(
45 &self,
46 req: CallRequest,
47 _block: Option<BlockIdVariant>,
49 state_override: Option<StateOverride>,
50 ) -> RpcResult<Bytes> {
51 self.node
52 .call_impl(req, state_override)
53 .await
54 .map_err(RpcErrorAdapter::into)
55 }
56
57 async fn estimate_gas(
58 &self,
59 req: CallRequest,
60 block: Option<BlockNumber>,
61 _state_override: Option<StateOverride>,
63 ) -> RpcResult<U256> {
64 self.node
65 .estimate_gas_impl(req, block)
66 .await
67 .map_err(RpcErrorAdapter::into)
68 }
69
70 async fn gas_price(&self) -> RpcResult<U256> {
71 self.node
72 .gas_price_impl()
73 .await
74 .map_err(RpcErrorAdapter::into)
75 }
76
77 async fn new_filter(&self, filter: Filter) -> RpcResult<U256> {
78 self.node
79 .new_filter_impl(filter)
80 .await
81 .map_err(RpcErrorAdapter::into)
82 }
83
84 async fn new_block_filter(&self) -> RpcResult<U256> {
85 self.node
86 .new_block_filter_impl()
87 .await
88 .map_err(RpcErrorAdapter::into)
89 }
90
91 async fn uninstall_filter(&self, idx: U256) -> RpcResult<bool> {
92 self.node
93 .uninstall_filter_impl(idx)
94 .await
95 .map_err(RpcErrorAdapter::into)
96 }
97
98 async fn new_pending_transaction_filter(&self) -> RpcResult<U256> {
99 self.node
100 .new_pending_transaction_filter_impl()
101 .await
102 .map_err(RpcErrorAdapter::into)
103 }
104
105 async fn get_logs(&self, filter: Filter) -> RpcResult<Vec<Log>> {
106 self.node
107 .get_logs_impl(filter)
108 .await
109 .map_err(RpcErrorAdapter::into)
110 }
111
112 async fn get_filter_logs(&self, filter_index: U256) -> RpcResult<FilterChanges> {
113 self.node
114 .get_filter_logs_impl(filter_index)
115 .await
116 .map_err(RpcErrorAdapter::into)
117 }
118
119 async fn get_filter_changes(&self, filter_index: U256) -> RpcResult<FilterChanges> {
120 self.node
121 .get_filter_changes_impl(filter_index)
122 .await
123 .map_err(RpcErrorAdapter::into)
124 }
125
126 async fn get_balance(
127 &self,
128 address: Address,
129 block: Option<BlockIdVariant>,
130 ) -> RpcResult<U256> {
131 self.node
132 .get_balance_impl(address, block)
133 .await
134 .map_err(RpcErrorAdapter::into)
135 }
136
137 async fn get_block_by_number(
138 &self,
139 block_number: BlockNumber,
140 full_transactions: bool,
141 ) -> RpcResult<Option<Block<TransactionVariant>>> {
142 self.node
143 .get_block_impl(api::BlockId::Number(block_number), full_transactions)
144 .await
145 .map_err(RpcErrorAdapter::into)
146 }
147
148 async fn get_block_by_hash(
149 &self,
150 hash: H256,
151 full_transactions: bool,
152 ) -> RpcResult<Option<Block<TransactionVariant>>> {
153 self.node
154 .get_block_impl(api::BlockId::Hash(hash), full_transactions)
155 .await
156 .map_err(RpcErrorAdapter::into)
157 }
158
159 async fn get_block_transaction_count_by_number(
160 &self,
161 block_number: BlockNumber,
162 ) -> RpcResult<Option<U256>> {
163 self.node
164 .get_block_transaction_count_impl(api::BlockId::Number(block_number))
165 .await
166 .map_err(RpcErrorAdapter::into)
167 }
168
169 #[named]
170 async fn get_block_receipts(
171 &self,
172 _block_id: api::BlockId,
173 ) -> RpcResult<Option<Vec<TransactionReceipt>>> {
174 rpc_unsupported(function_name!())
175 }
176
177 async fn get_block_transaction_count_by_hash(
178 &self,
179 block_hash: H256,
180 ) -> RpcResult<Option<U256>> {
181 self.node
182 .get_block_transaction_count_impl(api::BlockId::Hash(block_hash))
183 .await
184 .map_err(RpcErrorAdapter::into)
185 }
186
187 async fn get_code(&self, address: Address, block: Option<BlockIdVariant>) -> RpcResult<Bytes> {
188 self.node
189 .get_code_impl(address, block)
190 .await
191 .map_err(RpcErrorAdapter::into)
192 }
193
194 async fn get_storage_at(
195 &self,
196 address: Address,
197 idx: U256,
198 block: Option<BlockIdVariant>,
199 ) -> RpcResult<H256> {
200 self.node
201 .get_storage_impl(address, idx, block)
202 .await
203 .map_err(RpcErrorAdapter::into)
204 }
205
206 async fn get_transaction_count(
207 &self,
208 address: Address,
209 block: Option<BlockIdVariant>,
210 ) -> RpcResult<U256> {
211 self.node
212 .get_transaction_count_impl(address, block)
213 .await
214 .map_err(RpcErrorAdapter::into)
215 }
216
217 async fn get_transaction_by_hash(&self, hash: H256) -> RpcResult<Option<Transaction>> {
218 self.node
219 .get_transaction_by_hash_impl(hash)
220 .await
221 .map_err(RpcErrorAdapter::into)
222 }
223
224 async fn get_transaction_by_block_hash_and_index(
225 &self,
226 block_hash: H256,
227 index: Index,
228 ) -> RpcResult<Option<Transaction>> {
229 self.node
230 .get_transaction_by_block_and_index_impl(api::BlockId::Hash(block_hash), index)
231 .await
232 .map_err(RpcErrorAdapter::into)
233 }
234
235 async fn get_transaction_by_block_number_and_index(
236 &self,
237 block_number: BlockNumber,
238 index: Index,
239 ) -> RpcResult<Option<Transaction>> {
240 self.node
241 .get_transaction_by_block_and_index_impl(api::BlockId::Number(block_number), index)
242 .await
243 .map_err(RpcErrorAdapter::into)
244 }
245
246 async fn get_transaction_receipt(&self, hash: H256) -> RpcResult<Option<TransactionReceipt>> {
247 self.node
248 .get_transaction_receipt_impl(hash)
249 .await
250 .map_err(RpcErrorAdapter::into)
251 }
252
253 async fn protocol_version(&self) -> RpcResult<String> {
254 Ok(self.node.protocol_version_impl())
255 }
256
257 async fn send_raw_transaction(&self, tx_bytes: Bytes) -> RpcResult<H256> {
258 self.node
259 .send_raw_transaction_impl(tx_bytes)
260 .await
261 .map_err(RpcErrorAdapter::into)
262 }
263
264 async fn syncing(&self) -> RpcResult<SyncState> {
265 Ok(self.node.syncing_impl())
266 }
267
268 async fn accounts(&self) -> RpcResult<Vec<Address>> {
269 self.node
270 .accounts_impl()
271 .await
272 .map_err(RpcErrorAdapter::into)
273 }
274
275 #[named]
276 async fn coinbase(&self) -> RpcResult<Address> {
277 rpc_unsupported(function_name!())
278 }
279
280 #[named]
281 async fn compilers(&self) -> RpcResult<Vec<String>> {
282 rpc_unsupported(function_name!())
283 }
284
285 #[named]
286 async fn hashrate(&self) -> RpcResult<U256> {
287 rpc_unsupported(function_name!())
288 }
289
290 #[named]
291 async fn get_uncle_count_by_block_hash(&self, _hash: H256) -> RpcResult<Option<U256>> {
292 rpc_unsupported(function_name!())
293 }
294
295 #[named]
296 async fn get_uncle_count_by_block_number(
297 &self,
298 _number: BlockNumber,
299 ) -> RpcResult<Option<U256>> {
300 rpc_unsupported(function_name!())
301 }
302
303 #[named]
304 async fn mining(&self) -> RpcResult<bool> {
305 rpc_unsupported(function_name!())
306 }
307
308 async fn fee_history(
309 &self,
310 block_count: U64Number,
311 newest_block: BlockNumber,
312 reward_percentiles: Option<Vec<f32>>,
313 ) -> RpcResult<FeeHistory> {
314 self.node
315 .fee_history_impl(block_count.into(), newest_block, reward_percentiles)
316 .await
317 .map_err(RpcErrorAdapter::into)
318 }
319
320 #[named]
321 async fn max_priority_fee_per_gas(&self) -> RpcResult<U256> {
322 rpc_unsupported(function_name!())
323 }
324}