start porting model specs into heromodels

This commit is contained in:
timurgordon
2025-05-13 01:58:35 +03:00
parent 52528ca99f
commit c77d08033e
16 changed files with 1817 additions and 1 deletions

View File

@@ -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<Asset>, // 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<impl ToString>,
description: Option<impl ToString>,
address: Option<impl ToString>,
pubkey: Option<impl ToString>,
) -> 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
}
}

View File

@@ -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(())
}
}

View File

@@ -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<Utc>, // 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<DateTime<Utc>>, // Optional expiration date
pub sold_at: Option<DateTime<Utc>>, // Optional date when the item was sold
pub buyer_id: Option<String>, // Optional buyer ID
pub sale_price: Option<f64>, // Optional final sale price
pub bids: Vec<Bid>, // List of bids for auction type listings
pub tags: Vec<String>, // Tags for the listing
pub image_url: Option<String>, // 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<DateTime<Utc>>,
tags: Vec<String>,
image_url: Option<impl ToString>,
) -> 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<Self, &'static str> {
// 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<Self, &'static str> {
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<Self, &'static str> {
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<impl ToString>) -> Self {
for tag in tags {
self.tags.push(tag.to_string());
}
self
}
/// Update the listing details
pub fn update_details(
mut self,
title: Option<impl ToString>,
description: Option<impl ToString>,
price: Option<f64>,
image_url: Option<impl ToString>,
) -> 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
}
}

View File

@@ -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};