AI Trading Agent Development for Lighter Protocol

Abhishek

Abhishek Thakur

AI Trading Agent

An intelligent trading agent that uses AI models to make automated trading decisions on the Lighter Protocol. The system includes a backend API, trading-agent dashboard, and AI-powered trading logic.

Prerequisites

Bun runtime
PostgreSQL database
OpenRouter API account
Lighter Protocol API keys

Setup Instructions

1. Install Dependencies

# Install backend dependencies
bun install

# Install trading-agent dependencies
cd trading-agent
npm install
cd ..

2. Environment Variables

Create or update the .env file in the root directory with the following variables:
# Database Configuration
DATABASE_URL="postgresql://username:password@localhost:5432/ai_trading_agent"

# OpenRouter API Key (Required)
OPENROUTER_API_KEY="your_openrouter_api_key_here"

3. Required API Keys

OpenRouter API Key
Sign up at OpenRouter
Generate an API key from your dashboard
Replace your_openrouter_api_key_here in the .env file
Database Setup
Install and start PostgreSQL
Create a database named ai_trading_agent
Update the DATABASE_URL with your actual database credentials
Run Prisma migrations:
bunx prisma migrate dev
bunx prisma generate
Lighter Protocol API Keys
Create API keys for your trading accounts
Note the API key index (typically starts from 0, 1, 2, etc.)
Store these keys in the database through the Models table (see Database Configuration below)

4. Database Configuration

The trading agents are configured through the Models table in the database. Each model requires:
name: Unique identifier for the trading agent
openRoutermodelName: The AI model name from OpenRouter (e.g., "anthropic/claude-3.5-sonnet")
lighterApiKey: Your Lighter Protocol private API key
accountIndex: Your Lighter Protocol account index
invocationCount: Number of times the agent has been invoked (starts at 0)
Example database entry:
INSERT INTO "Models" (id, name, "openRoutermodelName", "lighterApiKey", "accountIndex", "invocationCount")
VALUES (
gen_random_uuid(),
'claude-trader-1',
'anthropic/claude-3.5-sonnet',
'your_lighter_private_key_here',
'0',
0
);

5. Configuration Files

Trading Configuration
Update config/trading.config.ts if needed:
API_KEY_INDEX: Set to match your Lighter Protocol API key index (default: 2)
BASE_URL: Lighter Protocol API endpoint (default: mainnet)
Market Configuration
The config/markets.config.ts contains supported trading pairs and market information.

Running the Application

Start the Backend API Server

bun run api/server.ts
The API server will start on http://localhost:3000

Start the trading-agent Dashboard

cd trading-agent
npm run dev
The trading-agent will start on http://localhost:5173

Run the Trading Agent

bun run index.ts
This starts the AI trading agent that will execute trades based on the configured models.

API Endpoints

GET /performance - Get portfolio performance data
GET /invocations - Get recent AI model invocations and trading decisions

Project Structure

├── api/                 # Backend API server
├── config/ # Configuration files
├── trading-agent/ # React trading-agent dashboard
├── services/ # Trading and market data services
├── types/ # TypeScript type definitions
├── utils/ # Utility functions
├── prisma/ # Database schema and migrations
└── lighter-sdk-ts/ # Lighter Protocol SDK

Security Notes

Never commit API keys to version control
Store sensitive keys in environment variables or secure key management
Use different API keys for development and production
Regularly rotate your API keys
Monitor your trading activity and set appropriate limits

Troubleshooting

Common Issues

Database Connection Error: Verify your PostgreSQL is running and DATABASE_URL is correct
OpenRouter API Error: Check your API key and account credits
Lighter Protocol Error: Verify your API keys and account permissions
Import Path Errors: Run bunx prisma generate to regenerate the Prisma client

Logs and Monitoring

Check the console output for trading decisions and API calls
Monitor the trading-agent dashboard for performance metrics
Review database logs for any data inconsistencies

Development

This project was created using bun init and uses Bun as the JavaScript runtime for optimal performance.
Like this project

Posted Oct 31, 2025

Developed an AI trading agent for Lighter Protocol using AI models.