multiversx_sdk/data/
vm.rs

1use multiversx_chain_core::std::Bech32Address;
2use serde::{Deserialize, Serialize};
3use serde_repr::{Deserialize_repr, Serialize_repr};
4use std::collections::HashMap;
5
6use crate::utils::base64_decode;
7
8#[derive(Serialize_repr, Deserialize_repr, Debug, Clone)]
9#[repr(u8)]
10pub enum CallType {
11    // DirectCall means that the call is an explicit SC invocation originating from a user Transaction
12    DirectCall = 0,
13
14    // AsynchronousCall means that the invocation was performed from within
15    // another SmartContract from another Shard, using asyncCall
16    AsynchronousCall = 1,
17
18    // AsynchronousCallBack means that an AsynchronousCall was performed
19    // previously, and now the control returns to the caller SmartContract's callBack method
20    AsynchronousCallBack = 2,
21
22    // ESDTTransferAndExecute means that there is a smart contract execution after the ESDT transfer
23    // this is needed in order to skip the check whether a contract is payable or not
24    ESDTTransferAndExecute = 3,
25}
26
27// VmValueRequest defines the request struct for values available in a VM
28#[derive(Debug, Clone, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct VMQueryInput {
31    pub sc_address: Bech32Address,
32    pub func_name: String,
33    pub args: Vec<String>,
34}
35
36// LogEntryApi is a wrapper over vmcommon's LogEntry
37#[derive(Debug, Clone, Serialize, Deserialize)]
38#[serde(rename_all = "camelCase")]
39pub struct LogEntryApi {
40    pub identifier: String,
41    pub address: Bech32Address,
42    pub topics: Vec<String>,
43    pub data: String,
44}
45
46// OutputTransferApi is a wrapper over vmcommon's OutputTransfer
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(rename_all = "camelCase")]
49pub struct OutputTransferApi {
50    pub value: String,
51    pub gas_limit: u64,
52    pub data: String,
53    pub call_type: CallType,
54    pub sender_address: Bech32Address,
55}
56
57// OutputAccountApi is a wrapper over vmcommon's OutputAccount
58#[derive(Debug, Clone, Serialize, Deserialize)]
59#[serde(rename_all = "camelCase")]
60pub struct OutputAccountApi {
61    address: Bech32Address,
62    nonce: u64,
63
64    // TODO: unknown type of data
65    // balance: Option<String>,
66    balance_delta: u64,
67    storage_updates: Option<HashMap<String, StorageUpdateApi>>,
68    code: Option<String>,
69    code_metadata: Option<String>,
70
71    #[serde(default)]
72    output_transfers: Vec<OutputTransferApi>,
73    call_type: CallType,
74}
75
76// StorageUpdateApi is a wrapper over vmcommon's StorageUpdate
77#[derive(Debug, Clone, Serialize, Deserialize)]
78#[serde(rename_all = "camelCase")]
79pub struct StorageUpdateApi {
80    offset: String,
81    data: String,
82}
83
84// VMOutputApi is a wrapper over the vmcommon's VMOutput
85#[derive(Debug, Clone, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub struct VMOutputApi {
88    #[serde(default)]
89    pub return_data: Option<Vec<String>>,
90    pub return_code: String,
91    pub return_message: String,
92    pub gas_remaining: u64,
93    pub gas_refund: u64,
94    pub output_accounts: HashMap<String, OutputAccountApi>,
95    pub deleted_accounts: Option<Vec<String>>,
96    pub touched_accounts: Option<Vec<String>>,
97    pub logs: Option<Vec<LogEntryApi>>,
98}
99
100impl VMOutputApi {
101    pub fn return_data(&self) -> &[String] {
102        if let Some(return_data) = &self.return_data {
103            return_data
104        } else {
105            &[]
106        }
107    }
108
109    pub fn return_data_base64_decode(&self) -> Vec<Vec<u8>> {
110        self.return_data().iter().map(base64_decode).collect()
111    }
112
113    pub fn is_ok(&self) -> bool {
114        self.return_code == "ok"
115    }
116}
117
118// VmValuesResponseData follows the format of the data field in an API response for a VM values query
119#[derive(Debug, Clone, Serialize, Deserialize)]
120#[serde(rename_all = "camelCase")]
121pub struct VmValuesResponseData {
122    pub data: VMOutputApi,
123}
124
125// ResponseVmValue defines a wrapper over string containing returned data in hex format
126#[derive(Debug, Clone, Serialize, Deserialize)]
127#[serde(rename_all = "camelCase")]
128pub struct ResponseVmValue {
129    pub data: Option<VmValuesResponseData>,
130    pub error: String,
131    pub code: String,
132}