//! 资产上链编排引擎 //! //! 协调所有适配器完成完整的上链流程 use crate::error::{OnboardingError, Result}; use crate::types::*; use crate::state_machine::{StateMachine, OnboardingState}; use crate::compliance::ComplianceAdapter; use crate::valuation::ValuationAdapter; use crate::dna::DNAAdapter; use crate::custody::CustodyAdapter; use crate::xtzh::XTZHAdapter; use crate::token::TokenAdapter; use crate::blockchain::BlockchainAdapter; use tracing::{info, error, warn}; use chrono::Utc; /// 编排引擎配置 #[derive(Debug, Clone)] pub struct OrchestratorConfig { /// ChatGPT API密钥(用于估值) pub chatgpt_key: String, /// DeepSeek API密钥(用于估值) pub deepseek_key: String, /// 豆包API密钥(用于估值) pub doubao_key: String, /// NAC RPC URL pub nac_rpc_url: String, } /// 资产上链编排引擎 pub struct Orchestrator { config: OrchestratorConfig, compliance: ComplianceAdapter, valuation: ValuationAdapter, dna: DNAAdapter, custody: CustodyAdapter, xtzh: XTZHAdapter, token: TokenAdapter, blockchain: BlockchainAdapter, } impl Orchestrator { /// 创建新的编排引擎 pub fn new(config: OrchestratorConfig) -> Result { info!("初始化资产上链编排引擎"); let compliance = ComplianceAdapter::new()?; let valuation = ValuationAdapter::new( config.chatgpt_key.clone(), config.deepseek_key.clone(), config.doubao_key.clone(), )?; let dna = DNAAdapter::new()?; let custody = CustodyAdapter::new()?; let xtzh = XTZHAdapter::new()?; let token = TokenAdapter::new()?; let blockchain = BlockchainAdapter::new(config.nac_rpc_url.clone())?; Ok(Self { config, compliance, valuation, dna, custody, xtzh, token, blockchain, }) } /// 执行完整的上链流程 pub async fn onboard_asset( &self, submission: AssetSubmission, ) -> Result { info!("开始资产上链流程: {}", submission.asset_name); let mut state_machine = StateMachine::new(); let mut process = OnboardingProcess { process_id: uuid::Uuid::new_v4().to_string(), user_id: submission.user_id.clone(), asset_name: submission.asset_name.clone(), state: state_machine.current_state(), compliance_result: None, valuation_result: None, dna_result: None, custody_result: None, xtzh_result: None, token_result: None, blockchain_result: None, created_at: Utc::now(), updated_at: Utc::now(), }; // 步骤1:AI合规审批 match self.step_compliance(&submission, &mut state_machine).await { Ok(result) => { process.compliance_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("合规审批失败: {}", e); state_machine.mark_failed(format!("合规审批失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } // 步骤2:AI估值 match self.step_valuation(&submission, &mut state_machine).await { Ok(result) => { process.valuation_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("AI估值失败: {}", e); state_machine.mark_failed(format!("AI估值失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } // 步骤3:DNA生成 match self.step_dna(&submission, &mut state_machine).await { Ok(result) => { process.dna_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("DNA生成失败: {}", e); state_machine.mark_failed(format!("DNA生成失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } // 步骤4:托管对接 let dna_hash = process.dna_result.as_ref().expect("mainnet: handle error").dna_hash.clone(); match self.step_custody(&submission, &dna_hash, &mut state_machine).await { Ok(result) => { process.custody_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("托管对接失败: {}", e); state_machine.mark_failed(format!("托管对接失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } // 步骤5:XTZH铸造 let valuation = process.valuation_result.as_ref().expect("mainnet: handle error"); let custody_hash = process.custody_result.as_ref().expect("mainnet: handle error").custody_agreement_hash.clone(); match self.step_xtzh(valuation, &dna_hash, &custody_hash, &mut state_machine).await { Ok(result) => { process.xtzh_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("XTZH铸造失败: {}", e); state_machine.mark_failed(format!("XTZH铸造失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } // 步骤6:代币发行 let xtzh_amount = process.xtzh_result.as_ref().expect("mainnet: handle error").xtzh_amount; match self.step_token(&submission, &dna_hash, xtzh_amount, &mut state_machine).await { Ok(result) => { process.token_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("代币发行失败: {}", e); state_machine.mark_failed(format!("代币发行失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } // 步骤7:区块链集成 let token_address = process.token_result.as_ref().expect("mainnet: handle error").token_address.clone(); match self.step_blockchain(&dna_hash, &token_address, &mut state_machine).await { Ok(result) => { process.blockchain_result = Some(result); process.state = state_machine.current_state(); process.updated_at = Utc::now(); } Err(e) => { error!("区块链集成失败: {}", e); state_machine.mark_failed(format!("区块链集成失败: {}", e)); process.state = state_machine.current_state(); return Err(e); } } info!("资产上链流程完成: {}", submission.asset_name); Ok(process) } /// 步骤1:AI合规审批 async fn step_compliance( &self, submission: &AssetSubmission, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始AI合规审批".to_string())?; let result = self.compliance.verify_compliance(submission).await?; if !result.passed { return Err(OnboardingError::ComplianceError( format!("合规审批未通过,评分: {}", result.score) )); } state_machine.transition("合规审批完成".to_string())?; Ok(result) } /// 步骤2:AI估值 async fn step_valuation( &self, submission: &AssetSubmission, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始AI估值".to_string())?; let result = self.valuation.appraise(submission).await?; state_machine.transition("估值完成".to_string())?; Ok(result) } /// 步骤3:DNA生成 async fn step_dna( &self, submission: &AssetSubmission, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始DNA生成".to_string())?; let result = self.dna.generate_dna(submission).await?; state_machine.transition("DNA生成完成".to_string())?; Ok(result) } /// 步骤4:托管对接 async fn step_custody( &self, submission: &AssetSubmission, dna_hash: &str, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始托管对接".to_string())?; let result = self.custody.arrange_custody(submission, dna_hash).await?; state_machine.transition("托管对接完成".to_string())?; Ok(result) } /// 步骤5:XTZH铸造 async fn step_xtzh( &self, valuation: &ValuationResult, dna_hash: &str, custody_hash: &str, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始XTZH铸造".to_string())?; let result = self.xtzh.mint_xtzh(valuation, dna_hash, custody_hash).await?; state_machine.transition("XTZH铸造完成".to_string())?; Ok(result) } /// 步骤6:代币发行 async fn step_token( &self, submission: &AssetSubmission, dna_hash: &str, xtzh_amount: rust_decimal::Decimal, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始代币发行".to_string())?; let result = self.token.issue_token(submission, dna_hash, xtzh_amount).await?; state_machine.transition("代币发行完成".to_string())?; Ok(result) } /// 步骤7:区块链集成 async fn step_blockchain( &self, dna_hash: &str, token_address: &str, state_machine: &mut StateMachine, ) -> Result { state_machine.transition("开始区块链集成".to_string())?; let result = self.blockchain.submit_to_chain(dna_hash, token_address).await?; state_machine.transition("区块链集成完成".to_string())?; Ok(result) } } #[cfg(test)] mod tests { use super::*; #[test] fn test_orchestrator_creation() { let config = OrchestratorConfig { chatgpt_key: "test".to_string(), deepseek_key: "test".to_string(), doubao_key: "test".to_string(), nac_rpc_url: "http://localhost:8545".to_string(), }; let orchestrator = Orchestrator::new(config); assert!(orchestrator.is_ok()); } }