๐Ÿ›ก๏ธ Aegis Protocol - AI Agents as a Service

Solidity Hardhat Polygon License: MIT

Aegis Protocol is a decentralized "AI Agents as a Service" (AaaS) platform built on Polygon. It enables creators to deploy and monetize AI agents through a rental model, while users can access these agents on-demand with low-cost transactions.

๐Ÿ—๏ธ Architecture

The protocol deploys two primary smart contracts on Polygon:

  1. AgentNFT (ERC-721): Represents ownership of AI agents
  2. RentalContract: Manages agent rental logic and on-chain rental payments

Pre-paid x402 credits are now administered by an off-chain facilitator gateway. The X402CreditContract remains in the repository as a deprecated stub to prevent accidental deployment while backwards compatibility is phased out.

๐Ÿ“‹ Prerequisites

  • Node.js v20.x or higher
  • npm or yarn
  • MetaMask or any Web3 wallet
  • MATIC tokens for deployment and transactions

๐Ÿš€ Quick Start

1. Clone the Repository

git clone https://github.com/your-org/aegis-protocol.git
cd aegis-protocol/AegisContracts

2. Install Dependencies

npm install

3. Configure Environment

Create a .env file based on .env.example:

cp .env.example .env

Edit .env with your configuration:

# Network RPC URLs
POLYGON_AMOY_RPC_URL=https://rpc-amoy.polygon.technology
POLYGON_MAINNET_RPC_URL=https://polygon-rpc.com

# Your wallet private key (NEVER commit this!)
PRIVATE_KEY=0x...

# Polygonscan API key for contract verification
POLYGONSCAN_API_KEY=YOUR_API_KEY

4. Compile Contracts

npm run compile

5. Run Tests

npm test

All tests should pass with output similar to:

  Aegis Protocol
    โœ“ AgentNFT tests (4 passing)
    โœ“ RentalContract tests (6 passing)
    โœ“ End-to-End Flow (1 passing)

  11 passing (~0.4s)

๐ŸŒ Deployment

Local Network (Hardhat)

  1. Start a local Hardhat node:
npm run node
  1. In a new terminal, deploy contracts:
npm run deploy:local

Polygon Testnet (Amoy)

  1. Get test MATIC from Polygon Faucet

  2. Deploy to Amoy:

npm run deploy:amoy

Polygon Mainnet

โš ๏ธ Warning: This will deploy to mainnet and cost real MATIC!

npx hardhat run scripts/deploy.ts --network polygon

๐Ÿ“ Contract Verification

After deployment, verify your contracts on Polygonscan:

npx hardhat verify --network <network> <CONTRACT_ADDRESS> [constructor args]

Example:

npx hardhat verify --network amoy 0x123... 
npx hardhat verify --network amoy 0x456... "0x123..."
npx hardhat verify --network amoy 0x789... "1000000000000000"

๐Ÿ”ง Contract Interaction

Use the provided interaction scripts to work with deployed contracts:

Mint an Agent NFT

npx hardhat run scripts/interact.ts --network localhost -- mint <creator_address> <token_uri>

Set Rental Price

npx hardhat run scripts/interact.ts --network localhost -- set-price <token_id> <price_per_second>

Rent an Agent

npx hardhat run scripts/interact.ts --network localhost -- rent <token_id> <duration_seconds>

โ„น๏ธ Credits now off-chain: Use the x402 facilitator REST gateway to purchase or inspect inference credits. See the frontend/lib/rentals.ts helpers for reference client calls.

๐Ÿ“œ Smart Contract Documentation

AgentNFT

ERC-721 token representing AI agent ownership.

Key Functions:

  • mintAgent(address creator, string memory tokenURI): Mint a new agent NFT
  • Standard ERC-721 functions (transfer, approve, etc.)

RentalContract

Manages agent rentals and payments.

Key Functions:

  • setRentalPrice(uint256 tokenId, uint256 pricePerSecond): Set rental price for an agent
  • rent(uint256 tokenId, uint256 durationInSeconds): Rent an agent
  • isRentalActive(uint256 tokenId, address user): Check if rental is active

X402CreditContract (deprecated)

This on-chain credit contract has been superseded by an off-chain facilitator. The Solidity file remains only as a revert-only placeholder to block unintended deployments while the new architecture rolls out.

๐Ÿ”„ End-to-End Flow

  1. Creator deploys an agent: Agent NFT is minted with metadata stored on IPFS
  2. Creator sets rental terms: Price per second is configured
  3. User rents the agent: Payment goes directly to creator
  4. User purchases credits: Off-chain via the x402 facilitator gateway
  5. User interacts with agent: Facilitator deducts credits per interaction
  6. Rental expires: After duration, rental becomes inactive

๐Ÿงช Testing

Run the full test suite:

npm test

Run with coverage:

npx hardhat coverage

Run specific tests:

npx hardhat test test/AegisProtocol.test.ts

๐Ÿ“Š Gas Reports

Enable gas reporting by setting in hardhat.config.ts:

gasReporter: {
  enabled: true,
  currency: 'USD',
  gasPrice: 30, // Polygon gas price in gwei
}

Then run tests:

npm test

๐Ÿ”’ Security Considerations

  1. Private Keys: Never commit private keys. Use environment variables
  2. Gateway Security: Host the x402 facilitator behind authenticated infrastructure with rate limiting and monitoring
  3. Nonces: The legacy contract enforced nonce-based replay protection; retain similar safeguards in the facilitator
  4. Payments: All rental payments go directly to creators (no intermediary risk)
  5. Audits: Consider professional audits before mainnet deployment

๐Ÿ› ๏ธ Development

Project Structure

AegisContracts/
โ”œโ”€โ”€ contracts/          # Smart contracts
โ”‚   โ”œโ”€โ”€ AgentNFT.sol
โ”‚   โ”œโ”€โ”€ RentalContract.sol
โ”‚   โ””โ”€โ”€ X402CreditContract.sol (deprecated placeholder)
โ”œโ”€โ”€ scripts/           # Deployment and interaction scripts
โ”‚   โ”œโ”€โ”€ deploy.ts
โ”‚   โ””โ”€โ”€ interact.ts
โ”œโ”€โ”€ test/              # Test files
โ”‚   โ””โ”€โ”€ AegisProtocol.test.ts
โ”œโ”€โ”€ deployments/       # Deployment artifacts (auto-generated)
โ””โ”€โ”€ hardhat.config.ts  # Hardhat configuration

Common Commands

npm run compile       # Compile contracts
npm test             # Run tests
npm run node         # Start local node
npm run deploy:local # Deploy to local network
npm run clean        # Clean artifacts

๐ŸŒŸ Features

  • โœ… Fully On-Chain: All state managed by smart contracts
  • โœ… Direct Payments: Creators receive payments instantly
  • โœ… Replay Protection: Nonce-based security for credit spending
  • โœ… Gas Optimized: Optimized for Polygon's low fees
  • โœ… ERC-721 Compliant: Standard NFT interface for agents
  • โœ… Flexible Pricing: Creators set their own rental rates
  • โœ… Credit System: Off-chain facilitator provides prepaid credits without extra gas

๐Ÿค Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ”— Links

โš ๏ธ Disclaimer

This software is provided "as is", without warranty of any kind. Use at your own risk. Always audit smart contracts before deploying to mainnet.


Built with โค๏ธ by the Aegis Protocol Team

Share this project:

Updates