智能合约 MVP

Solidity 实现:势枢、势位、势场三大注册表

版本: MVP | 语言: Solidity ^0.8.19 | 网络: EVM Compatible

0. 架构概览
┌─────────────────────────────────────────────────────────────┐ │ ECHO Shi Framework │ ├─────────────────┬─────────────────┬─────────────────────────┤ │ ShiShuRegistry │ ShiPositionReg │ ShiFieldRegistry │ │ (势枢注册表) │ (势位注册表) │ (势场注册表) │ │ │ │ │ │ • 三维坐标存储 │ • 六爻坐标存储 │ • 64势场区域 │ │ • 势场边界更新 │ • 跃迁历史记录 │ • 周期Merkle根 │ │ • 预言机接口 │ • 势能计算 │ • 资产-区域映射 │ └─────────────────┴─────────────────┴─────────────────────────┘ │ ▼ ┌──────────────────┐ │ ShiMerkle │ │ (Merkle验证器) │ └──────────────────┘

1. 势枢注册表 (ShiShuRegistry)

ShiShuRegistry.sol
存储三维编织的基础结构和势场边界,是 ECHO 宇宙的基础坐标系。

1.1 核心数据结构

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

/**
 * @title ShiShuRegistry
 * @notice 势枢核心注册表
 * @dev 存储三维编织的基础结构和势场边界
 */
contract ShiShuRegistry {
    
    // 维度枚举
    enum Dimension { Time, Space, Relation }
    
    // 势场边界结构
    struct ShiBoundaries {
        uint8[3] time;        // 时间维三档边界
        uint8[3] space;       // 空间维三档边界
        uint8[3] relation;    // 关系维三档边界
        uint256 updatedAt;    // 最后更新时间
    }
    
    // 资产在势枢中的坐标
    struct ShiShuCoordinate {
        uint8 timeLevel;      // 0-3
        uint8 spaceLevel;     // 0-3
        uint8 relationLevel;  // 0-3
        uint256 recordedAt;   // 记录时间
    }
    
    // 当前势场边界
    ShiBoundaries public currentBoundaries;
    
    // 资产 => 势枢坐标
    mapping(bytes32 => ShiShuCoordinate) public assetCoordinates;
    
    event CoordinateRecorded(
        bytes32 indexed assetId,
        uint8 timeLevel,
        uint8 spaceLevel,
        uint8 relationLevel
    );
}

1.2 核心函数

/**
 * @notice 记录资产在势枢中的坐标
 */
function recordCoordinate(
    bytes32 assetId,
    uint8 timeUsage,
    uint8 spaceCount,
    uint8 relationCount
) external onlyOracle returns (ShiShuCoordinate memory) {
    
    uint8 timeLevel = _calculateLevel(timeUsage, currentBoundaries.time);
    uint8 spaceLevel = _calculateLevel(spaceCount, currentBoundaries.space);
    uint8 relationLevel = _calculateLevel(relationCount, currentBoundaries.relation);
    
    ShiShuCoordinate memory coord = ShiShuCoordinate({
        timeLevel: timeLevel,
        spaceLevel: spaceLevel,
        relationLevel: relationLevel,
        recordedAt: block.timestamp
    });
    
    assetCoordinates[assetId] = coord;
    
    emit CoordinateRecorded(
        assetId, timeLevel, spaceLevel, relationLevel
    );
    
    return coord;
}

/**
 * @notice 计算档位
 */
function _calculateLevel(
    uint8 value,
    uint8[3] memory boundaries
) internal pure returns (uint8) {
    if (value == 0) return 0;                    // 沉寂/孤立
    if (value < boundaries[0]) return 1;         // 稳定/少平台/初连
    if (value < boundaries[1]) return 2;         // 活跃/多平台/网络
    return 3;                                     // 爆发/跨域/生态
}

2. 势位注册表 (ShiPositionRegistry)

ShiPositionRegistry.sol
存储资产的六爻坐标和势能,记录势位跃迁历史。

2.1 核心数据结构

contract ShiPositionRegistry {
    
    // 六爻枚举
    enum Yao { ChuJiu, JiuEr, JiuSan, JiuSi, JiuWu, ShangJiu }
    
    // 势位结构
    struct ShiPosition {
        Yao timeYao;          // 时间势位
        Yao spaceYao;         // 空间势位
        Yao relationYao;      // 关系势位
        uint256 shiEnergy;    // 势能值 (0-10000)
        uint256 lastUpdate;   // 最后更新
    }
    
    // 势位跃迁记录
    struct ShiTransition {
        uint8 dimension;           // 0=时间, 1=空间, 2=关系
        Yao fromYao;
        Yao toYao;
        uint8 percentileBefore;
        uint8 percentileAfter;
        uint256 timestamp;
        bytes32 triggerEvent;
    }
    
    // 资产 => 当前势位
    mapping(bytes32 => ShiPosition) public positions;
    
    // 资产 => 跃迁历史
    mapping(bytes32 => ShiTransition[]) public transitionHistory;
}

2.2 核心函数

/**
 * @notice 更新资产势位(由预言机调用)
 */
function updatePosition(
    bytes32 assetId,
    uint8 timePercentile,     // 0-100
    uint8 spacePercentile,
    uint8 relationPercentile,
    uint256 shiEnergy
) external onlyOracle {
    
    // 计算新的六爻坐标
    Yao newTimeYao = _percentileToYao(timePercentile);
    // ...
    
    ShiPosition storage current = positions[assetId];
    
    // 检查是否跃迁
    if (current.timeYao != newTimeYao) {
        _recordTransition(assetId, 0, current.timeYao, newTimeYao, ...);
    }
    
    // 更新势位
    positions[assetId] = ShiPosition({
        timeYao: newTimeYao,
        spaceYao: newSpaceYao,
        relationYao: newRelationYao,
        shiEnergy: shiEnergy,
        lastUpdate: block.timestamp
    });
}

/**
 * @notice 百分位转六爻
 */
function _percentileToYao(uint8 percentile) 
    internal pure returns (Yao) 
{
    if (percentile < 25) return Yao.ChuJiu;     // 初九: 后25%
    if (percentile < 50) return Yao.JiuEr;      // 九二: 25-50%
    if (percentile < 70) return Yao.JiuSan;     // 九三: 50-70%
    if (percentile < 85) return Yao.JiuSi;      // 九四: 70-85%
    if (percentile < 95) return Yao.JiuWu;      // 九五: 85-95%
    return Yao.ShangJiu;                    // 上九: 前5%
}

3. 势场注册表 (ShiFieldRegistry)

ShiFieldRegistry.sol
存储64势场区域,管理每周势场周期,记录资产-区域映射。

3.1 核心数据结构

contract ShiFieldRegistry {
    
    // 势场区域结构
    struct ShiFieldRegion {
        string traditionalName;   // 传统卦名,如"乾"
        uint8[4] center;          // 中心特征 [用, 扩, 衍, 益]
        uint256 memberCount;     // 成员数量
        string description;       // 描述
    }
    
    // 势场周期
    struct ShiFieldEpoch {
        uint256 timestamp;
        uint256 epoch;
        bytes32 merkleRoot;      // 资产-区域映射的Merkle根
        mapping(uint8 => ShiFieldRegion) regions;  // 1-64区域
    }
    
    // 周期ID => 势场周期
    mapping(uint256 => ShiFieldEpoch) public epochs;
    
    // 当前周期
    uint256 public currentEpoch;
    
    // 资产 => 当前所属区域
    mapping(bytes32 => uint8) public assetToRegion;
}

3.2 核心函数

/**
 * @notice 创建新的势场周期(每周调用)
 */
function createEpoch(
    bytes32 merkleRoot,
    ShiFieldRegion[64] calldata regions
) external onlyOracle returns (uint256 epochId) 
{
    uint256 newEpoch = currentEpoch + 1;
    
    ShiFieldEpoch storage epoch = epochs[newEpoch];
    epoch.timestamp = block.timestamp;
    epoch.epoch = newEpoch;
    epoch.merkleRoot = merkleRoot;
    
    // 存储64个区域信息
    for (uint8 i = 0; i < 64; i++) {
        epoch.regions[i + 1] = regions[i]; // 区域ID从1开始
    }
    
    currentEpoch = newEpoch;
    
    emit ShiFieldUpdated(newEpoch, block.timestamp, merkleRoot);
    
    return newEpoch;
}

/**
 * @notice 验证并记录资产所属区域
 */
function assignRegion(
    bytes32 assetId,
    uint8 regionId,
    bytes32[] calldata merkleProof
) external {
    // 验证Merkle证明...
    bool isValid = merkleVerifier.verify(
        epochs[currentEpoch].merkleRoot,
        assetId,
        regionId,
        merkleProof
    );
    
    require(isValid, "Invalid Merkle proof");
    
    assetToRegion[assetId] = regionId;
    
    emit AssetRegionAssigned(
        assetId, 
        regionId, 
        epochs[currentEpoch].regions[regionId].traditionalName
    );
}

4. 合约交互流程

每周势场更新流程: 1. 链下计算 ├── 抓取全网资产数据 ├── 计算新的势场边界 (75%/90%分位数) ├── 计算每个资产的新势位坐标 └── 生成资产-区域映射的Merkle树 2. 链上更新 (预言机调用) ├── ShiShuRegistry.updateBoundaries() ├── ShiPositionRegistry.batchUpdatePositions() └── ShiFieldRegistry.createEpoch() 3. 用户验证 (用户自己调用) └── ShiFieldRegistry.assignRegion() + Merkle证明

完整合约代码位于 reference/ECHO-Shi-Contracts.sol