nym_types/
transaction.rs

1use crate::currency::DecCoin;
2use crate::error::TypesError;
3use crate::gas::{Gas, GasInfo};
4use nym_validator_client::nyxd::cosmwasm_client::types::ExecuteResult;
5use nym_validator_client::nyxd::TxResponse;
6use serde::{Deserialize, Serialize};
7
8#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
9#[cfg_attr(
10    feature = "generate-ts",
11    ts(export, export_to = "ts-packages/types/src/types/rust/SendTxResult.ts")
12)]
13#[derive(Deserialize, Serialize, Debug)]
14pub struct SendTxResult {
15    pub block_height: u64,
16    pub code: u32,
17    pub details: TransactionDetails,
18    pub gas_used: Gas,
19    pub gas_wanted: Gas,
20    pub tx_hash: String,
21    pub fee: Option<DecCoin>,
22}
23
24impl SendTxResult {
25    pub fn new(t: TxResponse, details: TransactionDetails, fee: Option<DecCoin>) -> SendTxResult {
26        SendTxResult {
27            block_height: t.height.value(),
28            code: t.tx_result.code.value(),
29            details,
30            gas_used: t.tx_result.gas_used.into(),
31            gas_wanted: t.tx_result.gas_wanted.into(),
32            tx_hash: t.hash.to_string(),
33            fee,
34        }
35    }
36}
37
38#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
39#[cfg_attr(
40    feature = "generate-ts",
41    ts(
42        export,
43        export_to = "ts-packages/types/src/types/rust/TransactionDetails.ts"
44    )
45)]
46#[derive(Deserialize, Serialize, Debug)]
47pub struct TransactionDetails {
48    pub amount: DecCoin,
49    pub from_address: String,
50    pub to_address: String,
51}
52
53impl TransactionDetails {
54    pub fn new(amount: DecCoin, from_address: String, to_address: String) -> Self {
55        TransactionDetails {
56            amount,
57            from_address,
58            to_address,
59        }
60    }
61}
62
63#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
64#[cfg_attr(
65    feature = "generate-ts",
66    ts(
67        export,
68        export_to = "ts-packages/types/src/types/rust/TransactionExecuteResult.ts"
69    )
70)]
71#[derive(Deserialize, Serialize, Debug)]
72pub struct TransactionExecuteResult {
73    pub logs_json: String,
74    pub msg_responses_json: String,
75    pub transaction_hash: String,
76    pub gas_info: GasInfo,
77    pub fee: Option<DecCoin>,
78}
79
80impl TransactionExecuteResult {
81    pub fn from_execute_result(
82        value: ExecuteResult,
83        fee: Option<DecCoin>,
84    ) -> Result<TransactionExecuteResult, TypesError> {
85        Ok(TransactionExecuteResult {
86            gas_info: value.gas_info.into(),
87            transaction_hash: value.transaction_hash.to_string(),
88            msg_responses_json: ::serde_json::to_string_pretty(&value.msg_responses)?,
89            logs_json: ::serde_json::to_string_pretty(&value.logs)?,
90            fee,
91        })
92    }
93}
94
95#[cfg_attr(feature = "generate-ts", derive(ts_rs::TS))]
96#[cfg_attr(
97    feature = "generate-ts",
98    ts(
99        export,
100        export_to = "ts-packages/types/src/types/rust/RpcTransactionResponse.ts"
101    )
102)]
103#[derive(Deserialize, Serialize)]
104pub struct RpcTransactionResponse {
105    pub index: u32,
106    pub tx_result_json: String,
107    pub block_height: u64,
108    pub transaction_hash: String,
109    pub gas_used: Gas,
110    pub gas_wanted: Gas,
111    pub fee: Option<DecCoin>,
112}
113
114impl RpcTransactionResponse {
115    pub fn from_tx_response(
116        t: &TxResponse,
117        fee: Option<DecCoin>,
118    ) -> Result<RpcTransactionResponse, TypesError> {
119        Ok(RpcTransactionResponse {
120            index: t.index,
121            gas_used: t.tx_result.gas_used.into(),
122            gas_wanted: t.tx_result.gas_wanted.into(),
123            transaction_hash: t.hash.to_string(),
124            tx_result_json: ::serde_json::to_string_pretty(&t.tx_result)?,
125            block_height: t.height.value(),
126            fee,
127        })
128    }
129}