diff --git a/heromodels/examples/calendar_example/main.rs b/heromodels/examples/calendar_example/main.rs new file mode 100644 index 0000000..b83aa9d --- /dev/null +++ b/heromodels/examples/calendar_example/main.rs @@ -0,0 +1,129 @@ +use chrono::{Duration, Utc}; +use heromodels::db::{Collection, Db}; +use heromodels::models::calendar::{Attendee, AttendanceStatus, Calendar, Event}; +use heromodels_core::Model; + +fn main() { + // Create a new DB instance, reset before every run + let db_path = "/tmp/ourdb_calendar_example"; + let db = heromodels::db::hero::OurDB::new(db_path, true).expect("Can create DB"); + + println!("Hero Models - Calendar Usage Example"); + println!("===================================="); + + // --- Create Attendees --- + let attendee1 = Attendee::new("user_123".to_string()) + .status(AttendanceStatus::Accepted); + let attendee2 = Attendee::new("user_456".to_string()) + .status(AttendanceStatus::Tentative); + let attendee3 = Attendee::new("user_789".to_string()); // Default NoResponse + + // --- Create Events --- + let now = Utc::now(); + let event1 = Event::new( + "event_alpha".to_string(), + "Team Meeting", + now + Duration::seconds(3600), // Using Duration::seconds for more explicit chrono 0.4 compatibility + now + Duration::seconds(7200), + ) + .description("Weekly sync-up meeting.") + .location("Conference Room A") + .add_attendee(attendee1.clone()) + .add_attendee(attendee2.clone()); + + let event2 = Event::new( + "event_beta".to_string(), + "Project Brainstorm", + now + Duration::days(1), + now + Duration::days(1) + Duration::seconds(5400), // 90 minutes + ) + .description("Brainstorming session for new project features.") + .add_attendee(attendee1.clone()) + .add_attendee(attendee3.clone()); + + let event3_for_calendar2 = Event::new( + "event_gamma".to_string(), + "Client Call", + now + Duration::days(2), + now + Duration::days(2) + Duration::seconds(3600) + ); + + // --- Create Calendars --- + // Note: Calendar::new directly returns Calendar, no separate .build() step like the user example. + let calendar1 = Calendar::new(1, "Work Calendar") + .description("Calendar for all work-related events.") + .add_event(event1.clone()) + .add_event(event2.clone()); + + let calendar2 = Calendar::new(2, "Personal Calendar") + .add_event(event3_for_calendar2.clone()); + + + // --- Store Calendars in DB --- + let cal_collection = db.collection::().expect("can open calendar collection"); + + cal_collection.set(&calendar1).expect("can set calendar1"); + cal_collection.set(&calendar2).expect("can set calendar2"); + + println!("Created calendar1 (ID: {}): Name - '{}'", calendar1.get_id(), calendar1.name); + println!("Created calendar2 (ID: {}): Name - '{}'", calendar2.get_id(), calendar2.name); + + // --- Retrieve a Calendar by ID --- + let stored_calendar1_opt = cal_collection.get_by_id(calendar1.get_id()).expect("can try to load calendar1"); + assert!(stored_calendar1_opt.is_some(), "Calendar1 should be found in DB"); + let mut stored_calendar1 = stored_calendar1_opt.unwrap(); + + println!("\nRetrieved calendar1 from DB: Name - '{}', Events count: {}", stored_calendar1.name, stored_calendar1.events.len()); + assert_eq!(stored_calendar1.name, "Work Calendar"); + assert_eq!(stored_calendar1.events.len(), 2); + assert_eq!(stored_calendar1.events[0].title, "Team Meeting"); + + // --- Modify a Calendar (Reschedule an Event) --- + let event_id_to_reschedule = event1.id.as_str(); + let new_start_time = now + Duration::seconds(10800); // 3 hours from now + let new_end_time = now + Duration::seconds(14400); // 4 hours from now + + stored_calendar1 = stored_calendar1.update_event(event_id_to_reschedule, |event_to_update| { + println!("Rescheduling event '{}'...", event_to_update.title); + event_to_update.reschedule(new_start_time, new_end_time) + }); + + let rescheduled_event = stored_calendar1.events.iter().find(|e| e.id == event_id_to_reschedule) + .expect("Rescheduled event should exist"); + assert_eq!(rescheduled_event.start_time, new_start_time); + assert_eq!(rescheduled_event.end_time, new_end_time); + println!("Event '{}' rescheduled in stored_calendar1.", rescheduled_event.title); + + // --- Store the modified calendar --- + cal_collection.set(&stored_calendar1).expect("can set modified calendar1"); + let re_retrieved_calendar1_opt = cal_collection.get_by_id(calendar1.get_id()).expect("can try to load modified calendar1"); + let re_retrieved_calendar1 = re_retrieved_calendar1_opt.unwrap(); + let re_retrieved_event = re_retrieved_calendar1.events.iter().find(|e| e.id == event_id_to_reschedule) + .expect("Rescheduled event should exist in re-retrieved calendar"); + assert_eq!(re_retrieved_event.start_time, new_start_time, "Reschedule not persisted correctly"); + + println!("\nModified and re-saved calendar1. Rescheduled event start time: {}", re_retrieved_event.start_time); + + // --- Add a new event to an existing calendar --- + let event4_new = Event::new( + "event_delta".to_string(), + "1-on-1", + now + Duration::days(3), + now + Duration::days(3) + Duration::seconds(1800) // 30 minutes + ); + stored_calendar1 = stored_calendar1.add_event(event4_new); + assert_eq!(stored_calendar1.events.len(), 3); + cal_collection.set(&stored_calendar1).expect("can set calendar1 after adding new event"); + println!("Added new event '1-on-1' to stored_calendar1. Total events: {}", stored_calendar1.events.len()); + + // --- Delete a Calendar --- + cal_collection.delete_by_id(calendar2.get_id()).expect("can delete calendar2"); + let deleted_calendar2_opt = cal_collection.get_by_id(calendar2.get_id()).expect("can try to load deleted calendar2"); + assert!(deleted_calendar2_opt.is_none(), "Calendar2 should be deleted from DB"); + + println!("\nDeleted calendar2 (ID: {}) from DB.", calendar2.get_id()); + println!("Calendar model DB Prefix: {}", Calendar::db_prefix()); + + println!("\nExample finished. DB stored at {}", db_path); + println!("To clean up, you can manually delete the directory: {}", db_path); +} diff --git a/heromodels/examples/calendar_rhai/calendar.rhai b/heromodels/examples/calendar_rhai/calendar.rhai new file mode 100644 index 0000000..56310c9 --- /dev/null +++ b/heromodels/examples/calendar_rhai/calendar.rhai @@ -0,0 +1,58 @@ +// Get the database instance +let db = get_db(); + +// Create a new calendar +let calendar = calendar__builder(1); +calendar.name = "My First Calendar"; +set_description(calendar, "A calendar for testing Rhai integration"); + +print("Created calendar: " + calendar.name); + +// Save the calendar to the database +set_calendar(db, calendar); +print("Calendar saved to database"); + +// Check if calendar exists and retrieve it +if calendar_exists(db, 1) { + let retrieved_calendar = get_calendar_by_id(db, 1); + print("Retrieved calendar: " + retrieved_calendar.name); + let desc = get_description(retrieved_calendar); + if desc != "" { + print("Description: " + desc); + } else { + print("No description available"); + } +} else { + print("Failed to retrieve calendar with ID 1"); +} + +// Create another calendar +let calendar2 = calendar__builder(2); +calendar2.name = "My Second Calendar"; +set_description(calendar2, "Another calendar for testing"); + +set_calendar(db, calendar2); +print("Second calendar saved"); + +// Get all calendars +let all_calendars = get_all_calendars(db); +print("Total calendars: " + all_calendars.len()); + +for calendar in all_calendars { + print("Calendar ID: " + get_id(calendar) + ", Name: " + calendar.name); +} + +// Delete a calendar +delete_calendar_by_id(db, 1); +print("Deleted calendar with ID 1"); + +// Verify deletion +if !calendar_exists(db, 1) { + print("Calendar with ID 1 was successfully deleted"); +} else { + print("Failed to delete calendar with ID 1"); +} + +// Count remaining calendars +let remaining_calendars = get_all_calendars(db); +print("Remaining calendars: " + remaining_calendars.len()); \ No newline at end of file diff --git a/heromodels/examples/calendar_rhai/example.rs b/heromodels/examples/calendar_rhai/example.rs new file mode 100644 index 0000000..4963bbc --- /dev/null +++ b/heromodels/examples/calendar_rhai/example.rs @@ -0,0 +1,83 @@ +use heromodels::db::hero::OurDB; +use heromodels::models::calendar::Calendar; +use rhai::Engine; +use rhai_wrapper::wrap_vec_return; +use std::sync::Arc; +use std::{fs, path::Path}; + +fn main() -> Result<(), Box> { + // Initialize Rhai engine + let mut engine = Engine::new(); + + // Initialize database + let db = Arc::new(OurDB::new("temp_calendar_db", true).expect("Failed to create database")); + + // Register the Calendar type with Rhai + // This function is generated by the #[rhai_model_export] attribute + Calendar::register_rhai_bindings_for_calendar(&mut engine, db.clone()); + + // Register a function to get the database instance + engine.register_fn("get_db", move || db.clone()); + + // Register a calendar builder function + engine.register_fn("calendar__builder", |id: i64| { + Calendar::new(id as u32, "New Calendar") + }); + + // Register setter methods for Calendar properties + engine.register_fn("set_description", |calendar: &mut Calendar, desc: String| { + calendar.description = Some(desc); + }); + + // Register getter methods for Calendar properties + engine.register_fn("get_description", |calendar: Calendar| -> String { + calendar.description.clone().unwrap_or_default() + }); + + // Register getter for base_data.id + engine.register_fn("get_id", |calendar: Calendar| -> i64 { + calendar.base_data.id as i64 + }); + + // Register additional functions needed by the script + engine.register_fn("set_calendar", |_db: Arc, _calendar: Calendar| { + // In a real implementation, this would save the calendar to the database + println!("Calendar saved: {}", _calendar.name); + }); + + engine.register_fn("get_calendar_by_id", |_db: Arc, id: i64| -> Calendar { + // In a real implementation, this would retrieve the calendar from the database + Calendar::new(id as u32, "Retrieved Calendar") + }); + + // Register a function to check if a calendar exists + engine.register_fn("calendar_exists", |_db: Arc, id: i64| -> bool { + // In a real implementation, this would check if the calendar exists in the database + id == 1 || id == 2 + }); + + // Define the function separately to use with the wrap_vec_return macro + fn get_all_calendars(_db: Arc) -> Vec { + // In a real implementation, this would retrieve all calendars from the database + vec![Calendar::new(1, "Calendar 1"), Calendar::new(2, "Calendar 2")] + } + + // Register the function with the wrap_vec_return macro + engine.register_fn("get_all_calendars", wrap_vec_return!(get_all_calendars, Arc => Calendar)); + + engine.register_fn("delete_calendar_by_id", |_db: Arc, _id: i64| { + // In a real implementation, this would delete the calendar from the database + println!("Calendar deleted with ID: {}", _id); + }); + + // Load and evaluate the Rhai script + let script_path = Path::new("examples/calendar_rhai/calendar.rhai"); + let script = fs::read_to_string(script_path)?; + + match engine.eval::<()>(&script) { + Ok(_) => println!("Script executed successfully!"), + Err(e) => eprintln!("Script execution failed: {}", e), + } + + Ok(()) +} \ No newline at end of file diff --git a/heromodels/examples/finance_example/main.rs b/heromodels/examples/finance_example/main.rs new file mode 100644 index 0000000..716262c --- /dev/null +++ b/heromodels/examples/finance_example/main.rs @@ -0,0 +1,329 @@ +// heromodels/examples/finance_example/main.rs + +use chrono::{Utc, Duration}; +use heromodels::models::finance::{Account, Asset, AssetType}; +use heromodels::models::finance::marketplace::{Listing, ListingType, ListingStatus, Bid, BidStatus}; + +fn main() { + println!("Finance Models Example\n"); + + // --- PART 1: ACCOUNTS AND ASSETS --- + println!("=== ACCOUNTS AND ASSETS ===\n"); + + // Create a new account + let mut account = Account::new( + 1, // id + "Main ETH Wallet", // name + 1001, // user_id + "My primary Ethereum wallet", // description + "ethereum", // ledger + "0x1234567890abcdef1234567890abcdef12345678", // address + "0xpubkey123456789" // pubkey + ); + + println!("Created Account: '{}' (ID: {})", account.name, account.base_data.id); + println!("Owner: User {}", account.user_id); + println!("Blockchain: {}", account.ledger); + println!("Address: {}", account.address); + println!(""); + + // Create some assets + let eth_asset = Asset::new( + 101, // id + "Ethereum", // name + "Native ETH cryptocurrency", // description + 1.5, // amount + "0x0000000000000000000000000000000000000000", // address (ETH has no contract address) + AssetType::Native, // asset_type + 18, // decimals + ); + + let usdc_asset = Asset::new( + 102, // id + "USDC", // name + "USD Stablecoin on Ethereum", // description + 1000.0, // amount + "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", // address (USDC contract) + AssetType::Erc20, // asset_type + 6, // decimals + ); + + let nft_asset = Asset::new( + 103, // id + "CryptoPunk #1234", // name + "Rare digital collectible", // description + 1.0, // amount + "0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb", // address (CryptoPunks contract) + AssetType::Erc721, // asset_type + 0, // decimals + ); + + // Add assets to the account + account = account.add_asset(eth_asset.clone()); + account = account.add_asset(usdc_asset.clone()); + account = account.add_asset(nft_asset.clone()); + + println!("Added Assets to Account:"); + for asset in &account.assets { + println!("- {} ({:?}): {} units", asset.name, asset.asset_type, asset.formatted_amount()); + } + + println!("\nTotal Account Value (raw sum): {}", account.total_value()); + println!(""); + + // Update account details + account = account.update_details( + Some("Primary Ethereum Wallet"), // new name + None::, // keep same description + None::, // keep same address + Some("0xnewpubkey987654321"), // new pubkey + ); + + println!("Updated Account Details:"); + println!("New Name: {}", account.name); + println!("New Pubkey: {}", account.pubkey); + println!(""); + + // Find an asset by name + if let Some(found_asset) = account.find_asset_by_name("USDC") { + println!("Found USDC Asset:"); + println!("- Amount: {} USDC", found_asset.formatted_amount()); + println!("- Contract: {}", found_asset.address); + println!(""); + } + + // --- PART 2: MARKETPLACE LISTINGS --- + println!("\n=== MARKETPLACE LISTINGS ===\n"); + + // Create a fixed price listing + let mut fixed_price_listing = Listing::new( + 201, // id + "1000 USDC for Sale", // title + "Selling 1000 USDC tokens at fixed price", // description + "102", // asset_id (referencing the USDC asset) + AssetType::Erc20, // asset_type + "1001", // seller_id + 1.05, // price (in ETH) + "ETH", // currency + ListingType::FixedPrice, // listing_type + Some(Utc::now() + Duration::days(7)), // expires_at (7 days from now) + vec!["token".to_string(), "stablecoin".to_string()], // tags + Some("https://example.com/usdc.png"), // image_url + ); + + println!("Created Fixed Price Listing: '{}' (ID: {})", fixed_price_listing.title, fixed_price_listing.base_data.id); + println!("Price: {} {}", fixed_price_listing.price, fixed_price_listing.currency); + println!("Type: {:?}, Status: {:?}", fixed_price_listing.listing_type, fixed_price_listing.status); + println!("Expires: {}", fixed_price_listing.expires_at.unwrap()); + println!(""); + + // Complete the fixed price sale + match fixed_price_listing.complete_sale("2001", 1.05) { + Ok(updated_listing) => { + fixed_price_listing = updated_listing; + println!("Fixed Price Sale Completed:"); + println!("Status: {:?}", fixed_price_listing.status); + println!("Buyer: {}", fixed_price_listing.buyer_id.unwrap()); + println!("Sale Price: {} {}", fixed_price_listing.sale_price.unwrap(), fixed_price_listing.currency); + println!("Sold At: {}", fixed_price_listing.sold_at.unwrap()); + println!(""); + }, + Err(e) => println!("Error completing sale: {}", e), + } + + // Create an auction listing for the NFT + let mut auction_listing = Listing::new( + 202, // id + "CryptoPunk #1234 Auction", // title + "Rare CryptoPunk NFT for auction", // description + "103", // asset_id (referencing the NFT asset) + AssetType::Erc721, // asset_type + "1001", // seller_id + 10.0, // starting_price (in ETH) + "ETH", // currency + ListingType::Auction, // listing_type + Some(Utc::now() + Duration::days(3)), // expires_at (3 days from now) + vec!["nft".to_string(), "collectible".to_string(), "cryptopunk".to_string()], // tags + Some("https://example.com/cryptopunk1234.png"), // image_url + ); + + println!("Created Auction Listing: '{}' (ID: {})", auction_listing.title, auction_listing.base_data.id); + println!("Starting Price: {} {}", auction_listing.price, auction_listing.currency); + println!("Type: {:?}, Status: {:?}", auction_listing.listing_type, auction_listing.status); + println!(""); + + // Create some bids + let bid1 = Bid::new( + auction_listing.base_data.id.to_string(), // listing_id + 2001, // bidder_id + 11.0, // amount + "ETH", // currency + ); + + let bid2 = Bid::new( + auction_listing.base_data.id.to_string(), // listing_id + 2002, // bidder_id + 12.5, // amount + "ETH", // currency + ); + + let bid3 = Bid::new( + auction_listing.base_data.id.to_string(), // listing_id + 2003, // bidder_id + 15.0, // amount + "ETH", // currency + ); + + // Add bids to the auction + println!("Adding Bids to Auction:"); + + // Using clone() to avoid ownership issues with match expressions + match auction_listing.clone().add_bid(bid1) { + Ok(updated_listing) => { + auction_listing = updated_listing; + println!("- Bid added: 11.0 ETH from User 2001"); + }, + Err(e) => println!("Error adding bid: {}", e), + } + + match auction_listing.clone().add_bid(bid2) { + Ok(updated_listing) => { + auction_listing = updated_listing; + println!("- Bid added: 12.5 ETH from User 2002"); + }, + Err(e) => println!("Error adding bid: {}", e), + } + + match auction_listing.clone().add_bid(bid3) { + Ok(updated_listing) => { + auction_listing = updated_listing; + println!("- Bid added: 15.0 ETH from User 2003"); + }, + Err(e) => println!("Error adding bid: {}", e), + } + + println!("\nCurrent Auction Status:"); + println!("Current Price: {} {}", auction_listing.price, auction_listing.currency); + + if let Some(highest_bid) = auction_listing.highest_bid() { + println!("Highest Bid: {} {} from User {}", + highest_bid.amount, + highest_bid.currency, + highest_bid.bidder_id); + } + + println!("Total Bids: {}", auction_listing.bids.len()); + println!(""); + + // Complete the auction + match auction_listing.clone().complete_sale("2003", 15.0) { + Ok(updated_listing) => { + auction_listing = updated_listing; + println!("Auction Completed:"); + println!("Status: {:?}", auction_listing.status); + println!("Winner: User {}", auction_listing.buyer_id.as_ref().unwrap()); + println!("Winning Bid: {} {}", auction_listing.sale_price.as_ref().unwrap(), auction_listing.currency); + println!(""); + + println!("Final Bid Statuses:"); + for bid in &auction_listing.bids { + println!("- User {}: {} {} (Status: {:?})", + bid.bidder_id, + bid.amount, + bid.currency, + bid.status); + } + println!(""); + }, + Err(e) => println!("Error completing auction: {}", e), + } + + // Create an exchange listing + let exchange_listing = Listing::new( + 203, // id + "ETH for BTC Exchange", // title + "Looking to exchange ETH for BTC", // description + "101", // asset_id (referencing the ETH asset) + AssetType::Native, // asset_type + "1001", // seller_id + 1.0, // amount (1 ETH) + "BTC", // currency (what they want in exchange) + ListingType::Exchange, // listing_type + Some(Utc::now() + Duration::days(14)), // expires_at (14 days from now) + vec!["exchange".to_string(), "crypto".to_string()], // tags + None::, // image_url + ); + + println!("Created Exchange Listing: '{}' (ID: {})", exchange_listing.title, exchange_listing.base_data.id); + println!("Offering: Asset {} ({:?})", exchange_listing.asset_id, exchange_listing.asset_type); + println!("Wanted: {} {}", exchange_listing.price, exchange_listing.currency); + println!(""); + + // --- PART 3: DEMONSTRATING EDGE CASES --- + println!("\n=== EDGE CASES AND VALIDATIONS ===\n"); + + // Create a new auction listing for edge case testing + let test_auction = Listing::new( + 205, // id + "Test Auction", // title + "For testing edge cases", // description + "101", // asset_id + AssetType::Native, // asset_type + "1001", // seller_id + 10.0, // starting_price + "ETH", // currency + ListingType::Auction, // listing_type + Some(Utc::now() + Duration::days(1)), // expires_at + vec![], // tags + None::, // image_url + ); + + // Try to add a bid that's too low + let low_bid = Bid::new( + test_auction.base_data.id.to_string(), // listing_id + 2004, // bidder_id + 5.0, // amount (lower than starting price) + "ETH", // currency + ); + + println!("Attempting to add a bid that's too low (5.0 ETH):"); + match test_auction.add_bid(low_bid) { + Ok(_) => println!("Bid accepted (This shouldn't happen)"), + Err(e) => println!("Error as expected: {}", e), + } + println!(""); + + // Try to cancel a completed listing + println!("Attempting to cancel a completed listing:"); + match auction_listing.clone().cancel() { + Ok(_) => println!("Listing cancelled (This shouldn't happen)"), + Err(e) => println!("Error as expected: {}", e), + } + println!(""); + + // Create a listing that will expire + let mut expiring_listing = Listing::new( + 204, // id + "About to Expire", // title + "This listing will expire immediately", // description + "101", // asset_id + AssetType::Native, // asset_type + "1001", // seller_id + 0.1, // price + "ETH", // currency + ListingType::FixedPrice, // listing_type + Some(Utc::now() - Duration::hours(1)), // expires_at (1 hour ago) + vec![], // tags + None::, // image_url + ); + + println!("Created Expiring Listing: '{}' (ID: {})", expiring_listing.title, expiring_listing.base_data.id); + println!("Initial Status: {:?}", expiring_listing.status); + + // Check expiration + expiring_listing = expiring_listing.check_expiration(); + println!("After Checking Expiration: {:?}", expiring_listing.status); + println!(""); + + println!("Finance Models Example Completed."); +} diff --git a/heromodels/examples/governance_proposal_example/main.rs b/heromodels/examples/governance_proposal_example/main.rs new file mode 100644 index 0000000..9072c1e --- /dev/null +++ b/heromodels/examples/governance_proposal_example/main.rs @@ -0,0 +1,110 @@ +// heromodels/examples/governance_proposal_example/main.rs + +use chrono::{Utc, Duration}; +use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus}; + +fn main() { + println!("Governance Proposal Model Example\n"); + + // Create a new proposal + let mut proposal = Proposal::new( + 1, // id + "user_creator_123", // creator_id + "Community Fund Allocation for Q3", // title + "Proposal to allocate funds for community projects in the third quarter.", // description + Utc::now(), // vote_start_date + Utc::now() + Duration::days(14) // vote_end_date (14 days from now) + ); + + println!("Created Proposal: '{}' (ID: {})", proposal.title, proposal.base_data.id); + println!("Status: {:?}, Vote Status: {:?}", proposal.status, proposal.vote_status); + println!("Vote Period: {} to {}\n", proposal.vote_start_date, proposal.vote_end_date); + + // Add vote options + proposal = proposal.add_option(1, "Approve Allocation"); + proposal = proposal.add_option(2, "Reject Allocation"); + proposal = proposal.add_option(3, "Abstain"); + + println!("Added Vote Options:"); + for option in &proposal.options { + println!("- Option ID: {}, Text: '{}', Votes: {}", option.id, option.text, option.count); + } + println!(""); + + // Simulate casting votes + println!("Simulating Votes..."); + // User 1 votes for 'Approve Allocation' with 100 shares + proposal = proposal.cast_vote(101, 1, 1, 100); + // User 2 votes for 'Reject Allocation' with 50 shares + proposal = proposal.cast_vote(102, 2, 2, 50); + // User 3 votes for 'Approve Allocation' with 75 shares + proposal = proposal.cast_vote(103, 3, 1, 75); + // User 4 abstains with 20 shares + proposal = proposal.cast_vote(104, 4, 3, 20); + // User 5 attempts to vote for a non-existent option (should be handled gracefully) + proposal = proposal.cast_vote(105, 5, 99, 10); + // User 1 tries to vote again (not explicitly prevented by current model, but could be a future enhancement) + // proposal = proposal.cast_vote(106, 1, 1, 10); + + println!("\nVote Counts After Simulation:"); + for option in &proposal.options { + println!("- Option ID: {}, Text: '{}', Votes: {}", option.id, option.text, option.count); + } + + println!("\nBallots Cast:"); + for ballot in &proposal.ballots { + println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}", + ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count); + } + println!(""); + + // Change proposal status + proposal = proposal.change_proposal_status(ProposalStatus::Active); + println!("Changed Proposal Status to: {:?}", proposal.status); + + // Simulate closing the vote + proposal = proposal.change_vote_event_status(VoteEventStatus::Closed); + println!("Changed Vote Event Status to: {:?}", proposal.vote_status); + + // Attempt to cast a vote after closing (should be handled) + println!("\nAttempting to cast vote after voting is closed..."); + proposal = proposal.cast_vote(107, 6, 1, 25); + + // Final proposal state + println!("\nFinal Proposal State:"); + println!("Title: '{}'", proposal.title); + println!("Status: {:?}", proposal.status); + println!("Vote Status: {:?}", proposal.vote_status); + println!("Options:"); + for option in &proposal.options { + println!(" - {}: {} (Votes: {})", option.id, option.text, option.count); + } + println!("Total Ballots: {}", proposal.ballots.len()); + + // Example of a private proposal (not fully implemented in cast_vote eligibility yet) + let mut private_proposal = Proposal::new( + 2, + "user_admin_001", + "Internal Team Restructure Vote", + "Vote on proposed internal team changes.", + Utc::now(), + Utc::now() + Duration::days(7) + ); + private_proposal.private_group = Some(vec![10, 20, 30]); // Only users 10, 20, 30 can vote + private_proposal = private_proposal.add_option(1, "Accept Restructure"); + private_proposal = private_proposal.add_option(2, "Reject Restructure"); + + println!("\nCreated Private Proposal: '{}'", private_proposal.title); + println!("Eligible Voters (Group): {:?}", private_proposal.private_group); + // User 10 (eligible) votes + private_proposal = private_proposal.cast_vote(201, 10, 1, 100); + // User 40 (ineligible) tries to vote + private_proposal = private_proposal.cast_vote(202, 40, 1, 50); + + println!("Private Proposal Vote Counts:"); + for option in &private_proposal.options { + println!(" - {}: {} (Votes: {})", option.id, option.text, option.count); + } + + println!("\nGovernance Proposal Example Finished."); +} diff --git a/heromodels/examples/governance_rhai/example.rs b/heromodels/examples/governance_rhai/example.rs new file mode 100644 index 0000000..80e3541 --- /dev/null +++ b/heromodels/examples/governance_rhai/example.rs @@ -0,0 +1,189 @@ +use heromodels::db::hero::OurDB; +use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus, VoteOption, Ballot}; +use rhai::Engine; +use rhai_wrapper::wrap_vec_return; +use std::sync::Arc; +use std::{fs, path::Path}; +use chrono::{Utc, Duration}; + +fn main() -> Result<(), Box> { + // Initialize Rhai engine + let mut engine = Engine::new(); + + // Initialize database + let db = Arc::new(OurDB::new("temp_governance_db", true).expect("Failed to create database")); + + // Register the Proposal type with Rhai + // This function is generated by the #[rhai_model_export] attribute + Proposal::register_rhai_bindings_for_proposal(&mut engine, db.clone()); + + // Register the Ballot type with Rhai + Ballot::register_rhai_bindings_for_ballot(&mut engine, db.clone()); + + // Register a function to get the database instance + engine.register_fn("get_db", move || db.clone()); + + // Register builder functions for Proposal and related types + engine.register_fn("create_proposal", |id: i64, creator_id: String, title: String, description: String| { + let start_date = Utc::now(); + let end_date = start_date + Duration::days(14); + Proposal::new(id as u32, creator_id, title, description, start_date, end_date) + }); + + engine.register_fn("create_vote_option", |id: i64, text: String| { + VoteOption::new(id as u8, text) + }); + + engine.register_fn("create_ballot", |id: i64, user_id: i64, vote_option_id: i64, shares_count: i64| { + Ballot::new(id as u32, user_id as u32, vote_option_id as u8, shares_count) + }); + + // Register getter and setter methods for Proposal properties + engine.register_fn("get_title", |proposal: Proposal| -> String { + proposal.title.clone() + }); + + engine.register_fn("get_description", |proposal: Proposal| -> String { + proposal.description.clone() + }); + + engine.register_fn("get_creator_id", |proposal: Proposal| -> String { + proposal.creator_id.clone() + }); + + engine.register_fn("get_id", |proposal: Proposal| -> i64 { + proposal.base_data.id as i64 + }); + + engine.register_fn("get_status", |proposal: Proposal| -> String { + format!("{:?}", proposal.status) + }); + + engine.register_fn("get_vote_status", |proposal: Proposal| -> String { + format!("{:?}", proposal.vote_status) + }); + + // Register methods for proposal operations + engine.register_fn("add_option_to_proposal", |mut proposal: Proposal, option_id: i64, option_text: String| -> Proposal { + proposal.add_option(option_id as u8, option_text) + }); + + engine.register_fn("cast_vote_on_proposal", |mut proposal: Proposal, ballot_id: i64, user_id: i64, option_id: i64, shares: i64| -> Proposal { + proposal.cast_vote(ballot_id as u32, user_id as u32, option_id as u8, shares) + }); + + engine.register_fn("change_proposal_status", |mut proposal: Proposal, status_str: String| -> Proposal { + let new_status = match status_str.as_str() { + "Draft" => ProposalStatus::Draft, + "Active" => ProposalStatus::Active, + "Approved" => ProposalStatus::Approved, + "Rejected" => ProposalStatus::Rejected, + "Cancelled" => ProposalStatus::Cancelled, + _ => ProposalStatus::Draft, + }; + proposal.change_proposal_status(new_status) + }); + + engine.register_fn("change_vote_event_status", |mut proposal: Proposal, status_str: String| -> Proposal { + let new_status = match status_str.as_str() { + "Open" => VoteEventStatus::Open, + "Closed" => VoteEventStatus::Closed, + "Cancelled" => VoteEventStatus::Cancelled, + _ => VoteEventStatus::Open, + }; + proposal.change_vote_event_status(new_status) + }); + + // Register functions for database operations + engine.register_fn("save_proposal", |_db: Arc, proposal: Proposal| { + println!("Proposal saved: {}", proposal.title); + }); + + engine.register_fn("get_proposal_by_id", |_db: Arc, id: i64| -> Proposal { + // In a real implementation, this would retrieve the proposal from the database + let start_date = Utc::now(); + let end_date = start_date + Duration::days(14); + Proposal::new(id as u32, "Retrieved Creator", "Retrieved Proposal", "Retrieved Description", start_date, end_date) + }); + + // Register a function to check if a proposal exists + engine.register_fn("proposal_exists", |_db: Arc, id: i64| -> bool { + // In a real implementation, this would check if the proposal exists in the database + id == 1 || id == 2 + }); + + // Define the function for get_all_proposals + fn get_all_proposals(_db: Arc) -> Vec { + // In a real implementation, this would retrieve all proposals from the database + let start_date = Utc::now(); + let end_date = start_date + Duration::days(14); + vec![ + Proposal::new(1, "Creator 1", "Proposal 1", "Description 1", start_date, end_date), + Proposal::new(2, "Creator 2", "Proposal 2", "Description 2", start_date, end_date) + ] + } + + // Register the function with the wrap_vec_return macro + engine.register_fn("get_all_proposals", wrap_vec_return!(get_all_proposals, Arc => Proposal)); + + engine.register_fn("delete_proposal_by_id", |_db: Arc, _id: i64| { + // In a real implementation, this would delete the proposal from the database + println!("Proposal deleted with ID: {}", _id); + }); + + // Register helper functions for accessing proposal options and ballots + engine.register_fn("get_option_count", |proposal: Proposal| -> i64 { + proposal.options.len() as i64 + }); + + engine.register_fn("get_option_at", |proposal: Proposal, index: i64| -> VoteOption { + if index >= 0 && index < proposal.options.len() as i64 { + proposal.options[index as usize].clone() + } else { + VoteOption::new(0, "Invalid Option") + } + }); + + engine.register_fn("get_option_text", |option: VoteOption| -> String { + option.text.clone() + }); + + engine.register_fn("get_option_votes", |option: VoteOption| -> i64 { + option.count + }); + + engine.register_fn("get_ballot_count", |proposal: Proposal| -> i64 { + proposal.ballots.len() as i64 + }); + + engine.register_fn("get_ballot_at", |proposal: Proposal, index: i64| -> Ballot { + if index >= 0 && index < proposal.ballots.len() as i64 { + proposal.ballots[index as usize].clone() + } else { + Ballot::new(0, 0, 0, 0) + } + }); + + engine.register_fn("get_ballot_user_id", |ballot: Ballot| -> i64 { + ballot.user_id as i64 + }); + + engine.register_fn("get_ballot_option_id", |ballot: Ballot| -> i64 { + ballot.vote_option_id as i64 + }); + + engine.register_fn("get_ballot_shares", |ballot: Ballot| -> i64 { + ballot.shares_count + }); + + // Load and evaluate the Rhai script + let script_path = Path::new("examples/governance_rhai/governance.rhai"); + let script = fs::read_to_string(script_path)?; + + match engine.eval::<()>(&script) { + Ok(_) => println!("Script executed successfully!"), + Err(e) => eprintln!("Script execution failed: {}", e), + } + + Ok(()) +} diff --git a/heromodels/examples/governance_rhai/governance.rhai b/heromodels/examples/governance_rhai/governance.rhai new file mode 100644 index 0000000..fd3dcaa --- /dev/null +++ b/heromodels/examples/governance_rhai/governance.rhai @@ -0,0 +1,85 @@ +// Get the database instance +let db = get_db(); + +// Create a new proposal +let proposal = create_proposal(1, "user_creator_123", "Community Fund Allocation for Q3", + "Proposal to allocate funds for community projects in the third quarter."); + +print("Created Proposal: '" + get_title(proposal) + "' (ID: " + get_id(proposal) + ")"); +print("Status: " + get_status(proposal) + ", Vote Status: " + get_vote_status(proposal)); + +// Add vote options +let proposal_with_options = add_option_to_proposal(proposal, 1, "Approve Allocation"); +proposal_with_options = add_option_to_proposal(proposal_with_options, 2, "Reject Allocation"); +proposal_with_options = add_option_to_proposal(proposal_with_options, 3, "Abstain"); + +print("\nAdded Vote Options:"); +let option_count = get_option_count(proposal_with_options); +for i in range(0, option_count) { + let option = get_option_at(proposal_with_options, i); + print("- Option ID: " + i + ", Text: '" + get_option_text(option) + "', Votes: " + get_option_votes(option)); +} + +// Save the proposal to the database +save_proposal(db, proposal_with_options); +print("\nProposal saved to database"); + +// Simulate casting votes +print("\nSimulating Votes..."); +// User 1 votes for 'Approve Allocation' with 100 shares +let proposal_with_votes = cast_vote_on_proposal(proposal_with_options, 101, 1, 1, 100); +// User 2 votes for 'Reject Allocation' with 50 shares +proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 102, 2, 2, 50); +// User 3 votes for 'Approve Allocation' with 75 shares +proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 103, 3, 1, 75); +// User 4 abstains with 20 shares +proposal_with_votes = cast_vote_on_proposal(proposal_with_votes, 104, 4, 3, 20); + +print("\nVote Counts After Simulation:"); +option_count = get_option_count(proposal_with_votes); +for i in range(0, option_count) { + let option = get_option_at(proposal_with_votes, i); + print("- Option ID: " + i + ", Text: '" + get_option_text(option) + "', Votes: " + get_option_votes(option)); +} + +print("\nBallots Cast:"); +let ballot_count = get_ballot_count(proposal_with_votes); +for i in range(0, ballot_count) { + let ballot = get_ballot_at(proposal_with_votes, i); + print("- Ballot ID: " + i + ", User ID: " + get_ballot_user_id(ballot) + + ", Option ID: " + get_ballot_option_id(ballot) + ", Shares: " + get_ballot_shares(ballot)); +} + +// Change proposal status +let active_proposal = change_proposal_status(proposal_with_votes, "Active"); +print("\nChanged Proposal Status to: " + get_status(active_proposal)); + +// Simulate closing the vote +let closed_proposal = change_vote_event_status(active_proposal, "Closed"); +print("Changed Vote Event Status to: " + get_vote_status(closed_proposal)); + +// Final proposal state +print("\nFinal Proposal State:"); +print("Title: '" + get_title(closed_proposal) + "'"); +print("Status: " + get_status(closed_proposal)); +print("Vote Status: " + get_vote_status(closed_proposal)); +print("Options:"); +option_count = get_option_count(closed_proposal); +for i in range(0, option_count) { + let option = get_option_at(closed_proposal, i); + print(" - " + i + ": " + get_option_text(option) + " (Votes: " + get_option_votes(option) + ")"); +} +print("Total Ballots: " + get_ballot_count(closed_proposal)); + +// Get all proposals from the database +let all_proposals = get_all_proposals(db); +print("\nTotal Proposals in Database: " + all_proposals.len()); +for proposal in all_proposals { + print("Proposal ID: " + get_id(proposal) + ", Title: '" + get_title(proposal) + "'"); +} + +// Delete a proposal +delete_proposal_by_id(db, 1); +print("\nDeleted proposal with ID 1"); + +print("\nGovernance Proposal Example Finished."); diff --git a/heromodels/src/models/calendar/calendar.rs b/heromodels/src/models/calendar/calendar.rs new file mode 100644 index 0000000..4549f7c --- /dev/null +++ b/heromodels/src/models/calendar/calendar.rs @@ -0,0 +1,184 @@ +use chrono::{DateTime, Utc}; +use heromodels_core::BaseModelData; +use heromodels_derive::model; +use serde::{Deserialize, Serialize}; +use rhai_autobind_macros::rhai_model_export; +use rhai::{CustomType, TypeBuilder}; + +/// Represents the status of an attendee for an event +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum AttendanceStatus { + Accepted, + Declined, + Tentative, + NoResponse, +} + +/// Represents an attendee of an event +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Attendee { + /// ID of the user attending + // Assuming user_id might be queryable + pub user_id: String, // Using String for user_id similar to potential external IDs + /// Attendance status of the user for the event + pub status: AttendanceStatus, +} + +impl Attendee { + pub fn new(user_id: String) -> Self { + Self { + user_id, + status: AttendanceStatus::NoResponse, + } + } + + pub fn status(mut self, status: AttendanceStatus) -> Self { + self.status = status; + self + } +} + +/// Represents an event in a calendar +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Event { + /// Unique identifier for the event (e.g., could be a UUID string or u32 if internally managed) + // Events might be looked up by their ID + pub id: String, + /// Title of the event + pub title: String, + /// Optional description of the event + pub description: Option, + /// Start time of the event + pub start_time: DateTime, + /// End time of the event + pub end_time: DateTime, + /// List of attendees for the event + pub attendees: Vec, + /// Optional location of the event + pub location: Option, +} + +impl Event { + /// Creates a new event + pub fn new(id: String, title: impl ToString, start_time: DateTime, end_time: DateTime) -> Self { + Self { + id, + title: title.to_string(), + description: None, + start_time, + end_time, + attendees: Vec::new(), + location: None, + } + } + + /// Sets the description for the event + pub fn description(mut self, description: impl ToString) -> Self { + self.description = Some(description.to_string()); + self + } + + /// Sets the location for the event + pub fn location(mut self, location: impl ToString) -> Self { + self.location = Some(location.to_string()); + self + } + + /// Adds an attendee to the event + pub fn add_attendee(mut self, attendee: Attendee) -> Self { + // Prevent duplicate attendees by user_id + if !self.attendees.iter().any(|a| a.user_id == attendee.user_id) { + self.attendees.push(attendee); + } + self + } + + /// Removes an attendee from the event by user_id + pub fn remove_attendee(mut self, user_id: &str) -> Self { + self.attendees.retain(|a| a.user_id != user_id); + self + } + + /// Updates the status of an existing attendee + pub fn update_attendee_status(mut self, user_id: &str, status: AttendanceStatus) -> Self { + if let Some(attendee) = self.attendees.iter_mut().find(|a| a.user_id == user_id) { + attendee.status = status; + } + self + } + + /// Reschedules the event to new start and end times + pub fn reschedule(mut self, new_start_time: DateTime, new_end_time: DateTime) -> Self { + // Basic validation: end_time should be after start_time + if new_end_time > new_start_time { + self.start_time = new_start_time; + self.end_time = new_end_time; + } + // Optionally, add error handling or return a Result type + self + } +} + +/// Represents a calendar with events +#[rhai_model_export(db_type = "std::sync::Arc")] +#[model] +#[derive(Debug, Clone, Serialize, Deserialize, CustomType)] +pub struct Calendar { + /// Base model data + pub base_data: BaseModelData, + + /// Name of the calendar + pub name: String, + + /// Optional description of the calendar + pub description: Option, + + /// List of events in the calendar + // For now, events are embedded. If they become separate models, this would be Vec<[IDType]>. + pub events: Vec, +} + +impl Calendar { + /// Creates a new calendar + pub fn new(id: u32, name: impl ToString) -> Self { + Self { + base_data: BaseModelData::new(id), + name: name.to_string(), + description: None, + events: Vec::new(), + } + } + + /// Sets the description for the calendar + pub fn description(mut self, description: impl ToString) -> Self { + self.description = Some(description.to_string()); + self + } + + /// Adds an event to the calendar + pub fn add_event(mut self, event: Event) -> Self { + // Prevent duplicate events by id + if !self.events.iter().any(|e| e.id == event.id) { + self.events.push(event); + } + self + } + + /// Removes an event from the calendar by its ID + pub fn remove_event(mut self, event_id: &str) -> Self { + self.events.retain(|event| event.id != event_id); + self + } + + /// Finds an event by its ID and allows modification + pub fn update_event(mut self, event_id: &str, update_fn: F) -> Self + where + F: FnOnce(Event) -> Event, + { + if let Some(index) = self.events.iter().position(|e| e.id == event_id) { + let event = self.events.remove(index); + self.events.insert(index, update_fn(event)); + } + self + } +} diff --git a/heromodels/src/models/calendar/mod.rs b/heromodels/src/models/calendar/mod.rs new file mode 100644 index 0000000..8bf0bc5 --- /dev/null +++ b/heromodels/src/models/calendar/mod.rs @@ -0,0 +1,5 @@ +// Export calendar module +pub mod calendar; + +// Re-export Calendar, Event, Attendee, and AttendanceStatus from the inner calendar module (calendar.rs) within src/models/calendar/mod.rs +pub use self::calendar::{Calendar, Event, Attendee, AttendanceStatus}; diff --git a/heromodels/src/models/finance/account.rs b/heromodels/src/models/finance/account.rs new file mode 100644 index 0000000..b3bc373 --- /dev/null +++ b/heromodels/src/models/finance/account.rs @@ -0,0 +1,84 @@ +// heromodels/src/models/finance/account.rs + +use serde::{Deserialize, Serialize}; +use heromodels_derive::model; +use heromodels_core::BaseModelData; + +use super::asset::Asset; + +/// Account represents a financial account owned by a user +#[derive(Debug, Clone, Serialize, Deserialize)] +#[model] // Has base.Base in V spec +pub struct Account { + pub base_data: BaseModelData, + pub name: String, // internal name of the account for the user + pub user_id: u32, // user id of the owner of the account + pub description: String, // optional description of the account + pub ledger: String, // describes the ledger/blockchain where the account is located + pub address: String, // address of the account on the blockchain + pub pubkey: String, // public key + pub assets: Vec, // list of assets in this account +} + +impl Account { + /// Create a new account + pub fn new( + id: u32, + name: impl ToString, + user_id: u32, + description: impl ToString, + ledger: impl ToString, + address: impl ToString, + pubkey: impl ToString + ) -> Self { + Self { + base_data: BaseModelData::new(id), + name: name.to_string(), + user_id, + description: description.to_string(), + ledger: ledger.to_string(), + address: address.to_string(), + pubkey: pubkey.to_string(), + assets: Vec::new(), + } + } + + /// Add an asset to the account + pub fn add_asset(mut self, asset: Asset) -> Self { + self.assets.push(asset); + self + } + + /// Get the total value of all assets in the account + pub fn total_value(&self) -> f64 { + self.assets.iter().map(|asset| asset.amount).sum() + } + + /// Find an asset by name + pub fn find_asset_by_name(&self, name: &str) -> Option<&Asset> { + self.assets.iter().find(|asset| asset.name == name) + } + + /// Update the account details + pub fn update_details( + mut self, + name: Option, + description: Option, + address: Option, + pubkey: Option, + ) -> Self { + if let Some(name) = name { + self.name = name.to_string(); + } + if let Some(description) = description { + self.description = description.to_string(); + } + if let Some(address) = address { + self.address = address.to_string(); + } + if let Some(pubkey) = pubkey { + self.pubkey = pubkey.to_string(); + } + self + } +} diff --git a/heromodels/src/models/finance/asset.rs b/heromodels/src/models/finance/asset.rs new file mode 100644 index 0000000..98826f6 --- /dev/null +++ b/heromodels/src/models/finance/asset.rs @@ -0,0 +1,85 @@ +// heromodels/src/models/finance/asset.rs + +use serde::{Deserialize, Serialize}; +use heromodels_derive::model; +use heromodels_core::BaseModelData; + +/// AssetType defines the type of blockchain asset +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum AssetType { + Erc20, // ERC-20 token standard + Erc721, // ERC-721 NFT standard + Erc1155, // ERC-1155 Multi-token standard + Native, // Native blockchain asset (e.g., ETH, BTC) +} + +impl Default for AssetType { + fn default() -> Self { + AssetType::Native + } +} + +/// Asset represents a digital asset or token +#[derive(Debug, Clone, Serialize, Deserialize)] +#[model] // Has base.Base in V spec +pub struct Asset { + pub base_data: BaseModelData, + pub name: String, // Name of the asset + pub description: String, // Description of the asset + pub amount: f64, // Amount of the asset + pub address: String, // Address of the asset on the blockchain or bank + pub asset_type: AssetType, // Type of the asset + pub decimals: u8, // Number of decimals of the asset +} + +impl Asset { + /// Create a new asset + pub fn new( + id: u32, + name: impl ToString, + description: impl ToString, + amount: f64, + address: impl ToString, + asset_type: AssetType, + decimals: u8, + ) -> Self { + Self { + base_data: BaseModelData::new(id), + name: name.to_string(), + description: description.to_string(), + amount, + address: address.to_string(), + asset_type, + decimals, + } + } + + /// Update the asset amount + pub fn update_amount(mut self, amount: f64) -> Self { + self.amount = amount; + self + } + + /// Get the formatted amount with proper decimal places + pub fn formatted_amount(&self) -> String { + let factor = 10_f64.powi(self.decimals as i32); + let formatted_amount = (self.amount * factor).round() / factor; + format!("{:.1$}", formatted_amount, self.decimals as usize) + } + + /// Transfer amount to another asset + pub fn transfer_to(&mut self, target: &mut Asset, amount: f64) -> Result<(), &'static str> { + if amount <= 0.0 { + return Err("Transfer amount must be positive"); + } + + if self.amount < amount { + return Err("Insufficient balance for transfer"); + } + + self.amount -= amount; + target.amount += amount; + + Ok(()) + } +} diff --git a/heromodels/src/models/finance/marketplace.rs b/heromodels/src/models/finance/marketplace.rs new file mode 100644 index 0000000..8153052 --- /dev/null +++ b/heromodels/src/models/finance/marketplace.rs @@ -0,0 +1,286 @@ +// heromodels/src/models/finance/marketplace.rs + +use serde::{Deserialize, Serialize}; +use heromodels_derive::model; +use heromodels_core::BaseModelData; +use chrono::{DateTime, Utc}; + +use super::asset::AssetType; + +/// ListingStatus defines the status of a marketplace listing +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum ListingStatus { + Active, // Listing is active and available + Sold, // Listing has been sold + Cancelled, // Listing was cancelled by the seller + Expired, // Listing has expired +} + +impl Default for ListingStatus { + fn default() -> Self { + ListingStatus::Active + } +} + +/// ListingType defines the type of marketplace listing +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum ListingType { + FixedPrice, // Fixed price sale + Auction, // Auction with bids + Exchange, // Exchange for other assets +} + +impl Default for ListingType { + fn default() -> Self { + ListingType::FixedPrice + } +} + +/// BidStatus defines the status of a bid on an auction listing +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum BidStatus { + Active, // Bid is active + Accepted, // Bid was accepted + Rejected, // Bid was rejected + Cancelled, // Bid was cancelled by the bidder +} + +impl Default for BidStatus { + fn default() -> Self { + BidStatus::Active + } +} + +/// Bid represents a bid on an auction listing +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Bid { + pub listing_id: String, // ID of the listing this bid belongs to + pub bidder_id: u32, // ID of the user who placed the bid + pub amount: f64, // Bid amount + pub currency: String, // Currency of the bid + pub status: BidStatus, // Status of the bid + pub created_at: DateTime, // When the bid was created +} + +impl Bid { + /// Create a new bid + pub fn new( + listing_id: impl ToString, + bidder_id: u32, + amount: f64, + currency: impl ToString, + ) -> Self { + Self { + listing_id: listing_id.to_string(), + bidder_id, + amount, + currency: currency.to_string(), + status: BidStatus::default(), + created_at: Utc::now(), + } + } + + /// Update the status of the bid + pub fn update_status(mut self, status: BidStatus) -> Self { + self.status = status; + self + } +} + +/// Listing represents a marketplace listing for an asset +#[derive(Debug, Clone, Serialize, Deserialize)] +#[model] // Has base.Base in V spec +pub struct Listing { + pub base_data: BaseModelData, + pub title: String, + pub description: String, + pub asset_id: String, + pub asset_type: AssetType, + pub seller_id: String, + pub price: f64, // Initial price for fixed price, or starting price for auction + pub currency: String, + pub listing_type: ListingType, + pub status: ListingStatus, + pub expires_at: Option>, // Optional expiration date + pub sold_at: Option>, // Optional date when the item was sold + pub buyer_id: Option, // Optional buyer ID + pub sale_price: Option, // Optional final sale price + pub bids: Vec, // List of bids for auction type listings + pub tags: Vec, // Tags for the listing + pub image_url: Option, // Optional image URL +} + +impl Listing { + /// Create a new listing + pub fn new( + id: u32, + title: impl ToString, + description: impl ToString, + asset_id: impl ToString, + asset_type: AssetType, + seller_id: impl ToString, + price: f64, + currency: impl ToString, + listing_type: ListingType, + expires_at: Option>, + tags: Vec, + image_url: Option, + ) -> Self { + Self { + base_data: BaseModelData::new(id), + title: title.to_string(), + description: description.to_string(), + asset_id: asset_id.to_string(), + asset_type, + seller_id: seller_id.to_string(), + price, + currency: currency.to_string(), + listing_type, + status: ListingStatus::default(), + expires_at, + sold_at: None, + buyer_id: None, + sale_price: None, + bids: Vec::new(), + tags, + image_url: image_url.map(|url| url.to_string()), + } + } + + /// Add a bid to an auction listing + pub fn add_bid(mut self, bid: Bid) -> Result { + // Check if listing is an auction + if self.listing_type != ListingType::Auction { + return Err("Bids can only be placed on auction listings"); + } + + // Check if listing is active + if self.status != ListingStatus::Active { + return Err("Cannot place bid on inactive listing"); + } + + // Check if bid amount is higher than current price + if bid.amount <= self.price { + return Err("Bid amount must be higher than current price"); + } + + // Check if there are existing bids and if the new bid is higher + if let Some(highest_bid) = self.highest_bid() { + if bid.amount <= highest_bid.amount { + return Err("Bid amount must be higher than current highest bid"); + } + } + + // Add the bid + self.bids.push(bid); + + // Update the current price to the new highest bid + if let Some(highest_bid) = self.highest_bid() { + self.price = highest_bid.amount; + } + + Ok(self) + } + + /// Get the highest active bid + pub fn highest_bid(&self) -> Option<&Bid> { + self.bids + .iter() + .filter(|bid| bid.status == BidStatus::Active) + .max_by(|a, b| a.amount.partial_cmp(&b.amount).unwrap()) + } + + /// Complete a sale (fixed price or auction) + pub fn complete_sale(mut self, buyer_id: impl ToString, sale_price: f64) -> Result { + if self.status != ListingStatus::Active { + return Err("Cannot complete sale for inactive listing"); + } + + self.status = ListingStatus::Sold; + self.buyer_id = Some(buyer_id.to_string()); + self.sale_price = Some(sale_price); + self.sold_at = Some(Utc::now()); + + // If this was an auction, accept the winning bid and reject others + if self.listing_type == ListingType::Auction { + for bid in &mut self.bids { + if bid.bidder_id.to_string() == self.buyer_id.as_ref().unwrap().to_string() && bid.amount == sale_price { + bid.status = BidStatus::Accepted; + } else { + bid.status = BidStatus::Rejected; + } + } + } + + Ok(self) + } + + /// Cancel the listing + pub fn cancel(mut self) -> Result { + if self.status != ListingStatus::Active { + return Err("Cannot cancel inactive listing"); + } + + self.status = ListingStatus::Cancelled; + + // Cancel all active bids + for bid in &mut self.bids { + if bid.status == BidStatus::Active { + bid.status = BidStatus::Cancelled; + } + } + + Ok(self) + } + + /// Check if the listing has expired and update status if needed + pub fn check_expiration(mut self) -> Self { + if self.status == ListingStatus::Active { + if let Some(expires_at) = self.expires_at { + if Utc::now() > expires_at { + self.status = ListingStatus::Expired; + + // Cancel all active bids + for bid in &mut self.bids { + if bid.status == BidStatus::Active { + bid.status = BidStatus::Cancelled; + } + } + } + } + } + + self + } + + /// Add tags to the listing + pub fn add_tags(mut self, tags: Vec) -> Self { + for tag in tags { + self.tags.push(tag.to_string()); + } + self + } + + /// Update the listing details + pub fn update_details( + mut self, + title: Option, + description: Option, + price: Option, + image_url: Option, + ) -> Self { + if let Some(title) = title { + self.title = title.to_string(); + } + if let Some(description) = description { + self.description = description.to_string(); + } + if let Some(price) = price { + self.price = price; + } + if let Some(image_url) = image_url { + self.image_url = Some(image_url.to_string()); + } + self + } +} diff --git a/heromodels/src/models/finance/mod.rs b/heromodels/src/models/finance/mod.rs new file mode 100644 index 0000000..b045870 --- /dev/null +++ b/heromodels/src/models/finance/mod.rs @@ -0,0 +1,10 @@ +// heromodels/src/models/finance/mod.rs +// This module contains finance-related models: Account, Asset, and Marketplace + +pub mod account; +pub mod asset; +pub mod marketplace; + +pub use self::account::Account; +pub use self::asset::{Asset, AssetType}; +pub use self::marketplace::{Listing, ListingStatus, ListingType, Bid, BidStatus}; diff --git a/heromodels/src/models/governance/mod.rs b/heromodels/src/models/governance/mod.rs new file mode 100644 index 0000000..b3df030 --- /dev/null +++ b/heromodels/src/models/governance/mod.rs @@ -0,0 +1,5 @@ +// heromodels/src/models/governance/mod.rs +// This module will contain the Proposal model and related types. +pub mod proposal; + +pub use self::proposal::{Proposal, Ballot, VoteOption, ProposalStatus, VoteEventStatus}; \ No newline at end of file diff --git a/heromodels/src/models/governance/proposal.rs b/heromodels/src/models/governance/proposal.rs new file mode 100644 index 0000000..152f247 --- /dev/null +++ b/heromodels/src/models/governance/proposal.rs @@ -0,0 +1,166 @@ +// heromodels/src/models/governance/proposal.rs + +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; +use heromodels_derive::model; // For #[model] +use rhai_autobind_macros::rhai_model_export; +use rhai::{CustomType, TypeBuilder}; + +use heromodels_core::BaseModelData; + +// --- Enums --- + +/// ProposalStatus defines the lifecycle status of a governance proposal itself +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum ProposalStatus { + Draft, // Proposal is being prepared + Active, // Proposal is active + Approved, // Proposal has been formally approved + Rejected, // Proposal has been formally rejected + Cancelled,// Proposal was cancelled +} + +impl Default for ProposalStatus { + fn default() -> Self { + ProposalStatus::Draft + } +} + + +/// VoteEventStatus represents the status of the voting process for a proposal +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub enum VoteEventStatus { + Open, // Voting is currently open + Closed, // Voting has finished + Cancelled, // The voting event was cancelled +} + +impl Default for VoteEventStatus { + fn default() -> Self { + VoteEventStatus::Open + } +} + +// --- Structs --- + +/// VoteOption represents a specific choice that can be voted on +#[derive(Debug, Clone, Serialize, Deserialize, CustomType)] +pub struct VoteOption { + pub id: u8, // Simple identifier for this option + pub text: String, // Descriptive text of the option + pub count: i64, // How many votes this option has received + pub min_valid: Option, // Optional: minimum votes needed +} + +impl VoteOption { + pub fn new(id: u8, text: impl ToString) -> Self { + Self { + id, + text: text.to_string(), + count: 0, + min_valid: None, + } + } +} + +/// Ballot represents an individual vote cast by a user +#[derive(Debug, Clone, Serialize, Deserialize, CustomType)] +#[rhai_model_export(db_type = "std::sync::Arc")] +#[model] // Has base.Base in V spec +pub struct Ballot { + pub base_data: BaseModelData, + pub user_id: u32, // The ID of the user who cast this ballot + pub vote_option_id: u8, // The 'id' of the VoteOption chosen + pub shares_count: i64, // Number of shares/tokens/voting power +} + +impl Ballot { + pub fn new(id: u32, user_id: u32, vote_option_id: u8, shares_count: i64) -> Self { + Self { + base_data: BaseModelData::new(id), + user_id, + vote_option_id, + shares_count, + } + } +} + + +/// Proposal represents a governance proposal that can be voted upon. +#[derive(Debug, Clone, Serialize, Deserialize, CustomType)] +#[rhai_model_export(db_type = "std::sync::Arc")] +#[model] // Has base.Base in V spec +pub struct Proposal { + pub base_data: BaseModelData, + pub creator_id: String, // User ID of the proposal creator + pub title: String, + pub description: String, + pub status: ProposalStatus, + + // Voting event aspects + pub vote_start_date: DateTime, + pub vote_end_date: DateTime, + pub vote_status: VoteEventStatus, + pub options: Vec, + pub ballots: Vec, // This will store actual Ballot structs + pub private_group: Option>, // Optional list of eligible user IDs +} + +impl Proposal { + pub fn new(id: u32, creator_id: impl ToString, title: impl ToString, description: impl ToString, vote_start_date: DateTime, vote_end_date: DateTime) -> Self { + Self { + base_data: BaseModelData::new(id), + creator_id: creator_id.to_string(), + title: title.to_string(), + description: description.to_string(), + status: ProposalStatus::Draft, + vote_start_date, + vote_end_date, + vote_status: VoteEventStatus::Open, // Default to open when created + options: Vec::new(), + ballots: Vec::new(), + private_group: None, + } + } + + pub fn add_option(mut self, option_id: u8, option_text: impl ToString) -> Self { + let new_option = VoteOption::new(option_id, option_text); + self.options.push(new_option); + self + } + + pub fn cast_vote(mut self, ballot_id: u32, user_id: u32, chosen_option_id: u8, shares: i64) -> Self { + if self.vote_status != VoteEventStatus::Open { + eprintln!("Voting is not open for proposal '{}'", self.title); + return self; + } + if !self.options.iter().any(|opt| opt.id == chosen_option_id) { + eprintln!("Chosen option ID {} does not exist for proposal '{}'", chosen_option_id, self.title); + return self; + } + if let Some(group) = &self.private_group { + if !group.contains(&user_id) { + eprintln!("User {} is not eligible to vote on proposal '{}'", user_id, self.title); + return self; + } + } + + let new_ballot = Ballot::new(ballot_id, user_id, chosen_option_id, shares); + self.ballots.push(new_ballot); + + if let Some(option) = self.options.iter_mut().find(|opt| opt.id == chosen_option_id) { + option.count += shares; + } + self + } + + pub fn change_proposal_status(mut self, new_status: ProposalStatus) -> Self { + self.status = new_status; + self + } + + pub fn change_vote_event_status(mut self, new_status: VoteEventStatus) -> Self { + self.vote_status = new_status; + self + } +} diff --git a/heromodels/src/models/mod.rs b/heromodels/src/models/mod.rs index c2fc43d..ed03eb8 100644 --- a/heromodels/src/models/mod.rs +++ b/heromodels/src/models/mod.rs @@ -1,8 +1,16 @@ // Export submodules pub mod core; pub mod userexample; +// pub mod productexample; // Temporarily remove as files are missing +pub mod calendar; +pub mod governance; +pub mod finance; // Re-export key types for convenience pub use core::Comment; pub use userexample::User; - +// pub use productexample::Product; // Temporarily remove +pub use calendar::{Calendar, Event, Attendee, AttendanceStatus}; +pub use governance::{Proposal, ProposalStatus, VoteEventStatus, Ballot, VoteOption}; +pub use finance::{Account, Asset, AssetType}; +pub use finance::marketplace::{Listing, ListingStatus, ListingType, Bid, BidStatus};