Smart Contracts Overview

Opacus Protocol consists of 5 specialized smart contracts deployed on 0G Chain, each handling a specific aspect of the decentralized agent ecosystem.

Contract Architecture

The Opacus smart contract suite is designed with modularity and upgradability in mind:

πŸ›οΈ

OpacusCore

Central coordinator
Protocol governance
Contract registry

πŸ†”

AgentRegistry

Agent registration
Identity management
Public key storage

πŸ”

DACRegistry

Access control
Permission management
Role-based security

πŸ“‘

DataStream

High-throughput messaging
Stream management
Data channels

πŸ’°

MsgEscrow

Payment escrow
Service guarantees
Automated settlement

Contract Deployment Info

πŸ“ Deployed Addresses (0G Testnet)

OpacusCore:    0x5FbDB2315678afecb367f032d93F642f64180aa3
AgentRegistry: 0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0
DACRegistry:   0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
DataStream:    0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9
MsgEscrow:     0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9

Network:       0G Chain Testnet
Chain ID:      16661
RPC URL:       https://evmrpc-testnet.0g.ai

Technology Stack

Contract Interactions

1. Agent Registration Flow

// User calls AgentRegistry
AgentRegistry.registerAgent(
  metadata,
  edPublicKey,
  xPublicKey
)
  ↓
// AgentRegistry validates and stores
- Generates unique agent ID
- Stores public keys
- Emits AgentRegistered event
  ↓
// OpacusCore receives notification
- Updates agent count
- Adds to global registry

2. Attestation Creation Flow

// Attestor calls DACRegistry
DACRegistry.createAttestation(
  agentId,
  capability,
  proofData
)
  ↓
// DACRegistry validates
- Checks attestor permissions
- Verifies proof signature
- Stores attestation
  ↓
// AgentRegistry updates
- Increments attestation count
- Updates reputation score

3. Message Escrow Flow

// Sender creates escrow
MsgEscrow.createEscrow(
  recipientAgent,
  amount,
  conditions
)
  ↓
// Funds locked in contract
- Escrow ID generated
- Timeout starts
  ↓
// Service completed
MsgEscrow.releaseEscrow(escrowId)
  ↓
// Funds released to recipient
- Or refunded if timeout/dispute

Gas Costs

Operation Contract Gas Estimate Cost (0G)
Register Agent AgentRegistry ~100,000 ~0.0001
Update Metadata AgentRegistry ~50,000 ~0.00005
Create Attestation DACRegistry ~80,000 ~0.00008
Send Message DataStream ~30,000 ~0.00003
Create Escrow MsgEscrow ~60,000 ~0.00006

Security Features

πŸ”’ Access Control: OpenZeppelin AccessControl for role-based permissions
⏸️ Pausable: Emergency pause mechanism for all critical functions
πŸ”„ Upgradable: Transparent proxy pattern for seamless upgrades
βœ… Reentrancy Guard: Protection against reentrancy attacks
πŸ›‘οΈ Input Validation: Comprehensive checks on all user inputs
πŸ“Š Event Logging: Detailed events for all state changes

Events

AgentRegistry Events

event AgentRegistered(
    bytes32 indexed agentId,
    address indexed owner,
    string name
);

event AgentUpdated(
    bytes32 indexed agentId,
    string metadata
);

event AgentDeactivated(
    bytes32 indexed agentId
);

DACRegistry Events

event AttestationCreated(
    bytes32 indexed attestationId,
    bytes32 indexed agentId,
    address indexed attestor,
    string capability
);

event AttestationRevoked(
    bytes32 indexed attestationId
);

MsgEscrow Events

event EscrowCreated(
    bytes32 indexed escrowId,
    address indexed sender,
    bytes32 indexed recipientAgent,
    uint256 amount
);

event EscrowReleased(
    bytes32 indexed escrowId,
    address recipient
);

event EscrowRefunded(
    bytes32 indexed escrowId,
    address sender
);

Querying Contracts

Using ethers.js

import { ethers } from 'ethers';

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

// Connect to AgentRegistry
const agentRegistry = new ethers.Contract(
  AGENT_REGISTRY_ADDRESS,
  AGENT_REGISTRY_ABI,
  provider
);

// Get agent info
const agent = await agentRegistry.getAgent(agentId);
console.log('Agent:', {
  id: agent.id,
  owner: agent.owner,
  edPublicKey: agent.edPublicKey,
  active: agent.active
});

Using web3.js

import Web3 from 'web3';

const web3 = new Web3('https://evmrpc-testnet.0g.ai');

// Connect to DACRegistry
const dacRegistry = new web3.eth.Contract(
  DAC_REGISTRY_ABI,
  DAC_REGISTRY_ADDRESS
);

// Get attestations
const attestations = await dacRegistry.methods
  .getAgentAttestations(agentId)
  .call();

console.log('Attestations:', attestations);

Development Tools

Compile Contracts

cd contracts
npx hardhat compile

Run Tests

npx hardhat test
npx hardhat coverage  # Generate coverage report

Deploy to Network

npx hardhat run scripts/deploy.ts --network og-testnet

Verify on Explorer

npx hardhat verify --network og-testnet CONTRACT_ADDRESS

πŸ“š Learn More

Dive deeper into each contract:

Contract Source Code

All contracts are open source and available on GitHub:

πŸ” Audited & Secure

Opacus smart contracts have undergone comprehensive security audits and follow best practices from OpenZeppelin. All critical functions include access control, input validation, and event logging.