NAC_Blockchain/rwa/nac-asset-onboarding/src/xtzh.rs

98 lines
2.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! XTZH 稳定币铸造适配器
//!
//! 通过 ACC-XTZH 协议在 NAC 主网铸造 XTZH 稳定币。
//! XTZH 采用 SDR 锚定模型 + 黄金储备保障125% 抵押覆盖率。
use crate::error::{OnboardingError, Result};
use crate::types::{ValuationResult, XTZHResult, XTZHProtocol};
use rust_decimal::Decimal;
use chrono::Utc;
use tracing::info;
/// XTZH 铸造适配器
pub struct XTZHAdapter {
/// XTZH 协议实例
protocol: XTZHProtocol,
}
impl XTZHAdapter {
/// 创建新的 XTZH 适配器
pub fn new() -> Result<Self> {
Ok(Self {
protocol: XTZHProtocol::new(),
})
}
/// 铸造 XTZH 稳定币
///
/// 根据 AI 估值结果,通过 ACC-XTZH 协议铸造对应数量的稳定币。
/// 铸造需要 125% 抵押覆盖率(由宪法层强制执行)。
pub async fn mint_xtzh(
&self,
valuation: &ValuationResult,
dna_hash: &str,
custody_hash: &str,
) -> Result<XTZHResult> {
info!("开始铸造 XTZH: {} XTZH", valuation.valuation_xtzh);
// 将 Decimal 转为 u128XTZH 精度为 18 位)
let amount_u128 = valuation.valuation_xtzh
.to_string()
.parse::<f64>()
.unwrap_or(0.0) as u128;
// 通过 ACC-XTZH 协议铸造(需要 125% 抵押)
let mint_result = self.protocol.mint(amount_u128, custody_hash)
.await
.map_err(|e| OnboardingError::XTZHMintingError(format!("XTZH 铸造失败: {}", e)))?;
let tx_hash = mint_result.transaction_hash;
let xtzh_address = mint_result.xtzh_address;
info!(
"XTZH 铸造完成: amount={}, address={}, tx={}",
valuation.valuation_xtzh, xtzh_address, tx_hash
);
Ok(XTZHResult {
xtzh_amount: valuation.valuation_xtzh,
xtzh_address,
mint_tx_hash: tx_hash,
timestamp: Utc::now(),
})
}
/// 查询 XTZH 余额
pub async fn get_balance(&self, address: &str) -> Result<Decimal> {
let balance = self.protocol.balance_of(address)
.await
.map_err(|e| OnboardingError::XTZHMintingError(format!("查询余额失败: {}", e)))?;
Ok(Decimal::from(balance))
}
/// 查询当前 SDR 汇率
pub async fn get_sdr_rate(&self) -> Result<Decimal> {
let rate = self.protocol.get_sdr_rate()
.await
.map_err(|e| OnboardingError::XTZHMintingError(format!("查询 SDR 汇率失败: {}", e)))?;
Ok(Decimal::try_from(rate).unwrap_or(Decimal::ONE))
}
}
impl Default for XTZHAdapter {
fn default() -> Self {
Self::new().expect("XTZHAdapter 初始化失败")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_adapter_creation() {
let adapter = XTZHAdapter::new();
assert!(adapter.is_ok());
}
}