use crate::core::Account; use crate::error::TxError; #[derive(serde::Deserialize, serde::Serialize, Debug, Clone)] pub enum NetworkData { Transaction(Tx) } #[derive(serde::Deserialize, serde::Serialize, Debug, clap::Args, Clone)] pub struct Tx { from: Account, to: Account, value: u32, data: String } impl Tx { pub fn new(from: Account, to: Account, value: u32, data: String) -> Self { Self { from, to, value, data } } pub fn validate(&self) -> Result<(), TxError> { if self.from.is_empty() { return Err(TxError::FromEmpty) } else if self.to.is_empty() { return Err(TxError::ToEmpty) } else if self.value == 0 { return Err(TxError::ValueEmpty) } Ok(()) } pub fn is_new_account(&self) -> bool { return self.data == "new_account"; } pub fn is_reward(&self) -> bool { return self.data == "reward"; } pub fn from(&self) -> &Account { &self.from } pub fn to(&self) -> &Account { &self.to } pub fn value(&self) -> u32 { self.value } pub fn data(&self) -> &str { &self.data } }