starknet_rust_providers/
provider.rs

1use async_trait::async_trait;
2use auto_impl::auto_impl;
3use serde::Serialize;
4use starknet_rust_core::types::{
5    requests::*, BlockHashAndNumber, BlockId, BroadcastedDeclareTransaction,
6    BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, BroadcastedTransaction,
7    ConfirmedBlockId, ContractClass, ContractStorageKeys, DeclareTransactionResult,
8    DeployAccountTransactionResult, EventFilter, EventsPage, FeeEstimate, Felt, FunctionCall,
9    Hash256, InvokeTransactionResult, MaybePreConfirmedBlockWithReceipts,
10    MaybePreConfirmedBlockWithTxHashes, MaybePreConfirmedBlockWithTxs,
11    MaybePreConfirmedStateUpdate, MessageFeeEstimate, MessageStatus, MsgFromL1,
12    SimulatedTransaction, SimulationFlag, SimulationFlagForEstimateFee, StarknetError,
13    StorageProof, SubscriptionId, SyncStatusType, Transaction, TransactionReceiptWithBlockInfo,
14    TransactionStatus, TransactionTrace, TransactionTraceWithHash,
15};
16use std::{any::Any, error::Error, fmt::Debug};
17
18/// A generic interface for any type allowing communication with a Starknet network.
19///
20/// Historically, the only official way to access the network is through the sequencer gateway,
21/// implemented by [`SequencerGatewayProvider`](crate::sequencer::SequencerGatewayProvider), which
22/// has since been deprecated. Currently, the recommended way of accessing the network is via the
23/// JSON-RPC specification, implemented with [`JsonRpcClient`](crate::jsonrpc::JsonRpcClient).
24///
25/// The legacy [`SequencerGatewayProvider`](crate::sequencer::SequencerGatewayProvider) still
26/// implements this trait for backward compatibility reasons, but most of its methods no longer work
27/// in practice, as public sequencer servers have generally block access to most methods.
28#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
29#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
30#[auto_impl(&, Box, Arc)]
31pub trait Provider {
32    /// Returns the version of the Starknet JSON-RPC specification being used.
33    async fn spec_version(&self) -> Result<String, ProviderError>;
34
35    /// Returns the version of the Starknet being used.
36    async fn starknet_version<B>(&self, block_id: B) -> Result<String, ProviderError>
37    where
38        B: AsRef<BlockId> + Send + Sync;
39
40    /// Gets block information with transaction hashes given the block id.
41    async fn get_block_with_tx_hashes<B>(
42        &self,
43        block_id: B,
44    ) -> Result<MaybePreConfirmedBlockWithTxHashes, ProviderError>
45    where
46        B: AsRef<BlockId> + Send + Sync;
47
48    /// Gets block information with full transactions given the block id.
49    async fn get_block_with_txs<B>(
50        &self,
51        block_id: B,
52    ) -> Result<MaybePreConfirmedBlockWithTxs, ProviderError>
53    where
54        B: AsRef<BlockId> + Send + Sync;
55
56    /// Gets block information with full transactions and receipts given the block id.
57    async fn get_block_with_receipts<B>(
58        &self,
59        block_id: B,
60    ) -> Result<MaybePreConfirmedBlockWithReceipts, ProviderError>
61    where
62        B: AsRef<BlockId> + Send + Sync;
63
64    /// Gets the information about the result of executing the requested block.
65    async fn get_state_update<B>(
66        &self,
67        block_id: B,
68    ) -> Result<MaybePreConfirmedStateUpdate, ProviderError>
69    where
70        B: AsRef<BlockId> + Send + Sync;
71
72    /// Gets the value of the storage at the given address and key.
73    async fn get_storage_at<A, K, B>(
74        &self,
75        contract_address: A,
76        key: K,
77        block_id: B,
78    ) -> Result<Felt, ProviderError>
79    where
80        A: AsRef<Felt> + Send + Sync,
81        K: AsRef<Felt> + Send + Sync,
82        B: AsRef<BlockId> + Send + Sync;
83
84    /// Given an l1 tx hash, returns the associated `l1_handler` tx hashes and statuses for all L1 ->
85    /// L2 messages sent by the l1 transaction, ordered by the l1 tx sending order
86    async fn get_messages_status(
87        &self,
88        transaction_hash: Hash256,
89    ) -> Result<Vec<MessageStatus>, ProviderError>;
90
91    /// Gets the transaction status (possibly reflecting that the tx is still in the mempool, or
92    /// dropped from it).
93    async fn get_transaction_status<H>(
94        &self,
95        transaction_hash: H,
96    ) -> Result<TransactionStatus, ProviderError>
97    where
98        H: AsRef<Felt> + Send + Sync;
99
100    /// Gets the details and status of a submitted transaction.
101    async fn get_transaction_by_hash<H>(
102        &self,
103        transaction_hash: H,
104    ) -> Result<Transaction, ProviderError>
105    where
106        H: AsRef<Felt> + Send + Sync;
107
108    /// Gets the details of a transaction by a given block id and index.
109    async fn get_transaction_by_block_id_and_index<B>(
110        &self,
111        block_id: B,
112        index: u64,
113    ) -> Result<Transaction, ProviderError>
114    where
115        B: AsRef<BlockId> + Send + Sync;
116
117    /// Gets the details of a transaction by a given block number and index.
118    async fn get_transaction_receipt<H>(
119        &self,
120        transaction_hash: H,
121    ) -> Result<TransactionReceiptWithBlockInfo, ProviderError>
122    where
123        H: AsRef<Felt> + Send + Sync;
124
125    /// Gets the contract class definition in the given block associated with the given hash.
126    async fn get_class<B, H>(
127        &self,
128        block_id: B,
129        class_hash: H,
130    ) -> Result<ContractClass, ProviderError>
131    where
132        B: AsRef<BlockId> + Send + Sync,
133        H: AsRef<Felt> + Send + Sync;
134
135    /// Gets the contract class hash in the given block for the contract deployed at the given
136    /// address.
137    async fn get_class_hash_at<B, A>(
138        &self,
139        block_id: B,
140        contract_address: A,
141    ) -> Result<Felt, ProviderError>
142    where
143        B: AsRef<BlockId> + Send + Sync,
144        A: AsRef<Felt> + Send + Sync;
145
146    /// Gets the contract class definition in the given block at the given address.
147    async fn get_class_at<B, A>(
148        &self,
149        block_id: B,
150        contract_address: A,
151    ) -> Result<ContractClass, ProviderError>
152    where
153        B: AsRef<BlockId> + Send + Sync,
154        A: AsRef<Felt> + Send + Sync;
155
156    /// Gets the number of transactions in a block given a block id.
157    async fn get_block_transaction_count<B>(&self, block_id: B) -> Result<u64, ProviderError>
158    where
159        B: AsRef<BlockId> + Send + Sync;
160
161    /// Calls a starknet function without creating a Starknet transaction.
162    async fn call<R, B>(&self, request: R, block_id: B) -> Result<Vec<Felt>, ProviderError>
163    where
164        R: AsRef<FunctionCall> + Send + Sync,
165        B: AsRef<BlockId> + Send + Sync;
166
167    /// Estimates the fee for a given Starknet transaction.
168    async fn estimate_fee<R, S, B>(
169        &self,
170        request: R,
171        simulation_flags: S,
172        block_id: B,
173    ) -> Result<Vec<FeeEstimate>, ProviderError>
174    where
175        R: AsRef<[BroadcastedTransaction]> + Send + Sync,
176        S: AsRef<[SimulationFlagForEstimateFee]> + Send + Sync,
177        B: AsRef<BlockId> + Send + Sync;
178
179    /// Estimates the fee for sending an L1-to-L2 message.
180    async fn estimate_message_fee<M, B>(
181        &self,
182        message: M,
183        block_id: B,
184    ) -> Result<MessageFeeEstimate, ProviderError>
185    where
186        M: AsRef<MsgFromL1> + Send + Sync,
187        B: AsRef<BlockId> + Send + Sync;
188
189    /// Gets the most recent accepted block number.
190    async fn block_number(&self) -> Result<u64, ProviderError>;
191
192    /// Gets the most recent accepted block hash and number.
193    async fn block_hash_and_number(&self) -> Result<BlockHashAndNumber, ProviderError>;
194
195    /// Returns the currently configured Starknet chain id.
196    async fn chain_id(&self) -> Result<Felt, ProviderError>;
197
198    /// Returns an object about the sync status, or false if the node is not synching.
199    async fn syncing(&self) -> Result<SyncStatusType, ProviderError>;
200
201    /// Returns all events matching the given filter.
202    async fn get_events(
203        &self,
204        filter: EventFilter,
205        continuation_token: Option<String>,
206        chunk_size: u64,
207    ) -> Result<EventsPage, ProviderError>;
208
209    /// Gets the nonce associated with the given address in the given block.
210    async fn get_nonce<B, A>(
211        &self,
212        block_id: B,
213        contract_address: A,
214    ) -> Result<Felt, ProviderError>
215    where
216        B: AsRef<BlockId> + Send + Sync,
217        A: AsRef<Felt> + Send + Sync;
218
219    /// Get merkle paths in one of the state tries: global state, classes, individual contract.
220    /// A single request can query for any mix of the three types of storage proofs (classes,
221    /// contracts, and storage).
222    async fn get_storage_proof<B, H, A, K>(
223        &self,
224        block_id: B,
225        class_hashes: H,
226        contract_addresses: A,
227        contracts_storage_keys: K,
228    ) -> Result<StorageProof, ProviderError>
229    where
230        B: AsRef<ConfirmedBlockId> + Send + Sync,
231        H: AsRef<[Felt]> + Send + Sync,
232        A: AsRef<[Felt]> + Send + Sync,
233        K: AsRef<[ContractStorageKeys]> + Send + Sync;
234
235    /// Submits a new transaction to be added to the chain.
236    async fn add_invoke_transaction<I>(
237        &self,
238        invoke_transaction: I,
239    ) -> Result<InvokeTransactionResult, ProviderError>
240    where
241        I: AsRef<BroadcastedInvokeTransaction> + Send + Sync;
242
243    /// Submits a new transaction to be added to the chain.
244    async fn add_declare_transaction<D>(
245        &self,
246        declare_transaction: D,
247    ) -> Result<DeclareTransactionResult, ProviderError>
248    where
249        D: AsRef<BroadcastedDeclareTransaction> + Send + Sync;
250
251    /// Submits a new deploy account transaction.
252    async fn add_deploy_account_transaction<D>(
253        &self,
254        deploy_account_transaction: D,
255    ) -> Result<DeployAccountTransactionResult, ProviderError>
256    where
257        D: AsRef<BroadcastedDeployAccountTransaction> + Send + Sync;
258
259    /// For a given executed transaction, returns the trace of its execution, including internal
260    /// calls.
261    async fn trace_transaction<H>(
262        &self,
263        transaction_hash: H,
264    ) -> Result<TransactionTrace, ProviderError>
265    where
266        H: AsRef<Felt> + Send + Sync;
267
268    /// Simulates a given sequence of transactions on the requested state, and generate the
269    /// execution traces. Note that some of the transactions may revert, in which case no error is
270    /// thrown, but revert details can be seen on the returned trace object.
271    ///
272    /// Note that some of the transactions may revert, this will be reflected by the `revert_error`
273    /// property in the trace. Other types of failures (e.g. unexpected error or failure in the
274    /// validation phase) will result in `TRANSACTION_EXECUTION_ERROR`.
275    async fn simulate_transactions<B, T, S>(
276        &self,
277        block_id: B,
278        transactions: T,
279        simulation_flags: S,
280    ) -> Result<Vec<SimulatedTransaction>, ProviderError>
281    where
282        B: AsRef<BlockId> + Send + Sync,
283        T: AsRef<[BroadcastedTransaction]> + Send + Sync,
284        S: AsRef<[SimulationFlag]> + Send + Sync;
285
286    /// Retrieves traces for all transactions in the given block.
287    async fn trace_block_transactions<B>(
288        &self,
289        block_id: B,
290    ) -> Result<Vec<TransactionTraceWithHash>, ProviderError>
291    where
292        B: AsRef<ConfirmedBlockId> + Send + Sync;
293
294    /// Sends multiple requests in parallel. The function call fails if any of the requests fails.
295    /// Implementations must guarantee that responses follow the exact order as the requests.
296    async fn batch_requests<R>(
297        &self,
298        requests: R,
299    ) -> Result<Vec<ProviderResponseData>, ProviderError>
300    where
301        R: AsRef<[ProviderRequestData]> + Send + Sync;
302
303    /// Same as [`estimate_fee`](fn.estimate_fee), but only with one estimate.
304    async fn estimate_fee_single<R, S, B>(
305        &self,
306        request: R,
307        simulation_flags: S,
308        block_id: B,
309    ) -> Result<FeeEstimate, ProviderError>
310    where
311        R: AsRef<BroadcastedTransaction> + Send + Sync,
312        S: AsRef<[SimulationFlagForEstimateFee]> + Send + Sync,
313        B: AsRef<BlockId> + Send + Sync,
314    {
315        let mut result = self
316            .estimate_fee([request.as_ref().to_owned()], simulation_flags, block_id)
317            .await?;
318
319        if result.len() == 1 {
320            // Unwrapping here is safe becuase we already checked length
321            Ok(result.pop().unwrap())
322        } else {
323            Err(ProviderError::ArrayLengthMismatch)
324        }
325    }
326
327    /// Same as [`simulate_transactions`](fn.simulate_transactions), but only with one simulation.
328    async fn simulate_transaction<B, T, S>(
329        &self,
330        block_id: B,
331        transaction: T,
332        simulation_flags: S,
333    ) -> Result<SimulatedTransaction, ProviderError>
334    where
335        B: AsRef<BlockId> + Send + Sync,
336        T: AsRef<BroadcastedTransaction> + Send + Sync,
337        S: AsRef<[SimulationFlag]> + Send + Sync,
338    {
339        let mut result = self
340            .simulate_transactions(
341                block_id,
342                [transaction.as_ref().to_owned()],
343                simulation_flags,
344            )
345            .await?;
346
347        if result.len() == 1 {
348            // Unwrapping here is safe becuase we already checked length
349            Ok(result.pop().unwrap())
350        } else {
351            Err(ProviderError::ArrayLengthMismatch)
352        }
353    }
354}
355
356/// Trait for implementation-specific error type. These errors are irrelevant in most cases,
357/// assuming that users typically care more about the specifics of RPC errors instead of the
358/// underlying transport. Therefore, it makes little sense to bloat [`ProviderError`] with a generic
359/// parameter just for these errors. Instead, they're erased to this trait object.
360///
361/// This trait is used instead of a plain [`std::error::Error`] to allow downcasting, in case access
362/// to the specific error type is indeed desired. This is achieved with the `as_any()` method.
363pub trait ProviderImplError: Error + Debug + Send + Sync {
364    /// Returns a reference to the error as a trait object, allowing downcasting to the specific
365    /// error type.
366    fn as_any(&self) -> &dyn Any;
367}
368
369/// Errors using any [`Provider`] implementation. This type is deliberately not made generic such
370/// that:
371///
372/// - the [`Provider`] trait itself can be boxed;
373/// - error handling is easier.
374///
375/// As a downside, the [`Other`](ProviderError::Other) variant contains a boxed implementation-
376/// specific error. It's generally expected that users of [`Provider`] would not need to care about
377/// these errors, but in the case where they do, it's slightly harder to access than if generics are
378/// used instead.
379#[derive(Debug, thiserror::Error)]
380pub enum ProviderError {
381    /// A Starknet-related error, usually regarding the state or transaction.
382    #[error(transparent)]
383    StarknetError(StarknetError),
384    /// The request fails as the client is rate-limited.
385    #[error("Request rate limited")]
386    RateLimited,
387    /// When estimating fees for or simulating a single transaction, the server unexpectedly returns
388    /// data for zero or more than one transactions.
389    #[error("Array length mismatch")]
390    ArrayLengthMismatch,
391    /// Boxed implementation-specific errors.
392    #[error("{0}")]
393    Other(Box<dyn ProviderImplError>),
394}
395
396/// Typed request data for [`Provider`] requests.
397#[derive(Debug, Clone, Serialize)]
398#[serde(untagged)]
399pub enum ProviderRequestData {
400    /// Request data for `starknet_specVersion`.
401    SpecVersion(SpecVersionRequest),
402    /// Request data for `starknet_getBlockWithTxHashes`.
403    GetBlockWithTxHashes(GetBlockWithTxHashesRequest),
404    /// Request data for `starknet_getBlockWithTxs`.
405    GetBlockWithTxs(GetBlockWithTxsRequest),
406    /// Request data for `starknet_getBlockWithReceipts`.
407    GetBlockWithReceipts(GetBlockWithReceiptsRequest),
408    /// Request data for `starknet_getStateUpdate`.
409    GetStateUpdate(GetStateUpdateRequest),
410    /// Request data for `starknet_getStorageAt`.
411    GetStorageAt(GetStorageAtRequest),
412    /// Request data for `starknet_getMessagesStatus`.
413    GetMessagesStatus(GetMessagesStatusRequest),
414    /// Request data for `starknet_getTransactionStatus`.
415    GetTransactionStatus(GetTransactionStatusRequest),
416    /// Request data for `starknet_getTransactionByHash`.
417    GetTransactionByHash(GetTransactionByHashRequest),
418    /// Request data for `starknet_getTransactionByBlockIdAndIndex`.
419    GetTransactionByBlockIdAndIndex(GetTransactionByBlockIdAndIndexRequest),
420    /// Request data for `starknet_getTransactionReceipt`.
421    GetTransactionReceipt(GetTransactionReceiptRequest),
422    /// Request data for `starknet_getClass`.
423    GetClass(GetClassRequest),
424    /// Request data for `starknet_getClassHashAt`.
425    GetClassHashAt(GetClassHashAtRequest),
426    /// Request data for `starknet_getClassAt`.
427    GetClassAt(GetClassAtRequest),
428    /// Request data for `starknet_getBlockTransactionCount`.
429    GetBlockTransactionCount(GetBlockTransactionCountRequest),
430    /// Request data for `starknet_call`.
431    Call(CallRequest),
432    /// Request data for `starknet_estimateFee`.
433    EstimateFee(EstimateFeeRequest),
434    /// Request data for `starknet_estimateMessageFee`.
435    EstimateMessageFee(EstimateMessageFeeRequest),
436    /// Request data for `starknet_blockNumber`.
437    BlockNumber(BlockNumberRequest),
438    /// Request data for `starknet_blockHashAndNumber`.
439    BlockHashAndNumber(BlockHashAndNumberRequest),
440    /// Request data for `starknet_chainId`.
441    ChainId(ChainIdRequest),
442    /// Request data for `starknet_syncing`.
443    Syncing(SyncingRequest),
444    /// Request data for `starknet_getEvents`.
445    GetEvents(GetEventsRequest),
446    /// Request data for `starknet_getNonce`.
447    GetNonce(GetNonceRequest),
448    /// Request data for `starknet_getStorageProof`.
449    GetStorageProof(GetStorageProofRequest),
450    /// Request data for `starknet_addInvokeTransaction`.
451    AddInvokeTransaction(AddInvokeTransactionRequest),
452    /// Request data for `starknet_addDeclareTransaction`.
453    AddDeclareTransaction(AddDeclareTransactionRequest),
454    /// Request data for `starknet_addDeployAccountTransaction`.
455    AddDeployAccountTransaction(AddDeployAccountTransactionRequest),
456    /// Request data for `starknet_traceTransaction`.
457    TraceTransaction(TraceTransactionRequest),
458    /// Request data for `starknet_simulateTransactions`.
459    SimulateTransactions(SimulateTransactionsRequest),
460    /// Request data for `starknet_traceBlockTransactions`.
461    TraceBlockTransactions(TraceBlockTransactionsRequest),
462    /// Request data for `starknet_subscribeNewHeads`.
463    SubscribeNewHeads(SubscribeNewHeadsRequest),
464    /// Request data for `starknet_subscribeEvents`.
465    SubscribeEvents(SubscribeEventsRequest),
466    /// Request data for `starknet_subscribeTransactionStatus`.
467    SubscribeTransactionStatus(SubscribeTransactionStatusRequest),
468    /// Request data for `starknet_subscribeNewTransactionReceipts`.
469    SubscribeNewTransactionReceipts(SubscribeNewTransactionReceiptsRequest),
470    /// Request data for `starknet_subscribeNewTransactions`.
471    SubscribeNewTransactions(SubscribeNewTransactionsRequest),
472    /// Request data for `starknet_unsubscribe`.
473    Unsubscribe(UnsubscribeRequest),
474}
475
476/// Typed response data for [`Provider`] responses.
477#[allow(clippy::large_enum_variant)]
478#[derive(Debug, Clone)]
479pub enum ProviderResponseData {
480    /// Response data for `starknet_specVersion`.
481    SpecVersion(String),
482    /// Response data for `starknet_getBlockWithTxHashes`.
483    GetBlockWithTxHashes(MaybePreConfirmedBlockWithTxHashes),
484    /// Response data for `starknet_getBlockWithTxs`.
485    GetBlockWithTxs(MaybePreConfirmedBlockWithTxs),
486    /// Response data for `starknet_getBlockWithReceipts`.
487    GetBlockWithReceipts(MaybePreConfirmedBlockWithReceipts),
488    /// Response data for `starknet_getStateUpdate`.
489    GetStateUpdate(MaybePreConfirmedStateUpdate),
490    /// Response data for `starknet_getStorageAt`.
491    GetStorageAt(Felt),
492    /// Response data for `starknet_getMessagesStatus`.
493    GetMessagesStatus(Vec<MessageStatus>),
494    /// Response data for `starknet_getTransactionStatus`.
495    GetTransactionStatus(TransactionStatus),
496    /// Response data for `starknet_getTransactionByHash`.
497    GetTransactionByHash(Transaction),
498    /// Response data for `starknet_getTransactionByBlockIdAndIndex`.
499    GetTransactionByBlockIdAndIndex(Transaction),
500    /// Response data for `starknet_getTransactionReceipt`.
501    GetTransactionReceipt(TransactionReceiptWithBlockInfo),
502    /// Response data for `starknet_getClass`.
503    GetClass(ContractClass),
504    /// Response data for `starknet_getClassHashAt`.
505    GetClassHashAt(Felt),
506    /// Response data for `starknet_getClassAt`.
507    GetClassAt(ContractClass),
508    /// Response data for `starknet_getBlockTransactionCount`.
509    GetBlockTransactionCount(u64),
510    /// Response data for `starknet_call`.
511    Call(Vec<Felt>),
512    /// Response data for `starknet_estimateFee`.
513    EstimateFee(Vec<FeeEstimate>),
514    /// Response data for `starknet_estimateMessageFee`.
515    EstimateMessageFee(FeeEstimate),
516    /// Response data for `starknet_blockNumber`.
517    BlockNumber(u64),
518    /// Response data for `starknet_blockHashAndNumber`.
519    BlockHashAndNumber(BlockHashAndNumber),
520    /// Response data for `starknet_chainId`.
521    ChainId(Felt),
522    /// Response data for `starknet_syncing`.
523    Syncing(SyncStatusType),
524    /// Response data for `starknet_getEvents`.
525    GetEvents(EventsPage),
526    /// Response data for `starknet_getNonce`.
527    GetNonce(Felt),
528    /// Response data for `starknet_getStorageProof`.
529    GetStorageProof(StorageProof),
530    /// Response data for `starknet_addInvokeTransaction`.
531    AddInvokeTransaction(InvokeTransactionResult),
532    /// Response data for `starknet_addDeclareTransaction`.
533    AddDeclareTransaction(DeclareTransactionResult),
534    /// Response data for `starknet_addDeployAccountTransaction`.
535    AddDeployAccountTransaction(DeployAccountTransactionResult),
536    /// Response data for `starknet_traceTransaction`.
537    TraceTransaction(TransactionTrace),
538    /// Response data for `starknet_simulateTransactions`.
539    SimulateTransactions(Vec<SimulatedTransaction>),
540    /// Response data for `starknet_traceBlockTransactions`.
541    TraceBlockTransactions(Vec<TransactionTraceWithHash>),
542    /// Response data for `starknet_subscribeNewHeads`.
543    SubscribeNewHeads(SubscriptionId),
544    /// Response data for `starknet_subscribeEvents`.
545    SubscribeEvents(SubscriptionId),
546    /// Response data for `starknet_subscribeTransactionStatus`.
547    SubscribeTransactionStatus(SubscriptionId),
548    /// Response data for `starknet_subscribeNewTransactionReceipts`.
549    SubscribeNewTransactionReceipts(SubscriptionId),
550    /// Response data for `starknet_subscribeNewTransactions`.
551    SubscribeNewTransactions(SubscriptionId),
552    /// Response data for `starknet_unsubscribe`.
553    Unsubscribe(bool),
554}
555
556/// Typed data for stream updates.
557#[allow(clippy::enum_variant_names)]
558#[derive(Debug, Clone, Serialize)]
559#[serde(untagged)]
560pub enum StreamUpdateData {
561    /// Stream data for `starknet_subscriptionNewHeads`.
562    SubscriptionNewHeads(SubscriptionNewHeadsRequest),
563    /// Stream data for `starknet_subscriptionEvents`.
564    SubscriptionEvents(SubscriptionEventsRequest),
565    /// Stream data for `starknet_subscriptionTransactionStatus`.
566    SubscriptionTransactionStatus(SubscriptionTransactionStatusRequest),
567    /// Stream data for `starknet_subscriptionNewTransactionReceipts`.
568    SubscriptionNewTransactionReceipts(SubscriptionNewTransactionReceiptsRequest),
569    /// Stream data for `starknet_subscriptionNewTransaction`.
570    SubscriptionNewTransaction(SubscriptionNewTransactionRequest),
571    /// Stream data for `starknet_subscriptionReorg`.
572    SubscriptionReorg(SubscriptionReorgRequest),
573}
574
575impl StreamUpdateData {
576    /// Gets a reference to the subscription ID the update corresponds to.
577    pub fn subscription_id(&self) -> &SubscriptionId {
578        match self {
579            Self::SubscriptionNewHeads(update) => &update.subscription_id,
580            Self::SubscriptionEvents(update) => &update.subscription_id,
581            Self::SubscriptionTransactionStatus(update) => &update.subscription_id,
582            Self::SubscriptionNewTransactionReceipts(update) => &update.subscription_id,
583            Self::SubscriptionNewTransaction(update) => &update.subscription_id,
584            Self::SubscriptionReorg(update) => &update.subscription_id,
585        }
586    }
587}