Skip to main content

Building on LFG Perp

LFG Perp provides developers with powerful tools to build decentralized trading applications, automated strategies, and custom interfaces for perpetual futures trading.

What You Can Build

Trading Bots

Automated strategies that execute trades based on your algorithms and market conditions

Portfolio Managers

Applications for managing multiple positions, subaccounts, and risk across markets

Market Makers

High-frequency bots that provide liquidity and capture spreads

Analytics Dashboards

Custom UIs for tracking positions, PnL, funding rates, and market data

Arbitrage Bots

Tools to capitalize on price differences across markets and exchanges

Risk Management Tools

Applications for monitoring margin health, liquidation risks, and exposure

Development Stack

TypeScript/JavaScript SDK

The official @oraichain/lfg-client-js SDK provides:
  • CompositeClient: Unified interface combining Indexer (read) + Validator (write) - LocalWallet: Wallet management with mnemonic and private key support - SubaccountInfo: Manage multiple trading subaccounts - Type Safety: Full TypeScript support with comprehensive type definitions

Architecture Components

LFG Perp’s architecture consists of three main components:
Purpose: Fast read-only access to market and account dataProvides:
  • Orderbooks
  • Market prices and statistics
  • Account balances and positions
  • Trade history
  • Funding rate history
Technology: REST API + WebSocket subscriptions
Purpose: Blockchain interaction for transactionsProvides:
  • Order placement
  • Order cancellation
  • Deposits and withdrawals
  • Account queries
  • Block information
Technology: Cosmos SDK RPC endpoint
Purpose: Unified SDK that combines both Indexer and ValidatorBenefits:
  • Single interface for all operations
  • Automatic routing to appropriate endpoint
  • Connection management
  • Error handling
Language: TypeScript/JavaScript

Development Workflow

1. Setup & Installation

npm install @oraichain/lfg-client-js
Learn more →

2. Configuration

Choose your network and configure endpoints:
import { Network, CompositeClient } from "@oraichain/lfg-client-js";

// Staging for development
const network = Network.staging();

// Mainnet for production
// const network = Network.mainnet();

const client = await CompositeClient.connect(network);
Learn more →

3. Wallet Setup

Create or import a wallet:
import { LocalWallet } from "@oraichain/lfg-client-js";

const wallet = await LocalWallet.fromMnemonic(process.env.MNEMONIC, "lfg");
Learn more →

4. Build & Deploy

Implement your trading logic and deploy:
// Query markets
const markets = await client.indexerClient.markets.getPerpetualMarkets();

// Place orders
const tx = await client.placeShortTermOrder(/* ... */);

// Monitor positions
const account =
  await client.indexerClient.account.getParentSubaccount(/* ... */);
See examples →

Development Environments

Use for: Development, testing, and experimentation Characteristics:
  • Test tokens with no real value - Same features as mainnet - Freely available test USDC - Safe to experiment typescript const network = Network.staging();
    Always develop and test on staging before deploying to mainnet.

Common Use Cases

Trading Bot Example

class SimpleTradingBot {
  private client: CompositeClient;
  private wallet: LocalWallet;

  async initialize() {
    const network = Network.staging();
    this.client = await CompositeClient.connect(network);
    this.wallet = await LocalWallet.fromMnemonic(process.env.MNEMONIC, "lfg");
  }

  async executeTrade(
    market: string,
    side: OrderSide,
    price: number,
    size: number
  ) {
    const subaccount = SubaccountInfo.forLocalWallet(this.wallet, 0);
    const currentBlock =
      await this.client.validatorClient.get.latestBlockHeight();

    return await this.client.placeShortTermOrder(
      subaccount,
      market,
      side,
      price,
      size,
      Math.floor(Math.random() * 100000000),
      currentBlock + 20,
      Order_TimeInForce.TIME_IN_FORCE_UNSPECIFIED,
      false
    );
  }

  async monitorMarket(market: string) {
    const markets =
      await this.client.indexerClient.markets.getPerpetualMarkets();
    const marketData = markets.markets[market];

    return {
      price: parseFloat(marketData.oraclePrice),
      volume: parseFloat(marketData.volume24H),
      change: parseFloat(marketData.priceChange24H),
    };
  }
}
See complete examples →

Best Practices

  • Never hardcode private keys or mnemonics
  • Use environment variables for sensitive data
  • Implement proper key management
  • Use separate keys for different purposes
Learn more →
  • Implement retry logic for network failures
  • Handle rate limits gracefully
  • Validate responses before processing
  • Log errors for debugging
Learn more →
  • Cache account numbers for faster transactions
  • Use WebSocket for real-time data
  • Batch operations when possible
  • Monitor API rate limits
  • Always test on staging first
  • Use small amounts for initial tests
  • Monitor margin health in tests
  • Implement automated testing

Resources

Community & Support

Ready to start building? Head to the Quickstart Guide to build your first application!