Skip to content

Repository files navigation

πŸ”— Orchestrator Protocol Smart Contracts

A comprehensive blockchain protocol for managing AI agent registrations, payments, and reputation in your orchestrator system.

πŸ“‹ Overview

This protocol consists of three main smart contracts that enable decentralized coordination between orchestrators and AI agents:

  1. AgentRegistry: Manages agent registration and discovery
  2. ReputationLayer: Tracks agent performance and ratings
  3. OrchestrationContract: Handles payments, task execution, and dispute resolution

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   AgentRegistry β”‚    β”‚  ReputationLayer β”‚    β”‚ OrchestrationContractβ”‚
β”‚                 β”‚    β”‚                  β”‚    β”‚                     β”‚
β”‚ β€’ Registration  β”‚    β”‚ β€’ Task Results   β”‚    β”‚ β€’ Task Creation     β”‚
β”‚ β€’ Discovery     β”‚    β”‚ β€’ Ratings        β”‚    β”‚ β€’ Payment Escrow    β”‚
β”‚ β€’ Agent Status  β”‚    β”‚ β€’ Score Calc     β”‚    β”‚ β€’ Dispute Resolutionβ”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                        β”‚                        β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                  β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚      Your Orchestrator     β”‚
                    β”‚                            β”‚
                    β”‚ β€’ Workflow Planning        β”‚
                    β”‚ β€’ Agent Selection          β”‚
                    β”‚ β€’ Task Execution           β”‚
                    β”‚ β€’ Payment Management       β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Getting Started

Prerequisites

Installation

  1. Navigate to the protocol directory:
cd protocol
  1. Install Foundry dependencies:
forge install foundry-rs/forge-std
  1. Compile contracts:
forge build
  1. Run tests:
forge test

Deployment

Local Development

# Start local blockchain (from any directory)
anvil

# Navigate to protocol directory and deploy contracts with test data
cd protocol
forge script script/Deploy.s.sol:DeployLocal --rpc-url http://localhost:8545 --private-key <your-private-key> --broadcast

Testnet/Mainnet

# Set environment variables
export PRIVATE_KEY=<your-private-key>
export RPC_URL=<network-rpc-url>

# Navigate to protocol directory and deploy
cd protocol
forge script script/Deploy.s.sol:Deploy --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify

πŸ“– Contract Documentation

AgentRegistry

Manages the registration and discovery of AI agents.

Key Functions:

  • registerAgent(): Register a new agent
  • getActiveAgents(): Get all active agents
  • getAgentsByType(): Filter agents by type (HTTP/MCP)
  • updateAgent(): Update agent information
  • deactivateAgent(): Temporarily disable an agent

Example Usage:

// Register an HTTP agent
registry.registerAgent(
    0x123..., // agent wallet
    "http://localhost:3001", // endpoint
    "ipfs://QmHash...", // metadata
    1 ether, // cost per task
    AgentRegistry.AgentType.HTTP
);

ReputationLayer

Tracks agent performance and calculates reputation scores.

Key Functions:

  • recordTaskCompletion(): Record task success/failure
  • rateAgent(): Rate an agent (1-5 stars)
  • getReputationData(): Get comprehensive reputation info
  • getTopAgents(): Get highest-rated agents

Reputation Scoring:

  • Success Rate: 40% weight (0-4000 points)
  • User Ratings: 40% weight (0-4000 points)
  • Response Time: 20% weight (0-2000 points)
  • Total Score: 0-10000 (10000 = perfect)

OrchestrationContract

Handles task creation, payments, and dispute resolution.

Task Lifecycle:

  1. PENDING β†’ Agent can accept the task
  2. EXECUTING β†’ Agent is working on the task
  3. COMPLETED β†’ Agent submitted results, payment released
  4. FAILED β†’ Task failed, payment refunded
  5. DISPUTED β†’ Orchestrator disputed results
  6. CANCELLED β†’ Task cancelled before execution

Key Functions:

  • createTask(): Create and fund a new task
  • acceptTask(): Agent accepts a task
  • submitTaskResult(): Agent submits completion
  • disputeTask(): Orchestrator disputes results
  • withdrawBalance(): Agent withdraws earnings

πŸ”Œ Integration with Your Orchestrator

1. Agent Discovery Integration

Replace your current discovery mechanism:

// Current: orchestrator/src/JobRunner.ts
async discoverAgents(): Promise<Agent[]> {
  // Query blockchain registry instead of HTTP endpoints
  const activeAgents = await this.agentRegistry.getActiveAgents();

  return activeAgents.map(agent => ({
    name: agent.agentUrl.split('/').pop(),
    description: `Agent at ${agent.agentUrl}`,
    url: agent.agentUrl,
    type: agent.agentType === 0 ? "http" : "mcp",
    wallet: agent.wallet,
    costPerTask: agent.baseCostPerTask,
    // ... other fields
  }));
}

2. Payment Integration

Add blockchain payment to task execution:

// orchestrator/src/JobRunner.ts
async executeAgentTask<TIn, TOut>(
  agentUrl: string,
  input: TIn
): Promise<TOut> {
  // 1. Get agent from registry
  const agent = await this.agentRegistry.getAgentByUrl(agentUrl);

  // 2. Create blockchain task with payment
  const taskId = await this.orchestrationContract.createTask(
    agent.wallet,
    keccak256(JSON.stringify(input)),
    Date.now() + 3600000, // 1 hour deadline
    { value: agent.baseCostPerTask }
  );

  // 3. Execute task off-chain
  const result = await this.executeOffChain(agentUrl, input);

  // 4. Submit result on-chain
  await this.orchestrationContract.submitTaskResult(
    taskId,
    keccak256(JSON.stringify(result))
  );

  return result;
}

3. Reputation Integration

Update reputation after task completion:

// The OrchestrationContract automatically updates reputation
// when submitTaskResult() or markTaskFailed() is called

πŸ§ͺ Testing

Run All Tests

forge test -vvv

Run Specific Test Contract

forge test --match-contract AgentRegistryTest -vvv

Run Fuzz Tests

forge test --fuzz-runs 10000

Test Coverage

forge coverage

πŸ“Š Example Workflow

  1. Agent Registration:
cast send $AGENT_REGISTRY "registerAgent(address,string,string,uint256,uint8)" \
  0x123... "http://localhost:3001" "ipfs://QmHash" 1000000000000000000 0
  1. Create Task:
cast send $ORCHESTRATION_CONTRACT "createTask(address,bytes32,uint256)" \
  --value 0.5ether 0x123... 0xabc... 1640995200
  1. Agent Accepts & Completes:
cast send $ORCHESTRATION_CONTRACT "acceptTask(bytes32)" $TASK_ID
cast send $ORCHESTRATION_CONTRACT "submitTaskResult(bytes32,bytes32)" $TASK_ID 0xdef...
  1. Agent Withdraws Payment:
cast send $ORCHESTRATION_CONTRACT "withdrawBalance()"

πŸ”’ Security Features

  • Access Control: Only authorized orchestrators can update reputation
  • Payment Escrow: Funds held in contract until task completion
  • Dispute Resolution: Neutral arbitrators can resolve conflicts
  • Agent Verification: Only registered, active agents can receive tasks
  • Deadline Enforcement: Automatic task failure after deadline

πŸ› οΈ Development

Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ AgentRegistry.sol           # Agent registration & discovery
β”‚   β”œβ”€β”€ ReputationLayer.sol         # Performance tracking
β”‚   └── OrchestrationContract.sol   # Task & payment management
β”œβ”€β”€ test/
β”‚   β”œβ”€β”€ AgentRegistry.t.sol         # Registry tests
β”‚   β”œβ”€β”€ ReputationLayer.t.sol       # Reputation tests
β”‚   └── OrchestrationContract.t.sol # Orchestration tests
β”œβ”€β”€ script/
β”‚   └── Deploy.s.sol                # Deployment scripts
└── foundry.toml                    # Foundry configuration

Contract Addresses (after deployment)

AgentRegistry: 0x...
ReputationLayer: 0x...
OrchestrationContract: 0x...

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

πŸ“„ License

MIT License - see LICENSE file for details

πŸ†˜ Support

For integration questions or issues:

  • Create an issue in this repository
  • Check the test files for usage examples
  • Review the contract documentation above

πŸ”— Blockscout Integration

This project seamlessly integrates with Blockscout to provide a rich and detailed view of on-chain activities, leveraging its robust APIs and SDK for enhanced user experience and real-time transaction feedback. The smooth integration allows for easy addition of UI components, with potential for more customizable widgets in the future.


Ready to decentralize your AI agent orchestration! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages