klever_vm_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 slot: 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 block_nonce: u64,
67    pub block_hash: String,
68    pub timestamp: u64,
69    pub data: Option<String>,
70    pub status: String,
71    pub logs: Option<ApiLogs>,
72}
73
74// Events represents the events generated by a transaction with changed fields' types in order to make it friendly for API's json
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77pub struct Events {
78    pub address: Address,
79    pub identifier: String,
80    pub topics: Option<Vec<String>>,
81    pub data: Option<String>,
82}
83
84// ApiLogs represents logs with changed fields' types in order to make it friendly for API's json
85#[derive(Debug, Clone, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub struct ApiLogs {
88    pub address: Address,
89    pub events: Vec<Events>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize)]
93#[serde(rename_all = "camelCase")]
94pub struct ApiSmartContractResult {
95    pub hash: String,
96    pub nonce: u64,
97    pub value: u64,
98    pub receiver: Address,
99    pub sender: Address,
100    pub data: String,
101    pub prev_tx_hash: String,
102    pub original_tx_hash: String,
103    pub gas_limit: u64,
104    pub gas_price: u64,
105    pub call_type: CallType,
106    pub relayer_address: Option<String>,
107    pub relayed_value: Option<String>,
108    pub code: Option<String>,
109    pub code_metadata: Option<String>,
110    pub return_message: Option<String>,
111    pub original_sender: Option<String>,
112}
113
114#[derive(Debug, Clone, Serialize, Deserialize)]
115pub struct TransactionInfoData {
116    pub transaction: TransactionOnNetwork,
117}
118
119// TransactionInfo holds a transaction info response from the network
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct TransactionInfo {
122    #[serde(default)]
123    pub error: String,
124    pub code: String,
125    pub data: Option<TransactionInfoData>,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct TransactionStatusData {
130    pub status: String,
131}
132
133// TransactionStatus holds a transaction's status response from the network
134#[derive(Debug, Clone, Serialize, Deserialize)]
135pub struct TransactionStatus {
136    pub error: String,
137    pub code: String,
138    pub data: Option<TransactionStatusData>,
139}
140
141// ArgCreateTransaction will hold the transaction fields
142#[derive(Debug, Clone, Serialize, Deserialize)]
143pub struct ArgCreateTransaction {
144    pub nonce: u64,
145    pub value: String,
146    pub rcv_addr: Address,
147    pub snd_addr: Address,
148    pub gas_price: u64,
149    pub gas_limit: u64,
150    pub data: Option<String>,
151    pub signature: String,
152    pub chain_id: String,
153    pub version: u32,
154    pub options: u32,
155    pub available_balance: String,
156}
157
158#[derive(Debug, Clone, Serialize, Deserialize)]
159#[serde(rename_all = "camelCase")]
160pub struct SendTransactionData {
161    pub tx_hash: String,
162}
163
164// SendTransactionResponse holds the response received from the network when broadcasting a transaction
165#[derive(Debug, Clone, Serialize, Deserialize)]
166pub struct SendTransactionResponse {
167    pub error: String,
168    pub code: String,
169    pub data: Option<SendTransactionData>,
170}
171
172#[derive(Debug, Clone, Serialize, Deserialize)]
173#[serde(rename_all = "camelCase")]
174pub struct SendTransactionsResponseData {
175    pub num_of_sent_txs: i32,
176    pub txs_hashes: HashMap<i32, String>,
177}
178
179// SendTransactionsResponse holds the response received from the network when broadcasting multiple transactions
180#[derive(Debug, Clone, Serialize, Deserialize)]
181pub struct SendTransactionsResponse {
182    pub error: String,
183    pub code: String,
184    pub data: Option<SendTransactionsResponseData>,
185}