etopay_sdk/types/
transactions.rs

1use api_types::api::{
2    networks::ApiNetwork,
3    transactions::{ApiApplicationMetadata, ApiTxStatus},
4};
5use chrono::{LocalResult, TimeZone, Utc};
6use iota_sdk::{
7    types::block::{helper::network_name_to_id, output::Output, payload::transaction::TransactionEssence},
8    wallet::account::types::Transaction,
9};
10use serde::{Deserialize, Serialize};
11
12use super::currencies::CryptoAmount;
13
14/// Transaction list
15#[derive(Debug, Serialize)]
16pub struct TxList {
17    /// List of transaction info
18    pub txs: Vec<TxInfo>,
19}
20
21/// Transaction info
22#[derive(Debug, Serialize, Clone)]
23pub struct TxInfo {
24    /// Tx creation date, if available
25    pub date: Option<String>,
26    /// sender of the transaction
27    pub sender: String,
28    /// receiver of the transaction
29    pub receiver: String,
30    /// etopay reference id for the transaction
31    pub reference_id: String,
32    /// Application specific metadata attached to the tx
33    pub application_metadata: Option<ApiApplicationMetadata>,
34    /// Amount of transfer
35    pub amount: f64,
36    /// Currency of transfer
37    pub currency: String,
38    /// Status of the transfer
39    pub status: ApiTxStatus,
40    /// The transaction hash on the network
41    pub transaction_hash: Option<String>,
42    /// Exchange rate
43    pub course: f64,
44}
45/// wallet transaction info
46#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
47pub struct WalletTxInfo {
48    /// Tx creation date, if available
49    pub date: String,
50    /// Contains block id
51    pub block_id: Option<String>,
52    /// transaction id for particular transaction
53    pub transaction_id: String,
54    /// Describes type of transaction
55    pub incoming: bool,
56    /// The receiver of the transaction
57    pub receiver: String,
58    /// Amount of transfer
59    pub amount: f64,
60    /// Unique key representing a network
61    pub network_key: String,
62    /// Status of the transfer
63    pub status: String,
64    /// Url of network IOTA/ETH
65    pub explorer_url: Option<String>, // ok
66                                      // change based on the network either eth or iota
67                                      // base explorer url for IOTA = https://explorer.iota.org/mainnet/block/[block_id]
68                                      // base explorer url for EVM = [node url]
69}
70
71/// List of wallet transactions
72#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
73pub struct WalletTxInfoList {
74    /// Transactions that happens
75    pub transactions: Vec<WalletTxInfo>,
76}
77
78impl From<Transaction> for WalletTxInfo {
79    fn from(transaction: Transaction) -> Self {
80        let essence = transaction.payload.essence();
81
82        let TransactionEssence::Regular(regular) = essence;
83        let mut sum: u64 = 0u64;
84        for output in regular.outputs().iter() {
85            if let Output::Basic(output) = output {
86                sum += output.amount();
87            }
88        }
89        // convert from GLOW to IOTA/SMR
90        let sum = sum as f64 / 1_000_000.0;
91
92        let (network_key, explorer_url) = match transaction.network_id {
93            id if id == network_name_to_id("iota") => {
94                let explorer_url = transaction
95                    .block_id
96                    .map(|id| format!("https://explorer.iota.org/mainnet/block/{id}"));
97                ("IOTA", explorer_url)
98            }
99            id => {
100                log::error!("unknown network id: {id}");
101                ("", None)
102            }
103        };
104
105        let date = match Utc.timestamp_millis_opt(transaction.timestamp as i64) {
106            LocalResult::Single(timestamp) => timestamp.to_rfc3339(),
107            _ => {
108                log::error!("could not convert timestamp to date");
109                "".to_string()
110            }
111        };
112
113        WalletTxInfo {
114            block_id: transaction.block_id.map(|id| id.to_string()),
115            transaction_id: transaction.transaction_id.to_string(),
116            incoming: transaction.incoming,
117            receiver: String::new(), // TODO: iota is out anyways
118            amount: sum,
119            network_key: network_key.to_string(),
120            status: format!("{:?}", transaction.inclusion_state),
121            explorer_url,
122            date,
123        }
124    }
125}
126
127/// Purchase details
128#[derive(Clone)]
129pub struct PurchaseDetails {
130    /// The sender address where the fees goes to.
131    pub system_address: String,
132    /// The amount to be paid.
133    pub amount: CryptoAmount,
134    /// The status of transaction
135    pub status: ApiTxStatus,
136    /// The network that the transaction is sent in
137    pub network: ApiNetwork,
138}
139
140/// Gas estimation (EIP-1559)
141#[derive(Debug, PartialEq)]
142pub struct GasCostEstimation {
143    /// The maximum fee the sender is willing to pay per unit of gas.
144    pub max_fee_per_gas: u128,
145    /// The maximum tip the sender is willing to pay to miners (in EIP-1559).
146    pub max_priority_fee_per_gas: u128,
147    /// The maximum amount of gas that the transaction can consume.
148    pub gas_limit: u64,
149}