Build Your On-Chain AI Agent

Complete Step-by-Step Tutorial: On-chain AI Agent

This comprehensive tutorial will guide you through building an AI Agent that can handle both general conversations and blockchain operations, specifically token balance checking on the LazAI testnet.

Prerequisites

Before starting, ensure you have:

  • Node.js 18+ installed

  • npm or yarn package manager

  • Basic knowledge of React, TypeScript, and Next.js

  • OpenAI API key (for AI conversations)

  • Code editor (VS Code recommended)

Project Setup

Step 1: Create Next.js Project

# Create a new Next.js project with TypeScript
npx create-next-app@latest ai-agent --typescript --tailwind --eslint --app --src-dir=false --import-alias="@/*"

# Navigate to the project directory
cd ai-agent

Step 2: Verify Project Structure

Your project should look like this:

Dependencies Installation

Step 3: Install Required Packages

What each package does:

  • ethers: Ethereum library for blockchain interactions

  • alith: AI SDK for OpenAI integration

  • node-loader: Webpack loader for native modules

Next.js Configuration

Step 4: Configure next.config.ts

Create or update next.config.ts:

Why this configuration is needed:

  • Handles native modules that can't be bundled by webpack

  • Prevents client-side bundling of server-only packages

  • Ensures proper module resolution

Token Balance API

Step 5: Create API Directory Structure

Step 6: Create Token Balance API

Create app/api/token-balance/route.ts:

Key Features:

  • Validates Ethereum addresses

  • Handles BigInt serialization

  • Provides fallback values for missing token data

  • Comprehensive error handling

  • Returns both token and native currency balances

Step 7: Create Smart Chat API

Create app/api/chat/route.ts:

Key Features:

  • Smart message routing based on content analysis

  • Pattern recognition for balance requests

  • Automatic address extraction

  • Enhanced AI prompts with blockchain context

  • Comprehensive error handling

Chat Interface Component

Step 8: Create Chat Interface

Create app/components/ChatInterface.tsx:

Key Features:

  • Real-time message updates

  • Markdown rendering for formatted responses

  • Auto-scroll to latest messages

  • Loading indicators

  • Responsive design

  • Feature showcase for new users

Environment Setup

Step 9: Configure Environment Variables

Create a .env.local file in your project root:

Step 10: Get OpenAI API Key

  1. Sign up or log in to your account

  2. Navigate to "API Keys" in the sidebar

  3. Click "Create new secret key"

  4. Copy the generated key

  5. Paste it in your .env.local file

Security Note: Never commit your API key to version control!

Testing

Step 11: Start Development Server

Step 12: Test General Chat

Try these general questions:

  • "What is blockchain technology?"

  • "How does cryptocurrency work?"

  • "Tell me a joke"

  • "What are the benefits of decentralized systems?"

Step 13: Test Token Balance Checking

Try these formats:

  • "Check token balance for contract 0x1234567890123456789012345678901234567890 and wallet 0x0987654321098765432109876543210987654321"

  • "What's the balance of token 0x... in wallet 0x..."

  • "Check balance: contract 0x... wallet 0x..."

Note: You'll need valid contract and wallet addresses on the LazAI testnet for this to work.

Additional Resources

Documentation

Last updated