132 lines
3.9 KiB
Plaintext
132 lines
3.9 KiB
Plaintext
// Charter Standard Library - XTZH 汇率预言机接口
|
||
// Issue #61: 汇率数据源与验证
|
||
// 版本: 1.0
|
||
|
||
/// SDR 篮子货币枚举
|
||
public enum SdrCurrency {
|
||
USD = 0,
|
||
EUR = 1,
|
||
CNY = 2,
|
||
JPY = 3,
|
||
GBP = 4
|
||
}
|
||
|
||
/// 预言机数据源类型
|
||
public enum OracleType {
|
||
ZeroKnowledge = 0, // ZK 证明预言机
|
||
TrustedExecution = 1, // TEE 可信执行环境
|
||
Optimistic = 2 // 乐观预言机(有挑战期)
|
||
}
|
||
|
||
/// 预言机节点信息
|
||
public struct OracleNode {
|
||
did: DID,
|
||
oracle_type: OracleType,
|
||
stake: uint64, // 质押 XIC 数量
|
||
reputation: uint64, // 信誉分,定点数 1e4
|
||
is_active: bool
|
||
}
|
||
|
||
/// 汇率聚合结果
|
||
public struct RateAggregation {
|
||
rate: XTZHRate,
|
||
oracle_count: uint64, // 参与聚合的预言机数量
|
||
consensus_weight: uint64, // 加权共识比例,定点数 1e4
|
||
is_valid: bool
|
||
}
|
||
|
||
/// 汇率预言机聚合合约
|
||
contract XTZHRateOracle {
|
||
// 最小预言机数量(宪法要求)
|
||
const MIN_ORACLE_COUNT: uint64 = 5;
|
||
// 最小共识权重(宪法要求)
|
||
const MIN_CONSENSUS_WEIGHT: uint64 = 6700; // 67.00%
|
||
// 最大汇率偏离(触发告警)
|
||
const MAX_DEVIATION_BPS: uint64 = 200; // 2.00%
|
||
// AI 模型影子运行最短时长(纪元数)
|
||
const MIN_SHADOW_RUN_EPOCHS: uint64 = 30; // 30 天
|
||
|
||
state oracles: [OracleNode; 50];
|
||
state oracle_count: uint64;
|
||
state latest_rate: XTZHRate;
|
||
state latest_aggregation: RateAggregation;
|
||
state shadow_model_epoch: uint64; // 影子模型开始运行纪元
|
||
|
||
/// 提交汇率报告(仅白名单预言机)
|
||
pub fn submit_rate(
|
||
rate_sdr: uint64,
|
||
rate_usd: uint64,
|
||
epoch: uint64,
|
||
cr: ConstitutionalReceipt
|
||
) {
|
||
require(xtzh::verify_rate_receipt(cr), "无效的宪法收据");
|
||
require(epoch == self.latest_rate.epoch + 1, "纪元不连续");
|
||
require(rate_sdr > 0, "汇率必须大于 0");
|
||
|
||
// 检查偏离是否超标
|
||
if self.check_deviation(rate_sdr) {
|
||
emit XTZHRateDeviation {
|
||
epoch: epoch,
|
||
deviation_bps: self.calc_deviation_bps(rate_sdr),
|
||
direction: if rate_sdr > self.latest_rate.rate_sdr { "up" } else { "down" }
|
||
};
|
||
}
|
||
|
||
// 聚合逻辑由 NVM 系统调用处理
|
||
// 当达到 MIN_ORACLE_COUNT 且 consensus_weight >= MIN_CONSENSUS_WEIGHT 时
|
||
// 自动更新 latest_rate
|
||
}
|
||
|
||
/// 查询最新汇率
|
||
@view
|
||
pub fn get_latest() -> XTZHRate {
|
||
return self.latest_rate;
|
||
}
|
||
|
||
/// 查询聚合状态
|
||
@view
|
||
pub fn get_aggregation() -> RateAggregation {
|
||
return self.latest_aggregation;
|
||
}
|
||
|
||
/// 检查汇率偏离是否超标
|
||
@view
|
||
pub fn check_deviation(new_rate: uint64) -> bool {
|
||
if self.latest_rate.rate_sdr == 0 {
|
||
return false;
|
||
}
|
||
let deviation_bps = self.calc_deviation_bps(new_rate);
|
||
return deviation_bps > MAX_DEVIATION_BPS;
|
||
}
|
||
|
||
/// 计算汇率偏离基点数
|
||
@view
|
||
pub fn calc_deviation_bps(new_rate: uint64) -> uint64 {
|
||
if self.latest_rate.rate_sdr == 0 {
|
||
return 0;
|
||
}
|
||
let diff = if new_rate > self.latest_rate.rate_sdr {
|
||
new_rate - self.latest_rate.rate_sdr
|
||
} else {
|
||
self.latest_rate.rate_sdr - new_rate
|
||
};
|
||
return diff * 10000 / self.latest_rate.rate_sdr;
|
||
}
|
||
|
||
/// 激活新 AI 汇率模型(需先影子运行 30 天)
|
||
@system
|
||
pub fn activate_new_model(
|
||
model_hash: hash,
|
||
shadow_start_epoch: uint64,
|
||
cr: ConstitutionalReceipt
|
||
) {
|
||
require(xtzh::verify_rate_receipt(cr), "无效的宪法收据");
|
||
let rate = xtzh::get_rate();
|
||
require(
|
||
rate.epoch >= shadow_start_epoch + MIN_SHADOW_RUN_EPOCHS,
|
||
"影子运行时间不足 30 天"
|
||
);
|
||
// 激活新模型(NVM 系统处理)
|
||
}
|
||
}
|