From 628dd302fd03421c805fdf95a160b36440b37710 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 2 Oct 2024 17:29:45 +0300 Subject: [PATCH 1/2] sdk - chain simulator requests refactor --- sdk/core/src/gateway.rs | 11 ++ .../gateway/gateway_chain_simulator_blocks.rs | 67 +++++++++++ .../gateway_chain_simulator_send_funds.rs | 47 ++++++++ sdk/http/src/gateway_http_proxy.rs | 4 - .../http_chain_simulator.rs | 104 ++++-------------- 5 files changed, 146 insertions(+), 87 deletions(-) create mode 100644 sdk/core/src/gateway/gateway_chain_simulator_blocks.rs create mode 100644 sdk/core/src/gateway/gateway_chain_simulator_send_funds.rs diff --git a/sdk/core/src/gateway.rs b/sdk/core/src/gateway.rs index 3730381055..91292a361b 100644 --- a/sdk/core/src/gateway.rs +++ b/sdk/core/src/gateway.rs @@ -3,6 +3,8 @@ mod gateway_account_esdt_roles; mod gateway_account_esdt_tokens; mod gateway_account_storage; mod gateway_block; +mod gateway_chain_simulator_blocks; +mod gateway_chain_simulator_send_funds; mod gateway_network_config; mod gateway_network_economics; mod gateway_network_status; @@ -19,6 +21,8 @@ pub use gateway_account_esdt_roles::GetAccountEsdtRolesRequest; pub use gateway_account_esdt_tokens::GetAccountEsdtTokensRequest; pub use gateway_account_storage::GetAccountStorageRequest; pub use gateway_block::GetHyperBlockRequest; +pub use gateway_chain_simulator_blocks::ChainSimulatorGenerateBlocksRequest; +pub use gateway_chain_simulator_send_funds::ChainSimulatorSendFundsRequest; pub use gateway_network_config::NetworkConfigRequest; pub use gateway_network_economics::NetworkEconimicsRequest; pub use gateway_network_status::NetworkStatusRequest; @@ -51,6 +55,13 @@ const GET_TRANSACTION_INFO_ENDPOINT: &str = "transaction/"; const WITH_RESULTS_QUERY_PARAM: &str = "?withResults=true"; const VM_VALUES_ENDPOINT: &str = "vm-values/query"; +const SEND_USER_FUNDS_ENDPOINT: &str = "transaction/send-user-funds"; +const GENERATE_BLOCKS_ENDPOINT: &str = "simulator/generate-blocks"; +const GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT: &str = + "simulator/generate-blocks-until-transaction-processed"; +const GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT: &str = + "simulator/generate-blocks-until-epoch-reached"; + pub enum GatewayRequestType { Get, Post, diff --git a/sdk/core/src/gateway/gateway_chain_simulator_blocks.rs b/sdk/core/src/gateway/gateway_chain_simulator_blocks.rs new file mode 100644 index 0000000000..9faecf0327 --- /dev/null +++ b/sdk/core/src/gateway/gateway_chain_simulator_blocks.rs @@ -0,0 +1,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 { + match decoded.code.as_str() { + "successful" => Ok(decoded.code), + _ => Err(anyhow!("{}", decoded.error)), + } + } +} diff --git a/sdk/core/src/gateway/gateway_chain_simulator_send_funds.rs b/sdk/core/src/gateway/gateway_chain_simulator_send_funds.rs new file mode 100644 index 0000000000..5dd5ec5da6 --- /dev/null +++ b/sdk/core/src/gateway/gateway_chain_simulator_send_funds.rs @@ -0,0 +1,47 @@ +use std::collections::HashMap; + +use anyhow::anyhow; + +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 { + /// TODO: convert to argument to Address + pub fn to_address(receiver: String) -> Self { + let mut payload = HashMap::new(); + payload.insert("receiver", 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::Get + } + + 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 { + match decoded.code.as_str() { + "successful" => Ok(decoded.code), + _ => Err(anyhow!("{}", decoded.error)), + } + } +} diff --git a/sdk/http/src/gateway_http_proxy.rs b/sdk/http/src/gateway_http_proxy.rs index cb86e126ef..deb1e214bc 100644 --- a/sdk/http/src/gateway_http_proxy.rs +++ b/sdk/http/src/gateway_http_proxy.rs @@ -24,10 +24,6 @@ impl GatewayHttpProxy { } } - pub(crate) fn get_endpoint(&self, endpoint: &str) -> String { - format!("{}/{}", self.proxy_uri, endpoint) - } - /// Performs a request to the gateway. /// Can be either GET or POST, depending on the argument. pub async fn http_request(&self, request: G) -> anyhow::Result diff --git a/sdk/http/src/gateway_http_proxy/http_chain_simulator.rs b/sdk/http/src/gateway_http_proxy/http_chain_simulator.rs index ec262dca28..0a68349c02 100644 --- a/sdk/http/src/gateway_http_proxy/http_chain_simulator.rs +++ b/sdk/http/src/gateway_http_proxy/http_chain_simulator.rs @@ -1,66 +1,28 @@ -use std::collections::HashMap; - use super::GatewayHttpProxy; -use anyhow::{anyhow, Error}; -use serde::{Deserialize, Serialize}; - -const SEND_USER_FUNDS_ENDPOINT: &str = "transaction/send-user-funds"; -const GENERATE_BLOCKS_ENDPOINT: &str = "simulator/generate-blocks"; -const GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT: &str = - "simulator/generate-blocks-until-transaction-processed"; -const GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT: &str = - "simulator/generate-blocks-until-epoch-reached"; - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct GenerateBlocksResponse { - pub data: serde_json::Value, - pub error: String, - pub code: String, -} +use anyhow::Error; +use multiversx_sdk::gateway::{ + ChainSimulatorGenerateBlocksRequest, ChainSimulatorSendFundsRequest, GatewayAsyncService, +}; impl GatewayHttpProxy { - pub async fn send_user_funds(&self, receiver: &String) -> Result { + pub async fn send_user_funds(&self, receiver: &str) -> Result { if !self.chain_simulator { return Ok(String::from("no-simulator")); } - let mut r = HashMap::new(); - r.insert("receiver", receiver); - let endpoint_funds = self.get_endpoint(SEND_USER_FUNDS_ENDPOINT); - let resp = self - .client - .post(endpoint_funds) - .json(&r) - .send() - .await? - .json::() - .await?; - - match resp.code.as_str() { - "successful" => Ok(resp.code), - _ => Err(anyhow!("{}", resp.error)), - } + self.request(ChainSimulatorSendFundsRequest::to_address( + receiver.to_owned(), + )) + .await } - pub async fn generate_blocks(&self, number_blocks: u64) -> Result { + pub async fn generate_blocks(&self, num_blocks: u64) -> Result { if !self.chain_simulator { return Ok(String::from("no-simulator")); } - let uri_gen_blocks: String = format!("{}/{}", GENERATE_BLOCKS_ENDPOINT, number_blocks); - let endpoint_blocks = self.get_endpoint(&uri_gen_blocks); - let resp = self - .client - .post(endpoint_blocks) - .send() - .await? - .json::() - .await?; - - match resp.code.as_str() { - "successful" => Ok(resp.code), - _ => Err(anyhow!("{}", resp.error)), - } + self.request(ChainSimulatorGenerateBlocksRequest::num_blocks(num_blocks)) + .await } pub async fn generate_blocks_until_epoch(&self, epoch_number: u64) -> Result { @@ -68,44 +30,20 @@ impl GatewayHttpProxy { return Ok(String::from("no-simulator")); } - let uri_gen_blocks_until_reached_epoch: String = format!( - "{}/{}", - GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_ENDPOINT, epoch_number - ); - let endpoint_blocks = self.get_endpoint(&uri_gen_blocks_until_reached_epoch); - let resp = self - .client - .post(endpoint_blocks) - .send() - .await? - .json::() - .await?; - - match resp.code.as_str() { - "successful" => Ok(resp.code), - _ => Err(anyhow!("{}", resp.error)), - } + self.request(ChainSimulatorGenerateBlocksRequest::until_epoch( + epoch_number, + )) + .await } - pub async fn generate_blocks_until_tx_processed(&self, tx: &String) -> Result { + pub async fn generate_blocks_until_tx_processed(&self, tx_hash: &str) -> Result { if !self.chain_simulator { return Ok(String::from("no-simulator")); } - let url_gen_blocks_until_tx_processed: String = - format!("{}/{}", GENERATE_BLOCKS_UNTIL_TX_PROCESSED_ENDPOINT, tx); - let endpoint_blocks = self.get_endpoint(&url_gen_blocks_until_tx_processed); - let resp = self - .client - .post(endpoint_blocks) - .send() - .await? - .json::() - .await?; - - match resp.code.as_str() { - "successful" => Ok(resp.code), - _ => Err(anyhow!("{}", resp.error)), - } + self.request(ChainSimulatorGenerateBlocksRequest::until_tx_processed( + tx_hash, + )) + .await } } From af798e1968e4e83468ce1875f0687df8fc54766f Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 2 Oct 2024 17:31:45 +0300 Subject: [PATCH 2/2] cleanup --- Cargo.lock | 2 -- sdk/http/Cargo.toml | 4 ---- 2 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0834b88f90..a1fa41dbad 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -2493,8 +2493,6 @@ dependencies = [ "log", "multiversx-sdk", "reqwest", - "serde", - "serde_json", "tokio", ] diff --git a/sdk/http/Cargo.toml b/sdk/http/Cargo.toml index cb304039a3..6e40582f76 100644 --- a/sdk/http/Cargo.toml +++ b/sdk/http/Cargo.toml @@ -23,10 +23,6 @@ hex = "0.4.3" itertools = "0.13.0" log = "0.4.17" -# TEMP -serde = { version = "1.0.130", features = ["derive"] } -serde_json = { version = "1.0.68", features = ["preserve_order"] } - [dependencies.multiversx-sdk] version = "=0.6.1" path = "../core"