multiversx_sdk/gateway/
gateway_tx_info.rs

1use crate::data::transaction::{TransactionInfo, TransactionOnNetwork};
2use anyhow::anyhow;
3
4use super::{
5    GatewayRequest, GatewayRequestType, GET_TRANSACTION_INFO_ENDPOINT, WITH_RESULTS_QUERY_PARAM,
6};
7
8/// Retrieves transaction data from the network.
9pub struct GetTxInfo<'a> {
10    pub hash: &'a str,
11    pub with_results: bool,
12}
13
14impl<'a> GetTxInfo<'a> {
15    pub fn new(hash: &'a str) -> Self {
16        Self {
17            hash,
18            with_results: true,
19        }
20    }
21
22    pub fn with_results(self) -> Self {
23        Self {
24            hash: self.hash,
25            with_results: true,
26        }
27    }
28}
29
30impl GatewayRequest for GetTxInfo<'_> {
31    type Payload = ();
32    type DecodedJson = TransactionInfo;
33    type Result = TransactionOnNetwork;
34
35    fn request_type(&self) -> GatewayRequestType {
36        GatewayRequestType::Get
37    }
38
39    fn get_endpoint(&self) -> String {
40        let mut endpoint = format!("{GET_TRANSACTION_INFO_ENDPOINT}/{}", self.hash);
41
42        if self.with_results {
43            endpoint += WITH_RESULTS_QUERY_PARAM;
44        }
45
46        endpoint
47    }
48
49    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
50        match decoded.data {
51            None => Err(anyhow!("{}", decoded.error)),
52            Some(b) => Ok(b.transaction),
53        }
54    }
55}