Merge branch 'development_timur' of https://git.ourworld.tf/herocode/hostbasket into development_timur
This commit is contained in:
206
actix_mvc_app/src/models/defi.rs
Normal file
206
actix_mvc_app/src/models/defi.rs
Normal file
@@ -0,0 +1,206 @@
|
||||
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 {
|
||||
Lending,
|
||||
Borrowing,
|
||||
Liquidity,
|
||||
Staking,
|
||||
Collateral,
|
||||
}
|
||||
|
||||
impl DefiPositionType {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
DefiPositionType::Lending => "Lending",
|
||||
DefiPositionType::Borrowing => "Borrowing",
|
||||
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 apy: f64,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub expires_at: Option<DateTime<Utc>>,
|
||||
pub user_id: String,
|
||||
}
|
||||
|
||||
// Lending position
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LendingPosition {
|
||||
pub base: DefiPosition,
|
||||
pub duration_days: i32,
|
||||
pub interest_earned: f64,
|
||||
pub return_amount: f64,
|
||||
}
|
||||
|
||||
// Borrowing position
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct BorrowingPosition {
|
||||
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 interest_rate: f64,
|
||||
pub interest_owed: f64,
|
||||
pub total_to_repay: f64,
|
||||
pub collateral_ratio: f64,
|
||||
}
|
||||
|
||||
// In-memory database for DeFi positions
|
||||
pub struct DefiDatabase {
|
||||
lending_positions: HashMap<String, LendingPosition>,
|
||||
borrowing_positions: HashMap<String, BorrowingPosition>,
|
||||
}
|
||||
|
||||
impl DefiDatabase {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
lending_positions: HashMap::new(),
|
||||
borrowing_positions: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
// Lending operations
|
||||
pub fn add_lending_position(&mut self, position: LendingPosition) {
|
||||
self.lending_positions.insert(position.base.id.clone(), position);
|
||||
}
|
||||
|
||||
pub fn get_lending_position(&self, id: &str) -> Option<&LendingPosition> {
|
||||
self.lending_positions.get(id)
|
||||
}
|
||||
|
||||
pub fn get_all_lending_positions(&self) -> Vec<&LendingPosition> {
|
||||
self.lending_positions.values().collect()
|
||||
}
|
||||
|
||||
pub fn get_user_lending_positions(&self, user_id: &str) -> Vec<&LendingPosition> {
|
||||
self.lending_positions
|
||||
.values()
|
||||
.filter(|p| p.base.user_id == user_id)
|
||||
.collect()
|
||||
}
|
||||
|
||||
// Borrowing operations
|
||||
pub fn add_borrowing_position(&mut self, position: BorrowingPosition) {
|
||||
self.borrowing_positions.insert(position.base.id.clone(), position);
|
||||
}
|
||||
|
||||
pub fn get_borrowing_position(&self, id: &str) -> Option<&BorrowingPosition> {
|
||||
self.borrowing_positions.get(id)
|
||||
}
|
||||
|
||||
pub fn get_all_borrowing_positions(&self) -> Vec<&BorrowingPosition> {
|
||||
self.borrowing_positions.values().collect()
|
||||
}
|
||||
|
||||
pub fn get_user_borrowing_positions(&self, user_id: &str) -> Vec<&BorrowingPosition> {
|
||||
self.borrowing_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 lending positions
|
||||
let lending_position = LendingPosition {
|
||||
base: DefiPosition {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
position_type: DefiPositionType::Lending,
|
||||
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,
|
||||
apy: 4.2,
|
||||
created_at: Utc::now(),
|
||||
expires_at: Some(Utc::now() + chrono::Duration::days(30)),
|
||||
user_id: "user123".to_string(),
|
||||
},
|
||||
duration_days: 30,
|
||||
interest_earned: 3.5,
|
||||
return_amount: 1003.5,
|
||||
};
|
||||
db.add_lending_position(lending_position);
|
||||
|
||||
// Add mock borrowing positions
|
||||
let borrowing_position = BorrowingPosition {
|
||||
base: DefiPosition {
|
||||
id: Uuid::new_v4().to_string(),
|
||||
position_type: DefiPositionType::Borrowing,
|
||||
status: DefiPositionStatus::Active,
|
||||
asset_id: "ZAZ".to_string(),
|
||||
asset_name: "Zanzibar Token".to_string(),
|
||||
asset_symbol: "ZAZ".to_string(),
|
||||
amount: 500.0,
|
||||
value_usd: 250.0,
|
||||
apy: 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,
|
||||
interest_rate: 5.8,
|
||||
interest_owed: 3.625,
|
||||
total_to_repay: 503.625,
|
||||
collateral_ratio: 300.0,
|
||||
};
|
||||
db.add_borrowing_position(borrowing_position);
|
||||
}
|
@@ -7,9 +7,11 @@ pub mod flow;
|
||||
pub mod contract;
|
||||
pub mod asset;
|
||||
pub mod marketplace;
|
||||
pub mod defi;
|
||||
|
||||
// Re-export models for easier imports
|
||||
pub use user::User;
|
||||
pub use ticket::{Ticket, TicketComment, TicketStatus, TicketPriority};
|
||||
pub use calendar::{CalendarEvent, CalendarViewMode};
|
||||
pub use marketplace::{Listing, ListingStatus, ListingType, Bid, BidStatus, MarketplaceStatistics};
|
||||
pub use defi::{DefiPosition, DefiPositionType, DefiPositionStatus, LendingPosition, BorrowingPosition, DEFI_DB, initialize_mock_data};
|
||||
|
Reference in New Issue
Block a user