๐ก๏ธ Aegis Protocol - AI Agents as a Service
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:
- AgentNFT (ERC-721): Represents ownership of AI agents
- 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)
- Start a local Hardhat node:
npm run node
- In a new terminal, deploy contracts:
npm run deploy:local
Polygon Testnet (Amoy)
Get test MATIC from Polygon Faucet
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.tshelpers 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 agentrent(uint256 tokenId, uint256 durationInSeconds): Rent an agentisRentalActive(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
- Creator deploys an agent: Agent NFT is minted with metadata stored on IPFS
- Creator sets rental terms: Price per second is configured
- User rents the agent: Payment goes directly to creator
- User purchases credits: Off-chain via the x402 facilitator gateway
- User interacts with agent: Facilitator deducts credits per interaction
- 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
- Private Keys: Never commit private keys. Use environment variables
- Gateway Security: Host the x402 facilitator behind authenticated infrastructure with rate limiting and monitoring
- Nonces: The legacy contract enforced nonce-based replay protection; retain similar safeguards in the facilitator
- Payments: All rental payments go directly to creators (no intermediary risk)
- 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:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - 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
Log in or sign up for Devpost to join the conversation.