blockchain/
wallet.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4/// A wallet that holds a balance of a cryptocurrency.
5#[derive(Clone, Debug, Serialize, Deserialize)]
6pub struct Wallet {
7    /// Unique identifier of the wallet.
8    pub id: Uuid,
9
10    /// Unique email address associated with the wallet.
11    pub email: String,
12
13    /// Address uniquely identifying the wallet.
14    pub address: String,
15
16    /// The current balance of the wallet.
17    pub balance: f64,
18
19    /// A history of transactions associated with the wallet.
20    pub transaction_hashes: Vec<String>,
21}
22
23impl Wallet {
24    /// Create a new wallet.
25    ///
26    /// # Arguments
27    ///
28    /// - `email`: The email address associated with the wallet.
29    /// - `address`: The address uniquely identifying the wallet.
30    ///
31    /// # Returns
32    ///
33    /// A new wallet with the given email, address, and balance.
34    pub fn new(email: &str, address: &str) -> Self {
35        Wallet {
36            id: Uuid::new_v4(),
37            email: email.to_string(),
38            address: address.to_string(),
39            balance: 0.0,
40            transaction_hashes: vec![],
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_new_wallet() {
51        let email = "email".to_string();
52        let address = "0x 1234".to_string();
53        let wallet = Wallet::new(&email, &address);
54
55        assert_eq!(wallet.id.get_version(), Some(uuid::Version::Random));
56        assert_eq!(wallet.email, email);
57        assert_eq!(wallet.address, address);
58        assert_eq!(wallet.balance, 0.0);
59        assert!(wallet.transaction_hashes.is_empty());
60    }
61}