516 lines
12 KiB
Rust
516 lines
12 KiB
Rust
use heromodels_core::{Model, BaseModelData, IndexKey};
|
|
use heromodels_derive::model;
|
|
use serde::{Deserialize, Serialize};
|
|
use std::collections::HashMap;
|
|
|
|
/// Represents the status of an account
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum AccountStatus {
|
|
Active,
|
|
Inactive,
|
|
Suspended,
|
|
Archived,
|
|
}
|
|
|
|
impl Default for AccountStatus {
|
|
fn default() -> Self {
|
|
AccountStatus::Active
|
|
}
|
|
}
|
|
|
|
/// Represents the type of transaction
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub enum TransactionType {
|
|
Transfer,
|
|
Clawback,
|
|
Freeze,
|
|
Unfreeze,
|
|
Issue,
|
|
Burn,
|
|
}
|
|
|
|
impl Default for TransactionType {
|
|
fn default() -> Self {
|
|
TransactionType::Transfer
|
|
}
|
|
}
|
|
|
|
/// Represents a signature for transactions
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
|
pub struct Signature {
|
|
pub signer_id: u32,
|
|
pub signature: String,
|
|
pub timestamp: u64,
|
|
}
|
|
|
|
impl Signature {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
signer_id: 0,
|
|
signature: String::new(),
|
|
timestamp: 0,
|
|
}
|
|
}
|
|
|
|
pub fn signer_id(mut self, signer_id: u32) -> Self {
|
|
self.signer_id = signer_id;
|
|
self
|
|
}
|
|
|
|
pub fn signature(mut self, signature: impl ToString) -> Self {
|
|
self.signature = signature.to_string();
|
|
self
|
|
}
|
|
|
|
pub fn timestamp(mut self, timestamp: u64) -> Self {
|
|
self.timestamp = timestamp;
|
|
self
|
|
}
|
|
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Policy item for account operations
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct AccountPolicyItem {
|
|
pub signers: Vec<u32>,
|
|
pub min_signatures: u32,
|
|
pub enabled: bool,
|
|
pub threshold: f64,
|
|
pub recipient: u32,
|
|
}
|
|
|
|
impl AccountPolicyItem {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
signers: Vec::new(),
|
|
min_signatures: 0,
|
|
enabled: false,
|
|
threshold: 0.0,
|
|
recipient: 0,
|
|
}
|
|
}
|
|
|
|
pub fn add_signer(mut self, signer_id: u32) -> Self {
|
|
self.signers.push(signer_id);
|
|
self
|
|
}
|
|
|
|
pub fn signers(mut self, signers: Vec<u32>) -> Self {
|
|
self.signers = signers;
|
|
self
|
|
}
|
|
|
|
pub fn min_signatures(mut self, min_signatures: u32) -> Self {
|
|
self.min_signatures = min_signatures;
|
|
self
|
|
}
|
|
|
|
pub fn enabled(mut self, enabled: bool) -> Self {
|
|
self.enabled = enabled;
|
|
self
|
|
}
|
|
|
|
pub fn threshold(mut self, threshold: f64) -> Self {
|
|
self.threshold = threshold;
|
|
self
|
|
}
|
|
|
|
pub fn recipient(mut self, recipient: u32) -> Self {
|
|
self.recipient = recipient;
|
|
self
|
|
}
|
|
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Represents an account in the financial system
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct Account {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
pub owner_id: u32,
|
|
#[index]
|
|
pub address: String,
|
|
pub balance: f64,
|
|
pub currency: String,
|
|
pub assetid: u32,
|
|
pub last_activity: u64,
|
|
pub administrators: Vec<u32>,
|
|
pub accountpolicy: u32,
|
|
}
|
|
|
|
impl Account {
|
|
/// Create a new account instance
|
|
pub fn new(id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
base_data.update_id(id);
|
|
Self {
|
|
base_data,
|
|
owner_id: 0,
|
|
address: String::new(),
|
|
balance: 0.0,
|
|
currency: String::new(),
|
|
assetid: 0,
|
|
last_activity: 0,
|
|
administrators: Vec::new(),
|
|
accountpolicy: 0,
|
|
}
|
|
}
|
|
|
|
/// Set the owner ID (fluent)
|
|
pub fn owner_id(mut self, owner_id: u32) -> Self {
|
|
self.owner_id = owner_id;
|
|
self
|
|
}
|
|
|
|
/// Set the blockchain address (fluent)
|
|
pub fn address(mut self, address: impl ToString) -> Self {
|
|
self.address = address.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the balance (fluent)
|
|
pub fn balance(mut self, balance: f64) -> Self {
|
|
self.balance = balance;
|
|
self
|
|
}
|
|
|
|
/// Set the currency (fluent)
|
|
pub fn currency(mut self, currency: impl ToString) -> Self {
|
|
self.currency = currency.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the asset ID (fluent)
|
|
pub fn assetid(mut self, assetid: u32) -> Self {
|
|
self.assetid = assetid;
|
|
self
|
|
}
|
|
|
|
/// Set the last activity timestamp (fluent)
|
|
pub fn last_activity(mut self, last_activity: u64) -> Self {
|
|
self.last_activity = last_activity;
|
|
self
|
|
}
|
|
|
|
/// Add an administrator (fluent)
|
|
pub fn add_administrator(mut self, admin_id: u32) -> Self {
|
|
self.administrators.push(admin_id);
|
|
self
|
|
}
|
|
|
|
/// Set all administrators (fluent)
|
|
pub fn administrators(mut self, administrators: Vec<u32>) -> Self {
|
|
self.administrators = administrators;
|
|
self
|
|
}
|
|
|
|
/// Set the account policy ID (fluent)
|
|
pub fn accountpolicy(mut self, accountpolicy: u32) -> Self {
|
|
self.accountpolicy = accountpolicy;
|
|
self
|
|
}
|
|
|
|
/// Build the final account instance
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Represents an asset in the financial system
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct Asset {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
#[index]
|
|
pub address: String,
|
|
pub assetid: u32,
|
|
pub asset_type: String,
|
|
pub issuer: u32,
|
|
pub supply: f64,
|
|
pub decimals: u8,
|
|
pub is_frozen: bool,
|
|
pub metadata: HashMap<String, String>,
|
|
pub administrators: Vec<u32>,
|
|
pub min_signatures: u32,
|
|
}
|
|
|
|
impl Asset {
|
|
/// Create a new asset instance
|
|
pub fn new(id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
base_data.update_id(id);
|
|
Self {
|
|
base_data,
|
|
address: String::new(),
|
|
assetid: 0,
|
|
asset_type: String::new(),
|
|
issuer: 0,
|
|
supply: 0.0,
|
|
decimals: 0,
|
|
is_frozen: false,
|
|
metadata: HashMap::new(),
|
|
administrators: Vec::new(),
|
|
min_signatures: 0,
|
|
}
|
|
}
|
|
|
|
/// Set the blockchain address (fluent)
|
|
pub fn address(mut self, address: impl ToString) -> Self {
|
|
self.address = address.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the asset ID (fluent)
|
|
pub fn assetid(mut self, assetid: u32) -> Self {
|
|
self.assetid = assetid;
|
|
self
|
|
}
|
|
|
|
/// Set the asset type (fluent)
|
|
pub fn asset_type(mut self, asset_type: impl ToString) -> Self {
|
|
self.asset_type = asset_type.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the issuer (fluent)
|
|
pub fn issuer(mut self, issuer: u32) -> Self {
|
|
self.issuer = issuer;
|
|
self
|
|
}
|
|
|
|
/// Set the supply (fluent)
|
|
pub fn supply(mut self, supply: f64) -> Self {
|
|
self.supply = supply;
|
|
self
|
|
}
|
|
|
|
/// Set the decimals (fluent)
|
|
pub fn decimals(mut self, decimals: u8) -> Self {
|
|
self.decimals = decimals;
|
|
self
|
|
}
|
|
|
|
/// Set the frozen status (fluent)
|
|
pub fn is_frozen(mut self, is_frozen: bool) -> Self {
|
|
self.is_frozen = is_frozen;
|
|
self
|
|
}
|
|
|
|
/// Add metadata entry (fluent)
|
|
pub fn add_metadata(mut self, key: impl ToString, value: impl ToString) -> Self {
|
|
self.metadata.insert(key.to_string(), value.to_string());
|
|
self
|
|
}
|
|
|
|
/// Set all metadata (fluent)
|
|
pub fn metadata(mut self, metadata: HashMap<String, String>) -> Self {
|
|
self.metadata = metadata;
|
|
self
|
|
}
|
|
|
|
/// Add an administrator (fluent)
|
|
pub fn add_administrator(mut self, admin_id: u32) -> Self {
|
|
self.administrators.push(admin_id);
|
|
self
|
|
}
|
|
|
|
/// Set all administrators (fluent)
|
|
pub fn administrators(mut self, administrators: Vec<u32>) -> Self {
|
|
self.administrators = administrators;
|
|
self
|
|
}
|
|
|
|
/// Set minimum signatures required (fluent)
|
|
pub fn min_signatures(mut self, min_signatures: u32) -> Self {
|
|
self.min_signatures = min_signatures;
|
|
self
|
|
}
|
|
|
|
/// Build the final asset instance
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Represents account policies for various operations
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct AccountPolicy {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
pub transferpolicy: AccountPolicyItem,
|
|
pub adminpolicy: AccountPolicyItem,
|
|
pub clawbackpolicy: AccountPolicyItem,
|
|
pub freezepolicy: AccountPolicyItem,
|
|
}
|
|
|
|
impl AccountPolicy {
|
|
/// Create a new account policy instance
|
|
pub fn new(id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
base_data.update_id(id);
|
|
Self {
|
|
base_data,
|
|
transferpolicy: AccountPolicyItem::new(),
|
|
adminpolicy: AccountPolicyItem::new(),
|
|
clawbackpolicy: AccountPolicyItem::new(),
|
|
freezepolicy: AccountPolicyItem::new(),
|
|
}
|
|
}
|
|
|
|
/// Set the transfer policy (fluent)
|
|
pub fn transferpolicy(mut self, transferpolicy: AccountPolicyItem) -> Self {
|
|
self.transferpolicy = transferpolicy;
|
|
self
|
|
}
|
|
|
|
/// Set the admin policy (fluent)
|
|
pub fn adminpolicy(mut self, adminpolicy: AccountPolicyItem) -> Self {
|
|
self.adminpolicy = adminpolicy;
|
|
self
|
|
}
|
|
|
|
/// Set the clawback policy (fluent)
|
|
pub fn clawbackpolicy(mut self, clawbackpolicy: AccountPolicyItem) -> Self {
|
|
self.clawbackpolicy = clawbackpolicy;
|
|
self
|
|
}
|
|
|
|
/// Set the freeze policy (fluent)
|
|
pub fn freezepolicy(mut self, freezepolicy: AccountPolicyItem) -> Self {
|
|
self.freezepolicy = freezepolicy;
|
|
self
|
|
}
|
|
|
|
/// Build the final account policy instance
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Represents a financial transaction
|
|
#[model]
|
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
|
pub struct Transaction {
|
|
/// Base model data
|
|
pub base_data: BaseModelData,
|
|
pub txid: u32,
|
|
pub source: u32,
|
|
pub destination: u32,
|
|
pub assetid: u32,
|
|
pub amount: f64,
|
|
pub timestamp: u64,
|
|
pub status: String,
|
|
pub memo: String,
|
|
pub tx_type: TransactionType,
|
|
pub signatures: Vec<Signature>,
|
|
}
|
|
|
|
impl Transaction {
|
|
/// Create a new transaction instance
|
|
pub fn new(id: u32) -> Self {
|
|
let mut base_data = BaseModelData::new();
|
|
base_data.update_id(id);
|
|
Self {
|
|
base_data,
|
|
txid: 0,
|
|
source: 0,
|
|
destination: 0,
|
|
assetid: 0,
|
|
amount: 0.0,
|
|
timestamp: 0,
|
|
status: String::new(),
|
|
memo: String::new(),
|
|
tx_type: TransactionType::default(),
|
|
signatures: Vec::new(),
|
|
}
|
|
}
|
|
|
|
/// Set the transaction ID (fluent)
|
|
pub fn txid(mut self, txid: u32) -> Self {
|
|
self.txid = txid;
|
|
self
|
|
}
|
|
|
|
/// Set the source account (fluent)
|
|
pub fn source(mut self, source: u32) -> Self {
|
|
self.source = source;
|
|
self
|
|
}
|
|
|
|
/// Set the destination account (fluent)
|
|
pub fn destination(mut self, destination: u32) -> Self {
|
|
self.destination = destination;
|
|
self
|
|
}
|
|
|
|
/// Set the asset ID (fluent)
|
|
pub fn assetid(mut self, assetid: u32) -> Self {
|
|
self.assetid = assetid;
|
|
self
|
|
}
|
|
|
|
/// Set the amount (fluent)
|
|
pub fn amount(mut self, amount: f64) -> Self {
|
|
self.amount = amount;
|
|
self
|
|
}
|
|
|
|
/// Set the timestamp (fluent)
|
|
pub fn timestamp(mut self, timestamp: u64) -> Self {
|
|
self.timestamp = timestamp;
|
|
self
|
|
}
|
|
|
|
/// Set the status (fluent)
|
|
pub fn status(mut self, status: impl ToString) -> Self {
|
|
self.status = status.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the memo (fluent)
|
|
pub fn memo(mut self, memo: impl ToString) -> Self {
|
|
self.memo = memo.to_string();
|
|
self
|
|
}
|
|
|
|
/// Set the transaction type (fluent)
|
|
pub fn tx_type(mut self, tx_type: TransactionType) -> Self {
|
|
self.tx_type = tx_type;
|
|
self
|
|
}
|
|
|
|
/// Add a signature (fluent)
|
|
pub fn add_signature(mut self, signature: Signature) -> Self {
|
|
self.signatures.push(signature);
|
|
self
|
|
}
|
|
|
|
/// Set all signatures (fluent)
|
|
pub fn signatures(mut self, signatures: Vec<Signature>) -> Self {
|
|
self.signatures = signatures;
|
|
self
|
|
}
|
|
|
|
/// Build the final transaction instance
|
|
pub fn build(self) -> Self {
|
|
self
|
|
}
|
|
}
|
|
|
|
|