From 4b0aa6b602f20cb7e877b394870ec3c2d9109058 Mon Sep 17 00:00:00 2001 From: iqdecay Date: Wed, 8 Nov 2023 15:42:55 +0100 Subject: [PATCH] refactor: use a single type for transaction status (#1186) 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 <32431923+MujkicA@users.noreply.github.com> --- packages/fuels-accounts/src/provider.rs | 4 +-- packages/fuels-core/src/types/tx_status.rs | 35 +++++++++++++++++-- .../types/wrappers/transaction_response.rs | 22 ++---------- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/packages/fuels-accounts/src/provider.rs b/packages/fuels-accounts/src/provider.rs index 629d6a61b9..237a20601f 100644 --- a/packages/fuels-accounts/src/provider.rs +++ b/packages/fuels-accounts/src/provider.rs @@ -307,7 +307,7 @@ impl Provider { TxStatus::Revert { receipts, reason, - id: revert_id, + revert_id, } } TransactionStatus::Submitted { .. } => TxStatus::Submitted, @@ -384,7 +384,7 @@ impl Provider { Some(reason) => TxStatus::Revert { receipts, reason, - id: 0, + revert_id: 0, }, None => TxStatus::Success { receipts }, } diff --git a/packages/fuels-core/src/types/tx_status.rs b/packages/fuels-core/src/types/tx_status.rs index 7b6f7b7cc0..e6d7ca5e78 100644 --- a/packages/fuels-core/src/types/tx_status.rs +++ b/packages/fuels-core/src/types/tx_status.rs @@ -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, @@ -21,7 +25,7 @@ pub enum TxStatus { Revert { receipts: Vec, reason: String, - id: u64, + revert_id: u64, }, } @@ -32,7 +36,7 @@ impl TxStatus { Self::Revert { receipts, reason, - id, + revert_id: id, } => Self::map_revert_error(receipts, reason, *id, log_decoder), _ => Ok(()), } @@ -83,3 +87,30 @@ impl TxStatus { } } } +#[cfg(feature = "std")] +impl From 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 }, + } + } +} diff --git a/packages/fuels-core/src/types/wrappers/transaction_response.rs b/packages/fuels-core/src/types/wrappers/transaction_response.rs index 6d8f64d914..87118be29a 100644 --- a/packages/fuels-core/src/types/wrappers/transaction_response.rs +++ b/packages/fuels-core/src/types/wrappers/transaction_response.rs @@ -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, pub time: Option>, } -#[derive(Debug, Clone)] -pub enum TransactionStatus { - Submitted(), - Success(), - Failure(), - SqueezedOut(), -} - impl From for TransactionResponse { fn from(client_response: ClientTransactionResponse) -> Self { let block_id = match &client_response.status { @@ -63,14 +56,3 @@ impl From for TransactionResponse { } } } - -impl From 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(), - } - } -}