use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct StateTransfer {
pub asset_id: String,
pub from: String,
pub to: String,
pub amount: u64,
pub transition_id: String,
}
#[derive(Debug)]
pub struct StateValidator {
transfers: HashMap<String, StateTransfer>, }
impl Default for StateValidator {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct StateTransition {
pub id: String,
pub asset_id: String,
pub inputs: Vec<String>,
pub outputs: Vec<(String, u64)>, pub metadata: HashMap<String, String>,
}
impl StateValidator {
pub fn new() -> Self {
Self {
transfers: HashMap::new(),
}
}
pub fn register_transfer(&mut self, transfer: StateTransfer) -> Result<(), &'static str> {
self.transfers
.insert(transfer.transition_id.clone(), transfer);
Ok(())
}
pub fn validate_transfer(&self, transition_id: &str) -> Result<bool, &'static str> {
if let Some(_transfer) = self.transfers.get(transition_id) {
Ok(true)
} else {
Err("Transfer not found")
}
}
}
impl StateTransfer {
pub fn new(asset_id: &str, from: &str, to: &str, amount: u64) -> Self {
let transition_id = format!(
"transition:{:x}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
);
Self {
asset_id: asset_id.to_string(),
from: from.to_string(),
to: to.to_string(),
amount,
transition_id,
}
}
}
impl StateTransition {
pub fn new(asset_id: &str) -> Self {
let id = format!(
"transition:{:x}",
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
);
Self {
id,
asset_id: asset_id.to_string(),
inputs: Vec::new(),
outputs: Vec::new(),
metadata: HashMap::new(),
}
}
pub fn add_input(&mut self, input: &str) {
self.inputs.push(input.to_string());
}
pub fn add_output(&mut self, address: &str, amount: u64) {
self.outputs.push((address.to_string(), amount));
}
pub fn add_metadata(&mut self, key: &str, value: &str) {
self.metadata.insert(key.to_string(), value.to_string());
}
}