multiversx_sdk/gateway/
gateway_chain_simulator_send_funds.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 std::collections::HashMap;

use anyhow::anyhow;
use multiversx_chain_core::types::Address;

use super::{
    gateway_chain_simulator_blocks::GenerateBlocksResponse, GatewayRequest, GatewayRequestType,
    SEND_USER_FUNDS_ENDPOINT,
};

/// Generates blocks using the chain simulator API.
pub struct ChainSimulatorSendFundsRequest {
    payload: HashMap<&'static str, String>,
}

impl ChainSimulatorSendFundsRequest {
    pub fn to_address(receiver: &Address) -> Self {
        let mut payload = HashMap::new();
        payload.insert("receiver", crate::bech32::encode(receiver));
        Self { payload }
    }
}

impl GatewayRequest for ChainSimulatorSendFundsRequest {
    type Payload = HashMap<&'static str, String>;
    type DecodedJson = GenerateBlocksResponse;
    type Result = String;

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

    fn get_endpoint(&self) -> String {
        SEND_USER_FUNDS_ENDPOINT.to_owned()
    }

    fn get_payload(&self) -> Option<&Self::Payload> {
        Some(&self.payload)
    }

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