NAC_Blockchain/nac-api-server/src/models/mod.rs

131 lines
3.2 KiB
Rust

use serde::{Deserialize, Serialize};
use validator::{Validate, ValidationError};
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct TransferRequest {
#[validate(length(min = 40, max = 66))]
pub from: String,
#[validate(length(min = 40, max = 66))]
pub to: String,
#[validate(length(min = 1))]
pub amount: String,
#[validate(length(min = 1, max = 20))]
pub asset: String,
#[validate(length(min = 1))]
pub signature: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferResponse {
pub tx_hash: String,
pub status: String,
pub message: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceResponse {
pub address: String,
pub balance: String,
pub assets: Vec<AssetBalance>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetBalance {
pub symbol: String,
pub amount: String,
pub decimals: u8,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionResponse {
pub hash: String,
pub from: String,
pub to: String,
pub amount: String,
pub asset: String,
pub block_number: u64,
pub timestamp: i64,
pub status: String,
pub fee: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthResponse {
pub status: String,
pub version: String,
pub block_height: u64,
pub timestamp: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct CreateOrderRequest {
#[validate(length(min = 1))]
pub asset: String,
#[validate(length(min = 1))]
pub amount: String,
#[validate(length(min = 1))]
pub price: String,
#[validate(custom(function = "validate_order_type"))]
pub order_type: String, // "buy" or "sell"
}
fn validate_order_type(order_type: &str) -> Result<(), ValidationError> {
if order_type == "buy" || order_type == "sell" {
Ok(())
} else {
Err(ValidationError::new("invalid_order_type"))
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderResponse {
pub order_id: String,
pub asset: String,
pub amount: String,
pub price: String,
pub order_type: String,
pub status: String,
pub created_at: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketDataResponse {
pub asset: String,
pub price: String,
pub volume_24h: String,
pub change_24h: String,
pub high_24h: String,
pub low_24h: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_transfer_request_validation() {
let valid_req = TransferRequest {
from: "nac1234567890123456789012345678901234567890".to_string(),
to: "nac0987654321098765432109876543210987654321".to_string(),
amount: "100.00".to_string(),
asset: "XTZH".to_string(),
signature: "0x123456".to_string(),
};
assert!(valid_req.validate().is_ok());
}
#[test]
fn test_order_type_validation() {
assert!(validate_order_type("buy").is_ok());
assert!(validate_order_type("sell").is_ok());
assert!(validate_order_type("invalid").is_err());
}
}