Skip to content

Commit

Permalink
refactor: use receipts from TransactionStatus (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
hal3e authored Dec 13, 2023
1 parent 135a2c8 commit cb2e4c8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 49 deletions.
1 change: 0 additions & 1 deletion packages/fuels-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fuel-core-client = { workspace = true, optional = true }
fuel-crypto = { workspace = true, features = ["random"] }
fuel-tx = { workspace = true }
fuel-types = { workspace = true, features = ["random"] }
fuel-vm = { workspace = true }
fuels-core = { workspace = true, default-features = false }
hex = { workspace = true, default-features = false, features = ["std"] }
rand = { workspace = true, default-features = false }
Expand Down
45 changes: 8 additions & 37 deletions packages/fuels-accounts/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ mod supported_versions;
use std::sync::Arc;

use chrono::{DateTime, Utc};
#[cfg(feature = "coin-cache")]
use fuel_core_client::client::types::TransactionStatus;
use fuel_core_client::client::{
pagination::{PageDirection, PaginatedResult, PaginationRequest},
types::{balance::Balance, contract::ContractBalance, TransactionStatus},
types::{balance::Balance, contract::ContractBalance},
};
use fuel_tx::{
AssetId, ConsensusParameters, Receipt, ScriptExecutionResult, Transaction as FuelTransaction,
TxId, UtxoId,
};
use fuel_types::{Address, Bytes32, ChainId, Nonce};
use fuel_vm::state::ProgramState;
#[cfg(feature = "coin-cache")]
use fuels_core::types::coin_type_id::CoinTypeId;
use fuels_core::{
Expand Down Expand Up @@ -269,41 +270,11 @@ impl Provider {
}

pub async fn tx_status(&self, tx_id: &TxId) -> ProviderResult<TxStatus> {
let fetch_receipts = || async {
let receipts = self.client.receipts(tx_id).await?;
receipts.ok_or_else(|| ProviderError::ReceiptsNotPropagatedYet)
};

let tx_status = self.client.transaction_status(tx_id).await?;
let status = match tx_status {
TransactionStatus::Success { .. } => {
let receipts = fetch_receipts().await?;
TxStatus::Success { receipts }
}
TransactionStatus::Failure {
reason,
program_state,
..
} => {
let receipts = fetch_receipts().await?;
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,
reason,
revert_id,
}
}
TransactionStatus::Submitted { .. } => TxStatus::Submitted,
TransactionStatus::SqueezedOut { reason } => TxStatus::SqueezedOut { reason },
};

Ok(status)
self.client
.transaction_status(tx_id)
.await
.map(Into::into)
.map_err(Into::into)
}

pub async fn chain_info(&self) -> ProviderResult<ChainInfo> {
Expand Down
9 changes: 0 additions & 9 deletions packages/fuels-accounts/src/provider/retryable_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,6 @@ impl RetryableClient {
self.our_retry(|| self.client.submit(tx)).await
}

pub async fn receipts(&self, id: &TxId) -> io::Result<Option<Vec<Receipt>>> {
retry_util::retry(
|| self.client.receipts(id),
&self.retry_config,
|result| !matches!(result, Ok(Some(_))),
)
.await
}

pub async fn transaction_status(&self, id: &TxId) -> io::Result<TransactionStatus> {
self.our_retry(|| self.client.transaction_status(id)).await
}
Expand Down
6 changes: 4 additions & 2 deletions packages/fuels-core/src/types/tx_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ 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::Success { receipts, .. } => TxStatus::Success { receipts },
ClientTransactionStatus::Failure {
reason,
program_state,
receipts,
..
} => {
let revert_id = program_state
Expand All @@ -105,7 +107,7 @@ impl From<ClientTransactionStatus> for TxStatus {
})
.expect("Transaction failed without a `revert_id`");
TxStatus::Revert {
receipts: vec![],
receipts,
reason,
revert_id,
}
Expand Down

0 comments on commit cb2e4c8

Please sign in to comment.