From c6c2714a452b73f0fba69d5a4cd8b5d2ce54c8cf Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 20 Jun 2024 01:55:11 +0200 Subject: [PATCH] feat: add `is_` utils to `TransactionStatus` enums (#606) --- starknet-core/src/types/mod.rs | 27 ++++++++++++++ .../src/sequencer/models/conversions.rs | 2 +- .../src/sequencer/models/transaction.rs | 8 ++-- .../sequencer/models/transaction_receipt.rs | 37 +++++++++++++++++++ 4 files changed, 69 insertions(+), 5 deletions(-) diff --git a/starknet-core/src/types/mod.rs b/starknet-core/src/types/mod.rs index d46535bc..e5494a82 100644 --- a/starknet-core/src/types/mod.rs +++ b/starknet-core/src/types/mod.rs @@ -174,14 +174,41 @@ pub enum ContractClass { Legacy(CompressedLegacyContractClass), } +/// Represents the status of a transaction. #[derive(Debug, Clone, PartialEq, Eq)] pub enum TransactionStatus { + /// Transaction received and awaiting processing. Received, + /// Transaction rejected due to validation or other reasons. Rejected, + /// Transaction accepted on Layer 2 with a specific execution status. AcceptedOnL2(TransactionExecutionStatus), + /// Transaction accepted on Layer 1 with a specific execution status. AcceptedOnL1(TransactionExecutionStatus), } +impl TransactionStatus { + /// Returns `true` if the transaction status is `Received`. + pub const fn is_received(&self) -> bool { + matches!(self, Self::Received) + } + + /// Returns `true` if the transaction status is `Rejected`. + pub const fn is_rejected(&self) -> bool { + matches!(self, Self::Rejected) + } + + /// Returns `true` if the transaction status is `AcceptedOnL2`. + pub const fn is_accepted_on_l2(&self) -> bool { + matches!(self, Self::AcceptedOnL2(_)) + } + + /// Returns `true` if the transaction status is `AcceptedOnL1`. + pub const fn is_accepted_on_l1(&self) -> bool { + matches!(self, Self::AcceptedOnL1(_)) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] #[serde(tag = "type")] pub enum Transaction { diff --git a/starknet-providers/src/sequencer/models/conversions.rs b/starknet-providers/src/sequencer/models/conversions.rs index c1c0d618..8e2c6efe 100644 --- a/starknet-providers/src/sequencer/models/conversions.rs +++ b/starknet-providers/src/sequencer/models/conversions.rs @@ -902,7 +902,7 @@ impl TryFrom for core::TransactionStatus { type Error = ConversionError; fn try_from(value: TransactionStatusInfo) -> Result { - if let TransactionStatus::Rejected = value.status { + if value.status.is_rejected() { return Ok(Self::Rejected); } diff --git a/starknet-providers/src/sequencer/models/transaction.rs b/starknet-providers/src/sequencer/models/transaction.rs index db625f16..388ace7d 100644 --- a/starknet-providers/src/sequencer/models/transaction.rs +++ b/starknet-providers/src/sequencer/models/transaction.rs @@ -344,7 +344,7 @@ mod tests { let tx: TransactionInfo = serde_json::from_str(raw).unwrap(); assert_eq!(tx.block_number, None); - assert_eq!(tx.status, TransactionStatus::NotReceived); + assert!(tx.status.is_not_received()); } #[test] @@ -459,7 +459,7 @@ mod tests { let tx: TransactionStatusInfo = serde_json::from_str(raw).unwrap(); - assert_eq!(tx.status, TransactionStatus::AcceptedOnL1); + assert!(tx.status.is_accepted_on_l1()); assert_eq!( tx.block_hash, Some( @@ -478,7 +478,7 @@ mod tests { let tx: TransactionStatusInfo = serde_json::from_str(raw).unwrap(); - assert_eq!(tx.status, TransactionStatus::NotReceived); + assert!(tx.status.is_not_received()); assert!(tx.block_hash.is_none()); } @@ -492,7 +492,7 @@ mod tests { let tx: TransactionStatusInfo = serde_json::from_str(raw).unwrap(); - assert_eq!(tx.status, TransactionStatus::Rejected); + assert!(tx.status.is_rejected()); assert!(tx.block_hash.is_none()); assert!(tx.transaction_failure_reason.is_some()); } diff --git a/starknet-providers/src/sequencer/models/transaction_receipt.rs b/starknet-providers/src/sequencer/models/transaction_receipt.rs index fdcaf221..97f6e864 100644 --- a/starknet-providers/src/sequencer/models/transaction_receipt.rs +++ b/starknet-providers/src/sequencer/models/transaction_receipt.rs @@ -50,6 +50,43 @@ pub enum TransactionStatus { AcceptedOnL1, } +impl TransactionStatus { + /// Returns `true` if the transaction status is `NotReceived`. + pub const fn is_not_received(&self) -> bool { + matches!(self, Self::NotReceived) + } + + /// Returns `true` if the transaction status is `Received`. + pub const fn is_received(&self) -> bool { + matches!(self, Self::Received) + } + + /// Returns `true` if the transaction status is `Pending`. + pub const fn is_pending(&self) -> bool { + matches!(self, Self::Pending) + } + + /// Returns `true` if the transaction status is `Rejected`. + pub const fn is_rejected(&self) -> bool { + matches!(self, Self::Rejected) + } + + /// Returns `true` if the transaction status is `Reverted`. + pub const fn is_reverted(&self) -> bool { + matches!(self, Self::Reverted) + } + + /// Returns `true` if the transaction status is `AcceptedOnL2`. + pub const fn is_accepted_on_l2(&self) -> bool { + matches!(self, Self::AcceptedOnL2) + } + + /// Returns `true` if the transaction status is `AcceptedOnL1`. + pub const fn is_accepted_on_l1(&self) -> bool { + matches!(self, Self::AcceptedOnL1) + } +} + #[derive(Debug, Deserialize, PartialEq, Eq)] #[serde(rename_all = "SCREAMING_SNAKE_CASE")] #[cfg_attr(feature = "no_unknown_fields", serde(deny_unknown_fields))]