Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

safe-cex

> 前端框架
Open source

An OpenSource TypeScript library to create rich trading cryptocurrencies interfaces

100 stars0 likes1 views
WebsiteGitHub

About

An OpenSource TypeScript library to create rich trading cryptocurrencies interfaces

safe-cex

An OpenSource TypeScript library to create rich trading cryptocurrencies interfaces

Introduction

It is an Open-source TypeScript library that provides an easy-to-use interface for communicating with exchanges. It is designed to be used by trading platforms like tuleep.trade, allowing them to interact with exchanges like Bybit and Binance Futures to execute trades on behalf of their users.

The library includes a set of API methods that abstract away the complexities of the exchanges' REST and WebSocket APIs, providing a simple and consistent interface for managing orders, positions, balances, and more.

It also implements robust error handling, rate limiting, and other safeguards to prevent common trading pitfalls and ensure the safety of user funds.

One key feature of safe-cex is its support for multiple exchanges, which allows tuleep.trade to offer a wider range of trading options to its users.

Currently, the library supports Bybit and Binance Futures contracts, OKX Perpetual Swaps and WOO X Futures.

Differences with CCXT

  • safe-cex handles for you the lifecycle of fetching, and updating data from exchanges
  • safe-cex exposes simple methods to place any type of orders
  • safe-cex handles for you orders size limits and precisions of orders
  • safe-cex expose the same interface for every exchanges
  • safe-cex is oriented for in-browser usage

Exchanges supported

  • Bybit Futures USDT contracts & futures unified Margin
  • Binance USD-M futures (USDT contracts)
  • Bitget USDT-M Futures (USDT contracts)
  • WOO X Futures USDT & futures unified margin
  • OKX Perpetuals unified margin
  • GateIO Futures USDT contracts
  • Blofin Futures USDT contracts
  • Phemex Futures USDT contracts

Installation

  • npm install --save safe-cex

Getting started

To initialize the exchange library, you will need:

  • API key / secret
  • A CORS-Anywhere server if you are using Binance testnet (they do not support CORS)
…

Exchange store

safe-cex maintains a inner-store with all the up-to-date exchanges informations:

typescript
type Store = {
  latency: number;
  balance: Balance;
  markets: Market[];
  tickers: Ticker[];
  orders: Order[];
  positions: Position[];
  loaded: {
    balance: boolean;
    orders: boolean;
    markets: boolean;
    tickers: boolean;
    positions: boolean;
  };
};

Events

safe-cex creates an exchange is an event-emitter object, you can subscribe to events and react accordingly.

update

This event is called everytime the inner-store of safe-cex is updated.

typescript
let storeCopy = {};

exchange.on("update", (store: Store) => {
  console.log("exchange store update");
  storeCopy = store;
});

fill

This event is called when an order is filled, this allows you to display a notification when this happens.

typescript
type FillEvent = {
  amount: number;
  price: number;
  side: "buy" | "sell";
  symbol: string;
};

exchange.on("fill", (event: FillEvent) => {
  console.log(
    `${event.side} ${event.amount} ${event.symbol} at ${event.price}`
  );
});

error

This event is called when an error has occured or the exchange API responded with an error status.

typescript
exchange.on("error", (error: string) => {
  window.alert(error);
});

log

This event is called when a new log message is emitted, you can display those debuging purposes.

typescript
enum LogSeverity {
  Warning = "warning",
  Error = "error",
  Info = "info",
}

exchange.on("log", (message: string, severity: LogSeverity) => {
  console.log(`[${severity}] ${message}`);
});

Public methods

validateAccount()

  • Can be called before exchange.start() to ensure API keys are valid and have the right permissions
  • Returns an empty string if valid, otherwise returns a string with error

start()

  • Method to be called before trying to place orders
  • It starts the lifecycle of safe-cex and fetch all the needed data
  • You can use store.loaded object to know when it's ready

fetchOHLCV()

typescript
type Timeframe =
  | "1m"
  | "3m"
  | "5m"
  | "15m"
  | "30m"
  | "1h"
  | "2h"
  | "4h"
  | "6h"
  | "12h"
  | "1d"
  | "1w";

type OHLCVOptions = {
  symbol: string;
  interval: Timeframe;
};

type Candle = {
  timestamp: number;
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
};

const candles: Candle[] = await exchange.fetchOHLCV({
  // market symbol, can be found in exchange.store.markets[index].symbol
  symbol: "BTCUSDT",
  timeframe: "15m",
});

listenOHLCV()

  • Subscribe to exchange kline websocket
  • Takes a callback() function to be called when candle has been updated
  • Returns a dispose() function to un-subscribe websocket topic
typescript
const callback = (candle: Candle) => {
  console.log(`Candle updated, current price: ${candle.close}`);
};

const dispose = exchange.listenOHLCV(
  { symbol: "BTCUSDT", timeframe: "15m" },
  callback
);

// when finished listening
dispose();

listenOrderBook()

  • Subscribe to exchange symbol orderbook
  • Takes a callback() function to be called when order book has been updated
  • Returns a dispose() function to un-subscribe weboscket topic
typescript
type OrderBookOrders = {
  price: number;
  amount: number;
  total: number;
};

type OrderBook = {
  bids: OrderBookOrders[];
  asks: OrderBookOrders[];
};

const callback = (orderBook: OrderBook) => {
  console.log(JSON.stringify(orderBook, null, 4));
};

const dispose = exchange.listenOrderBook("BTCUSDT", callback);

// when finished
dispose();

placeOrder()

  • Method to create an order on exchange
  • Can set stopLoss / takeProfit at the same time
  • Returns an Array of orderIds
…

placeOrders()

  • Same method as placeOrder() but for multiple orders
  • Pease use this method when possible, it will batch orders creations on exchanges API
  • There's no limit of orders to be passed
  • Returns an Array of orderIds

updateOrder()

  • Method to be called for updating an order price or amount
  • This supports updating price of take profit and stop loss orders
  • Returns an Array of orderIds
typescript
//  we take the first existing order as example
const originalOrder = exchange.store.orders[0];

// update price of order
const updatedOrderIds: string[] = await exchange.updateOrder({
  order: originalOrder,
  update: { price: 1700 },
});

// update amount of order
const updatedOrderIds: string[] = await exchange.updateOrder({
  order: originalOrder,
  update: { amount: 2 },
});

cancelOrders()

  • Method for cancelling multiple orders at the same time
typescript
const orders = [exchange.store.orders[0], exchange.store.orders[1]];
await exchange.cancelOrders(orders);

cancelSymbolOrders()

  • Method for cancelling all orders relative to a symbol
typescript
await exchange.cancelSymbolOrders("BTCUSDT");

cancelAllOrders()

  • Method for cancelling all existing orders
typescript
await exchange.cancelAllOrders();

setLeverage()

  • Method used to update the leverage on a symbol
  • It will check for min/max leverage of market and will stay in those boundaries
    • eg: you set leverage x50 but market accepts max x25, it will be set to x25
typescript
await exchange.setLeverage("BTCUSDT", 125);

setAllLeverage()

  • Method used to update all the leverage setting for all markets
  • It will check for min/max leverage of market and will stay in those boundaries
typescript
await exchange.setAllLeverage(100);

changePositionMode()

  • Method to be used to switch between Hedge/One-way mode
  • It acts on every contracts
  • You must have no positions open to user
typescript
// switch hedge-mode
await exchange.changePositionMode(true);
// switch one-way
await exchange.changePositionMode(false);

nuke()

  • Closes all positions at market price
  • Cancel all existing orders
typescript
await exchange.nuke();

dispose()

  • Method to be called when you don't need the exchange instance anymore
  • It has to be called for cleaning all subscribtions, data streams with exchange, etc...
typescript
await exchange.dispose();

Known issues

Using with next.js

Because of nanoid package, in next.config.js add:

typescript
transpilePackages: ['safe-cex'],
experimental: {
  esmExternals: 'loose',
}

Donations

If you found this project interesting or useful, create accounts with my referral links:

  • Bybit Up to $30,000 deposit bonus
  • Blofin $5,000 welcome bonus & no KYC required
  • OKX 20% less fees, up to $10,000 deposit bonus
  • Bitget 10% less fees, up to $1,000 deposit bonus
  • WOO X Deposit $100 and 0 fees for 14 days!
  • Binance
  • GateIO
  • Phemex

Or buy me a coffee with a crypto donation:

  • ETH/BSC/MATIC/AVAX: 0xFF2da578C73bC694fd767A1CC77160002C2003E6

Contributions & Pull Requests

Feel free to create issues, PRs and start a discussion ❤️

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptapibinancebitcoinbybit

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架