AI Agent Payment Contract

What it does:
Automates payments to AI agents or services based on usage, subscription, or task completion, ensuring transparent and secure compensation for AI providers.

Why it matters:
Removes intermediaries, prevents disputes over service usage, ensures fair payment for AI services, and allows programmable compensation models.

How it works:

  • Users register AI agents with metadata, pricing, and usage terms

  • Payments are sent to the contract based on service usage or subscriptions

  • Smart contract releases funds to AI providers automatically upon task completion

  • Supports recurring payments, per-call fees, or milestone-based payments

  • Integrates with Creator Subscription Wallet, Play-to-Earn Reward Engine, or Virtual Asset Management

  • Dashboards track service usage, pending payments, and completed transactions

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

import "@openzeppelin/contracts/access/Ownable.sol";

/**
 * @title AIAgentPayment
 * @author Nam
 * @notice Automates payments to AI agents based on usage or subscription on-chain
 */
contract AIAgentPayment is Ownable {

    struct Agent {
        string name;
        address payable provider;
        uint256 pricePerCall; // in wei
        bool active;
    }

    mapping(uint256 => Agent) public agents;
    uint256 public agentCount;

    mapping(uint256 => uint256) public usageCount; // agentId => number of calls

    // -------------------- EVENTS --------------------

    event AgentRegistered(uint256 indexed agentId, string name, address provider, uint256 pricePerCall);
    event AgentUsagePaid(uint256 indexed agentId, address user, uint256 amount);
    event AgentDeactivated(uint256 indexed agentId);

    // -------------------- AGENT MANAGEMENT --------------------

    function registerAgent(string calldata _name, uint256 _pricePerCall) external {
        require(_pricePerCall > 0, "Price must be >0");

        agentCount += 1;
        agents[agentCount] = Agent({
            name: _name,
            provider: payable(msg.sender),
            pricePerCall: _pricePerCall,
            active: true
        });

        emit AgentRegistered(agentCount, _name, msg.sender, _pricePerCall);
    }

    function deactivateAgent(uint256 _agentId) external {
        Agent storage a = agents[_agentId];
        require(msg.sender == a.provider, "Not provider");
        a.active = false;

        emit AgentDeactivated(_agentId);
    }

    // -------------------- PAYMENT MANAGEMENT --------------------

    function useAgent(uint256 _agentId) external payable {
        Agent storage a = agents[_agentId];
        require(a.active, "Agent inactive");
        require(msg.value == a.pricePerCall, "Incorrect payment");

        usageCount[_agentId] += 1;
        a.provider.transfer(msg.value);

        emit AgentUsagePaid(_agentId, msg.sender, msg.value);
    }

    // -------------------- VIEW FUNCTIONS --------------------

    function getAgentUsage(uint256 _agentId) external view returns (uint256) {
        return usageCount[_agentId];
    }

    function isAgentActive(uint256 _agentId) external view returns (bool) {
        return agents[_agentId].active;
    }
}