multiversx_sdk/gateway/
gateway_chain_simulator_blocks.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
56
57
58
59
60
61
62
63
64
65
66
67
use anyhow::anyhow;
use serde::{Deserialize, Serialize};

use super::{
    GatewayRequest, GatewayRequestType, GENERATE_BLOCKS_ENDPOINT,
    GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT, GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenerateBlocksResponse {
    pub data: serde_json::Value,
    pub error: String,
    pub code: String,
}

/// Generates blocks using the chain simulator API.
pub struct ChainSimulatorGenerateBlocksRequest {
    pub query: String,
}

impl ChainSimulatorGenerateBlocksRequest {
    pub fn num_blocks(num_blocks: u64) -> Self {
        Self {
            query: format!("{}/{}", GENERATE_BLOCKS_ENDPOINT, num_blocks),
        }
    }

    pub fn until_epoch(epoch_number: u64) -> Self {
        Self {
            query: format!(
                "{}/{}",
                GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT, epoch_number
            ),
        }
    }

    /// TODO: convert arg to H256
    pub fn until_tx_processed(tx_hash: &str) -> Self {
        Self {
            query: format!(
                "{}/{}",
                GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT, tx_hash
            ),
        }
    }
}

impl GatewayRequest for ChainSimulatorGenerateBlocksRequest {
    type Payload = ();
    type DecodedJson = GenerateBlocksResponse;
    type Result = String;

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

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

    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
        match decoded.code.as_str() {
            "successful" => Ok(decoded.code),
            _ => Err(anyhow!("{}", decoded.error)),
        }
    }
}