Smart Contracts
Alfred402's autonomous payment system is powered by three core smart contracts that work together to enable secure, transparent, and autonomous transactions.
Contract Overview
┌─────────────────────────────────────┐
│ Alfred402 Ecosystem │
├─────────────────────────────────────┤
│ User Interface (Next.js App) │
├─────────────────────────────────────┤
│ AI Agent (Google Gemini) │
├─────────────────────────────────────┤
│ Smart Contracts │
│ ┌───────────────────────────────┐ │
│ │ PaymentGateway.sol │ │
│ │ - Payment processing │ │
│ │ - Escrow & release │ │
│ └───────────────────────────────┘ │
│ ┌───────────────────────────────┐ │
│ │ SpendingLimits.sol │ │
│ │ - Limit enforcement │ │
│ │ - Spending tracking │ │
│ └───────────────────────────────┘ │
│ ┌───────────────────────────────┐ │
│ │ X402Registry.sol │ │
│ │ - Service discovery │ │
│ │ - Provider registry │ │
│ └───────────────────────────────┘ │
├─────────────────────────────────────┤
│ Blockchain (Ethereum/Solana/etc) │
└─────────────────────────────────────┘1. PaymentGateway.sol
Purpose
Handles all payment processing, escrow, and fund transfers.
Key Features
Native token and ERC20 support
Payment escrow and release
Refund mechanism
Transaction tracking
Core Functions
Data Structures
Events
2. SpendingLimits.sol
Purpose
Enforces user-defined spending limits to prevent overspending.
Key Features
Daily/weekly/monthly limits
Automatic time-based resets
Approved spender management
Real-time limit checking
Core Functions
Data Structures
Events
3. X402Registry.sol
Purpose
Central registry for x402-compatible services and their pricing.
Key Features
Service registration
Multi-tier pricing
Service verification
Statistics tracking
Core Functions
Data Structures
Events
Contract Interactions
Payment Flow
Limit Enforcement Flow
Security Features
Access Control
Ownable: Admin functionsapprovedSpenders: Authorized agentsReentrancyGuard: Prevent reentrancy attacks
Input Validation
Non-zero addresses
Positive amounts
Valid limit hierarchies
Service existence checks
State Management
Payment completion flags
Spending trackers
Timestamp validations
Limit resets
Gas Optimization
Efficient Storage
Pack structs to save slots
Use
uint256for countersMinimize SLOAD operations
Batch Operations
Coming soon: Batch payments
Coming soon: Batch limit updates
Upgradeability
Current Status
Contracts are not upgradeable for security.
Future Plans
Consider proxy pattern with:
Timelock governance
Multi-sig approval
Community voting
Testing
Each contract has 30+ tests covering:
✅ Normal operations
✅ Edge cases
✅ Access control
✅ Error handling
✅ Gas optimization
✅ Integration scenarios
See /contracts/test for full test suite.
Deployment
Networks Supported
Ethereum Mainnet
Polygon
Base
Arbitrum
Optimism
Solana (adapted version)
Deployment Process
Contract Addresses
Mainnet
Testnet (Sepolia)
Audits
Status
⏳ Security audit in progress
Target: Q1 2025
Auditors
Trail of Bits (planned)
OpenZeppelin (planned)
Source Code
All contracts are open source:
GitHub: github.com/alfred402/alfred402
License: MIT
Folder:
/contracts
Learn More
Questions? Email [email protected]
Last updated
