From 09a6c8be3a77d8a15c72dcc7bf2b42edd5877f66 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 22:57:55 +0300 Subject: [PATCH 1/4] retrieve_tx_on_network moved to gateway proxy --- Cargo.lock | 1 + .../src/interactor_scenario/interactor_sc_call.rs | 2 +- .../src/interactor_scenario/interactor_sc_deploy.rs | 2 +- .../src/interactor_scenario/interactor_transfer.rs | 2 +- framework/snippets/src/lib.rs | 1 - .../src/multi/interactor_multi_sc_process.rs | 2 +- sdk/core/Cargo.toml | 1 + sdk/core/src/blockchain.rs | 2 ++ .../core/src/blockchain/gateway_tx_retrieve.rs | 12 ++++++------ 9 files changed, 14 insertions(+), 11 deletions(-) rename framework/snippets/src/interactor_retrieve.rs => sdk/core/src/blockchain/gateway_tx_retrieve.rs (87%) diff --git a/Cargo.lock b/Cargo.lock index fb08c18c3e..ec245b66b9 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -2076,6 +2076,7 @@ dependencies = [ "hex", "hmac", "itertools", + "log", "pbkdf2", "pem", "rand 0.8.5", diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 061005ea13..6802304ae9 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -14,7 +14,7 @@ impl Interactor { { let sc_call_step = sc_call_step.as_mut(); let tx_hash = self.launch_sc_call(sc_call_step).await; - let tx = self.retrieve_tx_on_network(tx_hash.clone()).await; + let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; sc_call_step.save_response(network_response::parse_tx_response(tx)); diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index 529efc38bb..59de249e83 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -53,7 +53,7 @@ impl Interactor { { let sc_deploy_step = sc_deploy_step.as_mut(); let tx_hash = self.launch_sc_deploy(sc_deploy_step).await; - let tx = self.retrieve_tx_on_network(tx_hash.clone()).await; + let tx = self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; let addr = sc_deploy_step.tx.from.clone(); let nonce = tx.nonce; diff --git a/framework/snippets/src/interactor_scenario/interactor_transfer.rs b/framework/snippets/src/interactor_scenario/interactor_transfer.rs index 2e99372b0f..6557e4119f 100644 --- a/framework/snippets/src/interactor_scenario/interactor_transfer.rs +++ b/framework/snippets/src/interactor_scenario/interactor_transfer.rs @@ -14,7 +14,7 @@ impl Interactor { println!("transfer tx hash: {tx_hash}"); info!("transfer tx hash: {}", tx_hash); - self.retrieve_tx_on_network(tx_hash.clone()).await; + self.proxy.retrieve_tx_on_network(tx_hash.clone()).await; self.post_runners.run_transfer_step(&transfer_step); diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index 6240a9f152..7d3b52c696 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -1,7 +1,6 @@ pub mod account_tool; mod interactor; mod interactor_dns; -mod interactor_retrieve; mod interactor_scenario; mod interactor_sender; mod interactor_tx; diff --git a/framework/snippets/src/multi/interactor_multi_sc_process.rs b/framework/snippets/src/multi/interactor_multi_sc_process.rs index f19dcfb171..3364a4d5ee 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_process.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_process.rs @@ -31,7 +31,7 @@ impl Interactor { .expect("failed to send transaction"); println!("process tx hash: {tx_hash} with nonce: {}", tx.nonce); - futures.push(self.retrieve_tx_on_network(tx_hash.clone())); + futures.push(self.proxy.retrieve_tx_on_network(tx_hash.clone())); } join_all(futures).await diff --git a/sdk/core/Cargo.toml b/sdk/core/Cargo.toml index 319c81b137..c8bf3c75fb 100644 --- a/sdk/core/Cargo.toml +++ b/sdk/core/Cargo.toml @@ -34,3 +34,4 @@ zeroize = "1.4.2" bech32 = "0.9" itertools = "0.12.0" pem = "3.0.2" +log = "0.4.17" diff --git a/sdk/core/src/blockchain.rs b/sdk/core/src/blockchain.rs index 73d97ef0ea..be80103195 100644 --- a/sdk/core/src/blockchain.rs +++ b/sdk/core/src/blockchain.rs @@ -1,3 +1,5 @@ +mod gateway_tx_retrieve; + use std::collections::HashMap; use crate::data::{ diff --git a/framework/snippets/src/interactor_retrieve.rs b/sdk/core/src/blockchain/gateway_tx_retrieve.rs similarity index 87% rename from framework/snippets/src/interactor_retrieve.rs rename to sdk/core/src/blockchain/gateway_tx_retrieve.rs index cd9291ba33..44a0357432 100644 --- a/framework/snippets/src/interactor_retrieve.rs +++ b/sdk/core/src/blockchain/gateway_tx_retrieve.rs @@ -1,28 +1,28 @@ -use crate::Interactor; +use crate::data::transaction::TransactionOnNetwork; use log::info; -use multiversx_sdk::data::transaction::TransactionOnNetwork; use std::time::{Duration, Instant}; +use super::CommunicationProxy; + const INITIAL_BACKOFF_DELAY: f32 = 1.4; const MAX_RETRIES: usize = 8; const MAX_BACKOFF_DELAY: Duration = Duration::from_secs(6); -impl Interactor { +impl CommunicationProxy { /// Retrieves a transaction from the network. - pub(crate) async fn retrieve_tx_on_network(&self, tx_hash: String) -> TransactionOnNetwork { + pub async fn retrieve_tx_on_network(&self, tx_hash: String) -> TransactionOnNetwork { let mut retries = 0; let mut backoff_delay = Duration::from_secs_f32(INITIAL_BACKOFF_DELAY); let start_time = Instant::now(); loop { - match self.proxy.get_transaction_status(&tx_hash).await { + match self.get_transaction_status(&tx_hash).await { Ok(status) => { // checks if transaction status is final match status.as_str() { "success" | "fail" => { // retrieve transaction info with results let transaction_info_with_results = self - .proxy .get_transaction_info_with_results(&tx_hash) .await .unwrap(); From 89524dbfada6cc6a975c58b5d53d7356872752c0 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 23:15:19 +0300 Subject: [PATCH 2/4] gateway proxy split into files --- sdk/core/src/blockchain.rs | 404 +-------------------- sdk/core/src/blockchain/gateway_account.rs | 112 ++++++ sdk/core/src/blockchain/gateway_block.rs | 66 ++++ sdk/core/src/blockchain/gateway_network.rs | 46 +++ sdk/core/src/blockchain/gateway_proxy.rs | 20 + sdk/core/src/blockchain/gateway_tx.rs | 184 ++++++++++ 6 files changed, 434 insertions(+), 398 deletions(-) create mode 100644 sdk/core/src/blockchain/gateway_account.rs create mode 100644 sdk/core/src/blockchain/gateway_block.rs create mode 100644 sdk/core/src/blockchain/gateway_network.rs create mode 100644 sdk/core/src/blockchain/gateway_proxy.rs create mode 100644 sdk/core/src/blockchain/gateway_tx.rs diff --git a/sdk/core/src/blockchain.rs b/sdk/core/src/blockchain.rs index be80103195..05bad7198a 100644 --- a/sdk/core/src/blockchain.rs +++ b/sdk/core/src/blockchain.rs @@ -1,25 +1,11 @@ +mod gateway_account; +mod gateway_block; +mod gateway_network; +mod gateway_proxy; +mod gateway_tx; mod gateway_tx_retrieve; -use std::collections::HashMap; - -use crate::data::{ - account::{Account, AccountResponse}, - account_storage::AccountStorageResponse, - address::Address, - esdt::{EsdtBalance, EsdtBalanceResponse, EsdtRolesResponse}, - hyperblock::{HyperBlock, HyperBlockResponse}, - network_config::{NetworkConfig, NetworkConfigResponse}, - network_economics::{NetworkEconomics, NetworkEconomicsResponse}, - network_status::NetworkStatusResponse, - transaction::{ - ArgCreateTransaction, ResponseTxCost, SendTransactionResponse, SendTransactionsResponse, - Transaction, TransactionInfo, TransactionOnNetwork, TransactionStatus, TxCostResponseData, - }, - vm::{ResponseVmValue, VmValueRequest, VmValuesResponseData}, -}; -use anyhow::{anyhow, Result}; -use itertools::Itertools; -use reqwest::Client; +pub use gateway_proxy::CommunicationProxy; pub const MAINNET_GATEWAY: &str = "https://gateway.multiversx.com"; pub const TESTNET_GATEWAY: &str = "https://testnet-gateway.multiversx.com"; @@ -27,381 +13,3 @@ pub const DEVNET_GATEWAY: &str = "https://devnet-gateway.multiversx.com"; // MetachainShardId will be used to identify a shard ID as metachain pub const METACHAIN_SHARD_ID: u32 = 0xFFFFFFFF; - -const NETWORK_CONFIG_ENDPOINT: &str = "network/config"; -const NETWORK_ECONOMICS_ENDPOINT: &str = "network/economics"; -const ACCOUNT_ENDPOINT: &str = "address/"; -const KEYS_ENDPOINT: &str = "/keys/"; -const COST_TRANSACTION_ENDPOINT: &str = "transaction/cost"; -const SEND_TRANSACTION_ENDPOINT: &str = "transaction/send"; -const SEND_MULTIPLE_TRANSACTIONS_ENDPOINT: &str = "transaction/send-multiple"; -const GET_TRANSACTION_INFO_ENDPOINT: &str = "transaction/"; -const GET_HYPER_BLOCK_BY_NONCE_ENDPOINT: &str = "hyperblock/by-nonce/"; -const GET_HYPER_BLOCK_BY_HASH_ENDPOINT: &str = "hyperblock/by-hash/"; -const GET_NETWORK_STATUS_ENDPOINT: &str = "network/status"; -const WITH_RESULTS_QUERY_PARAM: &str = "?withResults=true"; -const VM_VALUES_ENDPOINT: &str = "vm-values/query"; - -#[derive(Clone, Debug)] -pub struct CommunicationProxy { - proxy_url: String, - client: Client, -} - -impl CommunicationProxy { - pub fn new(proxy_url: String) -> Self { - Self { - proxy_url, - client: Client::new(), - } - } - - fn get_endpoint(&self, endpoint: &str) -> String { - format!("{}/{}", self.proxy_url, endpoint) - } - - // get_network_config retrieves the network configuration from the proxy - pub async fn get_network_config(&self) -> Result { - let endpoint = self.get_endpoint(NETWORK_CONFIG_ENDPOINT); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.config), - } - } - - // get_network_economics retrieves the network economics from the proxy - pub async fn get_network_economics(&self) -> Result { - let endpoint = self.get_endpoint(NETWORK_ECONOMICS_ENDPOINT); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.metrics), - } - } - - async fn get_hyper_block(&self, endpoint: &str) -> Result { - let endpoint = self.get_endpoint(endpoint); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.hyperblock), - } - } - - // get_hyper_block_by_hash retrieves a hyper block's info by hash from the network - pub async fn get_hyper_block_by_hash(&self, hash: &str) -> Result { - let endpoint = GET_HYPER_BLOCK_BY_HASH_ENDPOINT.to_string() + hash; - self.get_hyper_block(endpoint.as_str()).await - } - - // get_hyper_block_by_nonce retrieves a hyper block's info by nonce from the network - pub async fn get_hyper_block_by_nonce(&self, nonce: u64) -> Result { - let endpoint = GET_HYPER_BLOCK_BY_NONCE_ENDPOINT.to_string() + nonce.to_string().as_str(); - self.get_hyper_block(endpoint.as_str()).await - } - - // get_latest_hyper_block_nonce retrieves the latest hyper block (metachain) nonce from the network - pub async fn get_latest_hyper_block_nonce(&self, with_metachain: bool) -> Result { - let mut endpoint = GET_NETWORK_STATUS_ENDPOINT.to_string(); - - if with_metachain { - endpoint = format!("{GET_NETWORK_STATUS_ENDPOINT}/{METACHAIN_SHARD_ID}"); - } - - let endpoint = self.get_endpoint(endpoint.as_str()); - - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.status.nonce), - } - } - - // request_transaction_cost retrieves how many gas a transaction will consume - pub async fn request_transaction_cost(&self, tx: &Transaction) -> Result { - let endpoint = self.get_endpoint(COST_TRANSACTION_ENDPOINT); - let resp = self - .client - .post(endpoint) - .json(tx) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b), - } - } - - // get_account retrieves an account info from the network (nonce, balance) - pub async fn get_account(&self, address: &Address) -> Result { - if !address.is_valid() { - return Err(anyhow!("invalid address")); - } - - let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str(); - let endpoint = self.get_endpoint(endpoint.as_str()); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.account), - } - } - - // get_account_esdt_roles retrieves an all esdt roles of an account from the network - pub async fn get_account_esdt_roles( - &self, - address: &Address, - ) -> Result>> { - if !address.is_valid() { - return Err(anyhow!("invalid address")); - } - - let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + "/esdts/roles"; - let endpoint = self.get_endpoint(endpoint.as_str()); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.roles), - } - } - - // get_account_esdt_tokens retrieves an all esdt token of an account from the network - pub async fn get_account_esdt_tokens( - &self, - address: &Address, - ) -> Result> { - if !address.is_valid() { - return Err(anyhow!("invalid address")); - } - - let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + "/esdt"; - let endpoint = self.get_endpoint(endpoint.as_str()); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.esdts), - } - } - - // get_account_esdt_tokens retrieves an all esdt token of an account from the network - pub async fn get_account_storage_keys( - &self, - address: &Address, - ) -> Result> { - if !address.is_valid() { - return Err(anyhow!("invalid address")); - } - - let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + KEYS_ENDPOINT; - let endpoint = self.get_endpoint(endpoint.as_str()); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.pairs), - } - } - - async fn get_transaction_info_internal( - &self, - hash: &str, - with_results: bool, - ) -> Result { - let mut endpoint = GET_TRANSACTION_INFO_ENDPOINT.to_string() + hash; - - if with_results { - endpoint += WITH_RESULTS_QUERY_PARAM - } - - let endpoint = self.get_endpoint(endpoint.as_str()); - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.transaction), - } - } - - // get_transaction_info retrieves a transaction's details from the network - pub async fn get_transaction_info(&self, hash: &str) -> Result { - self.get_transaction_info_internal(hash, false).await - } - - // get_transaction_info_with_results retrieves a transaction's details from the network with events - pub async fn get_transaction_info_with_results( - &self, - hash: &str, - ) -> Result { - self.get_transaction_info_internal(hash, true).await - } - - // get_transaction_status retrieves a transaction's status from the network - pub async fn get_transaction_status(&self, hash: &str) -> Result { - let endpoint = format!("transaction/{hash}/status"); - let endpoint = self.get_endpoint(endpoint.as_str()); - - let resp = self - .client - .get(endpoint) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.status), - } - } - - // get_default_transaction_arguments will prepare the transaction creation argument by querying the account's info - pub async fn get_default_transaction_arguments( - &self, - address: &Address, - network_configs: &NetworkConfig, - ) -> Result { - let account = self.get_account(address).await?; - - Ok(ArgCreateTransaction { - nonce: account.nonce, - value: "".to_string(), - rcv_addr: address.clone(), - snd_addr: address.clone(), - gas_price: network_configs.min_gas_price, - gas_limit: network_configs.min_gas_limit, - data: None, - signature: "".to_string(), - chain_id: network_configs.chain_id.clone(), - version: network_configs.min_transaction_version, - options: 0, - available_balance: account.balance, - }) - } - - pub async fn send_transaction(&self, tx: &Transaction) -> Result { - let endpoint = self.get_endpoint(SEND_TRANSACTION_ENDPOINT); - let resp = self - .client - .post(endpoint) - .json(tx) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b.tx_hash), - } - } - - pub async fn send_transactions(&self, txs: &Vec) -> Result> { - let endpoint = self.get_endpoint(SEND_MULTIPLE_TRANSACTIONS_ENDPOINT); - let resp = self - .client - .post(endpoint) - .json(txs) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => { - let mut tx_hashs: Vec = vec![]; - for key in b.txs_hashes.keys().sorted() { - tx_hashs.push(b.txs_hashes[key].clone()); - } - - Ok(tx_hashs) - }, - } - } - - // execute_vmquery retrieves data from existing SC trie through the use of a VM - pub async fn execute_vmquery( - &self, - vm_request: &VmValueRequest, - ) -> Result { - let endpoint = self.get_endpoint(VM_VALUES_ENDPOINT); - let resp = self - .client - .post(endpoint) - .json(vm_request) - .send() - .await? - .json::() - .await?; - - match resp.data { - None => Err(anyhow!("{}", resp.error)), - Some(b) => Ok(b), - } - } -} diff --git a/sdk/core/src/blockchain/gateway_account.rs b/sdk/core/src/blockchain/gateway_account.rs new file mode 100644 index 0000000000..f28cf6f562 --- /dev/null +++ b/sdk/core/src/blockchain/gateway_account.rs @@ -0,0 +1,112 @@ +use crate::data::{ + account::{Account, AccountResponse}, + account_storage::AccountStorageResponse, + address::Address, + esdt::{EsdtBalance, EsdtBalanceResponse, EsdtRolesResponse}, +}; +use anyhow::{anyhow, Result}; +use std::collections::HashMap; + +use super::CommunicationProxy; + +const ACCOUNT_ENDPOINT: &str = "address/"; +const KEYS_ENDPOINT: &str = "/keys/"; + +impl CommunicationProxy { + // get_account retrieves an account info from the network (nonce, balance) + pub async fn get_account(&self, address: &Address) -> Result { + if !address.is_valid() { + return Err(anyhow!("invalid address")); + } + + let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str(); + let endpoint = self.get_endpoint(endpoint.as_str()); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.account), + } + } + + // get_account_esdt_roles retrieves an all esdt roles of an account from the network + pub async fn get_account_esdt_roles( + &self, + address: &Address, + ) -> Result>> { + if !address.is_valid() { + return Err(anyhow!("invalid address")); + } + + let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + "/esdts/roles"; + let endpoint = self.get_endpoint(endpoint.as_str()); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.roles), + } + } + + // get_account_esdt_tokens retrieves an all esdt token of an account from the network + pub async fn get_account_esdt_tokens( + &self, + address: &Address, + ) -> Result> { + if !address.is_valid() { + return Err(anyhow!("invalid address")); + } + + let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + "/esdt"; + let endpoint = self.get_endpoint(endpoint.as_str()); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.esdts), + } + } + + // get_account_esdt_tokens retrieves an all esdt token of an account from the network + pub async fn get_account_storage_keys( + &self, + address: &Address, + ) -> Result> { + if !address.is_valid() { + return Err(anyhow!("invalid address")); + } + + let endpoint = ACCOUNT_ENDPOINT.to_string() + address.to_string().as_str() + KEYS_ENDPOINT; + let endpoint = self.get_endpoint(endpoint.as_str()); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.pairs), + } + } +} diff --git a/sdk/core/src/blockchain/gateway_block.rs b/sdk/core/src/blockchain/gateway_block.rs new file mode 100644 index 0000000000..38827ea6db --- /dev/null +++ b/sdk/core/src/blockchain/gateway_block.rs @@ -0,0 +1,66 @@ +use crate::data::{ + hyperblock::{HyperBlock, HyperBlockResponse}, + network_status::NetworkStatusResponse, +}; +use anyhow::{anyhow, Result}; + +use super::CommunicationProxy; +use super::METACHAIN_SHARD_ID; + +const GET_HYPER_BLOCK_BY_NONCE_ENDPOINT: &str = "hyperblock/by-nonce/"; +const GET_HYPER_BLOCK_BY_HASH_ENDPOINT: &str = "hyperblock/by-hash/"; +const GET_NETWORK_STATUS_ENDPOINT: &str = "network/status"; + +impl CommunicationProxy { + async fn get_hyper_block(&self, endpoint: &str) -> Result { + let endpoint = self.get_endpoint(endpoint); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.hyperblock), + } + } + + // get_hyper_block_by_hash retrieves a hyper block's info by hash from the network + pub async fn get_hyper_block_by_hash(&self, hash: &str) -> Result { + let endpoint = GET_HYPER_BLOCK_BY_HASH_ENDPOINT.to_string() + hash; + self.get_hyper_block(endpoint.as_str()).await + } + + // get_hyper_block_by_nonce retrieves a hyper block's info by nonce from the network + pub async fn get_hyper_block_by_nonce(&self, nonce: u64) -> Result { + let endpoint = GET_HYPER_BLOCK_BY_NONCE_ENDPOINT.to_string() + nonce.to_string().as_str(); + self.get_hyper_block(endpoint.as_str()).await + } + + // get_latest_hyper_block_nonce retrieves the latest hyper block (metachain) nonce from the network + pub async fn get_latest_hyper_block_nonce(&self, with_metachain: bool) -> Result { + let mut endpoint = GET_NETWORK_STATUS_ENDPOINT.to_string(); + + if with_metachain { + endpoint = format!("{GET_NETWORK_STATUS_ENDPOINT}/{METACHAIN_SHARD_ID}"); + } + + let endpoint = self.get_endpoint(endpoint.as_str()); + + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.status.nonce), + } + } +} diff --git a/sdk/core/src/blockchain/gateway_network.rs b/sdk/core/src/blockchain/gateway_network.rs new file mode 100644 index 0000000000..2d425d461e --- /dev/null +++ b/sdk/core/src/blockchain/gateway_network.rs @@ -0,0 +1,46 @@ +use crate::data::{ + network_config::{NetworkConfig, NetworkConfigResponse}, + network_economics::{NetworkEconomics, NetworkEconomicsResponse}, +}; +use anyhow::{anyhow, Result}; + +use super::CommunicationProxy; + +const NETWORK_CONFIG_ENDPOINT: &str = "network/config"; +const NETWORK_ECONOMICS_ENDPOINT: &str = "network/economics"; + +impl CommunicationProxy { + // get_network_config retrieves the network configuration from the proxy + pub async fn get_network_config(&self) -> Result { + let endpoint = self.get_endpoint(NETWORK_CONFIG_ENDPOINT); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.config), + } + } + + // get_network_economics retrieves the network economics from the proxy + pub async fn get_network_economics(&self) -> Result { + let endpoint = self.get_endpoint(NETWORK_ECONOMICS_ENDPOINT); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.metrics), + } + } +} diff --git a/sdk/core/src/blockchain/gateway_proxy.rs b/sdk/core/src/blockchain/gateway_proxy.rs new file mode 100644 index 0000000000..9643abd5ca --- /dev/null +++ b/sdk/core/src/blockchain/gateway_proxy.rs @@ -0,0 +1,20 @@ +use reqwest::Client; + +#[derive(Clone, Debug)] +pub struct CommunicationProxy { + pub(crate) proxy_url: String, + pub(crate) client: Client, +} + +impl CommunicationProxy { + pub fn new(proxy_url: String) -> Self { + Self { + proxy_url, + client: Client::new(), + } + } + + pub(crate) fn get_endpoint(&self, endpoint: &str) -> String { + format!("{}/{}", self.proxy_url, endpoint) + } +} diff --git a/sdk/core/src/blockchain/gateway_tx.rs b/sdk/core/src/blockchain/gateway_tx.rs new file mode 100644 index 0000000000..641e1ae511 --- /dev/null +++ b/sdk/core/src/blockchain/gateway_tx.rs @@ -0,0 +1,184 @@ +use crate::data::{ + address::Address, + network_config::NetworkConfig, + transaction::{ + ArgCreateTransaction, ResponseTxCost, SendTransactionResponse, SendTransactionsResponse, + Transaction, TransactionInfo, TransactionOnNetwork, TransactionStatus, TxCostResponseData, + }, + vm::{ResponseVmValue, VmValueRequest, VmValuesResponseData}, +}; +use anyhow::{anyhow, Result}; +use itertools::Itertools; + +use super::CommunicationProxy; + +const COST_TRANSACTION_ENDPOINT: &str = "transaction/cost"; +const SEND_TRANSACTION_ENDPOINT: &str = "transaction/send"; +const SEND_MULTIPLE_TRANSACTIONS_ENDPOINT: &str = "transaction/send-multiple"; +const GET_TRANSACTION_INFO_ENDPOINT: &str = "transaction/"; +const WITH_RESULTS_QUERY_PARAM: &str = "?withResults=true"; +const VM_VALUES_ENDPOINT: &str = "vm-values/query"; + +impl CommunicationProxy { + // request_transaction_cost retrieves how many gas a transaction will consume + pub async fn request_transaction_cost(&self, tx: &Transaction) -> Result { + let endpoint = self.get_endpoint(COST_TRANSACTION_ENDPOINT); + let resp = self + .client + .post(endpoint) + .json(tx) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b), + } + } + + async fn get_transaction_info_internal( + &self, + hash: &str, + with_results: bool, + ) -> Result { + let mut endpoint = GET_TRANSACTION_INFO_ENDPOINT.to_string() + hash; + + if with_results { + endpoint += WITH_RESULTS_QUERY_PARAM + } + + let endpoint = self.get_endpoint(endpoint.as_str()); + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.transaction), + } + } + + // get_transaction_info retrieves a transaction's details from the network + pub async fn get_transaction_info(&self, hash: &str) -> Result { + self.get_transaction_info_internal(hash, false).await + } + + // get_transaction_info_with_results retrieves a transaction's details from the network with events + pub async fn get_transaction_info_with_results( + &self, + hash: &str, + ) -> Result { + self.get_transaction_info_internal(hash, true).await + } + + // get_transaction_status retrieves a transaction's status from the network + pub async fn get_transaction_status(&self, hash: &str) -> Result { + let endpoint = format!("transaction/{hash}/status"); + let endpoint = self.get_endpoint(endpoint.as_str()); + + let resp = self + .client + .get(endpoint) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.status), + } + } + + // get_default_transaction_arguments will prepare the transaction creation argument by querying the account's info + pub async fn get_default_transaction_arguments( + &self, + address: &Address, + network_configs: &NetworkConfig, + ) -> Result { + let account = self.get_account(address).await?; + + Ok(ArgCreateTransaction { + nonce: account.nonce, + value: "".to_string(), + rcv_addr: address.clone(), + snd_addr: address.clone(), + gas_price: network_configs.min_gas_price, + gas_limit: network_configs.min_gas_limit, + data: None, + signature: "".to_string(), + chain_id: network_configs.chain_id.clone(), + version: network_configs.min_transaction_version, + options: 0, + available_balance: account.balance, + }) + } + + pub async fn send_transaction(&self, tx: &Transaction) -> Result { + let endpoint = self.get_endpoint(SEND_TRANSACTION_ENDPOINT); + let resp = self + .client + .post(endpoint) + .json(tx) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b.tx_hash), + } + } + + pub async fn send_transactions(&self, txs: &Vec) -> Result> { + let endpoint = self.get_endpoint(SEND_MULTIPLE_TRANSACTIONS_ENDPOINT); + let resp = self + .client + .post(endpoint) + .json(txs) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => { + let mut tx_hashs: Vec = vec![]; + for key in b.txs_hashes.keys().sorted() { + tx_hashs.push(b.txs_hashes[key].clone()); + } + + Ok(tx_hashs) + }, + } + } + + // execute_vmquery retrieves data from existing SC trie through the use of a VM + pub async fn execute_vmquery( + &self, + vm_request: &VmValueRequest, + ) -> Result { + let endpoint = self.get_endpoint(VM_VALUES_ENDPOINT); + let resp = self + .client + .post(endpoint) + .json(vm_request) + .send() + .await? + .json::() + .await?; + + match resp.data { + None => Err(anyhow!("{}", resp.error)), + Some(b) => Ok(b), + } + } +} From ca237ea30d4b40a96324c59d9a0ae5bb7f1f9d90 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 23:17:09 +0300 Subject: [PATCH 3/4] gateway proxy rename --- framework/snippets/src/account_tool.rs | 6 +++--- framework/snippets/src/interactor.rs | 6 +++--- sdk/core/examples/account.rs | 4 ++-- sdk/core/examples/account_storage.rs | 4 ++-- sdk/core/examples/get_esdt_tokens.rs | 4 ++-- sdk/core/examples/get_hyper_block_by_hash.rs | 4 ++-- sdk/core/examples/get_hyper_block_by_nonce.rs | 4 ++-- sdk/core/examples/get_hyper_block_latest.rs | 4 ++-- sdk/core/examples/get_network_config.rs | 4 ++-- sdk/core/examples/get_network_economics.rs | 4 ++-- sdk/core/examples/sign_tx.rs | 4 ++-- sdk/core/examples/sign_txs.rs | 4 ++-- sdk/core/examples/tx_cost.rs | 4 ++-- sdk/core/examples/tx_default_args.rs | 4 ++-- sdk/core/examples/tx_info.rs | 4 ++-- sdk/core/examples/vm_query.rs | 4 ++-- sdk/core/src/blockchain.rs | 2 +- sdk/core/src/blockchain/gateway_account.rs | 4 ++-- sdk/core/src/blockchain/gateway_block.rs | 4 ++-- sdk/core/src/blockchain/gateway_network.rs | 4 ++-- sdk/core/src/blockchain/gateway_proxy.rs | 5 +++-- sdk/core/src/blockchain/gateway_tx.rs | 4 ++-- sdk/core/src/blockchain/gateway_tx_retrieve.rs | 4 ++-- 23 files changed, 48 insertions(+), 47 deletions(-) diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index 70eaafdace..401e68fd9c 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -4,7 +4,7 @@ use multiversx_sc_scenario::{ scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step}, }; use multiversx_sdk::{ - blockchain::CommunicationProxy, + blockchain::GatewayProxy, data::{address::Address, esdt::EsdtBalance}, }; use std::collections::{BTreeMap, HashMap}; @@ -17,7 +17,7 @@ pub async fn print_account_as_scenario_set_state( api_string: String, address_bech32_string: String, ) { - let api = CommunicationProxy::new(api_string); + let api = GatewayProxy::new(api_string); let address = Bech32Address::from_bech32_string(address_bech32_string); let set_state = retrieve_account_as_scenario_set_state(&api, &address).await; let scenario = build_scenario(set_state); @@ -34,7 +34,7 @@ fn build_scenario(set_state: SetStateStep) -> Scenario { } pub async fn retrieve_account_as_scenario_set_state( - api: &CommunicationProxy, + api: &GatewayProxy, address: &Bech32Address, ) -> SetStateStep { let sdk_address = Address::from_bech32_string(address.to_bech32_str()).unwrap(); diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index 82c59752f6..6daa44b6c8 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -5,7 +5,7 @@ use multiversx_sc_scenario::{ scenario_model::AddressValue, }; use multiversx_sdk::{ - blockchain::CommunicationProxy, + blockchain::GatewayProxy, data::{address::Address as ErdrsAddress, network_config::NetworkConfig}, wallet::Wallet, }; @@ -20,7 +20,7 @@ use crate::{account_tool::retrieve_account_as_scenario_set_state, Sender}; pub const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json"; pub struct Interactor { - pub proxy: CommunicationProxy, + pub proxy: GatewayProxy, pub network_config: NetworkConfig, pub sender_map: HashMap, @@ -33,7 +33,7 @@ pub struct Interactor { impl Interactor { pub async fn new(gateway_url: &str) -> Self { - let proxy = CommunicationProxy::new(gateway_url.to_string()); + let proxy = GatewayProxy::new(gateway_url.to_string()); let network_config = proxy.get_network_config().await.unwrap(); Self { proxy, diff --git a/sdk/core/examples/account.rs b/sdk/core/examples/account.rs index 2ad1d6c4cc..16a9e2d8a9 100644 --- a/sdk/core/examples/account.rs +++ b/sdk/core/examples/account.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, }; @@ -10,7 +10,7 @@ async fn main() { ) .unwrap(); - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let account = blockchain.get_account(&addr).await.unwrap(); println!("account: {account:#?}"); diff --git a/sdk/core/examples/account_storage.rs b/sdk/core/examples/account_storage.rs index 31adc7d0f9..c081e67e58 100644 --- a/sdk/core/examples/account_storage.rs +++ b/sdk/core/examples/account_storage.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, }; @@ -10,7 +10,7 @@ async fn main() { ) .unwrap(); - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let account_storage = blockchain.get_account_storage_keys(&addr).await.unwrap(); println!("Account Storage: {account_storage:#?}"); diff --git a/sdk/core/examples/get_esdt_tokens.rs b/sdk/core/examples/get_esdt_tokens.rs index faa4debfba..5dc62b5098 100644 --- a/sdk/core/examples/get_esdt_tokens.rs +++ b/sdk/core/examples/get_esdt_tokens.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, }; @@ -10,7 +10,7 @@ async fn main() { ) .unwrap(); - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let balances = blockchain.get_account_esdt_tokens(&addr).await.unwrap(); println!("{balances:#?}"); diff --git a/sdk/core/examples/get_hyper_block_by_hash.rs b/sdk/core/examples/get_hyper_block_by_hash.rs index c67b479767..b0f0d34a2d 100644 --- a/sdk/core/examples/get_hyper_block_by_hash.rs +++ b/sdk/core/examples/get_hyper_block_by_hash.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::blockchain::{CommunicationProxy, DEVNET_GATEWAY}; +use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let result = blockchain .get_hyper_block_by_hash("20b14ba0e68c465810c5ded091f220e51dad41629d7ccd87dab572206185e419") .await; diff --git a/sdk/core/examples/get_hyper_block_by_nonce.rs b/sdk/core/examples/get_hyper_block_by_nonce.rs index c3d270d2bc..e033b57156 100644 --- a/sdk/core/examples/get_hyper_block_by_nonce.rs +++ b/sdk/core/examples/get_hyper_block_by_nonce.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::blockchain::{CommunicationProxy, DEVNET_GATEWAY}; +use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let result = blockchain.get_hyper_block_by_nonce(7468).await; println!("block by nonce result: {result:?}") diff --git a/sdk/core/examples/get_hyper_block_latest.rs b/sdk/core/examples/get_hyper_block_latest.rs index b9ad946829..1ab8d51350 100644 --- a/sdk/core/examples/get_hyper_block_latest.rs +++ b/sdk/core/examples/get_hyper_block_latest.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::blockchain::{CommunicationProxy, DEVNET_GATEWAY}; +use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let result = blockchain.get_latest_hyper_block_nonce(false).await; println!("latest block result: {result:?}") diff --git a/sdk/core/examples/get_network_config.rs b/sdk/core/examples/get_network_config.rs index 7ca699b3c8..1c9ada08c6 100644 --- a/sdk/core/examples/get_network_config.rs +++ b/sdk/core/examples/get_network_config.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::blockchain::{CommunicationProxy, DEVNET_GATEWAY}; +use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let network_config = blockchain.get_network_config().await.unwrap(); println!("network_config: {network_config:#?}") diff --git a/sdk/core/examples/get_network_economics.rs b/sdk/core/examples/get_network_economics.rs index e562f3db46..568c3b8378 100644 --- a/sdk/core/examples/get_network_economics.rs +++ b/sdk/core/examples/get_network_economics.rs @@ -1,8 +1,8 @@ -use multiversx_sdk::blockchain::{CommunicationProxy, DEVNET_GATEWAY}; +use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let network_economics = blockchain.get_network_economics().await.unwrap(); println!("network_economics: {network_economics:#?}") diff --git a/sdk/core/examples/sign_tx.rs b/sdk/core/examples/sign_tx.rs index 1455061830..e9e7da0225 100644 --- a/sdk/core/examples/sign_tx.rs +++ b/sdk/core/examples/sign_tx.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::transaction::Transaction, wallet::Wallet, }; @@ -11,7 +11,7 @@ async fn main() { ) .unwrap(); let addr = wl.address(); - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let network_config = blockchain.get_network_config().await.unwrap(); let arg = blockchain diff --git a/sdk/core/examples/sign_txs.rs b/sdk/core/examples/sign_txs.rs index 6a4a12a231..672cf9f08f 100644 --- a/sdk/core/examples/sign_txs.rs +++ b/sdk/core/examples/sign_txs.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::transaction::Transaction, wallet::Wallet, }; @@ -11,7 +11,7 @@ async fn main() { ) .unwrap(); let addr = wl.address(); - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let network_config = blockchain.get_network_config().await.unwrap(); let arg = blockchain diff --git a/sdk/core/examples/tx_cost.rs b/sdk/core/examples/tx_cost.rs index b98cbd2e4b..0e05be8420 100644 --- a/sdk/core/examples/tx_cost.rs +++ b/sdk/core/examples/tx_cost.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::{address::Address, transaction::Transaction}, utils::base64_encode, }; @@ -26,7 +26,7 @@ async fn main() { signature: None, }; - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let cost = blockchain.request_transaction_cost(&tx).await.unwrap(); println!("tx cost: {cost:#?}"); diff --git a/sdk/core/examples/tx_default_args.rs b/sdk/core/examples/tx_default_args.rs index dad593eaf8..45dd3b9473 100644 --- a/sdk/core/examples/tx_default_args.rs +++ b/sdk/core/examples/tx_default_args.rs @@ -1,11 +1,11 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, }; #[tokio::main] async fn main() { - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let network_config = blockchain.get_network_config().await.unwrap(); let addr = Address::from_bech32_string( "erd1qqqqqqqqqqqqqpgqfzydqmdw7m2vazsp6u5p95yxz76t2p9rd8ss0zp9ts", diff --git a/sdk/core/examples/tx_info.rs b/sdk/core/examples/tx_info.rs index 33c929be50..bfad3fb1ca 100644 --- a/sdk/core/examples/tx_info.rs +++ b/sdk/core/examples/tx_info.rs @@ -1,9 +1,9 @@ -use multiversx_sdk::blockchain::{CommunicationProxy, DEVNET_GATEWAY}; +use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { let tx_hash = "49edb289892a655a0e988b360c19326c21107f9696c6197b435667c6e8c6e1a3"; - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let status = blockchain.get_transaction_status(tx_hash).await; println!("tx status: {status:?}"); diff --git a/sdk/core/examples/vm_query.rs b/sdk/core/examples/vm_query.rs index 41cdfd5dbf..df7d3b43ee 100644 --- a/sdk/core/examples/vm_query.rs +++ b/sdk/core/examples/vm_query.rs @@ -1,5 +1,5 @@ use multiversx_sdk::{ - blockchain::{CommunicationProxy, DEVNET_GATEWAY}, + blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::{address::Address, vm::VmValueRequest}, wallet::Wallet, }; @@ -11,7 +11,7 @@ async fn main() { ) .unwrap(); let addr = wl.address(); - let blockchain = CommunicationProxy::new(DEVNET_GATEWAY.to_string()); + let blockchain = GatewayProxy::new(DEVNET_GATEWAY.to_string()); let req = VmValueRequest { sc_address: Address::from_bech32_string( "erd1qqqqqqqqqqqqqpgqhn3ae8dpc957t7jadn7kywtg503dy7pnj9ts3umqxx", diff --git a/sdk/core/src/blockchain.rs b/sdk/core/src/blockchain.rs index 05bad7198a..35e2668b50 100644 --- a/sdk/core/src/blockchain.rs +++ b/sdk/core/src/blockchain.rs @@ -5,7 +5,7 @@ mod gateway_proxy; mod gateway_tx; mod gateway_tx_retrieve; -pub use gateway_proxy::CommunicationProxy; +pub use gateway_proxy::GatewayProxy; pub const MAINNET_GATEWAY: &str = "https://gateway.multiversx.com"; pub const TESTNET_GATEWAY: &str = "https://testnet-gateway.multiversx.com"; diff --git a/sdk/core/src/blockchain/gateway_account.rs b/sdk/core/src/blockchain/gateway_account.rs index f28cf6f562..7d72258544 100644 --- a/sdk/core/src/blockchain/gateway_account.rs +++ b/sdk/core/src/blockchain/gateway_account.rs @@ -7,12 +7,12 @@ use crate::data::{ use anyhow::{anyhow, Result}; use std::collections::HashMap; -use super::CommunicationProxy; +use super::GatewayProxy; const ACCOUNT_ENDPOINT: &str = "address/"; const KEYS_ENDPOINT: &str = "/keys/"; -impl CommunicationProxy { +impl GatewayProxy { // get_account retrieves an account info from the network (nonce, balance) pub async fn get_account(&self, address: &Address) -> Result { if !address.is_valid() { diff --git a/sdk/core/src/blockchain/gateway_block.rs b/sdk/core/src/blockchain/gateway_block.rs index 38827ea6db..b6daf57abb 100644 --- a/sdk/core/src/blockchain/gateway_block.rs +++ b/sdk/core/src/blockchain/gateway_block.rs @@ -4,14 +4,14 @@ use crate::data::{ }; use anyhow::{anyhow, Result}; -use super::CommunicationProxy; +use super::GatewayProxy; use super::METACHAIN_SHARD_ID; const GET_HYPER_BLOCK_BY_NONCE_ENDPOINT: &str = "hyperblock/by-nonce/"; const GET_HYPER_BLOCK_BY_HASH_ENDPOINT: &str = "hyperblock/by-hash/"; const GET_NETWORK_STATUS_ENDPOINT: &str = "network/status"; -impl CommunicationProxy { +impl GatewayProxy { async fn get_hyper_block(&self, endpoint: &str) -> Result { let endpoint = self.get_endpoint(endpoint); let resp = self diff --git a/sdk/core/src/blockchain/gateway_network.rs b/sdk/core/src/blockchain/gateway_network.rs index 2d425d461e..8b02a57d0d 100644 --- a/sdk/core/src/blockchain/gateway_network.rs +++ b/sdk/core/src/blockchain/gateway_network.rs @@ -4,12 +4,12 @@ use crate::data::{ }; use anyhow::{anyhow, Result}; -use super::CommunicationProxy; +use super::GatewayProxy; const NETWORK_CONFIG_ENDPOINT: &str = "network/config"; const NETWORK_ECONOMICS_ENDPOINT: &str = "network/economics"; -impl CommunicationProxy { +impl GatewayProxy { // get_network_config retrieves the network configuration from the proxy pub async fn get_network_config(&self) -> Result { let endpoint = self.get_endpoint(NETWORK_CONFIG_ENDPOINT); diff --git a/sdk/core/src/blockchain/gateway_proxy.rs b/sdk/core/src/blockchain/gateway_proxy.rs index 9643abd5ca..47925cd79f 100644 --- a/sdk/core/src/blockchain/gateway_proxy.rs +++ b/sdk/core/src/blockchain/gateway_proxy.rs @@ -1,12 +1,13 @@ use reqwest::Client; +/// Allows communication with the MultiversX gateway API. #[derive(Clone, Debug)] -pub struct CommunicationProxy { +pub struct GatewayProxy { pub(crate) proxy_url: String, pub(crate) client: Client, } -impl CommunicationProxy { +impl GatewayProxy { pub fn new(proxy_url: String) -> Self { Self { proxy_url, diff --git a/sdk/core/src/blockchain/gateway_tx.rs b/sdk/core/src/blockchain/gateway_tx.rs index 641e1ae511..47aa8b5a83 100644 --- a/sdk/core/src/blockchain/gateway_tx.rs +++ b/sdk/core/src/blockchain/gateway_tx.rs @@ -10,7 +10,7 @@ use crate::data::{ use anyhow::{anyhow, Result}; use itertools::Itertools; -use super::CommunicationProxy; +use super::GatewayProxy; const COST_TRANSACTION_ENDPOINT: &str = "transaction/cost"; const SEND_TRANSACTION_ENDPOINT: &str = "transaction/send"; @@ -19,7 +19,7 @@ const GET_TRANSACTION_INFO_ENDPOINT: &str = "transaction/"; const WITH_RESULTS_QUERY_PARAM: &str = "?withResults=true"; const VM_VALUES_ENDPOINT: &str = "vm-values/query"; -impl CommunicationProxy { +impl GatewayProxy { // request_transaction_cost retrieves how many gas a transaction will consume pub async fn request_transaction_cost(&self, tx: &Transaction) -> Result { let endpoint = self.get_endpoint(COST_TRANSACTION_ENDPOINT); diff --git a/sdk/core/src/blockchain/gateway_tx_retrieve.rs b/sdk/core/src/blockchain/gateway_tx_retrieve.rs index 44a0357432..821713f972 100644 --- a/sdk/core/src/blockchain/gateway_tx_retrieve.rs +++ b/sdk/core/src/blockchain/gateway_tx_retrieve.rs @@ -2,13 +2,13 @@ use crate::data::transaction::TransactionOnNetwork; use log::info; use std::time::{Duration, Instant}; -use super::CommunicationProxy; +use super::GatewayProxy; const INITIAL_BACKOFF_DELAY: f32 = 1.4; const MAX_RETRIES: usize = 8; const MAX_BACKOFF_DELAY: Duration = Duration::from_secs(6); -impl CommunicationProxy { +impl GatewayProxy { /// Retrieves a transaction from the network. pub async fn retrieve_tx_on_network(&self, tx_hash: String) -> TransactionOnNetwork { let mut retries = 0; From a31a3b25baba6cc4a2ef0a416a86b7137e1f6793 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 29 May 2024 23:22:37 +0300 Subject: [PATCH 4/4] gateway proxy mod rename --- framework/snippets/src/account_tool.rs | 2 +- framework/snippets/src/interactor.rs | 2 +- sdk/core/examples/account.rs | 2 +- sdk/core/examples/account_storage.rs | 2 +- sdk/core/examples/get_esdt_tokens.rs | 2 +- sdk/core/examples/get_hyper_block_by_hash.rs | 2 +- sdk/core/examples/get_hyper_block_by_nonce.rs | 2 +- sdk/core/examples/get_hyper_block_latest.rs | 2 +- sdk/core/examples/get_network_config.rs | 2 +- sdk/core/examples/get_network_economics.rs | 2 +- sdk/core/examples/sign_tx.rs | 2 +- sdk/core/examples/sign_txs.rs | 2 +- sdk/core/examples/tx_cost.rs | 2 +- sdk/core/examples/tx_default_args.rs | 2 +- sdk/core/examples/tx_info.rs | 2 +- sdk/core/examples/vm_query.rs | 2 +- sdk/core/src/{blockchain.rs => gateway.rs} | 0 sdk/core/src/{blockchain => gateway}/gateway_account.rs | 0 sdk/core/src/{blockchain => gateway}/gateway_block.rs | 0 sdk/core/src/{blockchain => gateway}/gateway_network.rs | 0 sdk/core/src/{blockchain => gateway}/gateway_proxy.rs | 0 sdk/core/src/{blockchain => gateway}/gateway_tx.rs | 0 sdk/core/src/{blockchain => gateway}/gateway_tx_retrieve.rs | 0 sdk/core/src/lib.rs | 2 +- 24 files changed, 17 insertions(+), 17 deletions(-) rename sdk/core/src/{blockchain.rs => gateway.rs} (100%) rename sdk/core/src/{blockchain => gateway}/gateway_account.rs (100%) rename sdk/core/src/{blockchain => gateway}/gateway_block.rs (100%) rename sdk/core/src/{blockchain => gateway}/gateway_network.rs (100%) rename sdk/core/src/{blockchain => gateway}/gateway_proxy.rs (100%) rename sdk/core/src/{blockchain => gateway}/gateway_tx.rs (100%) rename sdk/core/src/{blockchain => gateway}/gateway_tx_retrieve.rs (100%) diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index 401e68fd9c..b10baf4a80 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -4,8 +4,8 @@ use multiversx_sc_scenario::{ scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step}, }; use multiversx_sdk::{ - blockchain::GatewayProxy, data::{address::Address, esdt::EsdtBalance}, + gateway::GatewayProxy, }; use std::collections::{BTreeMap, HashMap}; diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index 6daa44b6c8..bb38873afe 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -5,8 +5,8 @@ use multiversx_sc_scenario::{ scenario_model::AddressValue, }; use multiversx_sdk::{ - blockchain::GatewayProxy, data::{address::Address as ErdrsAddress, network_config::NetworkConfig}, + gateway::GatewayProxy, wallet::Wallet, }; use std::{ diff --git a/sdk/core/examples/account.rs b/sdk/core/examples/account.rs index 16a9e2d8a9..5e9456f4b0 100644 --- a/sdk/core/examples/account.rs +++ b/sdk/core/examples/account.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, + gateway::{GatewayProxy, DEVNET_GATEWAY}, }; #[tokio::main] diff --git a/sdk/core/examples/account_storage.rs b/sdk/core/examples/account_storage.rs index c081e67e58..7693c26e08 100644 --- a/sdk/core/examples/account_storage.rs +++ b/sdk/core/examples/account_storage.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, + gateway::{GatewayProxy, DEVNET_GATEWAY}, }; #[tokio::main] diff --git a/sdk/core/examples/get_esdt_tokens.rs b/sdk/core/examples/get_esdt_tokens.rs index 5dc62b5098..fac424da27 100644 --- a/sdk/core/examples/get_esdt_tokens.rs +++ b/sdk/core/examples/get_esdt_tokens.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, + gateway::{GatewayProxy, DEVNET_GATEWAY}, }; #[tokio::main] diff --git a/sdk/core/examples/get_hyper_block_by_hash.rs b/sdk/core/examples/get_hyper_block_by_hash.rs index b0f0d34a2d..1d7f969ce1 100644 --- a/sdk/core/examples/get_hyper_block_by_hash.rs +++ b/sdk/core/examples/get_hyper_block_by_hash.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { diff --git a/sdk/core/examples/get_hyper_block_by_nonce.rs b/sdk/core/examples/get_hyper_block_by_nonce.rs index e033b57156..575255d6c9 100644 --- a/sdk/core/examples/get_hyper_block_by_nonce.rs +++ b/sdk/core/examples/get_hyper_block_by_nonce.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { diff --git a/sdk/core/examples/get_hyper_block_latest.rs b/sdk/core/examples/get_hyper_block_latest.rs index 1ab8d51350..934358fdbf 100644 --- a/sdk/core/examples/get_hyper_block_latest.rs +++ b/sdk/core/examples/get_hyper_block_latest.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { diff --git a/sdk/core/examples/get_network_config.rs b/sdk/core/examples/get_network_config.rs index 1c9ada08c6..d53e0e7ce1 100644 --- a/sdk/core/examples/get_network_config.rs +++ b/sdk/core/examples/get_network_config.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { diff --git a/sdk/core/examples/get_network_economics.rs b/sdk/core/examples/get_network_economics.rs index 568c3b8378..bb0410c8f0 100644 --- a/sdk/core/examples/get_network_economics.rs +++ b/sdk/core/examples/get_network_economics.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { diff --git a/sdk/core/examples/sign_tx.rs b/sdk/core/examples/sign_tx.rs index e9e7da0225..aa3f977321 100644 --- a/sdk/core/examples/sign_tx.rs +++ b/sdk/core/examples/sign_tx.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::transaction::Transaction, + gateway::{GatewayProxy, DEVNET_GATEWAY}, wallet::Wallet, }; diff --git a/sdk/core/examples/sign_txs.rs b/sdk/core/examples/sign_txs.rs index 672cf9f08f..2860d528f7 100644 --- a/sdk/core/examples/sign_txs.rs +++ b/sdk/core/examples/sign_txs.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::transaction::Transaction, + gateway::{GatewayProxy, DEVNET_GATEWAY}, wallet::Wallet, }; diff --git a/sdk/core/examples/tx_cost.rs b/sdk/core/examples/tx_cost.rs index 0e05be8420..a982b130ec 100644 --- a/sdk/core/examples/tx_cost.rs +++ b/sdk/core/examples/tx_cost.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::{address::Address, transaction::Transaction}, + gateway::{GatewayProxy, DEVNET_GATEWAY}, utils::base64_encode, }; diff --git a/sdk/core/examples/tx_default_args.rs b/sdk/core/examples/tx_default_args.rs index 45dd3b9473..01b91aa5a6 100644 --- a/sdk/core/examples/tx_default_args.rs +++ b/sdk/core/examples/tx_default_args.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::address::Address, + gateway::{GatewayProxy, DEVNET_GATEWAY}, }; #[tokio::main] diff --git a/sdk/core/examples/tx_info.rs b/sdk/core/examples/tx_info.rs index bfad3fb1ca..f7126daca9 100644 --- a/sdk/core/examples/tx_info.rs +++ b/sdk/core/examples/tx_info.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::blockchain::{GatewayProxy, DEVNET_GATEWAY}; +use multiversx_sdk::gateway::{GatewayProxy, DEVNET_GATEWAY}; #[tokio::main] async fn main() { diff --git a/sdk/core/examples/vm_query.rs b/sdk/core/examples/vm_query.rs index df7d3b43ee..d07f49b856 100644 --- a/sdk/core/examples/vm_query.rs +++ b/sdk/core/examples/vm_query.rs @@ -1,6 +1,6 @@ use multiversx_sdk::{ - blockchain::{GatewayProxy, DEVNET_GATEWAY}, data::{address::Address, vm::VmValueRequest}, + gateway::{GatewayProxy, DEVNET_GATEWAY}, wallet::Wallet, }; diff --git a/sdk/core/src/blockchain.rs b/sdk/core/src/gateway.rs similarity index 100% rename from sdk/core/src/blockchain.rs rename to sdk/core/src/gateway.rs diff --git a/sdk/core/src/blockchain/gateway_account.rs b/sdk/core/src/gateway/gateway_account.rs similarity index 100% rename from sdk/core/src/blockchain/gateway_account.rs rename to sdk/core/src/gateway/gateway_account.rs diff --git a/sdk/core/src/blockchain/gateway_block.rs b/sdk/core/src/gateway/gateway_block.rs similarity index 100% rename from sdk/core/src/blockchain/gateway_block.rs rename to sdk/core/src/gateway/gateway_block.rs diff --git a/sdk/core/src/blockchain/gateway_network.rs b/sdk/core/src/gateway/gateway_network.rs similarity index 100% rename from sdk/core/src/blockchain/gateway_network.rs rename to sdk/core/src/gateway/gateway_network.rs diff --git a/sdk/core/src/blockchain/gateway_proxy.rs b/sdk/core/src/gateway/gateway_proxy.rs similarity index 100% rename from sdk/core/src/blockchain/gateway_proxy.rs rename to sdk/core/src/gateway/gateway_proxy.rs diff --git a/sdk/core/src/blockchain/gateway_tx.rs b/sdk/core/src/gateway/gateway_tx.rs similarity index 100% rename from sdk/core/src/blockchain/gateway_tx.rs rename to sdk/core/src/gateway/gateway_tx.rs diff --git a/sdk/core/src/blockchain/gateway_tx_retrieve.rs b/sdk/core/src/gateway/gateway_tx_retrieve.rs similarity index 100% rename from sdk/core/src/blockchain/gateway_tx_retrieve.rs rename to sdk/core/src/gateway/gateway_tx_retrieve.rs diff --git a/sdk/core/src/lib.rs b/sdk/core/src/lib.rs index f37f2197e5..3a1db99fd7 100644 --- a/sdk/core/src/lib.rs +++ b/sdk/core/src/lib.rs @@ -1,5 +1,5 @@ -pub mod blockchain; pub mod crypto; pub mod data; +pub mod gateway; pub mod utils; pub mod wallet;