Skip to content

Commit

Permalink
feat: add is_ utils to TransactionStatus enums (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Jun 19, 2024
1 parent 1a28e95 commit c6c2714
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
27 changes: 27 additions & 0 deletions starknet-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion starknet-providers/src/sequencer/models/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl TryFrom<TransactionStatusInfo> for core::TransactionStatus {
type Error = ConversionError;

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

Expand Down
8 changes: 4 additions & 4 deletions starknet-providers/src/sequencer/models/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand All @@ -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());
}

Expand All @@ -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());
}
Expand Down
37 changes: 37 additions & 0 deletions starknet-providers/src/sequencer/models/transaction_receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down

0 comments on commit c6c2714

Please sign in to comment.