πŸ“‹AgentRegistry

The AgentRegistry is the core smart contract for managing AI agent registration and discovery on the Somnia blockchain.

🎯 Overview

The AgentRegistry contract provides:

  • βœ… Agent Registration - Register new AI agents on-chain

  • βœ… Agent Discovery - Find and query registered agents

  • βœ… Ownership Management - Control agent ownership and transfers

  • βœ… Metadata Storage - Store agent information via IPFS

  • βœ… Status Management - Activate/deactivate agents

πŸ“Š Contract Architecture

contract AgentRegistry {
    struct Agent {
        string name;             // Agent name
        string description;      // Agent description
        string ipfsMetadata;     // IPFS hash for metadata
        address owner;           // Agent owner address
        bool isActive;           // Active status
        uint256 registeredAt;    // Registration timestamp
        uint256 lastUpdated;     // Last update timestamp
        string[] capabilities;   // Agent capabilities
        uint256 executionCount;  // Execution counter
    }
    
    mapping(uint256 => Agent) public agents;
    mapping(address => uint256[]) public ownerAgents;
    uint256 public agentCounter;
}

πŸ”§ Core Functions

1. Register Agent

Register a new AI agent on the blockchain.

Parameters:

  • _name - Agent name

  • _description - Agent description

  • _ipfsMetadata - IPFS hash containing full metadata

  • _capabilities - Array of agent capabilities

Returns:

  • uint256 - Unique ID of the registered agent

Events Emitted:

Example Usage:

2. Get Agent Info

Retrieve complete information about an agent.

Parameters:

  • _agentId - ID of the agent to query

Returns:

  • Tuple containing agent details

Example:

3. Update Agent

Update agent information (owner only).

Requirements:

  • Caller must be agent owner

Events Emitted:

Example:

4. Set Agent Status

Activate or deactivate an agent (owner only).

Requirements:

  • Caller must be agent owner

Events Emitted:

Example:

circle-info

Note: Use setAgentStatus(agentId, isActive) instead of separate deactivateAgent() and reactivateAgent() methods.

5. Transfer Ownership

Transfer agent ownership to another address.

Requirements:

  • Caller must be current owner

  • New owner cannot be zero address

Events Emitted:

Example:

πŸ” Query Functions

Get Total Agents

Example:

Get Agents by Owner

Example:

Iterate Through All Agents

circle-exclamation

Example:

πŸ“‘ Events

AgentRegistered

Listen for new agents:

AgentUpdated

Example:

AgentStatusChanged

Example:

AgentOwnershipTransferred

πŸ”’ Access Control

Owner-Only Functions

These functions can only be called by the agent owner:

  • updateAgent()

  • setAgentStatus()

  • transferAgentOwnership()

Public Functions

These functions can be called by anyone:

  • registerAgent() - Anyone can register a new agent

  • All query functions (read-only)

πŸ’‘ Best Practices

1. Metadata Structure

Store comprehensive metadata in IPFS:

2. Error Handling

Always handle potential errors:

3. Verify Ownership

πŸ”— Contract Addresses

Somnia Testnet

Somnia Mainnet

⚠️ Security Considerations

  1. Metadata Validation - Always validate metadata before uploading to IPFS

  2. Owner Verification - Verify ownership before sensitive operations

  3. IPFS Availability - Ensure IPFS content is pinned and accessible

  4. Gas Costs - Be aware of gas costs for operations


Next: Learn about AgentManager for managing agent tasks.

Last updated