multiversx_sdk/
gateway.rs

1mod gateway_account;
2mod gateway_account_esdt_roles;
3mod gateway_account_esdt_tokens;
4mod gateway_account_storage;
5mod gateway_block;
6mod gateway_chain_simulator_add_keys;
7mod gateway_chain_simulator_blocks;
8mod gateway_chain_simulator_send_funds;
9mod gateway_chain_simulator_set_state;
10mod gateway_chain_simulator_set_state_overwrite;
11mod gateway_network_config;
12mod gateway_network_economics;
13mod gateway_network_status;
14mod gateway_tx_cost;
15mod gateway_tx_info;
16mod gateway_tx_process_status;
17mod gateway_tx_send;
18mod gateway_tx_send_multi;
19mod gateway_tx_status;
20mod gateway_tx_vmquery;
21
22use std::fmt::Display;
23
24pub use gateway_account::GetAccountRequest;
25pub use gateway_account_esdt_roles::GetAccountEsdtRolesRequest;
26pub use gateway_account_esdt_tokens::GetAccountEsdtTokensRequest;
27pub use gateway_account_storage::GetAccountStorageRequest;
28pub use gateway_block::GetHyperBlockRequest;
29pub use gateway_chain_simulator_add_keys::ChainSimulatorAddKeysRequest;
30pub use gateway_chain_simulator_blocks::ChainSimulatorGenerateBlocksRequest;
31pub use gateway_chain_simulator_send_funds::ChainSimulatorSendFundsRequest;
32pub use gateway_chain_simulator_set_state::{ChainSimulatorSetStateRequest, SetStateAccount};
33pub use gateway_chain_simulator_set_state_overwrite::ChainSimulatorSetStateOverwriteRequest;
34pub use gateway_network_config::NetworkConfigRequest;
35pub use gateway_network_economics::NetworkEconimicsRequest;
36pub use gateway_network_status::NetworkStatusRequest;
37pub use gateway_tx_cost::GetTxCost;
38pub use gateway_tx_info::GetTxInfo;
39pub use gateway_tx_process_status::GetTxProcessStatus;
40pub use gateway_tx_send::SendTxRequest;
41pub use gateway_tx_send_multi::SendMultiTxRequest;
42pub use gateway_tx_status::GetTxStatus;
43pub use gateway_tx_vmquery::VMQueryRequest;
44
45pub const MAINNET_GATEWAY: &str = "https://gateway.multiversx.com";
46pub const TESTNET_GATEWAY: &str = "https://testnet-gateway.multiversx.com";
47pub const DEVNET_GATEWAY: &str = "https://devnet-gateway.multiversx.com";
48
49// MetachainShardId will be used to identify a shard ID as metachain
50pub const METACHAIN_SHARD_ID: u32 = 0xFFFFFFFF;
51
52const ACCOUNT_ENDPOINT: &str = "address";
53const KEYS_ENDPOINT: &str = "keys";
54const NETWORK_CONFIG_ENDPOINT: &str = "network/config";
55const NETWORK_ECONOMICS_ENDPOINT: &str = "network/economics";
56const GET_NETWORK_STATUS_ENDPOINT: &str = "network/status";
57const GET_HYPER_BLOCK_BY_NONCE_ENDPOINT: &str = "hyperblock/by-nonce";
58const GET_HYPER_BLOCK_BY_HASH_ENDPOINT: &str = "hyperblock/by-hash";
59const COST_TRANSACTION_ENDPOINT: &str = "transaction/cost";
60const SEND_TRANSACTION_ENDPOINT: &str = "transaction/send";
61const SEND_MULTIPLE_TRANSACTIONS_ENDPOINT: &str = "transaction/send-multiple";
62const GET_TRANSACTION_INFO_ENDPOINT: &str = "transaction";
63const WITH_RESULTS_QUERY_PARAM: &str = "?withResults=true";
64const VM_VALUES_ENDPOINT: &str = "vm-values/query";
65
66const SEND_USER_FUNDS_ENDPOINT: &str = "transaction/send-user-funds";
67const GENERATE_BLOCKS_ENDPOINT: &str = "simulator/generate-blocks";
68const GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT: &str =
69    "simulator/generate-blocks-until-transaction-processed";
70const GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT: &str =
71    "simulator/generate-blocks-until-epoch-reached";
72const SET_STATE_ENDPOINT: &str = "simulator/set-state";
73const SET_STATE_OVERWRITE_ENDPOINT: &str = "simulator/set-state-overwrite";
74const ADD_KEYS: &str = "simulator/add-keys";
75
76#[derive(Clone, Copy, PartialEq, Eq, Debug)]
77pub enum GatewayRequestType {
78    Get,
79    Post,
80}
81
82impl Display for GatewayRequestType {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        // padding makes the logs prettier
85        let padded_str = match self {
86            GatewayRequestType::Get => "GET ",
87            GatewayRequestType::Post => "POST",
88        };
89        padded_str.fmt(f)
90    }
91}
92
93/// Models requests to the gateway.
94pub trait GatewayRequest {
95    type Payload: serde::ser::Serialize + ?Sized;
96
97    type DecodedJson: serde::de::DeserializeOwned;
98
99    type Result;
100
101    fn request_type(&self) -> GatewayRequestType;
102
103    fn get_endpoint(&self) -> String;
104
105    fn get_payload(&self) -> Option<&Self::Payload> {
106        None
107    }
108
109    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result>;
110}
111
112pub trait GatewayAsyncService {
113    /// Keeps track of elapsed time.
114    type Instant;
115
116    fn from_uri(uri: &str) -> Self;
117
118    fn request<G>(
119        &self,
120        request: G,
121    ) -> impl std::future::Future<Output = anyhow::Result<G::Result>>
122    where
123        G: GatewayRequest;
124
125    fn sleep(&self, millis: u64) -> impl std::future::Future<Output = ()>;
126
127    fn now(&self) -> Self::Instant;
128
129    fn elapsed_seconds(&self, instant: &Self::Instant) -> f32;
130}