TypeScript SDK

TypeScript SDK

Complete TypeScript SDK for LX - installation, configuration, and quick start guide

TypeScript SDK

The @luxfi/trading package provides a complete TypeScript client for interacting with LX. It supports both browser and Node.js environments with full type safety, CCXT compatibility, and Hummingbot integration.

Installation

npm install @luxfi/trading

Or with other package managers:

# pnpm
pnpm add @luxfi/trading

# yarn
yarn add @luxfi/trading

# bun
bun add @luxfi/trading

Requirements

  • Node.js 18+ or modern browser
  • TypeScript 5.0+ (optional but recommended)

Quick Start

Simple DEX Client

import { DEX } from '@luxfi/trading';

// Create DEX client (CLOB orderbook exchange)
const dex = await DEX({ rpcUrl: 'http://localhost:8080/rpc' });

// Place a limit buy order
const order = await dex.limitBuy('BTC-USDC', '0.1', '50000');
console.log('Order placed:', order.orderId);

// Get orderbook
const book = await dex.orderbook('BTC-USDC');
console.log('Best bid:', book.bestBid());

Simple AMM Client

import { AMM } from '@luxfi/trading';

// Create AMM client (C-chain automated market maker)
const amm = await AMM({ rpcUrl: 'http://localhost:8080/rpc' });

// Get swap quote
const quote = await amm.quote('BTC', 'USDC', '0.1', true, 'lx-amm');
console.log('Output:', quote.outputAmount.toString());

// Execute swap
const trade = await amm.swap('BTC', 'USDC', '0.1', true, '0.01', 'lx-amm');
console.log('Swap executed:', trade.tradeId);

Unified Multi-Venue Client

For advanced use cases connecting to multiple venues:

import { Client, Config } from '@luxfi/trading';

const config = Config.fromObject({
  native: {
    'lx-dex': { venueType: 'clob', apiUrl: 'http://localhost:8080/rpc' },
    'lx-amm': { venueType: 'amm', apiUrl: 'http://localhost:8080/rpc' }
  },
  ccxt: {
    'binance': { exchange: 'binance', apiKey: '...', secret: '...' }
  },
  hummingbot: {},
  general: {
    venuePriority: ['lx-dex', 'binance'],
    smartRouting: true
  }
});

const client = new Client(config);
await client.connect();

// Smart order routing across venues
const order = await client.buy('BTC-USDC', '0.1');

// Get aggregated orderbook from all venues
const aggBook = await client.aggregatedOrderbook('BTC-USDC');

Place Orders

import { DEX, Side, OrderType, TimeInForce } from '@luxfi/trading';

const dex = await DEX({ rpcUrl: 'http://localhost:8080/rpc' });

// Market buy
const marketOrder = await dex.buy('BTC-USDC', '0.1');

// Limit buy
const limitOrder = await dex.limitBuy('BTC-USDC', '0.1', '50000');

// Limit sell
const sellOrder = await dex.limitSell('BTC-USDC', '0.1', '55000');

// Cancel order
await dex.cancelOrder(limitOrder.orderId, 'BTC-USDC', 'lx-dex');

Real-Time Data

import { DEX } from '@luxfi/trading';

const dex = await DEX({
  rpcUrl: 'http://localhost:8080/rpc',
  wsUrl: 'ws://localhost:8081'
});

// Get orderbook
const book = await dex.orderbook('BTC-USDC');
console.log('Best Bid:', book.bestBid()?.price);
console.log('Best Ask:', book.bestAsk()?.price);
console.log('Spread:', book.spread());

// Get ticker
const ticker = await dex.ticker('BTC-USDC');
console.log('Last:', ticker.last?.toString());
console.log('24h Volume:', ticker.volume24h?.toString());

Environment Configuration

Development (Local Node)

const dex = await DEX({
  rpcUrl: 'http://localhost:8080/rpc',
  wsUrl: 'ws://localhost:8081'
});

Testnet

const dex = await DEX({
  rpcUrl: 'http://localhost:18080/rpc',
  wsUrl: 'ws://localhost:18081'
});

Production

const dex = await DEX({
  rpcUrl: 'https://api.lux.network/rpc',
  wsUrl: 'wss://api.lux.network/ws',
  apiKey: process.env.LX_API_KEY
});

Module Structure

The SDK exports the following modules:

ExportDescription
DEXFactory for LX DEX CLOB client
AMMFactory for LX AMM client
ClientUnified multi-venue client
ConfigConfiguration builder
OrderTypeEnum for order types (LIMIT, MARKET, etc.)
OrderSide / SideEnum for order sides (BUY, SELL)
OrderStatusEnum for order statuses
TimeInForceEnum for time-in-force options
OrderbookOrderbook data structure
AggregatedOrderbookMulti-venue aggregated orderbook
RiskManagerRisk management utilities
arbitrage.*Arbitrage tools and strategies

Execution Algorithms

import { TwapExecutor, VwapExecutor, IcebergExecutor, SniperExecutor } from '@luxfi/trading';

Financial Math

import {
  blackScholes,
  impliedVolatility,
  greeks,
  volatility,
  sharpeRatio,
  valueAtRisk,
  conditionalVaR
} from '@luxfi/trading';

Error Handling

All SDK methods throw typed errors that can be caught and handled:

import { DEX } from '@luxfi/trading';

async function safeOperation(): Promise<void> {
  const dex = await DEX({ rpcUrl: 'http://localhost:8080/rpc' });

  try {
    const order = await dex.limitBuy('BTC-USDC', '1.0', '50000');
  } catch (error) {
    if (error instanceof Error) {
      if (error.message.includes('insufficient')) {
        console.error('Insufficient balance');
      } else if (error.message.includes('rate limit')) {
        console.error('Rate limited - please wait');
      } else {
        console.error('Order failed:', error.message);
      }
    }
  }
}

Next Steps