multiversx_sdk/gateway/
gateway_tx_info.rs

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
use crate::data::transaction::{TransactionInfo, TransactionOnNetwork};
use anyhow::anyhow;

use super::{
    GatewayRequest, GatewayRequestType, GET_TRANSACTION_INFO_ENDPOINT, WITH_RESULTS_QUERY_PARAM,
};

/// Retrieves transaction data from the network.
pub struct GetTxInfo<'a> {
    pub hash: &'a str,
    pub with_results: bool,
}

impl<'a> GetTxInfo<'a> {
    pub fn new(hash: &'a str) -> Self {
        Self {
            hash,
            with_results: true,
        }
    }

    pub fn with_results(self) -> Self {
        Self {
            hash: self.hash,
            with_results: true,
        }
    }
}

impl GatewayRequest for GetTxInfo<'_> {
    type Payload = ();
    type DecodedJson = TransactionInfo;
    type Result = TransactionOnNetwork;

    fn request_type(&self) -> GatewayRequestType {
        GatewayRequestType::Get
    }

    fn get_endpoint(&self) -> String {
        let mut endpoint = format!("{GET_TRANSACTION_INFO_ENDPOINT}/{}", self.hash);

        if self.with_results {
            endpoint += WITH_RESULTS_QUERY_PARAM;
        }

        endpoint
    }

    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
        match decoded.data {
            None => Err(anyhow!("{}", decoded.error)),
            Some(b) => Ok(b.transaction),
        }
    }
}