starknet_gateway/jsonrpc/
mod.rs

1use async_trait::async_trait;
2use serde::{de::DeserializeOwned, Deserialize, Serialize};
3use serde_with::serde_as;
4use starknet_core::{
5    serde::unsigned_field_element::UfeHex,
6    types::{
7        requests::*, BlockHashAndNumber, BlockId, BroadcastedDeclareTransaction,
8        BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, BroadcastedTransaction,
9        ContractClass, DeclareTransactionResult, DeployAccountTransactionResult, EventFilter,
10        EventFilterWithPage, EventsPage, FeeEstimate, FieldElement, FunctionCall,
11        InvokeTransactionResult, MaybePendingBlockWithTxHashes, MaybePendingBlockWithTxs,
12        MaybePendingStateUpdate, MaybePendingTransactionReceipt, MsgFromL1, ResultPageRequest,
13        StarknetError, SyncStatusType, Transaction,
14    },
15};
16
17use crate::{
18    provider::{MaybeUnknownErrorCode, StarknetErrorWithMessage},
19    Provider, ProviderError,
20};
21
22mod transports;
23pub use transports::{HttpTransport, HttpTransportError, JsonRpcTransport};
24
25#[derive(Debug)]
26pub struct JsonRpcClient<T> {
27    transport: T,
28}
29
30#[derive(Debug, Serialize, Deserialize)]
31pub enum JsonRpcMethod {
32    #[serde(rename = "starknet_getBlockWithTxHashes")]
33    GetBlockWithTxHashes,
34    #[serde(rename = "starknet_getBlockWithTxs")]
35    GetBlockWithTxs,
36    #[serde(rename = "starknet_getStateUpdate")]
37    GetStateUpdate,
38    #[serde(rename = "starknet_getStorageAt")]
39    GetStorageAt,
40    #[serde(rename = "starknet_getTransactionByHash")]
41    GetTransactionByHash,
42    #[serde(rename = "starknet_getTransactionByBlockIdAndIndex")]
43    GetTransactionByBlockIdAndIndex,
44    #[serde(rename = "starknet_getTransactionReceipt")]
45    GetTransactionReceipt,
46    #[serde(rename = "starknet_getClass")]
47    GetClass,
48    #[serde(rename = "starknet_getClassHashAt")]
49    GetClassHashAt,
50    #[serde(rename = "starknet_getClassAt")]
51    GetClassAt,
52    #[serde(rename = "starknet_getBlockTransactionCount")]
53    GetBlockTransactionCount,
54    #[serde(rename = "starknet_call")]
55    Call,
56    #[serde(rename = "starknet_estimateFee")]
57    EstimateFee,
58    #[serde(rename = "starknet_estimateMessageFee")]
59    EstimateMessageFee,
60    #[serde(rename = "starknet_blockNumber")]
61    BlockNumber,
62    #[serde(rename = "starknet_blockHashAndNumber")]
63    BlockHashAndNumber,
64    #[serde(rename = "starknet_chainId")]
65    ChainId,
66    #[serde(rename = "starknet_pendingTransactions")]
67    PendingTransactions,
68    #[serde(rename = "starknet_syncing")]
69    Syncing,
70    #[serde(rename = "starknet_getEvents")]
71    GetEvents,
72    #[serde(rename = "starknet_getNonce")]
73    GetNonce,
74    #[serde(rename = "starknet_addInvokeTransaction")]
75    AddInvokeTransaction,
76    #[serde(rename = "starknet_addDeclareTransaction")]
77    AddDeclareTransaction,
78    #[serde(rename = "starknet_addDeployAccountTransaction")]
79    AddDeployAccountTransaction,
80}
81
82#[derive(Debug, Clone)]
83pub struct JsonRpcRequest {
84    pub id: u64,
85    pub data: JsonRpcRequestData,
86}
87
88#[derive(Debug, Clone)]
89pub enum JsonRpcRequestData {
90    GetBlockWithTxHashes(GetBlockWithTxHashesRequest),
91    GetBlockWithTxs(GetBlockWithTxsRequest),
92    GetStateUpdate(GetStateUpdateRequest),
93    GetStorageAt(GetStorageAtRequest),
94    GetTransactionByHash(GetTransactionByHashRequest),
95    GetTransactionByBlockIdAndIndex(GetTransactionByBlockIdAndIndexRequest),
96    GetTransactionReceipt(GetTransactionReceiptRequest),
97    GetClass(GetClassRequest),
98    GetClassHashAt(GetClassHashAtRequest),
99    GetClassAt(GetClassAtRequest),
100    GetBlockTransactionCount(GetBlockTransactionCountRequest),
101    Call(CallRequest),
102    EstimateFee(EstimateFeeRequest),
103    EstimateMessageFee(EstimateMessageFeeRequest),
104    BlockNumber(BlockNumberRequest),
105    BlockHashAndNumber(BlockHashAndNumberRequest),
106    ChainId(ChainIdRequest),
107    PendingTransactions(PendingTransactionsRequest),
108    Syncing(SyncingRequest),
109    GetEvents(GetEventsRequest),
110    GetNonce(GetNonceRequest),
111    AddInvokeTransaction(AddInvokeTransactionRequest),
112    AddDeclareTransaction(AddDeclareTransactionRequest),
113    AddDeployAccountTransaction(AddDeployAccountTransactionRequest),
114}
115
116#[derive(Debug, thiserror::Error)]
117pub enum JsonRpcClientError<T> {
118    #[error(transparent)]
119    JsonError(serde_json::Error),
120    #[error(transparent)]
121    TransportError(T),
122    #[error(transparent)]
123    RpcError(RpcError),
124}
125
126#[derive(Debug, thiserror::Error)]
127pub enum RpcError {
128    #[error(transparent)]
129    Code(StarknetError),
130    #[error(transparent)]
131    Unknown(JsonRpcError),
132}
133
134#[derive(Debug, thiserror::Error, Deserialize)]
135#[error("JSON-RPC error: code={code}, message=\"{message}\"")]
136pub struct JsonRpcError {
137    pub code: i64,
138    pub message: String,
139}
140
141#[derive(Debug, Deserialize)]
142#[serde(untagged)]
143pub enum JsonRpcResponse<T> {
144    Success { id: u64, result: T },
145    Error { id: u64, error: JsonRpcError },
146}
147
148#[serde_as]
149#[derive(Serialize, Deserialize)]
150struct Felt(#[serde_as(as = "UfeHex")] pub FieldElement);
151
152#[serde_as]
153#[derive(Serialize, Deserialize)]
154struct FeltArray(#[serde_as(as = "Vec<UfeHex>")] pub Vec<FieldElement>);
155
156impl<T> JsonRpcClient<T> {
157    pub fn new(transport: T) -> Self {
158        Self { transport }
159    }
160}
161
162impl<T> JsonRpcClient<T>
163where
164    T: JsonRpcTransport,
165{
166    async fn send_request<P, R>(
167        &self,
168        method: JsonRpcMethod,
169        params: P,
170    ) -> Result<R, ProviderError<JsonRpcClientError<T::Error>>>
171    where
172        P: Serialize + Send,
173        R: DeserializeOwned,
174    {
175        match self
176            .transport
177            .send_request(method, params)
178            .await
179            .map_err(|err| ProviderError::Other(JsonRpcClientError::TransportError(err)))?
180        {
181            JsonRpcResponse::Success { result, .. } => Ok(result),
182            JsonRpcResponse::Error { error, .. } => {
183                Err(ProviderError::StarknetError(StarknetErrorWithMessage {
184                    code: match error.code.try_into() {
185                        Ok(code) => MaybeUnknownErrorCode::Known(code),
186                        Err(_) => MaybeUnknownErrorCode::Unknown(error.code),
187                    },
188                    message: error.message,
189                }))
190            }
191        }
192    }
193}
194
195#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
196#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
197impl<T> Provider for JsonRpcClient<T>
198where
199    T: JsonRpcTransport + Sync + Send,
200{
201    type Error = JsonRpcClientError<T::Error>;
202
203    /// Get block information with transaction hashes given the block id
204    async fn get_block_with_tx_hashes<B>(
205        &self,
206        block_id: B,
207    ) -> Result<MaybePendingBlockWithTxHashes, ProviderError<Self::Error>>
208    where
209        B: AsRef<BlockId> + Send + Sync,
210    {
211        self.send_request(
212            JsonRpcMethod::GetBlockWithTxHashes,
213            GetBlockWithTxHashesRequestRef {
214                block_id: block_id.as_ref(),
215            },
216        )
217        .await
218    }
219
220    /// Get block information with full transactions given the block id
221    async fn get_block_with_txs<B>(
222        &self,
223        block_id: B,
224    ) -> Result<MaybePendingBlockWithTxs, ProviderError<Self::Error>>
225    where
226        B: AsRef<BlockId> + Send + Sync,
227    {
228        self.send_request(
229            JsonRpcMethod::GetBlockWithTxs,
230            GetBlockWithTxsRequestRef {
231                block_id: block_id.as_ref(),
232            },
233        )
234        .await
235    }
236
237    /// Get the information about the result of executing the requested block
238    async fn get_state_update<B>(
239        &self,
240        block_id: B,
241    ) -> Result<MaybePendingStateUpdate, ProviderError<Self::Error>>
242    where
243        B: AsRef<BlockId> + Send + Sync,
244    {
245        self.send_request(
246            JsonRpcMethod::GetStateUpdate,
247            GetStateUpdateRequestRef {
248                block_id: block_id.as_ref(),
249            },
250        )
251        .await
252    }
253
254    /// Get the value of the storage at the given address and key
255    async fn get_storage_at<A, K, B>(
256        &self,
257        contract_address: A,
258        key: K,
259        block_id: B,
260    ) -> Result<FieldElement, ProviderError<Self::Error>>
261    where
262        A: AsRef<FieldElement> + Send + Sync,
263        K: AsRef<FieldElement> + Send + Sync,
264        B: AsRef<BlockId> + Send + Sync,
265    {
266        Ok(self
267            .send_request::<_, Felt>(
268                JsonRpcMethod::GetStorageAt,
269                GetStorageAtRequestRef {
270                    contract_address: contract_address.as_ref(),
271                    key: key.as_ref(),
272                    block_id: block_id.as_ref(),
273                },
274            )
275            .await?
276            .0)
277    }
278
279    /// Get the details and status of a submitted transaction
280    async fn get_transaction_by_hash<H>(
281        &self,
282        transaction_hash: H,
283    ) -> Result<Transaction, ProviderError<Self::Error>>
284    where
285        H: AsRef<FieldElement> + Send + Sync,
286    {
287        self.send_request(
288            JsonRpcMethod::GetTransactionByHash,
289            GetTransactionByHashRequestRef {
290                transaction_hash: transaction_hash.as_ref(),
291            },
292        )
293        .await
294    }
295
296    /// Get the details of a transaction by a given block id and index
297    async fn get_transaction_by_block_id_and_index<B>(
298        &self,
299        block_id: B,
300        index: u64,
301    ) -> Result<Transaction, ProviderError<Self::Error>>
302    where
303        B: AsRef<BlockId> + Send + Sync,
304    {
305        self.send_request(
306            JsonRpcMethod::GetTransactionByBlockIdAndIndex,
307            GetTransactionByBlockIdAndIndexRequestRef {
308                block_id: block_id.as_ref(),
309                index: &index,
310            },
311        )
312        .await
313    }
314
315    /// Get the details of a transaction by a given block number and index
316    async fn get_transaction_receipt<H>(
317        &self,
318        transaction_hash: H,
319    ) -> Result<MaybePendingTransactionReceipt, ProviderError<Self::Error>>
320    where
321        H: AsRef<FieldElement> + Send + Sync,
322    {
323        self.send_request(
324            JsonRpcMethod::GetTransactionReceipt,
325            GetTransactionReceiptRequestRef {
326                transaction_hash: transaction_hash.as_ref(),
327            },
328        )
329        .await
330    }
331
332    /// Get the contract class definition in the given block associated with the given hash
333    async fn get_class<B, H>(
334        &self,
335        block_id: B,
336        class_hash: H,
337    ) -> Result<ContractClass, ProviderError<Self::Error>>
338    where
339        B: AsRef<BlockId> + Send + Sync,
340        H: AsRef<FieldElement> + Send + Sync,
341    {
342        self.send_request(
343            JsonRpcMethod::GetClass,
344            GetClassRequestRef {
345                block_id: block_id.as_ref(),
346                class_hash: class_hash.as_ref(),
347            },
348        )
349        .await
350    }
351
352    /// Get the contract class hash in the given block for the contract deployed at the given address
353    async fn get_class_hash_at<B, A>(
354        &self,
355        block_id: B,
356        contract_address: A,
357    ) -> Result<FieldElement, ProviderError<Self::Error>>
358    where
359        B: AsRef<BlockId> + Send + Sync,
360        A: AsRef<FieldElement> + Send + Sync,
361    {
362        Ok(self
363            .send_request::<_, Felt>(
364                JsonRpcMethod::GetClassHashAt,
365                GetClassHashAtRequestRef {
366                    block_id: block_id.as_ref(),
367                    contract_address: contract_address.as_ref(),
368                },
369            )
370            .await?
371            .0)
372    }
373
374    /// Get the contract class definition in the given block at the given address
375    async fn get_class_at<B, A>(
376        &self,
377        block_id: B,
378        contract_address: A,
379    ) -> Result<ContractClass, ProviderError<Self::Error>>
380    where
381        B: AsRef<BlockId> + Send + Sync,
382        A: AsRef<FieldElement> + Send + Sync,
383    {
384        self.send_request(
385            JsonRpcMethod::GetClassAt,
386            GetClassAtRequestRef {
387                block_id: block_id.as_ref(),
388                contract_address: contract_address.as_ref(),
389            },
390        )
391        .await
392    }
393
394    /// Get the number of transactions in a block given a block id
395    async fn get_block_transaction_count<B>(
396        &self,
397        block_id: B,
398    ) -> Result<u64, ProviderError<Self::Error>>
399    where
400        B: AsRef<BlockId> + Send + Sync,
401    {
402        self.send_request(
403            JsonRpcMethod::GetBlockTransactionCount,
404            GetBlockTransactionCountRequestRef {
405                block_id: block_id.as_ref(),
406            },
407        )
408        .await
409    }
410
411    /// Call a starknet function without creating a Starknet transaction
412    async fn call<R, B>(
413        &self,
414        request: R,
415        block_id: B,
416    ) -> Result<Vec<FieldElement>, ProviderError<Self::Error>>
417    where
418        R: AsRef<FunctionCall> + Send + Sync,
419        B: AsRef<BlockId> + Send + Sync,
420    {
421        Ok(self
422            .send_request::<_, FeltArray>(
423                JsonRpcMethod::Call,
424                CallRequestRef {
425                    request: request.as_ref(),
426                    block_id: block_id.as_ref(),
427                },
428            )
429            .await?
430            .0)
431    }
432
433    /// Estimate the fee for a given Starknet transaction
434    async fn estimate_fee<R, B>(
435        &self,
436        request: R,
437        block_id: B,
438    ) -> Result<Vec<FeeEstimate>, ProviderError<Self::Error>>
439    where
440        R: AsRef<[BroadcastedTransaction]> + Send + Sync,
441        B: AsRef<BlockId> + Send + Sync,
442    {
443        self.send_request(
444            JsonRpcMethod::EstimateFee,
445            EstimateFeeRequestRef {
446                request: request.as_ref(),
447                block_id: block_id.as_ref(),
448            },
449        )
450        .await
451    }
452
453    /// Estimate the L2 fee of a message sent on L1
454    async fn estimate_message_fee<M, B>(
455        &self,
456        message: M,
457        block_id: B,
458    ) -> Result<FeeEstimate, ProviderError<Self::Error>>
459    where
460        M: AsRef<MsgFromL1> + Send + Sync,
461        B: AsRef<BlockId> + Send + Sync,
462    {
463        self.send_request(
464            JsonRpcMethod::EstimateMessageFee,
465            EstimateMessageFeeRequestRef {
466                message: message.as_ref(),
467                block_id: block_id.as_ref(),
468            },
469        )
470        .await
471    }
472
473    /// Get the most recent accepted block number
474    async fn block_number(&self) -> Result<u64, ProviderError<Self::Error>> {
475        self.send_request(JsonRpcMethod::BlockNumber, BlockNumberRequest)
476            .await
477    }
478
479    /// Get the most recent accepted block hash and number
480    async fn block_hash_and_number(
481        &self,
482    ) -> Result<BlockHashAndNumber, ProviderError<Self::Error>> {
483        self.send_request(JsonRpcMethod::BlockHashAndNumber, BlockHashAndNumberRequest)
484            .await
485    }
486
487    /// Return the currently configured Starknet chain id
488    async fn chain_id(&self) -> Result<FieldElement, ProviderError<Self::Error>> {
489        Ok(self
490            .send_request::<_, Felt>(JsonRpcMethod::ChainId, ChainIdRequest)
491            .await?
492            .0)
493    }
494
495    /// Returns the transactions in the transaction pool, recognized by this sequencer
496    async fn pending_transactions(&self) -> Result<Vec<Transaction>, ProviderError<Self::Error>> {
497        self.send_request(
498            JsonRpcMethod::PendingTransactions,
499            PendingTransactionsRequest,
500        )
501        .await
502    }
503
504    /// Returns an object about the sync status, or false if the node is not synching
505    async fn syncing(&self) -> Result<SyncStatusType, ProviderError<Self::Error>> {
506        self.send_request(JsonRpcMethod::Syncing, SyncingRequest)
507            .await
508    }
509
510    /// Returns all events matching the given filter
511    async fn get_events(
512        &self,
513        filter: EventFilter,
514        continuation_token: Option<String>,
515        chunk_size: u64,
516    ) -> Result<EventsPage, ProviderError<Self::Error>> {
517        self.send_request(
518            JsonRpcMethod::GetEvents,
519            GetEventsRequestRef {
520                filter: &EventFilterWithPage {
521                    event_filter: filter,
522                    result_page_request: ResultPageRequest {
523                        continuation_token,
524                        chunk_size,
525                    },
526                },
527            },
528        )
529        .await
530    }
531
532    /// Get the nonce associated with the given address in the given block
533    async fn get_nonce<B, A>(
534        &self,
535        block_id: B,
536        contract_address: A,
537    ) -> Result<FieldElement, ProviderError<Self::Error>>
538    where
539        B: AsRef<BlockId> + Send + Sync,
540        A: AsRef<FieldElement> + Send + Sync,
541    {
542        Ok(self
543            .send_request::<_, Felt>(
544                JsonRpcMethod::GetNonce,
545                GetNonceRequestRef {
546                    block_id: block_id.as_ref(),
547                    contract_address: contract_address.as_ref(),
548                },
549            )
550            .await?
551            .0)
552    }
553
554    /// Submit a new transaction to be added to the chain
555    async fn add_invoke_transaction<I>(
556        &self,
557        invoke_transaction: I,
558    ) -> Result<InvokeTransactionResult, ProviderError<Self::Error>>
559    where
560        I: AsRef<BroadcastedInvokeTransaction> + Send + Sync,
561    {
562        self.send_request(
563            JsonRpcMethod::AddInvokeTransaction,
564            AddInvokeTransactionRequestRef {
565                invoke_transaction: invoke_transaction.as_ref(),
566            },
567        )
568        .await
569    }
570
571    /// Submit a new transaction to be added to the chain
572    async fn add_declare_transaction<D>(
573        &self,
574        declare_transaction: D,
575    ) -> Result<DeclareTransactionResult, ProviderError<Self::Error>>
576    where
577        D: AsRef<BroadcastedDeclareTransaction> + Send + Sync,
578    {
579        self.send_request(
580            JsonRpcMethod::AddDeclareTransaction,
581            AddDeclareTransactionRequestRef {
582                declare_transaction: declare_transaction.as_ref(),
583            },
584        )
585        .await
586    }
587
588    /// Submit a new deploy account transaction
589    async fn add_deploy_account_transaction<D>(
590        &self,
591        deploy_account_transaction: D,
592    ) -> Result<DeployAccountTransactionResult, ProviderError<Self::Error>>
593    where
594        D: AsRef<BroadcastedDeployAccountTransaction> + Send + Sync,
595    {
596        self.send_request(
597            JsonRpcMethod::AddDeployAccountTransaction,
598            AddDeployAccountTransactionRequestRef {
599                deploy_account_transaction: deploy_account_transaction.as_ref(),
600            },
601        )
602        .await
603    }
604}
605
606impl<'de> Deserialize<'de> for JsonRpcRequest {
607    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
608    where
609        D: serde::Deserializer<'de>,
610    {
611        #[derive(Deserialize)]
612        struct RawRequest {
613            id: u64,
614            method: JsonRpcMethod,
615            params: serde_json::Value,
616        }
617
618        let error_mapper =
619            |err| serde::de::Error::custom(format!("unable to decode params: {}", err));
620
621        let raw_request = RawRequest::deserialize(deserializer)?;
622        let request_data = match raw_request.method {
623            JsonRpcMethod::GetBlockWithTxHashes => JsonRpcRequestData::GetBlockWithTxHashes(
624                serde_json::from_value::<GetBlockWithTxHashesRequest>(raw_request.params)
625                    .map_err(error_mapper)?,
626            ),
627            JsonRpcMethod::GetBlockWithTxs => JsonRpcRequestData::GetBlockWithTxs(
628                serde_json::from_value::<GetBlockWithTxsRequest>(raw_request.params)
629                    .map_err(error_mapper)?,
630            ),
631            JsonRpcMethod::GetStateUpdate => JsonRpcRequestData::GetStateUpdate(
632                serde_json::from_value::<GetStateUpdateRequest>(raw_request.params)
633                    .map_err(error_mapper)?,
634            ),
635            JsonRpcMethod::GetStorageAt => JsonRpcRequestData::GetStorageAt(
636                serde_json::from_value::<GetStorageAtRequest>(raw_request.params)
637                    .map_err(error_mapper)?,
638            ),
639            JsonRpcMethod::GetTransactionByHash => JsonRpcRequestData::GetTransactionByHash(
640                serde_json::from_value::<GetTransactionByHashRequest>(raw_request.params)
641                    .map_err(error_mapper)?,
642            ),
643            JsonRpcMethod::GetTransactionByBlockIdAndIndex => {
644                JsonRpcRequestData::GetTransactionByBlockIdAndIndex(
645                    serde_json::from_value::<GetTransactionByBlockIdAndIndexRequest>(
646                        raw_request.params,
647                    )
648                    .map_err(error_mapper)?,
649                )
650            }
651            JsonRpcMethod::GetTransactionReceipt => JsonRpcRequestData::GetTransactionReceipt(
652                serde_json::from_value::<GetTransactionReceiptRequest>(raw_request.params)
653                    .map_err(error_mapper)?,
654            ),
655            JsonRpcMethod::GetClass => JsonRpcRequestData::GetClass(
656                serde_json::from_value::<GetClassRequest>(raw_request.params)
657                    .map_err(error_mapper)?,
658            ),
659            JsonRpcMethod::GetClassHashAt => JsonRpcRequestData::GetClassHashAt(
660                serde_json::from_value::<GetClassHashAtRequest>(raw_request.params)
661                    .map_err(error_mapper)?,
662            ),
663            JsonRpcMethod::GetClassAt => JsonRpcRequestData::GetClassAt(
664                serde_json::from_value::<GetClassAtRequest>(raw_request.params)
665                    .map_err(error_mapper)?,
666            ),
667            JsonRpcMethod::GetBlockTransactionCount => {
668                JsonRpcRequestData::GetBlockTransactionCount(
669                    serde_json::from_value::<GetBlockTransactionCountRequest>(raw_request.params)
670                        .map_err(error_mapper)?,
671                )
672            }
673            JsonRpcMethod::Call => JsonRpcRequestData::Call(
674                serde_json::from_value::<CallRequest>(raw_request.params).map_err(error_mapper)?,
675            ),
676            JsonRpcMethod::EstimateFee => JsonRpcRequestData::EstimateFee(
677                serde_json::from_value::<EstimateFeeRequest>(raw_request.params)
678                    .map_err(error_mapper)?,
679            ),
680            JsonRpcMethod::EstimateMessageFee => JsonRpcRequestData::EstimateMessageFee(
681                serde_json::from_value::<EstimateMessageFeeRequest>(raw_request.params)
682                    .map_err(error_mapper)?,
683            ),
684            JsonRpcMethod::BlockNumber => JsonRpcRequestData::BlockNumber(
685                serde_json::from_value::<BlockNumberRequest>(raw_request.params)
686                    .map_err(error_mapper)?,
687            ),
688            JsonRpcMethod::BlockHashAndNumber => JsonRpcRequestData::BlockHashAndNumber(
689                serde_json::from_value::<BlockHashAndNumberRequest>(raw_request.params)
690                    .map_err(error_mapper)?,
691            ),
692            JsonRpcMethod::ChainId => JsonRpcRequestData::ChainId(
693                serde_json::from_value::<ChainIdRequest>(raw_request.params)
694                    .map_err(error_mapper)?,
695            ),
696            JsonRpcMethod::PendingTransactions => JsonRpcRequestData::PendingTransactions(
697                serde_json::from_value::<PendingTransactionsRequest>(raw_request.params)
698                    .map_err(error_mapper)?,
699            ),
700            JsonRpcMethod::Syncing => JsonRpcRequestData::Syncing(
701                serde_json::from_value::<SyncingRequest>(raw_request.params)
702                    .map_err(error_mapper)?,
703            ),
704            JsonRpcMethod::GetEvents => JsonRpcRequestData::GetEvents(
705                serde_json::from_value::<GetEventsRequest>(raw_request.params)
706                    .map_err(error_mapper)?,
707            ),
708            JsonRpcMethod::GetNonce => JsonRpcRequestData::GetNonce(
709                serde_json::from_value::<GetNonceRequest>(raw_request.params)
710                    .map_err(error_mapper)?,
711            ),
712            JsonRpcMethod::AddInvokeTransaction => JsonRpcRequestData::AddInvokeTransaction(
713                serde_json::from_value::<AddInvokeTransactionRequest>(raw_request.params)
714                    .map_err(error_mapper)?,
715            ),
716            JsonRpcMethod::AddDeclareTransaction => JsonRpcRequestData::AddDeclareTransaction(
717                serde_json::from_value::<AddDeclareTransactionRequest>(raw_request.params)
718                    .map_err(error_mapper)?,
719            ),
720            JsonRpcMethod::AddDeployAccountTransaction => {
721                JsonRpcRequestData::AddDeployAccountTransaction(
722                    serde_json::from_value::<AddDeployAccountTransactionRequest>(
723                        raw_request.params,
724                    )
725                    .map_err(error_mapper)?,
726                )
727            }
728        };
729
730        Ok(Self {
731            id: raw_request.id,
732            data: request_data,
733        })
734    }
735}
736
737impl<T> From<serde_json::Error> for JsonRpcClientError<T> {
738    fn from(value: serde_json::Error) -> Self {
739        Self::JsonError(value)
740    }
741}