Ai Trading Agent

Conversational AI Trading Agent (EthAI) for instant multi-chain token swaps with Gemini AI.

Ai Trading Agent

Created At

ETHGlobal New Delhi

Project Description

The EthAI project is a full-stack AI Trading Agent application designed to provide a conversational, user-friendly interface for interacting with decentralized finance (DeFi) on the blockchain.

The core mission of EthAI is to abstract away the complexity of traditional crypto wallet management and token swapping by using Natural Language Processing (NLP) to understand and execute trades.

Key Components and Functionality

  1. Conversational AI Trading Agent Gemini AI Integration: The agent is powered by Gemini AI (specifically the gemini-1.5-flash model), which is used to parse and interpret natural language requests from the user, such as "swap 5 ETH for USDC".

Command Parsing: The AI system extracts critical trade parameters, including the action (swap), the amount, and the fromToken/toToken symbols.

Swap Execution: The agent interacts with the blockchain to get real-time price quotes and execute trades. It uses a dedicated Uniswap V3 Router address on the Polygon network for liquidity routing and best price execution.

  1. Secure Wallet Management Email-Based Authentication: Users sign up using standard email and password authentication provided by Supabase.

Automatic Wallet Generation: Upon sign-up, the system automatically generates a new Ethereum HD (Hierarchical Deterministic) wallet.

Encrypted Key Storage: The user's private key is immediately encrypted using their personal password combined with an application-specific encryption key, ensuring that only the user can decrypt and access their funds. The encrypted key and wallet address are stored in the user_wallets database table (with Row Level Security enabled for protection).

Full Control: The user is provided with the wallet address and private key upon creation for full, non-custodial ownership.

  1. Blockchain Network and Tokens Primary Network: The application is configured for the Polygon Mainnet (Chain ID 137).

Token Support: The application supports popular tokens on Polygon, including POL (the updated name for MATIC), WPOL, USDC, USDT, WETH, WBTC, DAI, and LINK.

  1. Technical Stack EthAI is a modern web application built with:

Frontend: React and TypeScript with a premium UI styled using Tailwind CSS and animations via Framer Motion.

Web3 Libraries: Ethers.js for wallet generation, transaction signing, and blockchain interaction (like balance fetching).

Database/Auth: Supabase provides the backend, handling user authentication and encrypted wallet storage.

Data Feeds: Integrates with Polygonscan (via an API key) to retrieve recent transaction history and a custom PriceService (mocking or integrating with APIs like CoinGecko) for real-time token prices and portfolio valuation.

The dashboard features dedicated sections for the AI Agent Chat, a Token Portfolio Management view showing real-time balances, and a Manage Wallet section for key management and transaction history.

How it's Made

The EthAI project is built as a complete full-stack AI Trading Agent designed to dramatically simplify decentralized finance (DeFi) interactions by executing complex blockchain operations through intuitive natural language commands. The development stack starts with a modern, high-performance single-page application built on React, TypeScript, and Vite, providing a foundation for type-safe and efficient code. The application's premium user interface, featuring a futuristic, glassmorphism design, is styled using Tailwind CSS and brought to life with fluid animations powered by Framer Motion.

At its core, the project leverages Google Gemini AI (specifically the gemini-1.5-flash model) to handle its conversational interface and Natural Language Processing (NLP). Gemini AI is essential for parsing user inputs like "swap 5 ETH for USDC" into a structured JSON object, allowing the app to isolate the precise trade parameters. The execution layer relies on Ethers.js for foundational Web3 tasks, such as generating secure HD wallets, connecting to the Polygon network's RPC endpoints, and signing the final swap transactions. Trade routing and quoting are managed by a custom service that interacts with the Uniswap V3 Router address on the Polygon network to ensure optimal liquidity and best execution prices for the specified tokens (including support for the native POL token).

The backend infrastructure and authentication are provided by Supabase, which acts as a key partner technology. Supabase manages all user authentication (Sign Up/Sign In) and serves as the database to securely store users' encrypted wallet information. A core security design involves immediately encrypting the wallet's private key using crypto-js with the user's password and a secret application key, which is then stored in the Supabase database under Row Level Security (RLS). For portfolio visibility, the project integrates with the Polygonscan API to fetch a user's recent transaction history and uses a dedicated PriceService to simulate or fetch real-time token prices, enabling accurate USD portfolio valuation on the dashboard.

A few notable and ingenious implementations were used to enhance the project. Most notably, a seamless wallet locking mechanism is implemented in the AuthContext. Upon login, the wallet is only partially loaded with a placeholder for the private key, which is marked as encrypted, compelling the user to re-enter their password in the WalletSetup.tsx modal to explicitly decrypt and unlock the private key for trading. This prevents automated or unauthorized use of the trading funds right after sign-in. Additionally, the project includes a dedicated service (src/lib/uniswapV4.ts) to mock the logic for Uniswap V4 (which is not yet deployed on Polygon), where the actual execution function throws a controlled development error, allowing the complete trade simulation flow to be demonstrated in the chat interface as a future-proof feature. Finally, for chat resilience, the GeminiAI service includes a fallback parser that uses regex to process basic swap commands if the primary Gemini API call encounters an error, ensuring a minimal level of usability at all times.

background image mobile

Join the mailing list

Get the latest news and updates