multiversx_sdk_http/gateway_http_proxy/
http_tx.rs

1use anyhow::Result;
2use multiversx_sdk::{
3    chain_core::types::Address,
4    data::{
5        network_config::NetworkConfig,
6        transaction::{
7            ArgCreateTransaction, Transaction, TransactionOnNetwork, TxCostResponseData,
8        },
9        vm::{VMQueryInput, VmValuesResponseData},
10    },
11    gateway::{
12        GetTxCost, GetTxInfo, GetTxProcessStatus, GetTxStatus, SendMultiTxRequest, SendTxRequest,
13        VMQueryRequest,
14    },
15};
16
17use super::GatewayHttpProxy;
18
19impl GatewayHttpProxy {
20    // request_transaction_cost retrieves how many gas a transaction will consume
21    pub async fn request_transaction_cost(&self, tx: &Transaction) -> Result<TxCostResponseData> {
22        self.http_request(GetTxCost(tx)).await
23    }
24
25    // get_transaction_info retrieves a transaction's details from the network
26    pub async fn get_transaction_info(&self, hash: &str) -> Result<TransactionOnNetwork> {
27        self.http_request(GetTxInfo::new(hash)).await
28    }
29
30    // get_transaction_info_with_results retrieves a transaction's details from the network with events
31    pub async fn get_transaction_info_with_results(
32        &self,
33        hash: &str,
34    ) -> Result<TransactionOnNetwork> {
35        self.http_request(GetTxInfo::new(hash).with_results()).await
36    }
37
38    // get_transaction_status retrieves a transaction's status from the network
39    pub async fn get_transaction_status(&self, hash: &str) -> Result<String> {
40        self.http_request(GetTxStatus::new(hash)).await
41    }
42
43    // get_transaction_process_status retrieves a transaction's status from the network using process-status API
44    pub async fn get_transaction_process_status(&self, hash: &str) -> Result<(String, String)> {
45        self.http_request(GetTxProcessStatus::new(hash)).await
46    }
47
48    // get_default_transaction_arguments will prepare the transaction creation argument by querying the account's info
49    pub async fn get_default_transaction_arguments(
50        &self,
51        address: &Address,
52        network_configs: &NetworkConfig,
53    ) -> Result<ArgCreateTransaction> {
54        let account = self
55            .get_account(&address.to_bech32(&network_configs.address_hrp))
56            .await?;
57
58        let address_bech32 = address.to_bech32(&network_configs.address_hrp);
59
60        Ok(ArgCreateTransaction {
61            nonce: account.nonce,
62            value: "".to_string(),
63            rcv_addr: address_bech32.clone(),
64            snd_addr: address_bech32,
65            gas_price: network_configs.min_gas_price,
66            gas_limit: network_configs.min_gas_limit,
67            data: None,
68            signature: "".to_string(),
69            chain_id: network_configs.chain_id.clone(),
70            version: network_configs.min_transaction_version,
71            options: 0,
72            available_balance: account.balance,
73        })
74    }
75
76    pub async fn send_transaction(&self, tx: &Transaction) -> Result<String> {
77        self.http_request(SendTxRequest(tx)).await
78    }
79
80    #[allow(clippy::ptr_arg)]
81    pub async fn send_transactions(&self, txs: &Vec<Transaction>) -> Result<Vec<String>> {
82        self.http_request(SendMultiTxRequest(txs)).await
83    }
84
85    // execute_vmquery retrieves data from existing SC trie through the use of a VM
86    pub async fn execute_vmquery(&self, vm_request: &VMQueryInput) -> Result<VmValuesResponseData> {
87        self.http_request(VMQueryRequest(vm_request)).await
88    }
89}