Agent
defai agent
About this agent
DefAI Agent – The Ultimate AI for DeFi Asset Management on Ethereum
DefAI Agent is an AI-powered DeFi automation tool that manages, trades, and optimizes your ETH, USDC, and USDT holdings on Uniswap V3. Designed for efficient and secure asset management, it monitors real-time market trends, executes trades, and maximizes profitability—fully automated.
🚀 Trade Smarter. Earn More. Automate DeFi with DefAI Agent!
defi
agent
management
Use Cases
defi
Defi agent
Requirements
Package | Installation |
---|---|
mesa | pip install mesa |
pymoo | pip install pymoo |
Agent Code
The main implementation code for this agent. You can view, copy, and use this code directly in your projects.
Agent Metadata (JSON)
All metadata and code for this agent, as a JSON object. Useful for programmatic use, export, or debugging.
{ "id": "bf169321-0c7c-4a7d-ad6c-ecb7cc72da2b", "name": "defai agent", "title": "Agent", "description": "DefAI Agent – The Ultimate AI for DeFi Asset Management on Ethereum\n\nDefAI Agent is an AI-powered DeFi automation tool that manages, trades, and optimizes your ETH, USDC, and USDT holdings on Uniswap V3. Designed for efficient and secure asset management, it monitors real-time market trends, executes trades, and maximizes profitability—fully automated.\n\n🚀 Trade Smarter. Earn More. Automate DeFi with DefAI Agent!", "tags": [ "defi", "agent", "management" ], "requirements": [ { "package": "mesa", "installation": "pip install mesa" }, { "package": "pymoo", "installation": "pip install pymoo" } ], "usecases": [ { "title": "defi", "description": "Defi agent" } ], "userId": "61dabdc7-92e4-4cad-a40a-28cf836a7ca2", "createdAt": "2025-02-07T14:42:26.86636+00:00", "links": [], "code": "import json\nimport os\nimport numpy as np\nimport time\nimport requests\nfrom web3 import Web3\nfrom uniswap import Uniswap\nfrom eth_account import Account\nfrom dotenv import load_dotenv\n\n# Load environment variables\nload_dotenv()\n\n# Configuration\nINFURA_URL = os.getenv(\"INFURA_URL\") # Infura or Alchemy RPC URL\nPRIVATE_KEY = os.getenv(\"PRIVATE_KEY\") # Wallet Private Key (Keep Secure!)\nWALLET_ADDRESS = os.getenv(\"WALLET_ADDRESS\") # Public Wallet Address\nUNISWAP_V3_ROUTER = \"0xE592427A0AEce92De3Edee1F18E0157C05861564\" # Uniswap V3 Router Address\nETH_USDC_POOL = \"0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8\" # Example Uniswap V3 Pool\nSLIPPAGE = 0.005 # 0.5% slippage\n\n# ERC-20 Token Addresses (Mainnet)\nUSDC = Web3.to_checksum_address(\"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48\")\nUSDT = Web3.to_checksum_address(\"0xdAC17F958D2ee523a2206206994597C13D831ec7\")\nWETH = Web3.to_checksum_address(\"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2\")\n\n# Initialize Web3 & Uniswap\nweb3 = Web3(Web3.HTTPProvider(INFURA_URL))\nuniswap = Uniswap(address=WALLET_ADDRESS, private_key=PRIVATE_KEY, version=3, web3=web3)\n\n# Ensure connection\nif not web3.is_connected():\n raise ConnectionError(\"Failed to connect to Ethereum network\")\n\nprint(f\"Connected to Ethereum, wallet: {WALLET_ADDRESS}\")\n\ndef get_token_price(token_in, token_out, amount_in):\n \"\"\" Fetch token price from Uniswap V3 \"\"\"\n try:\n price = uniswap.get_price_input(token_in, token_out, amount_in)\n return price\n except Exception as e:\n print(f\"Error fetching price: {e}\")\n return None\n\ndef get_balance(token_address):\n \"\"\" Get ERC-20 token balance \"\"\"\n balance = uniswap.get_balance(token_address)\n return web3.from_wei(balance, 'ether')\n\ndef execute_trade(token_in, token_out, amount):\n \"\"\" Execute a trade on Uniswap V3 \"\"\"\n try:\n tx = uniswap.make_trade(token_in, token_out, amount)\n print(f\"Trade executed: {tx}\")\n return tx\n except Exception as e:\n print(f\"Trade failed: {e}\")\n return None\n\ndef ai_decision():\n \"\"\" AI strategy to decide when to swap assets \"\"\"\n usdc_balance = get_balance(USDC)\n usdt_balance = get_balance(USDT)\n eth_balance = get_balance(WETH)\n\n print(f\"Balances - USDC: {usdc_balance}, USDT: {usdt_balance}, ETH: {eth_balance}\")\n\n # Get ETH price in USDC\n eth_price = get_token_price(WETH, USDC, web3.to_wei(1, 'ether'))\n\n if eth_price is None:\n print(\"Could not fetch ETH price, skipping trade\")\n return\n\n print(f\"Current ETH price: {eth_price} USDC\")\n\n # Simple AI strategy: Buy ETH if price drops significantly (example logic)\n if eth_price < 2800 and usdc_balance > 100: # Buy ETH if price is low\n print(\"Buying ETH with USDC\")\n execute_trade(USDC, WETH, web3.to_wei(50, 'ether'))\n\n # Sell ETH if price is high\n if eth_price > 3500 and eth_balance > 0.1:\n print(\"Selling ETH for USDC\")\n execute_trade(WETH, USDC, web3.to_wei(0.1, 'ether'))\n\ndef main():\n \"\"\" Main loop for AI trading bot \"\"\"\n while True:\n ai_decision()\n time.sleep(60) # Check market every minute\n\nif __name__ == \"__main__\":\n main()" }
Comments & Discussion
Items You'd Like
Check out similar agents that match your interests