84 lines
2.4 KiB
Rust
84 lines
2.4 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use crate::db::{SledModel, Storable};
|
|
use std::collections::HashMap;
|
|
|
|
/// Asset represents a cryptocurrency asset in a wallet
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Asset {
|
|
pub name: String, // Asset name (e.g., "USDC")
|
|
pub amount: f64, // Amount of the asset
|
|
}
|
|
|
|
impl Asset {
|
|
/// Create a new asset
|
|
pub fn new(name: String, amount: f64) -> Self {
|
|
Self {
|
|
name,
|
|
amount,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Wallet represents a cryptocurrency wallet
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Wallet {
|
|
pub id: u32, // unique id
|
|
pub name: String, // name of the wallet
|
|
pub description: String, // optional description
|
|
pub blockchain_name: String, // name of the blockchain
|
|
pub pubkey: String, // public key of the wallet
|
|
pub assets: Vec<Asset>, // assets in the wallet
|
|
}
|
|
|
|
impl Wallet {
|
|
/// Create a new wallet
|
|
pub fn new(id: u32, name: String, description: String, blockchain_name: String, pubkey: String) -> Self {
|
|
Self {
|
|
id,
|
|
name,
|
|
description,
|
|
blockchain_name,
|
|
pubkey,
|
|
assets: Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// Set an asset in the wallet (replaces if exists, adds if not)
|
|
pub fn set_asset(&mut self, name: String, amount: f64) {
|
|
// Check if the asset already exists
|
|
if let Some(asset) = self.assets.iter_mut().find(|a| a.name == name) {
|
|
// Update the amount
|
|
asset.amount = amount;
|
|
} else {
|
|
// Add a new asset
|
|
self.assets.push(Asset::new(name, amount));
|
|
}
|
|
}
|
|
|
|
/// Get the total value of all assets in the wallet
|
|
pub fn total_value(&self) -> f64 {
|
|
self.assets.iter().map(|a| a.amount).sum()
|
|
}
|
|
|
|
/// Returns a map of index keys for this wallet
|
|
pub fn index_keys(&self) -> HashMap<String, String> {
|
|
let mut keys = HashMap::new();
|
|
keys.insert("name".to_string(), self.name.clone());
|
|
keys.insert("blockchain".to_string(), self.blockchain_name.clone());
|
|
keys
|
|
}
|
|
}
|
|
|
|
// Implement Storable trait (provides default dump/load)
|
|
impl Storable for Wallet {}
|
|
|
|
// Implement SledModel trait
|
|
impl SledModel for Wallet {
|
|
fn get_id(&self) -> String {
|
|
self.id.to_string()
|
|
}
|
|
|
|
fn db_prefix() -> &'static str {
|
|
"wallet"
|
|
}
|
|
} |