Skip to main content

OKX DEX Starter

A comprehensive integration example for using OKX DEX with Solana Agent Kit. This package provides a CLI-based trading bot that allows you to get quotes, execute swaps, and manage tokens directly from your terminal.

Features

  • Token Swapping: Swap between any tokens available on OKX DEX
  • Price Quotes: Get real-time quotes with price impact and fee information
  • Token Discovery: Automatic token resolution and information caching
  • Interactive CLI: User-friendly command-line interface for trading
  • AI-Powered Assistance: Optional AI trading assistant using LangChain
  • Transaction Monitoring: Real-time feedback for transaction status

Prerequisites

  • Node.js 16 or higher
  • Solana wallet with private key
  • OKX account with API credentials

Installation

  1. Clone the repository or create a new project:
  1. Install dependencies:
  1. Set up environment variables by creating a .env file:

Quick Start

Create a file named okx-dex-solana.ts with the implementation code and run:

Usage

Once the trading bot is running, you can use the following commands:
  • swap [amount] [from_token] to [to_token] - Prepare a token swap
  • quote [amount] [from_token] to [to_token] - Get a quote without executing
  • confirm - Execute the prepared swap
  • cancel - Cancel the current swap
  • tokens - List all known tokens
  • tokens [search] - Search for a specific token
  • help - Show available commands
  • exit - Exit the bot

Examples

You can also ask natural language questions and get AI-powered trading assistance:

Core Components

Token Management

The integration includes a token cache system that learns about tokens as they’re used:

Getting Quotes

The integration uses the OKX DEX API to fetch quotes for token swaps:

Executing Swaps

Swaps are executed using the OKX DEX SDK:

AI Trading Assistant (Optional)

The integration includes an optional AI trading assistant using LangChain:

Configuration Details

OKX DEX Client Initialization

Amount Formatting

The integration includes utilities for formatting token amounts correctly:

Error Handling

The integration includes comprehensive error handling:

Token Resolution

The integration can resolve tokens by symbol or address:

Common Tokens

Here are some common tokens you can use with this integration:

Troubleshooting

Common Issues

  1. Invalid private key
    • Ensure your private key is in base58 format
    • Check for extra whitespace or special characters
  2. Connection errors
    • Verify your OKX API credentials
    • Check your internet connection
    • Try using a different RPC URL
  3. Swap failures
    • Check token balances
    • Verify token addresses
    • Increase slippage for volatile tokens

Debugging Tips

The integration includes extensive debug logging:

Advanced Features

Custom Slippage

Token Price Monitoring

Resources

License

MIT