Skip to main content

Overview

The LFG SDK uses a wallet and subaccount system to manage trading accounts. Understanding this architecture is crucial for building trading applications.

Wallet

Your main account identified by an address (e.g., lfg1abc...). Controls access and signs transactions.

Subaccount

Trading accounts under a wallet. Each subaccount has isolated collateral and positions.

Wallet Management

LocalWallet Class

LocalWallet handles cryptographic key management and transaction signing:

Creating a Wallet from Mnemonic

The recommended approach for production applications:
string
required
A 12 or 24-word BIP39 mnemonic phrase
string
required
Address prefix. Use "lfg" for LFG DEX networks
Store mnemonics securely in environment variables or secret management systems. Never hardcode them in your source code.

Creating a Wallet from Private Key

Useful for API keys and programmatic access:
string
required
Private key as hex string (with or without 0x prefix)
string
required
Address prefix ("lfg" for LFG DEX)
Private keys grant full control over the account. Protect them with the same security as mnemonics.

Generating a New Wallet

Generate a new random wallet:
You’ll need to install bip39 separately: npm install bip39

Subaccount System

What are Subaccounts?

Subaccounts allow you to:
  • Isolate risk by separating different trading strategies
  • Manage multiple positions independently
  • Organize trading activities across different markets
Each subaccount has:
  • Unique subaccount number (0-127)
  • Isolated collateral balance
  • Independent positions and orders

SubaccountInfo Class

SubaccountInfo represents a trading subaccount:

Creating a Subaccount for Local Wallet

For standard wallet-based trading:
LocalWallet
required
The LocalWallet instance that controls this subaccount
number
required
Subaccount index (0-127). Use 0 for your primary trading account.

Creating a Subaccount with API Key (Permissioned)

For API key-based trading where an API key controls operations on behalf of a main wallet:
LocalWallet
required
The API key wallet that signs transactions
string
required
The main wallet address that owns the subaccount
number
required
Subaccount index (0-127)
number[]
required
Array of authenticator IDs granting permission to the API key
See the API Key Trading Example below for complete setup.

Querying Subaccount Information

Get Subaccount Balance

Get Subaccount Positions

API Key Trading Example

Complete example of API key-based trading:
API keys must be registered with the main wallet’s authenticators before they can trade. This is typically done through the web interface.

Security Best Practices

Never expose private keys or mnemonics in:
  • Source code
  • Version control
  • Log files
  • Error messages
  • Client-side code
Use instead:
  • Environment variables
  • Secret management services (AWS Secrets Manager, HashiCorp Vault)
  • Hardware security modules (HSM) for production
Create different API keys for different purposes:
  • Trading Bot: Limited to order placement
  • Withdrawal Key: Separate key for withdrawals
  • Read-Only: For monitoring and analytics
This limits damage if a key is compromised.
Separate trading strategies into different subaccounts:
If one strategy fails, others remain unaffected.
Regularly rotate API keys and update your applications:
  1. Generate new API key
  2. Add to authenticators
  3. Update application configuration
  4. Remove old API key
  5. Monitor for unauthorized access

Advanced Usage

Multiple Subaccounts Example

Organize trading strategies across subaccounts:

Common Patterns

Singleton Wallet Instance

Wallet Factory

Next Steps

Place Orders

Learn how to place and manage orders

Manage Balances

Query and monitor account balances

Transfers

Deposit, withdraw, and transfer funds

LocalWallet Reference

Complete LocalWallet API documentation