multiversx_sdk/gateway/
gateway_chain_simulator_set_state_overwrite.rs

1use anyhow::anyhow;
2
3use super::{
4    gateway_chain_simulator_set_state::SetStateResponse, GatewayRequest, GatewayRequestType,
5    SetStateAccount, SET_STATE_OVERWRITE_ENDPOINT,
6};
7
8/// Sets state for a list of accounts using the chain simulator API.
9/// Overwrites previous state.
10pub struct ChainSimulatorSetStateOverwriteRequest {
11    pub accounts: Vec<SetStateAccount>,
12}
13
14impl ChainSimulatorSetStateOverwriteRequest {
15    pub fn for_accounts(accounts: Vec<SetStateAccount>) -> Self {
16        Self { accounts }
17    }
18}
19
20impl GatewayRequest for ChainSimulatorSetStateOverwriteRequest {
21    type Payload = Vec<SetStateAccount>;
22    type DecodedJson = SetStateResponse;
23    type Result = String;
24
25    fn get_payload(&self) -> Option<&Self::Payload> {
26        Some(&self.accounts)
27    }
28
29    fn request_type(&self) -> GatewayRequestType {
30        GatewayRequestType::Post
31    }
32
33    fn get_endpoint(&self) -> String {
34        SET_STATE_OVERWRITE_ENDPOINT.to_owned()
35    }
36
37    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
38        match decoded.code.as_str() {
39            "successful" => Ok(decoded.code),
40            _ => Err(anyhow!("{}", decoded.error)),
41        }
42    }
43}