start porting model specs into heromodels
This commit is contained in:
129
heromodels/examples/calendar_example/main.rs
Normal file
129
heromodels/examples/calendar_example/main.rs
Normal file
@@ -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::<Calendar>().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);
|
||||
}
|
58
heromodels/examples/calendar_rhai/calendar.rhai
Normal file
58
heromodels/examples/calendar_rhai/calendar.rhai
Normal file
@@ -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());
|
83
heromodels/examples/calendar_rhai/example.rs
Normal file
83
heromodels/examples/calendar_rhai/example.rs
Normal file
@@ -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<dyn std::error::Error>> {
|
||||
// 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<OurDB>, _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<OurDB>, 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<OurDB>, 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<OurDB>) -> Vec<Calendar> {
|
||||
// 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<OurDB> => Calendar));
|
||||
|
||||
engine.register_fn("delete_calendar_by_id", |_db: Arc<OurDB>, _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(())
|
||||
}
|
329
heromodels/examples/finance_example/main.rs
Normal file
329
heromodels/examples/finance_example/main.rs
Normal file
@@ -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::<String>, // keep same description
|
||||
None::<String>, // 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::<String>, // 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::<String>, // 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::<String>, // 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.");
|
||||
}
|
110
heromodels/examples/governance_proposal_example/main.rs
Normal file
110
heromodels/examples/governance_proposal_example/main.rs
Normal file
@@ -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.");
|
||||
}
|
189
heromodels/examples/governance_rhai/example.rs
Normal file
189
heromodels/examples/governance_rhai/example.rs
Normal file
@@ -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<dyn std::error::Error>> {
|
||||
// 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<OurDB>, proposal: Proposal| {
|
||||
println!("Proposal saved: {}", proposal.title);
|
||||
});
|
||||
|
||||
engine.register_fn("get_proposal_by_id", |_db: Arc<OurDB>, 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<OurDB>, 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<OurDB>) -> Vec<Proposal> {
|
||||
// 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<OurDB> => Proposal));
|
||||
|
||||
engine.register_fn("delete_proposal_by_id", |_db: Arc<OurDB>, _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(())
|
||||
}
|
85
heromodels/examples/governance_rhai/governance.rhai
Normal file
85
heromodels/examples/governance_rhai/governance.rhai
Normal file
@@ -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.");
|
Reference in New Issue
Block a user