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 networksCreating 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)Generating a New Wallet
Generate a new random wallet:You’ll need to install
bip39 separately: npm install bip39Subaccount 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
- 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:Security Best Practices
Secure Key Storage
Secure Key Storage
Never expose private keys or mnemonics in:
- Source code
- Version control
- Log files
- Error messages
- Client-side code
- Environment variables
- Secret management services (AWS Secrets Manager, HashiCorp Vault)
- Hardware security modules (HSM) for production
Separate API Keys by Purpose
Separate API Keys by Purpose
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
Use Subaccounts for Risk Isolation
Use Subaccounts for Risk Isolation
Separate trading strategies into different subaccounts:If one strategy fails, others remain unaffected.
Implement Key Rotation
Implement Key Rotation
Regularly rotate API keys and update your applications:
- Generate new API key
- Add to authenticators
- Update application configuration
- Remove old API key
- 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