use chrono::Utc;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::Chain;
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Transaction {
pub id: Uuid,
pub hash: String,
pub from: String,
pub to: String,
pub fee: f64,
pub amount: f64,
pub timestamp: i64,
}
impl Transaction {
pub fn new(from: String, to: String, fee: f64, amount: f64) -> Self {
let timestamp = Utc::now().timestamp();
let hash = Chain::hash(&(&from, &to, amount, timestamp));
Transaction {
id: Uuid::new_v4(),
hash,
from,
to,
fee,
amount,
timestamp,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_transaction() {
let from = "0x 1234".to_string();
let to = "0x 5678".to_string();
let fee = 0.1;
let amount = 100.0;
let transaction = Transaction::new(from.to_owned(), to.to_owned(), fee, amount);
assert_eq!(transaction.from, from);
assert_eq!(transaction.to, to);
assert_eq!(transaction.fee, fee);
assert_eq!(transaction.amount, amount);
}
}