multiversx_sdk/gateway/
gateway_block.rs

1use crate::data::hyperblock::{HyperBlock, HyperBlockResponse};
2use anyhow::anyhow;
3
4use super::{
5    GatewayRequest, GatewayRequestType, GET_HYPER_BLOCK_BY_HASH_ENDPOINT,
6    GET_HYPER_BLOCK_BY_NONCE_ENDPOINT,
7};
8
9/// Retrieves the data of a hyper block.
10pub struct GetHyperBlockRequest {
11    pub query: String,
12}
13
14impl GetHyperBlockRequest {
15    pub fn by_nonce(nonce: u64) -> Self {
16        Self {
17            query: format!("{GET_HYPER_BLOCK_BY_NONCE_ENDPOINT}/{nonce}"),
18        }
19    }
20
21    pub fn by_hash(hash: &str) -> Self {
22        Self {
23            query: format!("{GET_HYPER_BLOCK_BY_HASH_ENDPOINT}/{hash}"),
24        }
25    }
26}
27
28impl GatewayRequest for GetHyperBlockRequest {
29    type Payload = ();
30    type DecodedJson = HyperBlockResponse;
31    type Result = HyperBlock;
32
33    fn request_type(&self) -> GatewayRequestType {
34        GatewayRequestType::Get
35    }
36
37    fn get_endpoint(&self) -> String {
38        self.query.clone()
39    }
40
41    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
42        match decoded.data {
43            None => Err(anyhow!("{}", decoded.error)),
44            Some(b) => Ok(b.hyperblock),
45        }
46    }
47}