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

99 lines
2.6 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协议铸造稳定币
use crate::error::{OnboardingError, Result};
use crate::types::{ValuationResult, XTZHResult};
use nac_udm::l1_protocol::acc::xtzh::{XTZHProtocol, MintRequest};
use rust_decimal::Decimal;
use chrono::Utc;
use tracing::{info, error};
/// XTZH铸造适配器
pub struct XTZHAdapter {
protocol: XTZHProtocol,
}
impl XTZHAdapter {
/// 创建新的适配器
pub fn new() -> Result<Self> {
let protocol = XTZHProtocol::new()
.map_err(|e| OnboardingError::XTZHMintingError(format!("初始化失败: {}", e)))?;
Ok(Self { protocol })
}
/// 铸造XTZH
pub async fn mint_xtzh(
&self,
valuation: &ValuationResult,
dna_hash: &str,
custody_hash: &str,
) -> Result<XTZHResult> {
info!("开始铸造XTZH: {} XTZH", valuation.valuation_xtzh);
// 构建铸造请求
let request = MintRequest {
asset_id: dna_hash.to_string(),
custody_proof: custody_hash.to_string(),
valuation_xtzh: valuation.valuation_xtzh,
confidence: valuation.confidence,
};
// 执行铸造
let response = self.protocol.mint(&request)
.await
.map_err(|e| OnboardingError::XTZHMintingError(format!("铸造失败: {}", e)))?;
// 获取交易哈希
let tx_hash = response.transaction_hash;
// 获取XTZH地址NAC地址32字节
let xtzh_address = response.xtzh_address;
info!("XTZH铸造完成: amount={}, tx={}", valuation.valuation_xtzh, 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(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(rate)
}
}
impl Default for XTZHAdapter {
fn default() -> Self {
Self::new().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_adapter_creation() {
let adapter = XTZHAdapter::new();
assert!(adapter.is_ok());
}
}