LnexSdk SDK
A JavaScript SDK for LN Exchange (LnexSdk) that provides unified access to spot trading, perpetual trading, and utility functions. Built with Nostr protocol integration for secure and decentralized trading.
Features
-
Spot Trading : Complete spot trading API with order management
-
Perpetual Trading : Full perpetual contracts trading functionality
-
Nostr Integration : Secure authentication and communication via Nostr protocol
-
TypeScript Support : Full TypeScript definitions included
-
Lightweight : Minimal dependencies for optimal performance
-
Modern : ES6+ modules with tree-shaking support
Installation
``
bash
npm install lnex-sdk
or
yarn add lnex-sdk
or
pnpm add lnex-sdk
`
Quick Start
$3
`
javascript
import createLNEX from 'lnex-sdk';
// Initialize the SDK for browser (uses window.nostr automatically)
const lnex = createLNEX();
// Get public key
const publicKey = await lnex.getPublicKey();
console.log('Public Key:', publicKey);
// Use spot trading
const markets = await lnex.spot.getAllMarkets();
const userInfo = await lnex.spot.getUserInfo();
// Use perpetual trading
const perpMarkets = await lnex.perp.getAllMarkets();
const positions = await lnex.perp.getAllPosition();
`
$3
`
javascript
import createLNEX from 'lnex-sdk';
import { lnfiNostr } from 'lnfi-sdk';
// Create custom signer using lnfi-sdk
const signer = lnfiNostr({
privateKey: 'your-private-key-here'
});
// Initialize the SDK for Node.js
const lnex = createLNEX({
signer: signer
});
// Get public key
const publicKey = await lnex.getPublicKey();
console.log('Public Key:', publicKey);
// Use spot trading
const markets = await lnex.spot.getAllMarkets();
const userInfo = await lnex.spot.getUserInfo();
`
API Reference
$3
Parameters:
- env
(optional) : Environment ('development' | 'production', default: 'production')
- spotBaseURL
(optional) : Base URL for spot trading API (default: 'https://test-spots-api.ln.exchange')
- perpBaseURL
(optional) : Base URL for perpetual trading API (default: 'https://test-futures-api.ln.exchange')
- relay
(optional) : Nostr relay URL(s) - string or array (default: 'wss://relay01.lnfi.network')
- signer
(optional) : Custom signer instance (auto-detected from window.nostr if not provided)
- headers
(optional) : Additional HTTP headers object (default: {})
Spot Trading API
$3
#### createUser(referrals?)
Creates a new user account.
- Parameters:
- referrals
(string, optional) : Referral code
- Returns: Promise
#### getUserInfo()
Fetches user account information.
- Returns: Promise
#### enableTrade(symbolName)
Enables trading for a specific symbol.
- Parameters:
- symbolName (string, required) : Trading pair symbol (e.g., "BTC-USDT")
- Returns: Promise
$3
#### approve(tokenName, amount)
Approves a token for trading operations.
- Parameters:
- tokenName (string, required) : Token name (e.g., "USDT")
- amount (number, required) : Amount to approve
- Returns: Promise
#### deposit(tokenNameOrAssetId, amount)
Deposits assets from LNFI to LNExchange.
- Parameters:
- tokenNameOrAssetId (string, required) : Token name or asset ID
- amount (number, required) : Amount to deposit
- Returns: Promise
#### withdraw(assetsId, amount)
Withdraws assets from the exchange.
- Parameters:
- assetsId (string, required) : Asset ID
- amount (number, required) : Amount to withdraw
- Returns: Promise
$3
#### getAllMarkets()
Fetches all available trading markets.
- Returns: Promise
$3
#### createOrderApi(params)
Creates a new spot order.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.side (string, required) : Order side ("buy" | "sell")
- params.volume (number, required) : Order volume
- params.type (number, optional) : Order type (default: 2)
- params.price (number, optional) : Order price (default: 0 for market orders)
- params.timeInForce (number, optional) : Time in force (default: 2)
- params.leverageLevel (number, optional) : Leverage level (default: 1)
- Returns: Promise
#### cancelOrder(params)
Cancels a specific order.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.orderId (string, required) : Order ID to cancel
- params.isConditionOrder (boolean, optional) : Whether it's a conditional order (default: false)
- Returns: Promise
#### cancelAllOrder(params)
Cancels all orders for a specific symbol.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.isConditionOrder (boolean, optional) : Whether to cancel conditional orders (default: false)
- Returns: Promise
$3
#### currentOrderList(params)
Fetches current active orders.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- Returns: Promise
#### triggerOrderList(params)
Fetches trigger/conditional orders.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- Returns: Promise
#### historyOrderList(params)
Fetches historical orders.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- params.beginTime (string, optional) : Start time filter (default: "")
- params.endTime (string, optional) : End time filter (default: "")
- Returns: Promise
#### historyTriggerOrderList(params)
Fetches historical trigger orders.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- params.beginTime (string, optional) : Start time filter (default: "")
- params.endTime (string, optional) : End time filter (default: "")
- Returns: Promise
#### hisTradeList(params)
Fetches historical trade records.
- Parameters:
- params.symbolName (string, required) : Trading pair symbol
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Trade type filter (default: "")
- params.beginTime (string, optional) : Start time filter (default: "")
- params.endTime (string, optional) : End time filter (default: "")
- Returns: Promise
Perpetual Trading API
$3
#### createUser(referrals?)
Creates a new user account for perpetual trading.
- Parameters:
- referrals (string, optional) : Referral code
- Returns: Promise
#### getUserInfo()
Fetches user account information.
- Returns: Promise
#### enableTrade(contractName)
Enables trading for a specific perpetual contract.
- Parameters:
- contractName (string, required) : Contract name (e.g., "E-BTC-USDT")
- Returns: Promise
$3
#### approve(tokenName, amount)
Approves a token for perpetual trading operations.
- Parameters:
- tokenName (string, required) : Token name (e.g., "USDT")
- amount (number, required) : Amount to approve
- Returns: Promise
#### deposit(tokenNameOrAssetId, amount)
Deposits assets from LNFI to LNExchange for perpetual trading.
- Parameters:
- tokenNameOrAssetId (string, required) : Token name or asset ID
- amount (number, required) : Amount to deposit
- Returns: Promise
#### withdraw(assetsId, amount)
Withdraws assets from perpetual trading account.
- Parameters:
- assetsId (string, required) : Asset ID
- amount (number, required) : Amount to withdraw
- Returns: Promise
$3
#### getAllMarkets()
Fetches all available perpetual contracts.
- Returns: Promise
$3
#### getAllPosition(data?)
Fetches all positions.
- Parameters:
- data.positionStatus (string, optional) : Position status filter (default: "1")
- Returns: Promise
#### getHistoryPosition(data?)
Fetches historical positions.
- Parameters:
- data (object, optional) : Filter parameters
- Returns: Promise
#### editLevel(data)
Edits leverage level for a position.
- Parameters:
- data (object, required) : Leverage modification parameters
- Returns: Promise
$3
#### createOrderApi(params)
Creates a new perpetual order.
- Parameters:
- params.contractName (string, required) : Contract name
- params.side (string, required) : Order side ("buy" | "sell")
- params.volume (number, required) : Order volume
- params.openOrClose (string, required) : Position action ("open" | "close")
- params.type (number, optional) : Order type (default: 2)
- params.price (number, optional) : Order price (default: 0 for market orders)
- params.leverageLevel (number, optional) : Leverage level (default: 8)
- params.positionType (number, optional) : Position type (default: 2)
- Returns: Promise
#### cancelOrder(params)
Cancels a specific perpetual order.
- Parameters:
- params.contractName (string, required) : Contract name
- params.orderId (string, required) : Order ID to cancel
- params.isConditionOrder (boolean, optional) : Whether it's a conditional order (default: false)
- Returns: Promise
#### cancelAllOrder(params)
Cancels all orders for a specific contract.
- Parameters:
- params.contractName (string, required) : Contract name
- params.isConditionOrder (boolean, optional) : Whether to cancel conditional orders (default: false)
- Returns: Promise
$3
#### currentOrderList(params)
Fetches current active perpetual orders.
- Parameters:
- params.contractName (string, required) : Contract name
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- Returns: Promise
#### triggerOrderList(params)
Fetches trigger/conditional perpetual orders.
- Parameters:
- params.contractName (string, required) : Contract name
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- Returns: Promise
#### historyOrderList(params)
Fetches historical perpetual orders.
- Parameters:
- params.contractName (string, required) : Contract name
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- params.beginTime (string, optional) : Start time filter (default: "")
- params.endTime (string, optional) : End time filter (default: "")
- Returns: Promise
#### historyTriggerOrderList(params)
Fetches historical trigger perpetual orders.
- Parameters:
- params.contractName (string, required) : Contract name
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Order type filter (default: "")
- params.beginTime (string, optional) : Start time filter (default: "")
- params.endTime (string, optional) : End time filter (default: "")
- Returns: Promise
#### hisTradeList(params)
Fetches historical perpetual trade records.
- Parameters:
- params.contractName (string, required) : Contract name
- params.page (number, optional) : Page number (default: 1)
- params.limit (number, optional) : Items per page (default: 10)
- params.type (string, optional) : Trade type filter (default: "")
- params.beginTime (string, optional) : Start time filter (default: "")
- params.endTime (string, optional) : End time filter (default: "")
- Returns: Promise
Complete Examples
$3
`javascript
import createLNEX from 'lnex-sdk';
// Initialize for browser
const lnex = createLNEX({
env: 'development',
spotBaseURL: 'https://dev-spots-api.unift.xyz',
perpBaseURL: 'https://dev-futures-api.unift.xyz',
relay: 'wss://dev-relay.lnfi.network'
});
// Complete trading setup
async function setupTrading() {
try {
// Create user and setup account
await lnex.spot.createUser(); // referral code is optional
// Setup USDT for both spot and perpetual trading
// Note: This step deposits USDT from LNFI to LNExchange
await lnex.spot.approve("USDT", 200);
await lnex.spot.deposit("USDT", 200); // SDK will get assetId from publicInfo
await lnex.perp.approve("USDT", 200);
await lnex.perp.deposit("USDT", 200); // SDK will get assetId from publicInfo
// Enable trading pairs
await lnex.spot.enableTrade("BTC-USDT");
await lnex.perp.enableTrade("E-BTC-USDT");
// Place spot order
const spotOrder = await lnex.spot.createOrderApi({
symbolName: "BTC-USDT",
side: "buy",
volume: 1,
type: 1,
price: 30000
});
// Place perpetual order
const perpOrder = await lnex.perp.createOrderApi({
contractName: "E-BTC-USDT",
side: "buy",
volume: 0.1,
openOrClose: "open",
type: 1,
price: 50000,
leverageLevel: 10
});
console.log('Spot Order:', spotOrder);
console.log('Perp Order:', perpOrder);
} catch (error) {
console.error('Setup failed:', error);
}
}
setupTrading();
`
$3
`javascript
import createLNEX from 'lnex-sdk';
import { lnfiNostr } from 'lnfi-sdk';
// Create custom signer using lnfi-sdk
const signer = lnfiNostr({
privateKey: 'your-private-key-here'
});
// Initialize for Node.js
const lnex = createLNEX({
env: 'development',
spotBaseURL: 'https://dev-spots-api.unift.xyz',
perpBaseURL: 'https://dev-futures-api.unift.xyz',
relay: 'wss://dev-relay.lnfi.network',
signer: signer
});
// Complete trading setup
async function setupTrading() {
try {
// Get public key
const publicKey = await lnex.getPublicKey();
console.log('Public Key:', publicKey);
// Create user and setup account
await lnex.spot.createUser();
await lnex.perp.createUser();
// Setup USDT for both spot and perpetual trading
// Note: This step deposits USDT from LNFI to LNExchange
await lnex.spot.approve("USDT", 200);
await lnex.spot.deposit("USDT", 200);
await lnex.perp.approve("USDT", 200);
await lnex.perp.deposit("USDT", 200);
// Enable trading pairs
await lnex.spot.enableTrade("BTC-USDT");
await lnex.perp.enableTrade("E-BTC-USDT");
// Get market data
const spotMarkets = await lnex.spot.getAllMarkets();
const perpMarkets = await lnex.perp.getAllMarkets();
const userInfo = await lnex.spot.getUserInfo();
const positions = await lnex.perp.getAllPosition();
console.log('Spot Markets:', spotMarkets);
console.log('Perp Markets:', perpMarkets);
console.log('User Info:', userInfo);
console.log('Positions:', positions);
} catch (error) {
console.error('Setup failed:', error);
}
}
setupTrading();
``
Dependencies
- axios : HTTP client for API requests
- dayjs : Date manipulation library
- nostr-tools : Nostr protocol implementation
Environment Support
- Browser : Supports modern browsers with window.nostr extension
- Node.js : Requires custom signer implementation (e.g., lnfi-sdk)
- TypeScript : Full type definitions included
Contributing
We welcome contributions! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- GitHub Issues : Report bugs or request features
- Documentation : Full API documentation
- Discord : Join our community