@pear-protocol/exchanges-sdk
Unified SDK for connecting to cryptocurrency derivative exchanges. Provides real-time account state tracking including balances, positions, and leverage management through a single, exchange-agnostic interface with automatic WebSocket streaming and credential resolution.
Supported Exchanges
| Binance | binance | USDM Futures |
| Bybit | bybit | Linear Perpetuals |
| Hyperliquid | hyperliquid | Perpetuals |
| OKX | okx | Linear SWAP Perpetuals |
Installation
npm install @pear-protocol/exchanges-sdk
Quick Start
import PearSDK from '@pear-protocol/core-sdk';
import { ExchangesSDK } from '@pear-protocol/exchanges-sdk';
const sdk = new PearSDK({ });
const exchanges = new ExchangesSDK({ sdk });
await exchanges.account.connect(tradeAccountId, 'binanceusdm');
const balanceTracker = exchanges.account.trackBalance((balance) => {
console.log(balance.totalEquity, balance.unrealizedPnl);
});
const positionsTracker = exchanges.account.trackPositions((positions) => {
for (const pos of positions) {
console.log(pos.symbol, pos.side, pos.size, pos.entryPrice);
}
});
balanceTracker.untrack();
positionsTracker.untrack();
exchanges.destroy();
Usage
Initialization
const exchanges = new ExchangesSDK({ sdk });
const exchanges = new ExchangesSDK({ sdk, demo: true });
The ExchangesSDK exposes three top-level modules:
exchanges.account | Account connection, balance/position tracking, leverage management |
exchanges.credentials | Exchange API credential fetching and caching |
exchanges.sdk | Reference to the underlying PearSDK instance |
Connecting to an Exchange
Credentials are fetched automatically from the backend when you call connect. One exchange connection is active at a time.
await exchanges.account.connect(tradeAccountId, 'binance');
await exchanges.account.connect(tradeAccountId, 'bybit');
await exchanges.account.connect(tradeAccountId, 'hyperliquid');
Check connection status:
exchanges.account.isConnected;
exchanges.account.isInitialized;
Tracking Balance
Subscribe to real-time balance updates. The callback fires on every balance change from the exchange.
const tracker = exchanges.account.trackBalance((balance) => {
balance.totalEquity;
balance.walletBalance;
balance.unrealizedPnl;
balance.availableToTrade;
balance.initialMarginUsed;
balance.maintenanceMargin;
balance.marginRatio;
for (const asset of balance.assets) {
asset.asset;
asset.free;
asset.used;
asset.total;
asset.walletBalance;
asset.usdValue;
}
});
const current = tracker.get();
tracker.untrack();
Tracking Positions
Subscribe to real-time position updates. The callback fires with the full list of open positions on every change.
const tracker = exchanges.account.trackPositions((positions) => {
for (const pos of positions) {
pos.symbol;
pos.side;
pos.size;
pos.entryPrice;
pos.unrealizedPnl;
pos.leverage;
pos.marginType;
pos.liquidationPrice;
}
});
const current = tracker.get();
tracker.untrack();
Positions with a size of "0" are automatically removed from the tracked state.
Tracking a Specific Asset
Retrieve leverage, margin type, and trade size constraints for a specific coin. Useful when preparing to place a trade.
const tracker = exchanges.account.trackAsset('ETH', (info) => {
info.coin;
info.leverage;
info.marginType;
});
const current = tracker.get();
tracker.untrack();
Symbol format per exchange: trackAsset takes the exchange's native symbol:
- Binance / Bybit:
BTCUSDT
- Hyperliquid:
BTC
- OKX:
BTC-USDT-SWAP
Managing Leverage
const leverage = exchanges.account.getLeverage('BTCUSDT');
Reading Full State
Access a read-only snapshot of the complete account state at any time.
const state = exchanges.account.getState();
if (state) {
state.balance;
state.positions;
state.leverageSettings;
state.trackedAssets;
state.lastUpdated;
state.initialized;
}
Credential Management
Credentials are resolved automatically on connect, but you can manage them directly:
const creds = await exchanges.credentials.fetch(tradeAccountId);
const cached = exchanges.credentials.get(tradeAccountId);
const creds = await exchanges.credentials.getOrFetch(tradeAccountId);
exchanges.credentials.clear(tradeAccountId);
exchanges.credentials.clear();
Cleanup
exchanges.account.disconnect();
exchanges.destroy();