starknet-providers 0.16.0

Provider implementations for the starknet crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use async_trait::async_trait;
use auto_impl::auto_impl;
use serde::Serialize;
use starknet_core::types::{
    requests::*, BlockHashAndNumber, BlockId, BroadcastedDeclareTransaction,
    BroadcastedDeployAccountTransaction, BroadcastedInvokeTransaction, BroadcastedTransaction,
    ConfirmedBlockId, ContractClass, ContractStorageKeys, DeclareTransactionResult,
    DeployAccountTransactionResult, EventFilter, EventsPage, FeeEstimate, Felt, FunctionCall,
    Hash256, InvokeTransactionResult, MaybePreConfirmedBlockWithReceipts,
    MaybePreConfirmedBlockWithTxHashes, MaybePreConfirmedBlockWithTxs,
    MaybePreConfirmedStateUpdate, MessageFeeEstimate, MessageStatus, MsgFromL1,
    SimulatedTransaction, SimulationFlag, SimulationFlagForEstimateFee, StarknetError,
    StorageProof, SubscriptionId, SyncStatusType, Transaction, TransactionReceiptWithBlockInfo,
    TransactionStatus, TransactionTrace, TransactionTraceWithHash,
};
use std::{any::Any, error::Error, fmt::Debug};

/// A generic interface for any type allowing communication with a Starknet network.
///
/// Historically, the only official way to access the network is through the sequencer gateway,
/// implemented by [`SequencerGatewayProvider`](crate::sequencer::SequencerGatewayProvider), which
/// has since been deprecated. Currently, the recommended way of accessing the network is via the
/// JSON-RPC specification, implemented with [`JsonRpcClient`](crate::jsonrpc::JsonRpcClient).
///
/// The legacy [`SequencerGatewayProvider`](crate::sequencer::SequencerGatewayProvider) still
/// implements this trait for backward compatibility reasons, but most of its methods no longer work
/// in practice, as public sequencer servers have generally block access to most methods.
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[auto_impl(&, Box, Arc)]
pub trait Provider {
    /// Returns the version of the Starknet JSON-RPC specification being used.
    async fn spec_version(&self) -> Result<String, ProviderError>;

    /// Gets block information with transaction hashes given the block id.
    async fn get_block_with_tx_hashes<B>(
        &self,
        block_id: B,
    ) -> Result<MaybePreConfirmedBlockWithTxHashes, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync;

    /// Gets block information with full transactions given the block id.
    async fn get_block_with_txs<B>(
        &self,
        block_id: B,
    ) -> Result<MaybePreConfirmedBlockWithTxs, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync;

    /// Gets block information with full transactions and receipts given the block id.
    async fn get_block_with_receipts<B>(
        &self,
        block_id: B,
    ) -> Result<MaybePreConfirmedBlockWithReceipts, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync;

    /// Gets the information about the result of executing the requested block.
    async fn get_state_update<B>(
        &self,
        block_id: B,
    ) -> Result<MaybePreConfirmedStateUpdate, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync;

    /// Gets the value of the storage at the given address and key.
    async fn get_storage_at<A, K, B>(
        &self,
        contract_address: A,
        key: K,
        block_id: B,
    ) -> Result<Felt, ProviderError>
    where
        A: AsRef<Felt> + Send + Sync,
        K: AsRef<Felt> + Send + Sync,
        B: AsRef<BlockId> + Send + Sync;

    /// Given an l1 tx hash, returns the associated `l1_handler` tx hashes and statuses for all L1 ->
    /// L2 messages sent by the l1 transaction, ordered by the l1 tx sending order
    async fn get_messages_status(
        &self,
        transaction_hash: Hash256,
    ) -> Result<Vec<MessageStatus>, ProviderError>;

    /// Gets the transaction status (possibly reflecting that the tx is still in the mempool, or
    /// dropped from it).
    async fn get_transaction_status<H>(
        &self,
        transaction_hash: H,
    ) -> Result<TransactionStatus, ProviderError>
    where
        H: AsRef<Felt> + Send + Sync;

    /// Gets the details and status of a submitted transaction.
    async fn get_transaction_by_hash<H>(
        &self,
        transaction_hash: H,
    ) -> Result<Transaction, ProviderError>
    where
        H: AsRef<Felt> + Send + Sync;

    /// Gets the details of a transaction by a given block id and index.
    async fn get_transaction_by_block_id_and_index<B>(
        &self,
        block_id: B,
        index: u64,
    ) -> Result<Transaction, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync;

    /// Gets the details of a transaction by a given block number and index.
    async fn get_transaction_receipt<H>(
        &self,
        transaction_hash: H,
    ) -> Result<TransactionReceiptWithBlockInfo, ProviderError>
    where
        H: AsRef<Felt> + Send + Sync;

    /// Gets the contract class definition in the given block associated with the given hash.
    async fn get_class<B, H>(
        &self,
        block_id: B,
        class_hash: H,
    ) -> Result<ContractClass, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync,
        H: AsRef<Felt> + Send + Sync;

    /// Gets the contract class hash in the given block for the contract deployed at the given
    /// address.
    async fn get_class_hash_at<B, A>(
        &self,
        block_id: B,
        contract_address: A,
    ) -> Result<Felt, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync,
        A: AsRef<Felt> + Send + Sync;

    /// Gets the contract class definition in the given block at the given address.
    async fn get_class_at<B, A>(
        &self,
        block_id: B,
        contract_address: A,
    ) -> Result<ContractClass, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync,
        A: AsRef<Felt> + Send + Sync;

    /// Gets the number of transactions in a block given a block id.
    async fn get_block_transaction_count<B>(&self, block_id: B) -> Result<u64, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync;

    /// Calls a starknet function without creating a Starknet transaction.
    async fn call<R, B>(&self, request: R, block_id: B) -> Result<Vec<Felt>, ProviderError>
    where
        R: AsRef<FunctionCall> + Send + Sync,
        B: AsRef<BlockId> + Send + Sync;

    /// Estimates the fee for a given Starknet transaction.
    async fn estimate_fee<R, S, B>(
        &self,
        request: R,
        simulation_flags: S,
        block_id: B,
    ) -> Result<Vec<FeeEstimate>, ProviderError>
    where
        R: AsRef<[BroadcastedTransaction]> + Send + Sync,
        S: AsRef<[SimulationFlagForEstimateFee]> + Send + Sync,
        B: AsRef<BlockId> + Send + Sync;

    /// Estimates the fee for sending an L1-to-L2 message.
    async fn estimate_message_fee<M, B>(
        &self,
        message: M,
        block_id: B,
    ) -> Result<MessageFeeEstimate, ProviderError>
    where
        M: AsRef<MsgFromL1> + Send + Sync,
        B: AsRef<BlockId> + Send + Sync;

    /// Gets the most recent accepted block number.
    async fn block_number(&self) -> Result<u64, ProviderError>;

    /// Gets the most recent accepted block hash and number.
    async fn block_hash_and_number(&self) -> Result<BlockHashAndNumber, ProviderError>;

    /// Returns the currently configured Starknet chain id.
    async fn chain_id(&self) -> Result<Felt, ProviderError>;

    /// Returns an object about the sync status, or false if the node is not synching.
    async fn syncing(&self) -> Result<SyncStatusType, ProviderError>;

    /// Returns all events matching the given filter.
    async fn get_events(
        &self,
        filter: EventFilter,
        continuation_token: Option<String>,
        chunk_size: u64,
    ) -> Result<EventsPage, ProviderError>;

    /// Gets the nonce associated with the given address in the given block.
    async fn get_nonce<B, A>(
        &self,
        block_id: B,
        contract_address: A,
    ) -> Result<Felt, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync,
        A: AsRef<Felt> + Send + Sync;

    /// Get merkle paths in one of the state tries: global state, classes, individual contract.
    /// A single request can query for any mix of the three types of storage proofs (classes,
    /// contracts, and storage).
    async fn get_storage_proof<B, H, A, K>(
        &self,
        block_id: B,
        class_hashes: H,
        contract_addresses: A,
        contracts_storage_keys: K,
    ) -> Result<StorageProof, ProviderError>
    where
        B: AsRef<ConfirmedBlockId> + Send + Sync,
        H: AsRef<[Felt]> + Send + Sync,
        A: AsRef<[Felt]> + Send + Sync,
        K: AsRef<[ContractStorageKeys]> + Send + Sync;

    /// Submits a new transaction to be added to the chain.
    async fn add_invoke_transaction<I>(
        &self,
        invoke_transaction: I,
    ) -> Result<InvokeTransactionResult, ProviderError>
    where
        I: AsRef<BroadcastedInvokeTransaction> + Send + Sync;

    /// Submits a new transaction to be added to the chain.
    async fn add_declare_transaction<D>(
        &self,
        declare_transaction: D,
    ) -> Result<DeclareTransactionResult, ProviderError>
    where
        D: AsRef<BroadcastedDeclareTransaction> + Send + Sync;

    /// Submits a new deploy account transaction.
    async fn add_deploy_account_transaction<D>(
        &self,
        deploy_account_transaction: D,
    ) -> Result<DeployAccountTransactionResult, ProviderError>
    where
        D: AsRef<BroadcastedDeployAccountTransaction> + Send + Sync;

    /// For a given executed transaction, returns the trace of its execution, including internal
    /// calls.
    async fn trace_transaction<H>(
        &self,
        transaction_hash: H,
    ) -> Result<TransactionTrace, ProviderError>
    where
        H: AsRef<Felt> + Send + Sync;

    /// Simulates a given sequence of transactions on the requested state, and generate the
    /// execution traces. Note that some of the transactions may revert, in which case no error is
    /// thrown, but revert details can be seen on the returned trace object.
    ///
    /// Note that some of the transactions may revert, this will be reflected by the `revert_error`
    /// property in the trace. Other types of failures (e.g. unexpected error or failure in the
    /// validation phase) will result in `TRANSACTION_EXECUTION_ERROR`.
    async fn simulate_transactions<B, T, S>(
        &self,
        block_id: B,
        transactions: T,
        simulation_flags: S,
    ) -> Result<Vec<SimulatedTransaction>, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync,
        T: AsRef<[BroadcastedTransaction]> + Send + Sync,
        S: AsRef<[SimulationFlag]> + Send + Sync;

    /// Retrieves traces for all transactions in the given block.
    async fn trace_block_transactions<B>(
        &self,
        block_id: B,
    ) -> Result<Vec<TransactionTraceWithHash>, ProviderError>
    where
        B: AsRef<ConfirmedBlockId> + Send + Sync;

    /// Sends multiple requests in parallel. The function call fails if any of the requests fails.
    /// Implementations must guarantee that responses follow the exact order as the requests.
    async fn batch_requests<R>(
        &self,
        requests: R,
    ) -> Result<Vec<ProviderResponseData>, ProviderError>
    where
        R: AsRef<[ProviderRequestData]> + Send + Sync;

    /// Same as [`estimate_fee`](fn.estimate_fee), but only with one estimate.
    async fn estimate_fee_single<R, S, B>(
        &self,
        request: R,
        simulation_flags: S,
        block_id: B,
    ) -> Result<FeeEstimate, ProviderError>
    where
        R: AsRef<BroadcastedTransaction> + Send + Sync,
        S: AsRef<[SimulationFlagForEstimateFee]> + Send + Sync,
        B: AsRef<BlockId> + Send + Sync,
    {
        let mut result = self
            .estimate_fee([request.as_ref().to_owned()], simulation_flags, block_id)
            .await?;

        if result.len() == 1 {
            // Unwrapping here is safe becuase we already checked length
            Ok(result.pop().unwrap())
        } else {
            Err(ProviderError::ArrayLengthMismatch)
        }
    }

    /// Same as [`simulate_transactions`](fn.simulate_transactions), but only with one simulation.
    async fn simulate_transaction<B, T, S>(
        &self,
        block_id: B,
        transaction: T,
        simulation_flags: S,
    ) -> Result<SimulatedTransaction, ProviderError>
    where
        B: AsRef<BlockId> + Send + Sync,
        T: AsRef<BroadcastedTransaction> + Send + Sync,
        S: AsRef<[SimulationFlag]> + Send + Sync,
    {
        let mut result = self
            .simulate_transactions(
                block_id,
                [transaction.as_ref().to_owned()],
                simulation_flags,
            )
            .await?;

        if result.len() == 1 {
            // Unwrapping here is safe becuase we already checked length
            Ok(result.pop().unwrap())
        } else {
            Err(ProviderError::ArrayLengthMismatch)
        }
    }
}

/// Trait for implementation-specific error type. These errors are irrelevant in most cases,
/// assuming that users typically care more about the specifics of RPC errors instead of the
/// underlying transport. Therefore, it makes little sense to bloat [`ProviderError`] with a generic
/// parameter just for these errors. Instead, they're erased to this trait object.
///
/// This trait is used instead of a plain [`std::error::Error`] to allow downcasting, in case access
/// to the specific error type is indeed desired. This is achieved with the `as_any()` method.
pub trait ProviderImplError: Error + Debug + Send + Sync {
    /// Returns a reference to the error as a trait object, allowing downcasting to the specific
    /// error type.
    fn as_any(&self) -> &dyn Any;
}

/// Errors using any [`Provider`] implementation. This type is deliberately not made generic such
/// that:
///
/// - the [`Provider`] trait itself can be boxed;
/// - error handling is easier.
///
/// As a downside, the [`Other`](ProviderError::Other) variant contains a boxed implementation-
/// specific error. It's generally expected that users of [`Provider`] would not need to care about
/// these errors, but in the case where they do, it's slightly harder to access than if generics are
/// used instead.
#[derive(Debug, thiserror::Error)]
pub enum ProviderError {
    /// A Starknet-related error, usually regarding the state or transaction.
    #[error(transparent)]
    StarknetError(StarknetError),
    /// The request fails as the client is rate-limited.
    #[error("Request rate limited")]
    RateLimited,
    /// When estimating fees for or simulating a single transaction, the server unexpectedly returns
    /// data for zero or more than one transactions.
    #[error("Array length mismatch")]
    ArrayLengthMismatch,
    /// Boxed implementation-specific errors.
    #[error("{0}")]
    Other(Box<dyn ProviderImplError>),
}

/// Typed request data for [`Provider`] requests.
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum ProviderRequestData {
    /// Request data for `starknet_specVersion`.
    SpecVersion(SpecVersionRequest),
    /// Request data for `starknet_getBlockWithTxHashes`.
    GetBlockWithTxHashes(GetBlockWithTxHashesRequest),
    /// Request data for `starknet_getBlockWithTxs`.
    GetBlockWithTxs(GetBlockWithTxsRequest),
    /// Request data for `starknet_getBlockWithReceipts`.
    GetBlockWithReceipts(GetBlockWithReceiptsRequest),
    /// Request data for `starknet_getStateUpdate`.
    GetStateUpdate(GetStateUpdateRequest),
    /// Request data for `starknet_getStorageAt`.
    GetStorageAt(GetStorageAtRequest),
    /// Request data for `starknet_getMessagesStatus`.
    GetMessagesStatus(GetMessagesStatusRequest),
    /// Request data for `starknet_getTransactionStatus`.
    GetTransactionStatus(GetTransactionStatusRequest),
    /// Request data for `starknet_getTransactionByHash`.
    GetTransactionByHash(GetTransactionByHashRequest),
    /// Request data for `starknet_getTransactionByBlockIdAndIndex`.
    GetTransactionByBlockIdAndIndex(GetTransactionByBlockIdAndIndexRequest),
    /// Request data for `starknet_getTransactionReceipt`.
    GetTransactionReceipt(GetTransactionReceiptRequest),
    /// Request data for `starknet_getClass`.
    GetClass(GetClassRequest),
    /// Request data for `starknet_getClassHashAt`.
    GetClassHashAt(GetClassHashAtRequest),
    /// Request data for `starknet_getClassAt`.
    GetClassAt(GetClassAtRequest),
    /// Request data for `starknet_getBlockTransactionCount`.
    GetBlockTransactionCount(GetBlockTransactionCountRequest),
    /// Request data for `starknet_call`.
    Call(CallRequest),
    /// Request data for `starknet_estimateFee`.
    EstimateFee(EstimateFeeRequest),
    /// Request data for `starknet_estimateMessageFee`.
    EstimateMessageFee(EstimateMessageFeeRequest),
    /// Request data for `starknet_blockNumber`.
    BlockNumber(BlockNumberRequest),
    /// Request data for `starknet_blockHashAndNumber`.
    BlockHashAndNumber(BlockHashAndNumberRequest),
    /// Request data for `starknet_chainId`.
    ChainId(ChainIdRequest),
    /// Request data for `starknet_syncing`.
    Syncing(SyncingRequest),
    /// Request data for `starknet_getEvents`.
    GetEvents(GetEventsRequest),
    /// Request data for `starknet_getNonce`.
    GetNonce(GetNonceRequest),
    /// Request data for `starknet_getStorageProof`.
    GetStorageProof(GetStorageProofRequest),
    /// Request data for `starknet_addInvokeTransaction`.
    AddInvokeTransaction(AddInvokeTransactionRequest),
    /// Request data for `starknet_addDeclareTransaction`.
    AddDeclareTransaction(AddDeclareTransactionRequest),
    /// Request data for `starknet_addDeployAccountTransaction`.
    AddDeployAccountTransaction(AddDeployAccountTransactionRequest),
    /// Request data for `starknet_traceTransaction`.
    TraceTransaction(TraceTransactionRequest),
    /// Request data for `starknet_simulateTransactions`.
    SimulateTransactions(SimulateTransactionsRequest),
    /// Request data for `starknet_traceBlockTransactions`.
    TraceBlockTransactions(TraceBlockTransactionsRequest),
    /// Request data for `starknet_subscribeNewHeads`.
    SubscribeNewHeads(SubscribeNewHeadsRequest),
    /// Request data for `starknet_subscribeEvents`.
    SubscribeEvents(SubscribeEventsRequest),
    /// Request data for `starknet_subscribeTransactionStatus`.
    SubscribeTransactionStatus(SubscribeTransactionStatusRequest),
    /// Request data for `starknet_subscribeNewTransactionReceipts`.
    SubscribeNewTransactionReceipts(SubscribeNewTransactionReceiptsRequest),
    /// Request data for `starknet_subscribeNewTransactions`.
    SubscribeNewTransactions(SubscribeNewTransactionsRequest),
    /// Request data for `starknet_unsubscribe`.
    Unsubscribe(UnsubscribeRequest),
}

/// Typed response data for [`Provider`] responses.
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum ProviderResponseData {
    /// Response data for `starknet_specVersion`.
    SpecVersion(String),
    /// Response data for `starknet_getBlockWithTxHashes`.
    GetBlockWithTxHashes(MaybePreConfirmedBlockWithTxHashes),
    /// Response data for `starknet_getBlockWithTxs`.
    GetBlockWithTxs(MaybePreConfirmedBlockWithTxs),
    /// Response data for `starknet_getBlockWithReceipts`.
    GetBlockWithReceipts(MaybePreConfirmedBlockWithReceipts),
    /// Response data for `starknet_getStateUpdate`.
    GetStateUpdate(MaybePreConfirmedStateUpdate),
    /// Response data for `starknet_getStorageAt`.
    GetStorageAt(Felt),
    /// Response data for `starknet_getMessagesStatus`.
    GetMessagesStatus(Vec<MessageStatus>),
    /// Response data for `starknet_getTransactionStatus`.
    GetTransactionStatus(TransactionStatus),
    /// Response data for `starknet_getTransactionByHash`.
    GetTransactionByHash(Transaction),
    /// Response data for `starknet_getTransactionByBlockIdAndIndex`.
    GetTransactionByBlockIdAndIndex(Transaction),
    /// Response data for `starknet_getTransactionReceipt`.
    GetTransactionReceipt(TransactionReceiptWithBlockInfo),
    /// Response data for `starknet_getClass`.
    GetClass(ContractClass),
    /// Response data for `starknet_getClassHashAt`.
    GetClassHashAt(Felt),
    /// Response data for `starknet_getClassAt`.
    GetClassAt(ContractClass),
    /// Response data for `starknet_getBlockTransactionCount`.
    GetBlockTransactionCount(u64),
    /// Response data for `starknet_call`.
    Call(Vec<Felt>),
    /// Response data for `starknet_estimateFee`.
    EstimateFee(Vec<FeeEstimate>),
    /// Response data for `starknet_estimateMessageFee`.
    EstimateMessageFee(FeeEstimate),
    /// Response data for `starknet_blockNumber`.
    BlockNumber(u64),
    /// Response data for `starknet_blockHashAndNumber`.
    BlockHashAndNumber(BlockHashAndNumber),
    /// Response data for `starknet_chainId`.
    ChainId(Felt),
    /// Response data for `starknet_syncing`.
    Syncing(SyncStatusType),
    /// Response data for `starknet_getEvents`.
    GetEvents(EventsPage),
    /// Response data for `starknet_getNonce`.
    GetNonce(Felt),
    /// Response data for `starknet_getStorageProof`.
    GetStorageProof(StorageProof),
    /// Response data for `starknet_addInvokeTransaction`.
    AddInvokeTransaction(InvokeTransactionResult),
    /// Response data for `starknet_addDeclareTransaction`.
    AddDeclareTransaction(DeclareTransactionResult),
    /// Response data for `starknet_addDeployAccountTransaction`.
    AddDeployAccountTransaction(DeployAccountTransactionResult),
    /// Response data for `starknet_traceTransaction`.
    TraceTransaction(TransactionTrace),
    /// Response data for `starknet_simulateTransactions`.
    SimulateTransactions(Vec<SimulatedTransaction>),
    /// Response data for `starknet_traceBlockTransactions`.
    TraceBlockTransactions(Vec<TransactionTraceWithHash>),
    /// Response data for `starknet_subscribeNewHeads`.
    SubscribeNewHeads(SubscriptionId),
    /// Response data for `starknet_subscribeEvents`.
    SubscribeEvents(SubscriptionId),
    /// Response data for `starknet_subscribeTransactionStatus`.
    SubscribeTransactionStatus(SubscriptionId),
    /// Response data for `starknet_subscribeNewTransactionReceipts`.
    SubscribeNewTransactionReceipts(SubscriptionId),
    /// Response data for `starknet_subscribeNewTransactions`.
    SubscribeNewTransactions(SubscriptionId),
    /// Response data for `starknet_unsubscribe`.
    Unsubscribe(bool),
}

/// Typed data for stream updates.
#[allow(clippy::enum_variant_names)]
#[derive(Debug, Clone, Serialize)]
#[serde(untagged)]
pub enum StreamUpdateData {
    /// Stream data for `starknet_subscriptionNewHeads`.
    SubscriptionNewHeads(SubscriptionNewHeadsRequest),
    /// Stream data for `starknet_subscriptionEvents`.
    SubscriptionEvents(SubscriptionEventsRequest),
    /// Stream data for `starknet_subscriptionTransactionStatus`.
    SubscriptionTransactionStatus(SubscriptionTransactionStatusRequest),
    /// Stream data for `starknet_subscriptionNewTransactionReceipts`.
    SubscriptionNewTransactionReceipts(SubscriptionNewTransactionReceiptsRequest),
    /// Stream data for `starknet_subscriptionNewTransaction`.
    SubscriptionNewTransaction(SubscriptionNewTransactionRequest),
    /// Stream data for `starknet_subscriptionReorg`.
    SubscriptionReorg(SubscriptionReorgRequest),
}

impl StreamUpdateData {
    /// Gets a reference to the subscription ID the update corresponds to.
    pub fn subscription_id(&self) -> &SubscriptionId {
        match self {
            Self::SubscriptionNewHeads(update) => &update.subscription_id,
            Self::SubscriptionEvents(update) => &update.subscription_id,
            Self::SubscriptionTransactionStatus(update) => &update.subscription_id,
            Self::SubscriptionNewTransactionReceipts(update) => &update.subscription_id,
            Self::SubscriptionNewTransaction(update) => &update.subscription_id,
            Self::SubscriptionReorg(update) => &update.subscription_id,
        }
    }
}