207 lines
6.2 KiB
Rust
207 lines
6.2 KiB
Rust
use chrono::{DateTime, Utc};
|
|
use serde::{Serialize, Deserialize};
|
|
use std::sync::{Arc, Mutex};
|
|
use std::collections::HashMap;
|
|
use lazy_static::lazy_static;
|
|
use uuid::Uuid;
|
|
|
|
// DeFi position status
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum DefiPositionStatus {
|
|
Active,
|
|
Completed,
|
|
Liquidated,
|
|
Cancelled
|
|
}
|
|
|
|
impl DefiPositionStatus {
|
|
pub fn as_str(&self) -> &str {
|
|
match self {
|
|
DefiPositionStatus::Active => "Active",
|
|
DefiPositionStatus::Completed => "Completed",
|
|
DefiPositionStatus::Liquidated => "Liquidated",
|
|
DefiPositionStatus::Cancelled => "Cancelled",
|
|
}
|
|
}
|
|
}
|
|
|
|
// DeFi position type
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum DefiPositionType {
|
|
Providing,
|
|
Receiving,
|
|
Liquidity,
|
|
Staking,
|
|
Collateral,
|
|
}
|
|
|
|
impl DefiPositionType {
|
|
pub fn as_str(&self) -> &str {
|
|
match self {
|
|
DefiPositionType::Providing => "Providing",
|
|
DefiPositionType::Receiving => "Receiving",
|
|
DefiPositionType::Liquidity => "Liquidity",
|
|
DefiPositionType::Staking => "Staking",
|
|
DefiPositionType::Collateral => "Collateral",
|
|
}
|
|
}
|
|
}
|
|
|
|
// Base DeFi position
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct DefiPosition {
|
|
pub id: String,
|
|
pub position_type: DefiPositionType,
|
|
pub status: DefiPositionStatus,
|
|
pub asset_id: String,
|
|
pub asset_name: String,
|
|
pub asset_symbol: String,
|
|
pub amount: f64,
|
|
pub value_usd: f64,
|
|
pub expected_return: f64,
|
|
pub created_at: DateTime<Utc>,
|
|
pub expires_at: Option<DateTime<Utc>>,
|
|
pub user_id: String,
|
|
}
|
|
|
|
// Providing position
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ProvidingPosition {
|
|
pub base: DefiPosition,
|
|
pub duration_days: i32,
|
|
pub profit_share_earned: f64,
|
|
pub return_amount: f64,
|
|
}
|
|
|
|
// Receiving position
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct ReceivingPosition {
|
|
pub base: DefiPosition,
|
|
pub collateral_asset_id: String,
|
|
pub collateral_asset_name: String,
|
|
pub collateral_asset_symbol: String,
|
|
pub collateral_amount: f64,
|
|
pub collateral_value_usd: f64,
|
|
pub duration_days: i32,
|
|
pub profit_share_rate: f64,
|
|
pub profit_share_owed: f64,
|
|
pub total_to_repay: f64,
|
|
pub collateral_ratio: f64,
|
|
}
|
|
|
|
// In-memory database for DeFi positions
|
|
pub struct DefiDatabase {
|
|
providing_positions: HashMap<String, ProvidingPosition>,
|
|
receiving_positions: HashMap<String, ReceivingPosition>,
|
|
}
|
|
|
|
impl DefiDatabase {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
providing_positions: HashMap::new(),
|
|
receiving_positions: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
// Providing operations
|
|
pub fn add_providing_position(&mut self, position: ProvidingPosition) {
|
|
self.providing_positions.insert(position.base.id.clone(), position);
|
|
}
|
|
|
|
pub fn get_providing_position(&self, id: &str) -> Option<&ProvidingPosition> {
|
|
self.providing_positions.get(id)
|
|
}
|
|
|
|
pub fn get_all_providing_positions(&self) -> Vec<&ProvidingPosition> {
|
|
self.providing_positions.values().collect()
|
|
}
|
|
|
|
pub fn get_user_providing_positions(&self, user_id: &str) -> Vec<&ProvidingPosition> {
|
|
self.providing_positions
|
|
.values()
|
|
.filter(|p| p.base.user_id == user_id)
|
|
.collect()
|
|
}
|
|
|
|
// Receiving operations
|
|
pub fn add_receiving_position(&mut self, position: ReceivingPosition) {
|
|
self.receiving_positions.insert(position.base.id.clone(), position);
|
|
}
|
|
|
|
pub fn get_receiving_position(&self, id: &str) -> Option<&ReceivingPosition> {
|
|
self.receiving_positions.get(id)
|
|
}
|
|
|
|
pub fn get_all_receiving_positions(&self) -> Vec<&ReceivingPosition> {
|
|
self.receiving_positions.values().collect()
|
|
}
|
|
|
|
pub fn get_user_receiving_positions(&self, user_id: &str) -> Vec<&ReceivingPosition> {
|
|
self.receiving_positions
|
|
.values()
|
|
.filter(|p| p.base.user_id == user_id)
|
|
.collect()
|
|
}
|
|
}
|
|
|
|
// Global instance of the DeFi database
|
|
lazy_static! {
|
|
pub static ref DEFI_DB: Arc<Mutex<DefiDatabase>> = Arc::new(Mutex::new(DefiDatabase::new()));
|
|
}
|
|
|
|
// Initialize the database with mock data
|
|
pub fn initialize_mock_data() {
|
|
let mut db = DEFI_DB.lock().unwrap();
|
|
|
|
// Add mock providing positions
|
|
let providing_position = ProvidingPosition {
|
|
base: DefiPosition {
|
|
id: Uuid::new_v4().to_string(),
|
|
position_type: DefiPositionType::Providing,
|
|
status: DefiPositionStatus::Active,
|
|
asset_id: "TFT".to_string(),
|
|
asset_name: "ThreeFold Token".to_string(),
|
|
asset_symbol: "TFT".to_string(),
|
|
amount: 1000.0,
|
|
value_usd: 500.0,
|
|
expected_return: 4.2,
|
|
created_at: Utc::now(),
|
|
expires_at: Some(Utc::now() + chrono::Duration::days(30)),
|
|
user_id: "user123".to_string(),
|
|
},
|
|
duration_days: 30,
|
|
profit_share_earned: 3.5,
|
|
return_amount: 1003.5,
|
|
};
|
|
db.add_providing_position(providing_position);
|
|
|
|
// Add mock receiving positions
|
|
let receiving_position = ReceivingPosition {
|
|
base: DefiPosition {
|
|
id: Uuid::new_v4().to_string(),
|
|
position_type: DefiPositionType::Receiving,
|
|
status: DefiPositionStatus::Active,
|
|
asset_id: "ZDFZ".to_string(),
|
|
asset_name: "Zanzibar Token".to_string(),
|
|
asset_symbol: "ZDFZ".to_string(),
|
|
amount: 500.0,
|
|
value_usd: 250.0,
|
|
expected_return: 5.8,
|
|
created_at: Utc::now(),
|
|
expires_at: Some(Utc::now() + chrono::Duration::days(90)),
|
|
user_id: "user123".to_string(),
|
|
},
|
|
collateral_asset_id: "TFT".to_string(),
|
|
collateral_asset_name: "ThreeFold Token".to_string(),
|
|
collateral_asset_symbol: "TFT".to_string(),
|
|
collateral_amount: 1500.0,
|
|
collateral_value_usd: 750.0,
|
|
duration_days: 90,
|
|
profit_share_rate: 5.8,
|
|
profit_share_owed: 3.625,
|
|
total_to_repay: 503.625,
|
|
collateral_ratio: 300.0,
|
|
};
|
|
db.add_receiving_position(receiving_position);
|
|
}
|