multiversx_sdk/gateway/
gateway_block.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
use crate::data::hyperblock::{HyperBlock, HyperBlockResponse};
use anyhow::anyhow;

use super::{
    GatewayRequest, GatewayRequestType, GET_HYPER_BLOCK_BY_HASH_ENDPOINT,
    GET_HYPER_BLOCK_BY_NONCE_ENDPOINT,
};

/// Retrieves the data of a hyper block.
pub struct GetHyperBlockRequest {
    pub query: String,
}

impl GetHyperBlockRequest {
    pub fn by_nonce(nonce: u64) -> Self {
        Self {
            query: format!("{GET_HYPER_BLOCK_BY_NONCE_ENDPOINT}/{nonce}"),
        }
    }

    pub fn by_hash(hash: &str) -> Self {
        Self {
            query: format!("{GET_HYPER_BLOCK_BY_HASH_ENDPOINT}/{hash}"),
        }
    }
}

impl GatewayRequest for GetHyperBlockRequest {
    type Payload = ();
    type DecodedJson = HyperBlockResponse;
    type Result = HyperBlock;

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

    fn get_endpoint(&self) -> String {
        self.query.clone()
    }

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