Skip to content

Commit

Permalink
refactor: use a single type for transaction status (#1186)
Browse files Browse the repository at this point in the history
This PR closes #1179 by removing the `TransactionStatus` type that was
used in only one place.

Further work is needed because the current `TxStatus` holds receipts
which the client `TransactionStatus` does not,
and doesn't hold some information the client's type does (block id,
time, etc).

---------

Co-authored-by: MujkicA <[email protected]>
  • Loading branch information
iqdecay and MujkicA authored Nov 8, 2023
1 parent dd5b6b2 commit 4b0aa6b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl Provider {
TxStatus::Revert {
receipts,
reason,
id: revert_id,
revert_id,
}
}
TransactionStatus::Submitted { .. } => TxStatus::Submitted,
Expand Down Expand Up @@ -384,7 +384,7 @@ impl Provider {
Some(reason) => TxStatus::Revert {
receipts,
reason,
id: 0,
revert_id: 0,
},
None => TxStatus::Success { receipts },
}
Expand Down
35 changes: 33 additions & 2 deletions packages/fuels-core/src/types/tx_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ use fuel_abi_types::error_codes::{
FAILED_ASSERT_EQ_SIGNAL, FAILED_ASSERT_SIGNAL, FAILED_REQUIRE_SIGNAL,
FAILED_SEND_MESSAGE_SIGNAL, FAILED_TRANSFER_TO_ADDRESS_SIGNAL,
};
#[cfg(feature = "std")]
use fuel_core_client::client::types::TransactionStatus as ClientTransactionStatus;
use fuel_tx::Receipt;
#[cfg(feature = "std")]
use fuel_vm::state::ProgramState;

use crate::{
codec::LogDecoder,
Expand All @@ -21,7 +25,7 @@ pub enum TxStatus {
Revert {
receipts: Vec<Receipt>,
reason: String,
id: u64,
revert_id: u64,
},
}

Expand All @@ -32,7 +36,7 @@ impl TxStatus {
Self::Revert {
receipts,
reason,
id,
revert_id: id,
} => Self::map_revert_error(receipts, reason, *id, log_decoder),
_ => Ok(()),
}
Expand Down Expand Up @@ -83,3 +87,30 @@ impl TxStatus {
}
}
}
#[cfg(feature = "std")]
impl From<ClientTransactionStatus> for TxStatus {
fn from(client_status: ClientTransactionStatus) -> Self {
match client_status {
ClientTransactionStatus::Submitted { .. } => TxStatus::Submitted {},
ClientTransactionStatus::Success { .. } => TxStatus::Success { receipts: vec![] },
ClientTransactionStatus::Failure {
reason,
program_state,
..
} => {
let revert_id = program_state
.and_then(|state| match state {
ProgramState::Revert(revert_id) => Some(revert_id),
_ => None,
})
.expect("Transaction failed without a `revert_id`");
TxStatus::Revert {
receipts: vec![],
reason,
revert_id,
}
}
ClientTransactionStatus::SqueezedOut { reason } => TxStatus::SqueezedOut { reason },
}
}
}
22 changes: 2 additions & 20 deletions packages/fuels-core/src/types/wrappers/transaction_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,16 @@ use fuel_tx::Transaction;
use fuel_types::Bytes32;

use crate::types::transaction::{CreateTransaction, ScriptTransaction, TransactionType};
use crate::types::tx_status::TxStatus;

#[derive(Debug, Clone)]
pub struct TransactionResponse {
pub transaction: TransactionType,
pub status: TransactionStatus,
pub status: TxStatus,
pub block_id: Option<Bytes32>,
pub time: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone)]
pub enum TransactionStatus {
Submitted(),
Success(),
Failure(),
SqueezedOut(),
}

impl From<ClientTransactionResponse> for TransactionResponse {
fn from(client_response: ClientTransactionResponse) -> Self {
let block_id = match &client_response.status {
Expand Down Expand Up @@ -63,14 +56,3 @@ impl From<ClientTransactionResponse> for TransactionResponse {
}
}
}

impl From<ClientTransactionStatus> for TransactionStatus {
fn from(client_status: ClientTransactionStatus) -> Self {
match client_status {
ClientTransactionStatus::Submitted { .. } => TransactionStatus::Submitted(),
ClientTransactionStatus::Success { .. } => TransactionStatus::Success(),
ClientTransactionStatus::Failure { .. } => TransactionStatus::Failure(),
ClientTransactionStatus::SqueezedOut { .. } => TransactionStatus::SqueezedOut(),
}
}
}

0 comments on commit 4b0aa6b

Please sign in to comment.