etopay_sdk/wallet/
tx_version.rs

1use chrono::{DateTime, Utc};
2use etopay_wallet::types::{CryptoAmount, WalletTransaction};
3use serde::{Deserialize, Serialize};
4
5use crate::types::WalletTxStatus;
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8/// Legacy format (version 1) of a wallet transaction.
9///
10/// This structure represents the original schema used for wallet transactions
11/// before the introduction of `WalletTransaction` in version 2. It is retained
12/// for backward compatibility and data migration purposes.
13pub struct WalletTxInfoV1 {
14    /// Timestamp of the transaction.
15    pub date: DateTime<Utc>,
16    /// Optional block number and block hash associated with the transaction.
17    pub block_number_hash: Option<(u64, String)>,
18    /// Unique identifier (hash) of the transaction.
19    pub transaction_hash: String,
20    /// Wallet address of the sender.
21    pub sender: String,
22    /// Wallet address of the receiver.
23    pub receiver: String,
24    /// Amount of cryptocurrency transferred.
25    pub amount: CryptoAmount,
26    /// Identifier for the network.
27    pub network_key: String,
28    /// Status of the transaction.
29    pub status: WalletTxStatus,
30    /// Optional link to a blockchain explorer showing the transaction details.
31    pub explorer_url: Option<String>,
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
35/// A versioned representation of a wallet transaction.
36pub enum VersionedWalletTransaction {
37    /// Legacy transaction format (version 1).
38    V1(WalletTxInfoV1),
39    /// Current transaction format (version 2).
40    V2(WalletTransaction),
41}
42
43impl VersionedWalletTransaction {
44    /// Returns the timestamp of the transaction.
45    pub fn date(&self) -> DateTime<Utc> {
46        match self {
47            VersionedWalletTransaction::V1(w) => w.date,
48            VersionedWalletTransaction::V2(w) => w.date,
49        }
50    }
51
52    /// Returns the transaction hash.
53    pub fn transaction_hash(&self) -> &str {
54        match self {
55            VersionedWalletTransaction::V1(v1) => &v1.transaction_hash,
56            VersionedWalletTransaction::V2(v2) => &v2.transaction_hash,
57        }
58    }
59
60    /// Returns the network key associated with the transaction.
61    pub fn network_key(&self) -> &str {
62        match self {
63            VersionedWalletTransaction::V1(v1) => &v1.network_key,
64            VersionedWalletTransaction::V2(v2) => &v2.network_key,
65        }
66    }
67
68    /// Returns the current status of the transaction.
69    pub fn status(&self) -> WalletTxStatus {
70        match self {
71            VersionedWalletTransaction::V1(v1) => v1.status,
72            VersionedWalletTransaction::V2(v2) => v2.status,
73        }
74    }
75}
76
77impl From<WalletTransaction> for VersionedWalletTransaction {
78    fn from(value: WalletTransaction) -> Self {
79        Self::V2(WalletTransaction {
80            date: value.date,
81            block_number_hash: value.block_number_hash,
82            transaction_hash: value.transaction_hash,
83            sender: value.sender,
84            receiver: value.receiver,
85            amount: value.amount,
86            network_key: value.network_key,
87            status: value.status,
88            explorer_url: value.explorer_url,
89            gas_fee: value.gas_fee,
90            is_sender: value.is_sender,
91        })
92    }
93}
94
95impl From<VersionedWalletTransaction> for WalletTransaction {
96    fn from(value: VersionedWalletTransaction) -> Self {
97        match value {
98            VersionedWalletTransaction::V1(v1) => WalletTransaction {
99                date: v1.date,
100                block_number_hash: v1.block_number_hash,
101                transaction_hash: v1.transaction_hash,
102                sender: v1.sender,
103                receiver: v1.receiver,
104                amount: v1.amount,
105                network_key: v1.network_key,
106                status: v1.status,
107                explorer_url: v1.explorer_url,
108                gas_fee: None,
109                is_sender: false,
110            },
111            VersionedWalletTransaction::V2(v2) => v2,
112        }
113    }
114}