Skip to content

Commit c6c2714

Browse files
authored
feat: add is_ utils to TransactionStatus enums (#606)
1 parent 1a28e95 commit c6c2714

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

starknet-core/src/types/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,41 @@ pub enum ContractClass {
174174
Legacy(CompressedLegacyContractClass),
175175
}
176176

177+
/// Represents the status of a transaction.
177178
#[derive(Debug, Clone, PartialEq, Eq)]
178179
pub enum TransactionStatus {
180+
/// Transaction received and awaiting processing.
179181
Received,
182+
/// Transaction rejected due to validation or other reasons.
180183
Rejected,
184+
/// Transaction accepted on Layer 2 with a specific execution status.
181185
AcceptedOnL2(TransactionExecutionStatus),
186+
/// Transaction accepted on Layer 1 with a specific execution status.
182187
AcceptedOnL1(TransactionExecutionStatus),
183188
}
184189

190+
impl TransactionStatus {
191+
/// Returns `true` if the transaction status is `Received`.
192+
pub const fn is_received(&self) -> bool {
193+
matches!(self, Self::Received)
194+
}
195+
196+
/// Returns `true` if the transaction status is `Rejected`.
197+
pub const fn is_rejected(&self) -> bool {
198+
matches!(self, Self::Rejected)
199+
}
200+
201+
/// Returns `true` if the transaction status is `AcceptedOnL2`.
202+
pub const fn is_accepted_on_l2(&self) -> bool {
203+
matches!(self, Self::AcceptedOnL2(_))
204+
}
205+
206+
/// Returns `true` if the transaction status is `AcceptedOnL1`.
207+
pub const fn is_accepted_on_l1(&self) -> bool {
208+
matches!(self, Self::AcceptedOnL1(_))
209+
}
210+
}
211+
185212
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
186213
#[serde(tag = "type")]
187214
pub enum Transaction {

starknet-providers/src/sequencer/models/conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ impl TryFrom<TransactionStatusInfo> for core::TransactionStatus {
902902
type Error = ConversionError;
903903

904904
fn try_from(value: TransactionStatusInfo) -> Result<Self, Self::Error> {
905-
if let TransactionStatus::Rejected = value.status {
905+
if value.status.is_rejected() {
906906
return Ok(Self::Rejected);
907907
}
908908

starknet-providers/src/sequencer/models/transaction.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ mod tests {
344344
let tx: TransactionInfo = serde_json::from_str(raw).unwrap();
345345

346346
assert_eq!(tx.block_number, None);
347-
assert_eq!(tx.status, TransactionStatus::NotReceived);
347+
assert!(tx.status.is_not_received());
348348
}
349349

350350
#[test]
@@ -459,7 +459,7 @@ mod tests {
459459

460460
let tx: TransactionStatusInfo = serde_json::from_str(raw).unwrap();
461461

462-
assert_eq!(tx.status, TransactionStatus::AcceptedOnL1);
462+
assert!(tx.status.is_accepted_on_l1());
463463
assert_eq!(
464464
tx.block_hash,
465465
Some(
@@ -478,7 +478,7 @@ mod tests {
478478

479479
let tx: TransactionStatusInfo = serde_json::from_str(raw).unwrap();
480480

481-
assert_eq!(tx.status, TransactionStatus::NotReceived);
481+
assert!(tx.status.is_not_received());
482482
assert!(tx.block_hash.is_none());
483483
}
484484

@@ -492,7 +492,7 @@ mod tests {
492492

493493
let tx: TransactionStatusInfo = serde_json::from_str(raw).unwrap();
494494

495-
assert_eq!(tx.status, TransactionStatus::Rejected);
495+
assert!(tx.status.is_rejected());
496496
assert!(tx.block_hash.is_none());
497497
assert!(tx.transaction_failure_reason.is_some());
498498
}

starknet-providers/src/sequencer/models/transaction_receipt.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ pub enum TransactionStatus {
5050
AcceptedOnL1,
5151
}
5252

53+
impl TransactionStatus {
54+
/// Returns `true` if the transaction status is `NotReceived`.
55+
pub const fn is_not_received(&self) -> bool {
56+
matches!(self, Self::NotReceived)
57+
}
58+
59+
/// Returns `true` if the transaction status is `Received`.
60+
pub const fn is_received(&self) -> bool {
61+
matches!(self, Self::Received)
62+
}
63+
64+
/// Returns `true` if the transaction status is `Pending`.
65+
pub const fn is_pending(&self) -> bool {
66+
matches!(self, Self::Pending)
67+
}
68+
69+
/// Returns `true` if the transaction status is `Rejected`.
70+
pub const fn is_rejected(&self) -> bool {
71+
matches!(self, Self::Rejected)
72+
}
73+
74+
/// Returns `true` if the transaction status is `Reverted`.
75+
pub const fn is_reverted(&self) -> bool {
76+
matches!(self, Self::Reverted)
77+
}
78+
79+
/// Returns `true` if the transaction status is `AcceptedOnL2`.
80+
pub const fn is_accepted_on_l2(&self) -> bool {
81+
matches!(self, Self::AcceptedOnL2)
82+
}
83+
84+
/// Returns `true` if the transaction status is `AcceptedOnL1`.
85+
pub const fn is_accepted_on_l1(&self) -> bool {
86+
matches!(self, Self::AcceptedOnL1)
87+
}
88+
}
89+
5390
#[derive(Debug, Deserialize, PartialEq, Eq)]
5491
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
5592
#[cfg_attr(feature = "no_unknown_fields", serde(deny_unknown_fields))]

0 commit comments

Comments
 (0)