go-ethereum/ai-defi-agent/contracts/MockERC20.sol
Claude 97e3564448
Add AI Agent for Autonomous DeFi Trading prototype
This commit introduces a production-inspired prototype for autonomous DeFi
trading that combines off-chain AI intelligence with on-chain execution.

Key Components:

1. Off-Chain AI Planner (Python + PyTorch)
   - Q-learning neural network for trading decisions
   - Reinforcement learning from simulated/real price data
   - Three actions: Buy, Hold, Sell
   - Configurable learning parameters (learning rate, gamma, epsilon)

2. On-Chain Execution Layer (Solidity)
   - SimpleDEX: Secure DEX contract for ETH <-> Token swaps
   - MockERC20: Test token for development
   - Security features: daily limits, min/max constraints, rate checks
   - Emergency controls and liquidity management

3. Blockchain Integration (Web3.py)
   - Connects AI decisions to blockchain execution
   - Transaction signing and submission
   - Balance tracking and price monitoring
   - Gas optimization and retry logic

4. Hybrid Architecture
   - AI planning happens off-chain for flexibility
   - All trades executed on-chain for transparency
   - Separation of concerns: perception, planning, action
   - Production-ready patterns inspired by real DeFi agents

Features:
- Multi-mode operation: Monitor (watch), Paper (simulate), Live (execute)
- Risk management: stop-loss, take-profit, daily limits
- Comprehensive configuration system
- Model persistence (save/load trained agents)
- Real-time performance monitoring

Structure:
ai-defi-agent/
├── contracts/          # Solidity smart contracts
│   ├── SimpleDEX.sol   # DEX implementation
│   └── MockERC20.sol   # Test token
├── src/                # Python source code
│   ├── ai_agent.py     # Q-learning agent
│   ├── blockchain_connector.py  # Web3 integration
│   ├── config.py       # Configuration
│   └── trading_agent.py  # Main orchestrator
├── tests/              # Unit tests
├── README.md           # Comprehensive documentation
├── DEPLOYMENT.md       # Deployment guide
├── demo.py            # Interactive demo
└── requirements.txt    # Python dependencies

This prototype demonstrates hybrid AI-blockchain architecture suitable for
educational purposes and can be extended for production use with proper
security audits, oracle integration, and multi-sig controls.
2026-01-10 22:27:58 +00:00

101 lines
2.9 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title MockERC20
* @dev Simple ERC20 token for testing the AI DeFi trading agent
* Represents a stablecoin-like asset (e.g., mock USDC)
*/
contract MockERC20 {
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Mint(address indexed to, uint256 amount);
event Burn(address indexed from, uint256 amount);
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals,
uint256 _initialSupply
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
totalSupply = _initialSupply;
balanceOf[msg.sender] = _initialSupply;
emit Transfer(address(0), msg.sender, _initialSupply);
}
function transfer(address to, uint256 amount) external returns (bool) {
require(to != address(0), "Invalid recipient");
require(balanceOf[msg.sender] >= amount, "Insufficient balance");
balanceOf[msg.sender] -= amount;
balanceOf[to] += amount;
emit Transfer(msg.sender, to, amount);
return true;
}
function approve(address spender, uint256 amount) external returns (bool) {
require(spender != address(0), "Invalid spender");
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool) {
require(from != address(0), "Invalid sender");
require(to != address(0), "Invalid recipient");
require(balanceOf[from] >= amount, "Insufficient balance");
require(allowance[from][msg.sender] >= amount, "Insufficient allowance");
balanceOf[from] -= amount;
balanceOf[to] += amount;
allowance[from][msg.sender] -= amount;
emit Transfer(from, to, amount);
return true;
}
/**
* @dev Mint new tokens (for testing/liquidity)
*/
function mint(address to, uint256 amount) external {
require(to != address(0), "Invalid recipient");
totalSupply += amount;
balanceOf[to] += amount;
emit Mint(to, amount);
emit Transfer(address(0), to, amount);
}
/**
* @dev Burn tokens
*/
function burn(uint256 amount) external {
require(balanceOf[msg.sender] >= amount, "Insufficient balance");
totalSupply -= amount;
balanceOf[msg.sender] -= amount;
emit Burn(msg.sender, amount);
emit Transfer(msg.sender, address(0), amount);
}
}