npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

lnexchange-api-test

v1.0.9-beta.16

Published

LN Exchange SDK for spot and perpetual trading

Downloads

70

Readme

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

npm install lnex-sdk
# or
yarn add lnex-sdk
# or
pnpm add lnex-sdk

Quick Start

Client-side Usage (Browser with window.nostr)

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();

Node.js Usage (with lnfi-sdk)

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

Configuration Options

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

Account Management

createUser(referrals?)

Creates a new user account.

  • Parameters:
    • referrals (string, optional): Referral code
  • Returns: Promise<Object>

getUserInfo()

Fetches user account information.

  • Returns: Promise<Object>

enableTrade(symbolName)

Enables trading for a specific symbol.

  • Parameters:
    • symbolName (string, required): Trading pair symbol (e.g., "BTC-USDT")
  • Returns: Promise<Object>

Asset Operations

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<Object>

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<Object>

withdraw(assetsId, amount)

Withdraws assets from the exchange.

  • Parameters:
    • assetsId (string, required): Asset ID
    • amount (number, required): Amount to withdraw
  • Returns: Promise<Object>

Market Data

getAllMarkets()

Fetches all available trading markets.

  • Returns: Promise<Object>

Order Management

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<Object>

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<Object>

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<Object>

Order History and Lists

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<Object>

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<Object>

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<Object>

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<Object>

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<Object>

Perpetual Trading API

Account Management

createUser(referrals?)

Creates a new user account for perpetual trading.

  • Parameters:
    • referrals (string, optional): Referral code
  • Returns: Promise<Object>

getUserInfo()

Fetches user account information.

  • Returns: Promise<Object>

enableTrade(contractName)

Enables trading for a specific perpetual contract.

  • Parameters:
    • contractName (string, required): Contract name (e.g., "E-BTC-USDT")
  • Returns: Promise<Object>

Asset Operations

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<Object>

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<Object>

withdraw(assetsId, amount)

Withdraws assets from perpetual trading account.

  • Parameters:
    • assetsId (string, required): Asset ID
    • amount (number, required): Amount to withdraw
  • Returns: Promise<Object>

Market Data

getAllMarkets()

Fetches all available perpetual contracts.

  • Returns: Promise<Object>

Position Management

getAllPosition(data?)

Fetches all positions.

  • Parameters:
    • data.positionStatus (string, optional): Position status filter (default: "1")
  • Returns: Promise<Object>

getHistoryPosition(data?)

Fetches historical positions.

  • Parameters:
    • data (object, optional): Filter parameters
  • Returns: Promise<Object>

editLevel(data)

Edits leverage level for a position.

  • Parameters:
    • data (object, required): Leverage modification parameters
  • Returns: Promise<Object>

Order Management

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<Object>

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<Object>

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<Object>

Order History and Lists

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<Object>

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<Object>

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<Object>

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<Object>

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<Object>

Complete Examples

Client-side Complete Setup

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();

Node.js Complete Setup

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