1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::str::FromStr;

use chrono::DateTime;
use chrono::NaiveDateTime;
use chrono::Utc;
use fuel_gql_client::client::types::TransactionResponse as ClientTransactionResponse;
use fuel_gql_client::client::types::TransactionStatus as ClientTransactionStatus;
use fuel_tx::Bytes32;
use fuel_tx::Transaction;

#[derive(Debug, Clone)]
pub struct TransactionResponse {
    pub transaction: Transaction,
    pub status: TransactionStatus,
    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 {
            ClientTransactionStatus::Submitted { .. }
            | ClientTransactionStatus::SqueezedOut { .. } => None,
            ClientTransactionStatus::Success { block_id, .. }
            | ClientTransactionStatus::Failure { block_id, .. } => Some(block_id),
        };
        let block_id = block_id.map(|id| {
            Bytes32::from_str(id).expect("Client returned block id with invalid format.")
        });

        let time = match &client_response.status {
            ClientTransactionStatus::Submitted { .. }
            | ClientTransactionStatus::SqueezedOut { .. } => None,
            ClientTransactionStatus::Success { time, .. }
            | ClientTransactionStatus::Failure { time, .. } => {
                let native = NaiveDateTime::from_timestamp_opt(time.0 as i64, 0);
                native.map(|time| DateTime::<Utc>::from_utc(time, Utc))
            }
        };

        Self {
            transaction: client_response.transaction,
            status: client_response.status.into(),
            block_id,
            time,
        }
    }
}

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(),
        }
    }
}