fuels_programs/responses/
submit.rs

1use std::fmt::Debug;
2
3use fuel_types::Bytes32;
4use fuels_accounts::Account;
5use fuels_core::{
6    traits::{Parameterize, Tokenizable},
7    types::errors::Result,
8};
9
10use crate::{
11    calls::{
12        traits::{ContractDependencyConfigurator, ResponseParser, TransactionTuner},
13        CallHandler, ContractCall,
14    },
15    responses::CallResponse,
16};
17
18/// Represents the response of a submitted transaction with customizable retry behavior.
19///
20/// This struct holds information about the retry configuration, transaction ID (`tx_id`),
21/// and the call handler that manages the type of call (contract or script).
22///
23/// # Type Parameters
24///
25/// - `T`: The account type associated with the transaction.
26/// - `D`: The data type representing the response value.
27/// - `C`: The call type.
28///
29/// # Fields
30///
31/// - `retry_config`: The retry configuration for the transaction.
32/// - `tx_id`: The optional transaction ID of the submitted transaction.
33/// - `call_handler`: The call handler that manages the type of call.
34///
35/// ```
36#[derive(Debug)]
37pub struct SubmitResponse<A, C, T> {
38    tx_id: Bytes32,
39    call_handler: CallHandler<A, C, T>,
40}
41
42impl<A, C, T> SubmitResponse<A, C, T>
43where
44    A: Account,
45    C: ContractDependencyConfigurator + TransactionTuner + ResponseParser,
46    T: Tokenizable + Parameterize + Debug,
47{
48    pub fn new(tx_id: Bytes32, call_handler: CallHandler<A, C, T>) -> Self {
49        Self {
50            tx_id,
51            call_handler,
52        }
53    }
54
55    pub async fn response(self) -> Result<CallResponse<T>> {
56        let provider = self.call_handler.account.try_provider()?;
57        let tx_status = provider.tx_status(&self.tx_id).await?;
58
59        self.call_handler.get_response(tx_status)
60    }
61
62    pub fn tx_id(&self) -> Bytes32 {
63        self.tx_id
64    }
65}
66
67/// Represents the response of a submitted transaction with multiple contract calls.
68impl<A: Account> SubmitResponse<A, Vec<ContractCall>, ()> {
69    pub fn new(tx_id: Bytes32, call_handler: CallHandler<A, Vec<ContractCall>, ()>) -> Self {
70        Self {
71            tx_id,
72            call_handler,
73        }
74    }
75
76    pub async fn response<T: Tokenizable + Debug>(self) -> Result<CallResponse<T>> {
77        let provider = self.call_handler.account.try_provider()?;
78        let tx_status = provider.tx_status(&self.tx_id).await?;
79
80        self.call_handler.get_response(tx_status)
81    }
82
83    pub fn tx_id(&self) -> Bytes32 {
84        self.tx_id
85    }
86}