multiversx_sdk/gateway/
gateway_chain_simulator_blocks.rs

1use anyhow::anyhow;
2use serde::{Deserialize, Serialize};
3
4use super::{
5    GatewayRequest, GatewayRequestType, GENERATE_BLOCKS_ENDPOINT,
6    GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT, GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT,
7};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct GenerateBlocksResponse {
11    pub data: serde_json::Value,
12    pub error: String,
13    pub code: String,
14}
15
16/// Generates blocks using the chain simulator API.
17pub struct ChainSimulatorGenerateBlocksRequest {
18    pub query: String,
19}
20
21impl ChainSimulatorGenerateBlocksRequest {
22    pub fn num_blocks(num_blocks: u64) -> Self {
23        Self {
24            query: format!("{}/{}", GENERATE_BLOCKS_ENDPOINT, num_blocks),
25        }
26    }
27
28    pub fn until_epoch(epoch_number: u64) -> Self {
29        Self {
30            query: format!(
31                "{}/{}",
32                GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT, epoch_number
33            ),
34        }
35    }
36
37    /// TODO: convert arg to H256
38    pub fn until_tx_processed(tx_hash: &str) -> Self {
39        Self {
40            query: format!(
41                "{}/{}",
42                GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT, tx_hash
43            ),
44        }
45    }
46}
47
48impl GatewayRequest for ChainSimulatorGenerateBlocksRequest {
49    type Payload = ();
50    type DecodedJson = GenerateBlocksResponse;
51    type Result = String;
52
53    fn request_type(&self) -> GatewayRequestType {
54        GatewayRequestType::Post
55    }
56
57    fn get_endpoint(&self) -> String {
58        self.query.clone()
59    }
60
61    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
62        match decoded.code.as_str() {
63            "successful" => Ok(decoded.code),
64            _ => Err(anyhow!("{}", decoded.error)),
65        }
66    }
67}