drt_sdk/data/
transaction.rs

1use std::collections::HashMap;
2
3use super::{address::Address, vm::CallType};
4use serde::{Deserialize, Serialize};
5
6// Transaction holds the fields of a transaction to be broadcasted to the network
7#[derive(Debug, Clone, Serialize, Deserialize)]
8#[serde(rename_all = "camelCase")]
9pub struct Transaction {
10    pub nonce: u64,
11    pub value: String,
12    pub receiver: Address,
13    pub sender: Address,
14    pub gas_price: u64,
15    pub gas_limit: u64,
16    #[serde(skip_serializing_if = "Option::is_none")]
17    pub data: Option<String>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub signature: Option<String>,
20    #[serde(rename = "chainID")]
21    pub chain_id: String,
22    pub version: u32,
23    #[serde(skip_serializing_if = "is_zero")]
24    pub options: u32,
25}
26
27/// This is only used for serialize
28#[allow(clippy::trivially_copy_pass_by_ref)]
29fn is_zero(num: &u32) -> bool {
30    *num == 0
31}
32
33// TxCostResponseData follows the format of the data field of a transaction cost request
34#[derive(Debug, Clone, Serialize, Deserialize)]
35#[serde(rename_all = "camelCase")]
36pub struct TxCostResponseData {
37    pub tx_gas_units: u64,
38    pub return_message: String,
39}
40
41// ResponseTxCost defines a response from the node holding the transaction cost
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[serde(rename_all = "camelCase")]
44pub struct ResponseTxCost {
45    pub data: Option<TxCostResponseData>,
46    pub error: String,
47    pub code: String,
48}
49
50// TransactionOnNetwork holds a transaction's info entry in a hyper block
51#[derive(Debug, Clone, Serialize, Deserialize, Default)]
52#[serde(rename_all = "camelCase")]
53pub struct TransactionOnNetwork {
54    #[serde(rename = "type")]
55    pub kind: String,
56    pub hash: Option<String>,
57    pub nonce: u64,
58    pub round: u64,
59    pub epoch: u64,
60    pub value: String,
61    pub receiver: Address,
62    pub sender: Address,
63    pub gas_price: u64,
64    pub gas_limit: u64,
65    pub signature: String,
66    pub source_shard: u32,
67    pub destination_shard: u32,
68    pub block_nonce: u64,
69    pub block_hash: String,
70    pub notarized_at_source_in_meta_nonce: Option<u64>,
71    #[serde(rename = "NotarizedAtSourceInMetaHash")]
72    pub notarized_at_source_in_meta_hash: Option<String>,
73    pub notarized_at_destination_in_meta_nonce: Option<u64>,
74    pub notarized_at_destination_in_meta_hash: Option<String>,
75    pub processing_type_on_destination: String,
76    pub miniblock_type: String,
77    pub miniblock_hash: String,
78    pub timestamp: u64,
79    pub data: Option<String>,
80    pub status: String,
81    pub hyperblock_nonce: Option<u64>,
82    pub hyperblock_hash: Option<String>,
83    #[serde(default)]
84    pub smart_contract_results: Vec<ApiSmartContractResult>,
85    pub logs: Option<ApiLogs>,
86}
87
88// Events represents the events generated by a transaction with changed fields' types in order to make it friendly for API's json
89#[derive(Debug, Clone, Serialize, Deserialize)]
90#[serde(rename_all = "camelCase")]
91pub struct Events {
92    pub address: Address,
93    pub identifier: String,
94    pub topics: Option<Vec<String>>,
95    pub data: Option<String>,
96}
97
98// ApiLogs represents logs with changed fields' types in order to make it friendly for API's json
99#[derive(Debug, Clone, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101pub struct ApiLogs {
102    pub address: Address,
103    pub events: Vec<Events>,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107#[serde(rename_all = "camelCase")]
108pub struct ApiSmartContractResult {
109    pub hash: String,
110    pub nonce: u64,
111    pub value: u64,
112    pub receiver: Address,
113    pub sender: Address,
114    pub data: String,
115    pub prev_tx_hash: String,
116    pub original_tx_hash: String,
117    pub gas_limit: u64,
118    pub gas_price: u64,
119    pub call_type: CallType,
120    pub relayer_address: Option<String>,
121    pub relayed_value: Option<String>,
122    pub code: Option<String>,
123    pub code_metadata: Option<String>,
124    pub return_message: Option<String>,
125    pub original_sender: Option<String>,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct TransactionInfoData {
130    pub transaction: TransactionOnNetwork,
131}
132
133// TransactionInfo holds a transaction info response from the network
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct TransactionInfo {
136    #[serde(default)]
137    pub error: String,
138    pub code: String,
139    pub data: Option<TransactionInfoData>,
140}
141
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct TransactionStatusData {
144    pub status: String,
145}
146
147// TransactionStatus holds a transaction's status response from the network
148#[derive(Debug, Clone, Serialize, Deserialize)]
149pub struct TransactionStatus {
150    pub error: String,
151    pub code: String,
152    pub data: Option<TransactionStatusData>,
153}
154
155// ArgCreateTransaction will hold the transaction fields
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct ArgCreateTransaction {
158    pub nonce: u64,
159    pub value: String,
160    pub rcv_addr: Address,
161    pub snd_addr: Address,
162    pub gas_price: u64,
163    pub gas_limit: u64,
164    pub data: Option<String>,
165    pub signature: String,
166    pub chain_id: String,
167    pub version: u32,
168    pub options: u32,
169    pub available_balance: String,
170}
171
172#[derive(Debug, Clone, Serialize, Deserialize)]
173#[serde(rename_all = "camelCase")]
174pub struct SendTransactionData {
175    pub tx_hash: String,
176}
177
178// SendTransactionResponse holds the response received from the network when broadcasting a transaction
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct SendTransactionResponse {
181    pub error: String,
182    pub code: String,
183    pub data: Option<SendTransactionData>,
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize)]
187#[serde(rename_all = "camelCase")]
188pub struct SendTransactionsResponseData {
189    pub num_of_sent_txs: i32,
190    pub txs_hashes: HashMap<i32, String>,
191}
192
193// SendTransactionsResponse holds the response received from the network when broadcasting multiple transactions
194#[derive(Debug, Clone, Serialize, Deserialize)]
195pub struct SendTransactionsResponse {
196    pub error: String,
197    pub code: String,
198    pub data: Option<SendTransactionsResponseData>,
199}