Project Setup
This section covers the initial project setup, dependency installation, and directory structure for your trading agent.
Initialize Your Project
# Create project directory
mkdir trading-agent && cd trading-agent
# Initialize with pnpm
pnpm initPackage Configuration
Update your package.json with the following scripts to build, run, and format your trading agent:
{
"name": "trading-agent",
"version": "1.0.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc", // Compile TypeScript to JavaScript
"start": "tsc && node dist/src/index.js", // Build and run the agent
"format": "prettier --write . --ignore-path .prettierignore", // Auto-format code
"install:all": "pnpm install" // Install all dependencies
}
}Install Dependencies
Now install the required dependencies:
# Install runtime dependencies
pnpm install fuels@0.102.0 axios ccxt decimal.js pino pino-pretty yaml ethers
# Install dev dependencies
pnpm install -D typescript @types/node prettier ts-nodeConfigure TypeScript
Create tsconfig.json:
touch tsconfig.jsonAdd the following TypeScript configuration:
{
"compilerOptions": {
"target": "ES2020", // Target modern JavaScript
"module": "commonjs", // Use CommonJS modules for Node.js
"outDir": "dist", // Output compiled JS to dist/
"rootDir": ".", // Source root is project root
"strict": true, // Enable strict type checking
"esModuleInterop": true, // Allow default imports from CommonJS modules
"baseUrl": ".", // Base directory for module resolution
"skipLibCheck": true, // Skip type checking of .d.ts files (faster)
"moduleResolution": "node", // Use Node.js module resolution
"sourceMap": true // Generate source maps for debugging
},
"include": ["src", "lib"], // Compile both src/ and lib/ directories
"exclude": []
}Create the source directories:
mkdir -p src/types src/utilso2 API Example Code Integration
The trading agent includes inline o2 API example code in the lib/ directory. This is example code that provides TypeScript interfaces for interacting with the o2 Exchange API.
Why Build a Trading Agent?
A trading agent automates market interactions on decentralized exchanges. By leveraging external price feeds and programmable order execution, trading agents can:
- Execute automated trading strategies - Market making, arbitrage
- Maintain continuous market presence - Trade 24/7 without manual intervention
- React to market conditions in real-time - Adjust prices based on external feeds
Setup the Example Code
Copy the o2 API example code into the lib/ directory from the trading-agent-bot repository:
# Clone the trading-agent-bot repository
git clone https://github.com/FuelLabs/trading-agent-bot.git temp-trading-bot
# Copy the lib folder to your project
cp -r temp-trading-bot/lib ./lib
# Clean up temporary clone
rm -rf temp-trading-botAlpha Software
The o2 API code in lib/ is alpha-stage and not feature-complete. Use at your own risk!
Important: The lib/ directory contains TypeScript source files that will be compiled alongside your bot code using your project's tsconfig.json. It does not have its own package.json or separate build process - everything compiles together as one project.
Example Code Structure
The example code is organized into several modules:
lib/
├── index.ts # Main exports
├── rest-api/ # REST API client implementation
├── types/ # TypeScript type definitions & contract ABIs
└── websocket/ # WebSocket client (for real-time data)What the Example Code Provides
The inline o2 API example code includes:
- REST API Client - Client for o2 API endpoints (markets, orders, trades, balances)
- Session Management - Secure session-based trading authentication
- Type Definitions - TypeScript types for all API interactions
- Contract Types - Auto-generated Fuel contract ABIs
- WebSocket Support - Real-time market data streaming
Install Dependencies
Install all required dependencies with a single command:
pnpm install