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