starknet_api 0.18.0-dev.2

Starknet Rust types related to computation and execution.
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#[cfg(test)]
#[path = "rpc_transaction_test.rs"]
mod rpc_transaction_test;

use std::collections::HashMap;

use apollo_sizeof::SizeOf;
use cairo_lang_starknet_classes::contract_class::ContractEntryPoints as CairoLangContractEntryPoints;
use serde::{Deserialize, Serialize};
use starknet_core::types::EntryPointsByType as StarknetCoreEntryPointsByType;
use strum::{EnumDiscriminants, EnumIter, IntoStaticStr, VariantNames};

use crate::contract_class::EntryPointType;
use crate::core::{ChainId, ClassHash, CompiledClassHash, ContractAddress, Nonce};
use crate::data_availability::DataAvailabilityMode;
use crate::state::{EntryPoint, SierraContractClass};
use crate::transaction::fields::{
    AccountDeploymentData,
    AllResourceBounds,
    Calldata,
    ContractAddressSalt,
    PaymasterData,
    Proof,
    ProofFacts,
    Tip,
    TransactionSignature,
    ValidResourceBounds,
};
use crate::transaction::{
    CalculateContractAddress,
    DeclareTransaction,
    DeclareTransactionV3,
    DeployAccountTransaction,
    DeployAccountTransactionV3,
    DeployTransactionTrait,
    InvokeTransaction,
    InvokeTransactionV3,
    Transaction,
    TransactionHash,
    TransactionHasher,
    TransactionVersion,
};
use crate::transaction_hash::{
    get_declare_transaction_v3_hash,
    get_deploy_account_transaction_v3_hash,
    get_invoke_transaction_v3_hash,
    DeclareTransactionV3Trait,
    DeployAccountTransactionV3Trait,
    InvokeTransactionV3Trait,
};
use crate::{impl_deploy_transaction_trait, StarknetApiError};

/// Transactions that are ready to be broadcasted to the network through RPC and are not included in
/// a block.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash, EnumDiscriminants)]
#[strum_discriminants(
    name(RpcTransactionLabelValue),
    derive(IntoStaticStr, EnumIter, VariantNames),
    strum(serialize_all = "snake_case")
)]
#[serde(tag = "type")]
#[serde(deny_unknown_fields)]
pub enum RpcTransaction {
    #[serde(rename = "DECLARE")]
    Declare(RpcDeclareTransaction),
    #[serde(rename = "DEPLOY_ACCOUNT")]
    DeployAccount(RpcDeployAccountTransaction),
    #[serde(rename = "INVOKE")]
    Invoke(RpcInvokeTransaction),
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash, SizeOf)]
pub struct InternalRpcDeployAccountTransaction {
    pub tx: RpcDeployAccountTransaction,
    pub contract_address: ContractAddress,
}

impl InternalRpcDeployAccountTransaction {
    pub fn version(&self) -> TransactionVersion {
        self.tx.version()
    }
}

impl TransactionHasher for InternalRpcDeployAccountTransaction {
    fn calculate_transaction_hash(
        &self,
        chain_id: &ChainId,
        transaction_version: &TransactionVersion,
    ) -> Result<TransactionHash, StarknetApiError> {
        match &self.tx {
            RpcDeployAccountTransaction::V3(tx) => {
                tx.calculate_transaction_hash(chain_id, transaction_version)
            }
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash, EnumDiscriminants, SizeOf)]
#[strum_discriminants(
    name(InternalRpcTransactionLabelValue),
    derive(IntoStaticStr, EnumIter, VariantNames),
    strum(serialize_all = "snake_case")
)]
#[serde(tag = "type")]
#[serde(deny_unknown_fields)]
pub enum InternalRpcTransactionWithoutTxHash {
    #[serde(rename = "DECLARE")]
    Declare(InternalRpcDeclareTransactionV3),
    #[serde(rename = "DEPLOY_ACCOUNT")]
    DeployAccount(InternalRpcDeployAccountTransaction),
    #[serde(rename = "INVOKE")]
    Invoke(InternalRpcInvokeTransactionV3),
}

impl InternalRpcTransactionWithoutTxHash {
    pub fn version(&self) -> TransactionVersion {
        match self {
            InternalRpcTransactionWithoutTxHash::Declare(tx) => tx.version(),
            InternalRpcTransactionWithoutTxHash::Invoke(tx) => tx.version(),
            InternalRpcTransactionWithoutTxHash::DeployAccount(tx) => tx.version(),
        }
    }

    pub fn calculate_transaction_hash(
        &self,
        chain_id: &ChainId,
    ) -> Result<TransactionHash, StarknetApiError> {
        let transaction_version = &self.version();
        match self {
            InternalRpcTransactionWithoutTxHash::Declare(tx) => {
                tx.calculate_transaction_hash(chain_id, transaction_version)
            }
            InternalRpcTransactionWithoutTxHash::Invoke(tx) => {
                tx.calculate_transaction_hash(chain_id, transaction_version)
            }
            InternalRpcTransactionWithoutTxHash::DeployAccount(tx) => {
                tx.calculate_transaction_hash(chain_id, transaction_version)
            }
        }
    }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash, SizeOf)]
pub struct InternalRpcTransaction {
    pub tx: InternalRpcTransactionWithoutTxHash,
    pub tx_hash: TransactionHash,
}

macro_rules! implement_ref_getters {
    ($(($member_name:ident, $member_type:ty)), *) => {
        $(pub fn $member_name(&self) -> &$member_type {
            match self {
                RpcTransaction::Declare(
                    RpcDeclareTransaction::V3(tx)
                ) => &tx.$member_name,
                RpcTransaction::DeployAccount(
                    RpcDeployAccountTransaction::V3(tx)
                ) => &tx.$member_name,
                RpcTransaction::Invoke(
                    RpcInvokeTransaction::V3(tx)
                ) => &tx.$member_name
            }
        })*
    };
}

impl RpcTransaction {
    implement_ref_getters!(
        (nonce, Nonce),
        (resource_bounds, AllResourceBounds),
        (signature, TransactionSignature),
        (tip, Tip),
        (nonce_data_availability_mode, DataAvailabilityMode),
        (fee_data_availability_mode, DataAvailabilityMode)
    );

    pub fn calculate_sender_address(&self) -> Result<ContractAddress, StarknetApiError> {
        match self {
            RpcTransaction::Declare(RpcDeclareTransaction::V3(tx)) => Ok(tx.sender_address),
            RpcTransaction::DeployAccount(RpcDeployAccountTransaction::V3(tx)) => {
                tx.calculate_contract_address()
            }
            RpcTransaction::Invoke(RpcInvokeTransaction::V3(tx)) => Ok(tx.sender_address),
        }
    }
}

// TODO(Arni): Replace this with RPCTransaction -> InternalRpcTransaction conversion (don't use From
// because it contains hash calculations).
impl From<RpcTransaction> for Transaction {
    fn from(rpc_transaction: RpcTransaction) -> Self {
        match rpc_transaction {
            RpcTransaction::Declare(tx) => Transaction::Declare(tx.into()),
            RpcTransaction::DeployAccount(tx) => Transaction::DeployAccount(tx.into()),
            RpcTransaction::Invoke(tx) => Transaction::Invoke(tx.into()),
        }
    }
}

macro_rules! implement_internal_getters_for_internal_rpc {
    ($(($field_name:ident, $field_ty:ty)),* $(,)?) => {
        $(
            pub fn $field_name(&self) -> $field_ty {
                match &self.tx {
                    InternalRpcTransactionWithoutTxHash::Declare(tx) => tx.$field_name.clone(),
                    InternalRpcTransactionWithoutTxHash::DeployAccount(tx) => {
                        let RpcDeployAccountTransaction::V3(tx) = &tx.tx;
                        tx.$field_name.clone()
                    },
                    InternalRpcTransactionWithoutTxHash::Invoke(tx) => tx.$field_name.clone(),
                }
            }
        )*
    };
}

impl InternalRpcTransaction {
    implement_internal_getters_for_internal_rpc!(
        (nonce, Nonce),
        (resource_bounds, AllResourceBounds),
        (signature, TransactionSignature),
        (tip, Tip),
    );

    pub fn contract_address(&self) -> ContractAddress {
        match &self.tx {
            InternalRpcTransactionWithoutTxHash::Declare(tx) => tx.sender_address,
            InternalRpcTransactionWithoutTxHash::DeployAccount(tx) => tx.contract_address,
            InternalRpcTransactionWithoutTxHash::Invoke(tx) => tx.sender_address,
        }
    }

    pub fn total_bytes(&self) -> u64 {
        self.size_bytes()
            .try_into()
            .expect("The transaction size in bytes should fit in a u64 value.")
    }

    pub fn tx_hash(&self) -> TransactionHash {
        self.tx_hash
    }
}
/// A RPC declare transaction.
///
/// This transaction is equivalent to the component DECLARE_TXN in the
/// [`Starknet specs`] with a contract class (DECLARE_TXN allows having
/// either a contract class or a class hash).
///
/// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash)]
#[serde(tag = "version")]
pub enum RpcDeclareTransaction {
    #[serde(rename = "0x3")]
    V3(RpcDeclareTransactionV3),
}

impl From<RpcDeclareTransaction> for DeclareTransaction {
    fn from(rpc_declare_transaction: RpcDeclareTransaction) -> Self {
        match rpc_declare_transaction {
            RpcDeclareTransaction::V3(tx) => DeclareTransaction::V3(tx.into()),
        }
    }
}

/// A RPC deploy account transaction.
///
/// This transaction is equivalent to the component DEPLOY_ACCOUNT_TXN in the
/// [`Starknet specs`].
///
/// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, SizeOf)]
#[serde(tag = "version")]
pub enum RpcDeployAccountTransaction {
    #[serde(rename = "0x3")]
    V3(RpcDeployAccountTransactionV3),
}

impl RpcDeployAccountTransaction {
    fn version(&self) -> TransactionVersion {
        match self {
            RpcDeployAccountTransaction::V3(_) => TransactionVersion::THREE,
        }
    }
}

impl From<RpcDeployAccountTransaction> for DeployAccountTransaction {
    fn from(rpc_deploy_account_transaction: RpcDeployAccountTransaction) -> Self {
        match rpc_deploy_account_transaction {
            RpcDeployAccountTransaction::V3(tx) => DeployAccountTransaction::V3(tx.into()),
        }
    }
}

impl TryFrom<DeployAccountTransactionV3> for RpcDeployAccountTransactionV3 {
    type Error = StarknetApiError;

    fn try_from(value: DeployAccountTransactionV3) -> Result<Self, Self::Error> {
        Ok(Self {
            resource_bounds: match value.resource_bounds {
                ValidResourceBounds::AllResources(bounds) => bounds,
                _ => {
                    return Err(StarknetApiError::OutOfRange {
                        string: "resource_bounds".to_string(),
                    });
                }
            },
            signature: value.signature,
            nonce: value.nonce,
            class_hash: value.class_hash,
            contract_address_salt: value.contract_address_salt,
            constructor_calldata: value.constructor_calldata,
            tip: value.tip,
            paymaster_data: value.paymaster_data,
            nonce_data_availability_mode: value.nonce_data_availability_mode,
            fee_data_availability_mode: value.fee_data_availability_mode,
        })
    }
}

/// A RPC invoke transaction.
///
/// This transaction is equivalent to the component INVOKE_TXN in the
/// [`Starknet specs`].
///
/// [`Starknet specs`]: https://github.com/starkware-libs/starknet-specs/blob/master/api/starknet_api_openrpc.json
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, SizeOf)]
#[serde(tag = "version")]
pub enum RpcInvokeTransaction {
    #[serde(rename = "0x3")]
    V3(RpcInvokeTransactionV3),
}

impl RpcInvokeTransaction {
    pub fn version(&self) -> TransactionVersion {
        match self {
            RpcInvokeTransaction::V3(_) => TransactionVersion::THREE,
        }
    }
}

impl From<RpcInvokeTransaction> for InvokeTransaction {
    fn from(rpc_invoke_tx: RpcInvokeTransaction) -> Self {
        match rpc_invoke_tx {
            RpcInvokeTransaction::V3(tx) => InvokeTransaction::V3(tx.into()),
        }
    }
}

/// A declare transaction of a Cairo-v1 contract class that can be added to Starknet through the
/// RPC.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash)]
pub struct RpcDeclareTransactionV3 {
    // TODO(Mohammad): Check with Shahak why we need to keep the DeclareType.
    // pub r#type: DeclareType,
    pub sender_address: ContractAddress,
    pub compiled_class_hash: CompiledClassHash,
    pub signature: TransactionSignature,
    pub nonce: Nonce,
    pub contract_class: SierraContractClass,
    pub resource_bounds: AllResourceBounds,
    pub tip: Tip,
    pub paymaster_data: PaymasterData,
    pub account_deployment_data: AccountDeploymentData,
    pub nonce_data_availability_mode: DataAvailabilityMode,
    pub fee_data_availability_mode: DataAvailabilityMode,
}

impl From<RpcDeclareTransactionV3> for DeclareTransactionV3 {
    fn from(tx: RpcDeclareTransactionV3) -> Self {
        Self {
            class_hash: tx.contract_class.calculate_class_hash(),
            resource_bounds: ValidResourceBounds::AllResources(tx.resource_bounds),
            tip: tx.tip,
            signature: tx.signature,
            nonce: tx.nonce,
            compiled_class_hash: tx.compiled_class_hash,
            sender_address: tx.sender_address,
            nonce_data_availability_mode: tx.nonce_data_availability_mode,
            fee_data_availability_mode: tx.fee_data_availability_mode,
            paymaster_data: tx.paymaster_data,
            account_deployment_data: tx.account_deployment_data,
        }
    }
}

/// An [RpcDeclareTransactionV3] that contains a class hash instead of the full contract class.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, Hash, SizeOf)]
pub struct InternalRpcDeclareTransactionV3 {
    pub sender_address: ContractAddress,
    pub compiled_class_hash: CompiledClassHash,
    pub signature: TransactionSignature,
    pub nonce: Nonce,
    pub class_hash: ClassHash,
    pub resource_bounds: AllResourceBounds,
    pub tip: Tip,
    pub paymaster_data: PaymasterData,
    pub account_deployment_data: AccountDeploymentData,
    pub nonce_data_availability_mode: DataAvailabilityMode,
    pub fee_data_availability_mode: DataAvailabilityMode,
}

impl InternalRpcDeclareTransactionV3 {
    pub fn version(&self) -> TransactionVersion {
        TransactionVersion::THREE
    }
}

impl DeclareTransactionV3Trait for InternalRpcDeclareTransactionV3 {
    fn resource_bounds(&self) -> ValidResourceBounds {
        ValidResourceBounds::AllResources(self.resource_bounds)
    }
    fn tip(&self) -> &Tip {
        &self.tip
    }
    fn paymaster_data(&self) -> &PaymasterData {
        &self.paymaster_data
    }
    fn nonce_data_availability_mode(&self) -> &DataAvailabilityMode {
        &self.nonce_data_availability_mode
    }
    fn fee_data_availability_mode(&self) -> &DataAvailabilityMode {
        &self.fee_data_availability_mode
    }
    fn account_deployment_data(&self) -> &AccountDeploymentData {
        &self.account_deployment_data
    }
    fn sender_address(&self) -> &ContractAddress {
        &self.sender_address
    }
    fn nonce(&self) -> &Nonce {
        &self.nonce
    }
    fn class_hash(&self) -> &ClassHash {
        &self.class_hash
    }
    fn compiled_class_hash(&self) -> &CompiledClassHash {
        &self.compiled_class_hash
    }
}

impl TransactionHasher for InternalRpcDeclareTransactionV3 {
    fn calculate_transaction_hash(
        &self,
        chain_id: &ChainId,
        transaction_version: &TransactionVersion,
    ) -> Result<TransactionHash, StarknetApiError> {
        get_declare_transaction_v3_hash(self, chain_id, transaction_version)
    }
}

impl From<InternalRpcDeclareTransactionV3> for DeclareTransactionV3 {
    fn from(tx: InternalRpcDeclareTransactionV3) -> Self {
        Self {
            class_hash: tx.class_hash,
            resource_bounds: ValidResourceBounds::AllResources(tx.resource_bounds),
            tip: tx.tip,
            signature: tx.signature,
            nonce: tx.nonce,
            compiled_class_hash: tx.compiled_class_hash,
            sender_address: tx.sender_address,
            nonce_data_availability_mode: tx.nonce_data_availability_mode,
            fee_data_availability_mode: tx.fee_data_availability_mode,
            paymaster_data: tx.paymaster_data,
            account_deployment_data: tx.account_deployment_data,
        }
    }
}

impl From<InternalRpcDeclareTransactionV3> for DeclareTransaction {
    fn from(tx: InternalRpcDeclareTransactionV3) -> Self {
        Self::V3(tx.into())
    }
}

/// A deploy account transaction that can be added to Starknet through the RPC.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, SizeOf)]
pub struct RpcDeployAccountTransactionV3 {
    pub signature: TransactionSignature,
    pub nonce: Nonce,
    pub class_hash: ClassHash,
    pub contract_address_salt: ContractAddressSalt,
    pub constructor_calldata: Calldata,
    pub resource_bounds: AllResourceBounds,
    pub tip: Tip,
    pub paymaster_data: PaymasterData,
    pub nonce_data_availability_mode: DataAvailabilityMode,
    pub fee_data_availability_mode: DataAvailabilityMode,
}

impl_deploy_transaction_trait!(RpcDeployAccountTransactionV3);

impl From<RpcDeployAccountTransactionV3> for DeployAccountTransactionV3 {
    fn from(tx: RpcDeployAccountTransactionV3) -> Self {
        Self {
            resource_bounds: ValidResourceBounds::AllResources(tx.resource_bounds),
            tip: tx.tip,
            signature: tx.signature,
            nonce: tx.nonce,
            class_hash: tx.class_hash,
            contract_address_salt: tx.contract_address_salt,
            constructor_calldata: tx.constructor_calldata,
            nonce_data_availability_mode: tx.nonce_data_availability_mode,
            fee_data_availability_mode: tx.fee_data_availability_mode,
            paymaster_data: tx.paymaster_data,
        }
    }
}

impl DeployAccountTransactionV3Trait for RpcDeployAccountTransactionV3 {
    fn resource_bounds(&self) -> ValidResourceBounds {
        ValidResourceBounds::AllResources(self.resource_bounds)
    }
    fn tip(&self) -> &Tip {
        &self.tip
    }
    fn paymaster_data(&self) -> &PaymasterData {
        &self.paymaster_data
    }
    fn nonce_data_availability_mode(&self) -> &DataAvailabilityMode {
        &self.nonce_data_availability_mode
    }
    fn fee_data_availability_mode(&self) -> &DataAvailabilityMode {
        &self.fee_data_availability_mode
    }
    fn constructor_calldata(&self) -> &Calldata {
        &self.constructor_calldata
    }
    fn nonce(&self) -> &Nonce {
        &self.nonce
    }
    fn class_hash(&self) -> &ClassHash {
        &self.class_hash
    }
    fn contract_address_salt(&self) -> &ContractAddressSalt {
        &self.contract_address_salt
    }
}

impl TransactionHasher for RpcDeployAccountTransactionV3 {
    fn calculate_transaction_hash(
        &self,
        chain_id: &ChainId,
        transaction_version: &TransactionVersion,
    ) -> Result<TransactionHash, StarknetApiError> {
        get_deploy_account_transaction_v3_hash(self, chain_id, transaction_version)
    }
}

/// An invoke account transaction that can be added to Starknet through the RPC.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, SizeOf)]
pub struct RpcInvokeTransactionV3 {
    pub sender_address: ContractAddress,
    pub calldata: Calldata,
    pub signature: TransactionSignature,
    pub nonce: Nonce,
    pub resource_bounds: AllResourceBounds,
    pub tip: Tip,
    pub paymaster_data: PaymasterData,
    pub account_deployment_data: AccountDeploymentData,
    pub nonce_data_availability_mode: DataAvailabilityMode,
    pub fee_data_availability_mode: DataAvailabilityMode,
    #[serde(default, skip_serializing_if = "ProofFacts::is_empty")]
    pub proof_facts: ProofFacts,
    #[serde(default, skip_serializing_if = "Proof::is_empty")]
    pub proof: Proof,
}

impl From<RpcInvokeTransactionV3> for InvokeTransactionV3 {
    fn from(tx: RpcInvokeTransactionV3) -> Self {
        Self {
            resource_bounds: ValidResourceBounds::AllResources(tx.resource_bounds),
            tip: tx.tip,
            signature: tx.signature,
            nonce: tx.nonce,
            sender_address: tx.sender_address,
            calldata: tx.calldata,
            nonce_data_availability_mode: tx.nonce_data_availability_mode,
            fee_data_availability_mode: tx.fee_data_availability_mode,
            paymaster_data: tx.paymaster_data,
            account_deployment_data: tx.account_deployment_data,
            proof_facts: tx.proof_facts,
        }
    }
}

impl TryFrom<InvokeTransactionV3> for RpcInvokeTransactionV3 {
    type Error = StarknetApiError;

    fn try_from(value: InvokeTransactionV3) -> Result<Self, Self::Error> {
        Ok(Self {
            resource_bounds: match value.resource_bounds {
                ValidResourceBounds::AllResources(bounds) => bounds,
                _ => {
                    return Err(StarknetApiError::OutOfRange {
                        string: "resource_bounds".to_string(),
                    });
                }
            },
            signature: value.signature,
            nonce: value.nonce,
            tip: value.tip,
            paymaster_data: value.paymaster_data,
            nonce_data_availability_mode: value.nonce_data_availability_mode,
            fee_data_availability_mode: value.fee_data_availability_mode,
            sender_address: value.sender_address,
            calldata: value.calldata,
            account_deployment_data: value.account_deployment_data,
            proof_facts: value.proof_facts,
            proof: Proof::default(),
        })
    }
}

/// An [RpcInvokeTransactionV3] that excludes the proof field (only keeps proof_facts).
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, SizeOf)]
pub struct InternalRpcInvokeTransactionV3 {
    pub sender_address: ContractAddress,
    pub calldata: Calldata,
    pub signature: TransactionSignature,
    pub nonce: Nonce,
    pub resource_bounds: AllResourceBounds,
    pub tip: Tip,
    pub paymaster_data: PaymasterData,
    pub account_deployment_data: AccountDeploymentData,
    pub nonce_data_availability_mode: DataAvailabilityMode,
    pub fee_data_availability_mode: DataAvailabilityMode,
    pub proof_facts: ProofFacts,
}

impl InternalRpcInvokeTransactionV3 {
    pub fn version(&self) -> TransactionVersion {
        TransactionVersion::THREE
    }
}

impl InvokeTransactionV3Trait for InternalRpcInvokeTransactionV3 {
    fn resource_bounds(&self) -> ValidResourceBounds {
        ValidResourceBounds::AllResources(self.resource_bounds)
    }
    fn tip(&self) -> &Tip {
        &self.tip
    }
    fn paymaster_data(&self) -> &PaymasterData {
        &self.paymaster_data
    }
    fn nonce_data_availability_mode(&self) -> &DataAvailabilityMode {
        &self.nonce_data_availability_mode
    }
    fn fee_data_availability_mode(&self) -> &DataAvailabilityMode {
        &self.fee_data_availability_mode
    }
    fn account_deployment_data(&self) -> &AccountDeploymentData {
        &self.account_deployment_data
    }
    fn sender_address(&self) -> &ContractAddress {
        &self.sender_address
    }
    fn nonce(&self) -> &Nonce {
        &self.nonce
    }
    fn calldata(&self) -> &Calldata {
        &self.calldata
    }
    fn proof_facts(&self) -> &ProofFacts {
        &self.proof_facts
    }
}

impl TransactionHasher for InternalRpcInvokeTransactionV3 {
    fn calculate_transaction_hash(
        &self,
        chain_id: &ChainId,
        transaction_version: &TransactionVersion,
    ) -> Result<TransactionHash, StarknetApiError> {
        get_invoke_transaction_v3_hash(self, chain_id, transaction_version)
    }
}

impl From<InternalRpcInvokeTransactionV3> for InvokeTransactionV3 {
    fn from(tx: InternalRpcInvokeTransactionV3) -> Self {
        Self {
            resource_bounds: ValidResourceBounds::AllResources(tx.resource_bounds),
            tip: tx.tip,
            signature: tx.signature,
            nonce: tx.nonce,
            sender_address: tx.sender_address,
            calldata: tx.calldata,
            nonce_data_availability_mode: tx.nonce_data_availability_mode,
            fee_data_availability_mode: tx.fee_data_availability_mode,
            paymaster_data: tx.paymaster_data,
            account_deployment_data: tx.account_deployment_data,
            proof_facts: tx.proof_facts,
        }
    }
}

impl From<RpcInvokeTransactionV3> for InternalRpcInvokeTransactionV3 {
    fn from(tx: RpcInvokeTransactionV3) -> Self {
        Self {
            sender_address: tx.sender_address,
            calldata: tx.calldata,
            signature: tx.signature,
            nonce: tx.nonce,
            resource_bounds: tx.resource_bounds,
            tip: tx.tip,
            paymaster_data: tx.paymaster_data,
            account_deployment_data: tx.account_deployment_data,
            nonce_data_availability_mode: tx.nonce_data_availability_mode,
            fee_data_availability_mode: tx.fee_data_availability_mode,
            proof_facts: tx.proof_facts,
            // Note: proof field is dropped
        }
    }
}

impl From<RpcInvokeTransaction> for InternalRpcInvokeTransactionV3 {
    fn from(rpc_invoke_tx: RpcInvokeTransaction) -> Self {
        match rpc_invoke_tx {
            RpcInvokeTransaction::V3(tx) => tx.into(),
        }
    }
}

impl From<InternalRpcInvokeTransactionV3> for InvokeTransaction {
    fn from(tx: InternalRpcInvokeTransactionV3) -> Self {
        InvokeTransaction::V3(tx.into())
    }
}

// TODO(Aviv): remove duplication with sequencer/crates/apollo_rpc/src/v0_8/state.rs
#[derive(Debug, Clone, Default, Eq, PartialEq, Deserialize, Serialize, Hash)]
pub struct EntryPointByType {
    #[serde(rename = "CONSTRUCTOR")]
    pub constructor: Vec<EntryPoint>,
    #[serde(rename = "EXTERNAL")]
    pub external: Vec<EntryPoint>,
    #[serde(rename = "L1_HANDLER")]
    pub l1handler: Vec<EntryPoint>,
}

// TODO(AVIV): Consider removing this conversion and using CairoLangContractEntryPoints instead of
// defining the EntryPointByType struct.
impl From<CairoLangContractEntryPoints> for EntryPointByType {
    fn from(value: CairoLangContractEntryPoints) -> Self {
        Self {
            constructor: value.constructor.into_iter().map(EntryPoint::from).collect(),
            external: value.external.into_iter().map(EntryPoint::from).collect(),
            l1handler: value.l1_handler.into_iter().map(EntryPoint::from).collect(),
        }
    }
}

impl From<StarknetCoreEntryPointsByType> for EntryPointByType {
    fn from(value: StarknetCoreEntryPointsByType) -> Self {
        Self {
            constructor: value.constructor.into_iter().map(EntryPoint::from).collect(),
            external: value.external.into_iter().map(EntryPoint::from).collect(),
            l1handler: value.l1_handler.into_iter().map(EntryPoint::from).collect(),
        }
    }
}

impl EntryPointByType {
    pub fn from_hash_map(entry_points_by_type: HashMap<EntryPointType, Vec<EntryPoint>>) -> Self {
        macro_rules! get_entrypoint_by_type {
            ($variant:ident) => {
                (*(entry_points_by_type.get(&EntryPointType::$variant).unwrap_or(&vec![]))).to_vec()
            };
        }

        Self {
            constructor: get_entrypoint_by_type!(Constructor),
            external: get_entrypoint_by_type!(External),
            l1handler: get_entrypoint_by_type!(L1Handler),
        }
    }
    pub fn to_hash_map(&self) -> HashMap<EntryPointType, Vec<EntryPoint>> {
        HashMap::from_iter([
            (EntryPointType::Constructor, self.constructor.clone()),
            (EntryPointType::External, self.external.clone()),
            (EntryPointType::L1Handler, self.l1handler.clone()),
        ])
    }
}