Files
smom-dbis-138/contracts/vault/XAUOracle.sol

199 lines
6.7 KiB
Solidity
Raw Normal View History

feat: Implement Universal Cross-Chain Asset Hub - All phases complete PRODUCTION-GRADE IMPLEMENTATION - All 7 Phases Done This is a complete, production-ready implementation of an infinitely extensible cross-chain asset hub that will never box you in architecturally. ## Implementation Summary ### Phase 1: Foundation ✅ - UniversalAssetRegistry: 10+ asset types with governance - Asset Type Handlers: ERC20, GRU, ISO4217W, Security, Commodity - GovernanceController: Hybrid timelock (1-7 days) - TokenlistGovernanceSync: Auto-sync tokenlist.json ### Phase 2: Bridge Infrastructure ✅ - UniversalCCIPBridge: Main bridge (258 lines) - GRUCCIPBridge: GRU layer conversions - ISO4217WCCIPBridge: eMoney/CBDC compliance - SecurityCCIPBridge: Accredited investor checks - CommodityCCIPBridge: Certificate validation - BridgeOrchestrator: Asset-type routing ### Phase 3: Liquidity Integration ✅ - LiquidityManager: Multi-provider orchestration - DODOPMMProvider: DODO PMM wrapper - PoolManager: Auto-pool creation ### Phase 4: Extensibility ✅ - PluginRegistry: Pluggable components - ProxyFactory: UUPS/Beacon proxy deployment - ConfigurationRegistry: Zero hardcoded addresses - BridgeModuleRegistry: Pre/post hooks ### Phase 5: Vault Integration ✅ - VaultBridgeAdapter: Vault-bridge interface - BridgeVaultExtension: Operation tracking ### Phase 6: Testing & Security ✅ - Integration tests: Full flows - Security tests: Access control, reentrancy - Fuzzing tests: Edge cases - Audit preparation: AUDIT_SCOPE.md ### Phase 7: Documentation & Deployment ✅ - System architecture documentation - Developer guides (adding new assets) - Deployment scripts (5 phases) - Deployment checklist ## Extensibility (Never Box In) 7 mechanisms to prevent architectural lock-in: 1. Plugin Architecture - Add asset types without core changes 2. Upgradeable Contracts - UUPS proxies 3. Registry-Based Config - No hardcoded addresses 4. Modular Bridges - Asset-specific contracts 5. Composable Compliance - Stackable modules 6. Multi-Source Liquidity - Pluggable providers 7. Event-Driven - Loose coupling ## Statistics - Contracts: 30+ created (~5,000+ LOC) - Asset Types: 10+ supported (infinitely extensible) - Tests: 5+ files (integration, security, fuzzing) - Documentation: 8+ files (architecture, guides, security) - Deployment Scripts: 5 files - Extensibility Mechanisms: 7 ## Result A future-proof system supporting: - ANY asset type (tokens, GRU, eMoney, CBDCs, securities, commodities, RWAs) - ANY chain (EVM + future non-EVM via CCIP) - WITH governance (hybrid risk-based approval) - WITH liquidity (PMM integrated) - WITH compliance (built-in modules) - WITHOUT architectural limitations Add carbon credits, real estate, tokenized bonds, insurance products, or any future asset class via plugins. No redesign ever needed. Status: Ready for Testing → Audit → Production
2026-01-24 07:01:37 -08:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "./interfaces/IXAUOracle.sol";
import "../oracle/IAggregator.sol";
/**
* @title XAUOracle
* @notice Multi-source oracle aggregator for ETH/XAU pricing with safety margins
* @dev Aggregates prices from multiple Aggregator feeds
*
* COMPLIANCE NOTES:
* - XAU (Gold) is the ISO 4217 commodity code used as universal unit of account
* - All currency conversions in the system MUST triangulate through XAU
* - XAU is NOT legal tender, but serves as the normalization standard
*/
contract XAUOracle is IXAUOracle, AccessControl, Pausable {
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
bytes32 public constant FEED_MANAGER_ROLE = keccak256("FEED_MANAGER_ROLE");
struct PriceFeed {
address feed;
uint256 weight; // in basis points (10000 = 100%)
bool active;
}
PriceFeed[] private _priceFeeds;
mapping(address => uint256) private _feedIndex; // feed address => index + 1 (0 means not found)
mapping(address => bool) private _isFeed;
uint256 public constant BASIS_POINTS = 10000;
uint256 public constant SAFETY_MARGIN_BPS = 500; // 5% safety margin for liquidations
uint256 public constant PRICE_DECIMALS = 18;
uint256 private _lastPrice;
uint256 private _lastUpdate;
constructor(address admin) {
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_grantRole(ADMIN_ROLE, admin);
_grantRole(FEED_MANAGER_ROLE, admin);
}
/**
* @notice Get ETH price in XAU
* @return price ETH price in XAU (18 decimals)
* @return timestamp Last update timestamp
*/
function getETHPriceInXAU() external view override returns (uint256 price, uint256 timestamp) {
require(!paused(), "XAUOracle: paused");
require(_lastUpdate > 0, "XAUOracle: no price data");
return (_lastPrice, _lastUpdate);
}
/**
* @notice Get liquidation price for a vault (with safety margin)
* @param vault Vault address (not used in current implementation, reserved for future use)
* @return price Liquidation threshold price in XAU
*/
function getLiquidationPrice(address vault) external view override returns (uint256 price) {
require(!paused(), "XAUOracle: paused");
require(_lastUpdate > 0, "XAUOracle: no price data");
// Apply safety margin: liquidation price = current price * (1 - safety margin)
price = (_lastPrice * (BASIS_POINTS - SAFETY_MARGIN_BPS)) / BASIS_POINTS;
}
/**
* @notice Update price from aggregated feeds
* @dev Can be called by anyone, but typically called by keeper
*/
function updatePrice() external whenNotPaused {
require(_priceFeeds.length > 0, "XAUOracle: no feeds");
uint256 totalWeight = 0;
uint256 weightedSum = 0;
for (uint256 i = 0; i < _priceFeeds.length; i++) {
if (!_priceFeeds[i].active) continue;
IAggregator feed = IAggregator(_priceFeeds[i].feed);
(, int256 answer, , uint256 updatedAt, ) = feed.latestRoundData();
require(answer > 0, "XAUOracle: invalid price");
require(updatedAt > 0, "XAUOracle: stale price");
// Check price staleness (30 seconds)
require(block.timestamp - updatedAt <= 30, "XAUOracle: stale price");
uint256 price = uint256(answer);
// Convert from feed decimals to 18 decimals
uint8 feedDecimals = feed.decimals();
if (feedDecimals < PRICE_DECIMALS) {
price = price * (10 ** (PRICE_DECIMALS - feedDecimals));
} else if (feedDecimals > PRICE_DECIMALS) {
price = price / (10 ** (feedDecimals - PRICE_DECIMALS));
}
weightedSum += price * _priceFeeds[i].weight;
totalWeight += _priceFeeds[i].weight;
}
require(totalWeight > 0, "XAUOracle: no active feeds");
uint256 aggregatedPrice = weightedSum / totalWeight;
_lastPrice = aggregatedPrice;
_lastUpdate = block.timestamp;
emit PriceUpdated(aggregatedPrice, block.timestamp);
}
/**
* @notice Add a price feed source
* @param feed Price feed address (must implement Aggregator interface)
* @param weight Weight for this feed (in basis points)
*/
function addPriceFeed(address feed, uint256 weight) external onlyRole(FEED_MANAGER_ROLE) {
require(feed != address(0), "XAUOracle: zero feed");
require(weight > 0 && weight <= BASIS_POINTS, "XAUOracle: invalid weight");
require(!_isFeed[feed], "XAUOracle: feed already exists");
_priceFeeds.push(PriceFeed({
feed: feed,
weight: weight,
active: true
}));
_feedIndex[feed] = _priceFeeds.length; // index + 1
_isFeed[feed] = true;
emit PriceFeedAdded(feed, weight);
}
/**
* @notice Remove a price feed source
* @param feed Price feed address to remove
*/
function removePriceFeed(address feed) external onlyRole(FEED_MANAGER_ROLE) {
require(_isFeed[feed], "XAUOracle: feed not found");
uint256 index = _feedIndex[feed] - 1;
_priceFeeds[index].active = false;
_isFeed[feed] = false;
_feedIndex[feed] = 0;
emit PriceFeedRemoved(feed);
}
/**
* @notice Update feed weight
* @param feed Price feed address
* @param weight New weight
*/
function updateFeedWeight(address feed, uint256 weight) external onlyRole(FEED_MANAGER_ROLE) {
require(_isFeed[feed], "XAUOracle: feed not found");
require(weight > 0 && weight <= BASIS_POINTS, "XAUOracle: invalid weight");
uint256 index = _feedIndex[feed] - 1;
uint256 oldWeight = _priceFeeds[index].weight;
_priceFeeds[index].weight = weight;
emit FeedWeightUpdated(feed, oldWeight, weight);
}
/**
* @notice Freeze oracle (emergency)
*/
function freeze() external onlyRole(ADMIN_ROLE) {
_pause();
emit OracleFrozen(block.timestamp);
}
/**
* @notice Unfreeze oracle
*/
function unfreeze() external onlyRole(ADMIN_ROLE) {
_unpause();
emit OracleUnfrozen(block.timestamp);
}
/**
* @notice Check if oracle is frozen
* @return frozen True if frozen
*/
function isFrozen() external view override returns (bool) {
return paused();
}
/**
* @notice Get all price feeds
* @return feeds Array of price feed structs
*/
function getPriceFeeds() external view returns (PriceFeed[] memory feeds) {
return _priceFeeds;
}
}