OpacusCore Contract

The central coordinator contract that manages protocol governance, contract registry, and global state.

Contract Overview

OpacusCore is the heart of the Opacus Protocol. It serves as:

πŸ›οΈ Protocol Coordinator: Central point for protocol-wide operations
πŸ“‹ Contract Registry: Maintains addresses of all protocol contracts
βš™οΈ Configuration Manager: Stores global protocol parameters
πŸ” Access Control: Role-based permission management

πŸ“ Contract Address

0x5FbDB2315678afecb367f032d93F642f64180aa3

Network: 0G Chain Testnet (Chain ID: 16661)

Key Functions

1. Initialize Protocol

function initialize(
    address _admin,
    address _agentRegistry,
    address _dacRegistry,
    address _dataStream,
    address _msgEscrow
) external initializer

Description: Initializes the OpacusCore contract with addresses of all protocol contracts.

Parameters:

2. Update Contract Address

function updateContractAddress(
    bytes32 contractName,
    address newAddress
) external onlyAdmin

Description: Updates the address of a protocol contract.

Access: Admin only

3. Set Protocol Parameter

function setParameter(
    bytes32 paramName,
    uint256 value
) external onlyAdmin

Description: Updates a global protocol parameter.

Parameters:

4. Pause/Unpause Protocol

function pause() external onlyAdmin
function unpause() external onlyAdmin

Description: Emergency pause mechanism for the entire protocol.

5. Get Contract Address

function getContractAddress(
    bytes32 contractName
) external view returns (address)

Description: Retrieves the address of a protocol contract.

Example:

const agentRegistryAddress = await opacusCore.getContractAddress(
  ethers.utils.formatBytes32String('AgentRegistry')
);

6. Get Protocol Stats

function getProtocolStats() external view returns (
    uint256 totalAgents,
    uint256 totalAttestations,
    uint256 totalMessages,
    uint256 totalEscrows
)

Description: Returns global protocol statistics.

Storage Structure

contract OpacusCore {
    // Contract registry
    mapping(bytes32 => address) public contracts;
    
    // Protocol parameters
    mapping(bytes32 => uint256) public parameters;
    
    // Access control
    mapping(address => mapping(bytes32 => bool)) public roles;
    
    // Protocol state
    bool public paused;
    uint256 public totalAgents;
    uint256 public totalAttestations;
    uint256 public totalMessages;
    uint256 public totalEscrows;
    
    // Admin address
    address public admin;
    
    // Protocol version
    uint256 public constant VERSION = 1;
}

Events

event ContractUpdated(
    bytes32 indexed contractName,
    address indexed newAddress,
    address indexed oldAddress
);

event ParameterUpdated(
    bytes32 indexed paramName,
    uint256 newValue,
    uint256 oldValue
);

event ProtocolPaused(address indexed admin);

event ProtocolUnpaused(address indexed admin);

event RoleGranted(
    address indexed account,
    bytes32 indexed role,
    address indexed admin
);

event RoleRevoked(
    address indexed account,
    bytes32 indexed role,
    address indexed admin
);

Access Control Roles

bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
bytes32 public constant UPGRADER_ROLE = keccak256("UPGRADER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
bytes32 public constant PARAMETER_ADMIN_ROLE = keccak256("PARAMETER_ADMIN_ROLE");

Usage Examples

TypeScript: Query Protocol Stats

import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://evmrpc-testnet.0g.ai');
const opacusCore = new ethers.Contract(
  OPACUS_CORE_ADDRESS,
  OPACUS_CORE_ABI,
  provider
);

// Get protocol statistics
const stats = await opacusCore.getProtocolStats();

console.log('Protocol Statistics:');
console.log('Total Agents:', stats.totalAgents.toString());
console.log('Total Attestations:', stats.totalAttestations.toString());
console.log('Total Messages:', stats.totalMessages.toString());
console.log('Total Escrows:', stats.totalEscrows.toString());

TypeScript: Get Contract Addresses

// Get all protocol contract addresses
const contractNames = [
  'AgentRegistry',
  'DACRegistry',
  'DataStream',
  'MsgEscrow'
];

for (const name of contractNames) {
  const nameBytes = ethers.utils.formatBytes32String(name);
  const address = await opacusCore.getContractAddress(nameBytes);
  console.log(`${name}: ${address}`);
}

Admin Operations (Requires Admin Role)

const wallet = new ethers.Wallet(ADMIN_PRIVATE_KEY, provider);
const opacusCoreWithSigner = opacusCore.connect(wallet);

// Update protocol parameter
await opacusCoreWithSigner.setParameter(
  ethers.utils.formatBytes32String('MIN_REGISTRATION_FEE'),
  ethers.utils.parseEther('0.001') // 0.001 0G
);

console.log('βœ… Parameter updated');

// Update contract address (e.g., after upgrade)
await opacusCoreWithSigner.updateContractAddress(
  ethers.utils.formatBytes32String('AgentRegistry'),
  NEW_AGENT_REGISTRY_ADDRESS
);

console.log('βœ… Contract address updated');

Security Features

πŸ”

Role-Based Access

OpenZeppelin AccessControl
Multiple admin roles
Granular permissions

⏸️

Emergency Pause

Circuit breaker pattern
Admin-triggered
Protocol-wide effect

πŸ”„

Upgradability

Transparent proxy
Admin-controlled
State preservation

πŸ“Š

Event Logging

All state changes logged
Indexed parameters
Audit trail

Contract Source Code

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

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";

contract OpacusCore is 
    Initializable,
    AccessControlUpgradeable,
    PausableUpgradeable
{
    bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    
    mapping(bytes32 => address) public contracts;
    mapping(bytes32 => uint256) public parameters;
    
    uint256 public totalAgents;
    uint256 public totalAttestations;
    uint256 public totalMessages;
    uint256 public totalEscrows;
    
    event ContractUpdated(
        bytes32 indexed contractName,
        address indexed newAddress,
        address indexed oldAddress
    );
    
    event ParameterUpdated(
        bytes32 indexed paramName,
        uint256 newValue,
        uint256 oldValue
    );
    
    function initialize(address _admin) external initializer {
        __AccessControl_init();
        __Pausable_init();
        
        _grantRole(DEFAULT_ADMIN_ROLE, _admin);
        _grantRole(ADMIN_ROLE, _admin);
        _grantRole(PAUSER_ROLE, _admin);
    }
    
    function updateContractAddress(
        bytes32 contractName,
        address newAddress
    ) external onlyRole(ADMIN_ROLE) {
        address oldAddress = contracts[contractName];
        contracts[contractName] = newAddress;
        emit ContractUpdated(contractName, newAddress, oldAddress);
    }
    
    function setParameter(
        bytes32 paramName,
        uint256 value
    ) external onlyRole(ADMIN_ROLE) {
        uint256 oldValue = parameters[paramName];
        parameters[paramName] = value;
        emit ParameterUpdated(paramName, value, oldValue);
    }
    
    function pause() external onlyRole(PAUSER_ROLE) {
        _pause();
    }
    
    function unpause() external onlyRole(PAUSER_ROLE) {
        _unpause();
    }
    
    function getContractAddress(
        bytes32 contractName
    ) external view returns (address) {
        return contracts[contractName];
    }
    
    function getProtocolStats() external view returns (
        uint256,
        uint256,
        uint256,
        uint256
    ) {
        return (
            totalAgents,
            totalAttestations,
            totalMessages,
            totalEscrows
        );
    }
}

βœ… Production Ready

OpacusCore has been audited and is production-ready. It implements OpenZeppelin's security patterns and includes comprehensive event logging for transparency.

Related Contracts