New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bybit-api

Package Overview
Dependencies
Maintainers
1
Versions
138
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bybit-api - npm Package Compare versions

Comparing version 3.1.3 to 3.2.0

lib/contract-client.d.ts

6

lib/constants/enum.d.ts

@@ -55,2 +55,8 @@ export declare const linearPositionModeEnum: {

readonly RISK_ID_NOT_MODIFIED: 134026;
readonly CONTRACT_ORDER_NOT_EXISTS: 140001;
readonly CONTRACT_INSUFFICIENT_BALANCE: 140007;
readonly CONTRACT_POSITION_MODE_NOT_MODIFIED: 140025;
readonly CONTRACT_MARGIN_MODE_NOT_MODIFIED: 140026;
readonly CONTRACT_RISK_LIMIT_INFO_NOT_EXISTS: 140031;
readonly CONTRACT_SET_LEVERAGE_NOT_MODIFIED: 140043;
/** E.g. USDC Options trading, trying to access a symbol that is no longer active */

@@ -57,0 +63,0 @@ readonly CONTRACT_NAME_NOT_EXIST: 3100111;

@@ -58,2 +58,8 @@ "use strict";

RISK_ID_NOT_MODIFIED: 134026,
CONTRACT_ORDER_NOT_EXISTS: 140001,
CONTRACT_INSUFFICIENT_BALANCE: 140007,
CONTRACT_POSITION_MODE_NOT_MODIFIED: 140025,
CONTRACT_MARGIN_MODE_NOT_MODIFIED: 140026,
CONTRACT_RISK_LIMIT_INFO_NOT_EXISTS: 140031,
CONTRACT_SET_LEVERAGE_NOT_MODIFIED: 140043,
/** E.g. USDC Options trading, trying to access a symbol that is no longer active */

@@ -60,0 +66,0 @@ CONTRACT_NAME_NOT_EXIST: 3100111,

1

lib/index.d.ts

@@ -11,2 +11,3 @@ export * from './account-asset-client';

export * from './unified-margin-client';
export * from './contract-client';
export * from './websocket-client';

@@ -13,0 +14,0 @@ export * from './util/logger';

@@ -23,2 +23,3 @@ "use strict";

__exportStar(require("./unified-margin-client"), exports);
__exportStar(require("./contract-client"), exports);
__exportStar(require("./websocket-client"), exports);

@@ -25,0 +26,0 @@ __exportStar(require("./util/logger"), exports);

@@ -10,1 +10,2 @@ export * from './account-asset';

export * from './unified-margin';
export * from './contract';

@@ -22,2 +22,3 @@ "use strict";

__exportStar(require("./unified-margin"), exports);
__exportStar(require("./contract"), exports);
//# sourceMappingURL=index.js.map

3

lib/types/shared.d.ts

@@ -0,1 +1,2 @@

import { ContractClient } from '../contract-client';
import { InverseClient } from '../inverse-client';

@@ -8,3 +9,3 @@ import { LinearClient } from '../linear-client';

import { USDCPerpetualClient } from '../usdc-perpetual-client';
export declare type RESTClient = InverseClient | LinearClient | SpotClient | SpotClientV3 | USDCOptionClient | USDCPerpetualClient | UnifiedMarginClient;
export declare type RESTClient = InverseClient | LinearClient | SpotClient | SpotClientV3 | USDCOptionClient | USDCPerpetualClient | UnifiedMarginClient | ContractClient;
export declare type numberInString = string;

@@ -11,0 +12,0 @@ export declare type OrderSide = 'Buy' | 'Sell';

import { RestClientOptions, WS_KEY_MAP } from '../util';
/** For spot markets, spotV3 is recommended */
export declare type APIMarket = 'inverse' | 'linear' | 'spot' | 'spotv3' | 'usdcOption' | 'usdcPerp' | 'unifiedPerp' | 'unifiedOption';
export declare type APIMarket = 'inverse' | 'linear' | 'spot' | 'spotv3' | 'usdcOption' | 'usdcPerp' | 'unifiedPerp' | 'unifiedOption' | 'contractUSDT' | 'contractInverse';
export declare type WsPublicInverseTopic = 'orderBookL2_25' | 'orderBookL2_200' | 'trade' | 'insurance' | 'instrument_info' | 'klineV2';

@@ -5,0 +5,0 @@ export declare type WsPublicUSDTPerpTopic = 'orderBookL2_25' | 'orderBookL2_200' | 'trade' | 'insurance' | 'instrument_info' | 'kline';

@@ -23,2 +23,3 @@ "use strict";

getClientType() {
// Follows the same authentication mechanism as other v3 APIs (e.g. USDC)
return util_1.REST_CLIENT_TYPE_ENUM.v3;

@@ -25,0 +26,0 @@ }

@@ -26,2 +26,6 @@ import { APIMarket, WsKey } from '../types';

readonly unifiedPerpUSDCPublic: "unifiedPerpUSDCPublic";
readonly contractUSDTPublic: "contractUSDTPublic";
readonly contractUSDTPrivate: "contractUSDTPrivate";
readonly contractInversePublic: "contractInversePublic";
readonly contractInversePrivate: "contractInversePrivate";
};

@@ -28,0 +32,0 @@ export declare const WS_AUTH_ON_CONNECT_KEYS: WsKey[];

@@ -113,2 +113,22 @@ "use strict";

},
contractUSDT: {
public: {
livenet: 'wss://stream.bybit.com/contract/usdt/public/v3',
testnet: 'wss://stream-testnet.bybit.com/contract/usdt/public/v3',
},
private: {
livenet: 'wss://stream.bybit.com/contract/private/v3',
testnet: 'wss://stream-testnet.bybit.com/contract/private/v3',
},
},
contractInverse: {
public: {
livenet: 'wss://stream.bybit.com/contract/inverse/public/v3',
testnet: 'wss://stream-testnet.bybit.com/contract/inverse/public/v3',
},
private: {
livenet: 'wss://stream.bybit.com/contract/private/v3',
testnet: 'wss://stream-testnet.bybit.com/contract/private/v3',
},
},
};

@@ -131,2 +151,6 @@ exports.WS_KEY_MAP = {

unifiedPerpUSDCPublic: 'unifiedPerpUSDCPublic',
contractUSDTPublic: 'contractUSDTPublic',
contractUSDTPrivate: 'contractUSDTPrivate',
contractInversePublic: 'contractInversePublic',
contractInversePrivate: 'contractInversePrivate',
};

@@ -138,2 +162,4 @@ exports.WS_AUTH_ON_CONNECT_KEYS = [

exports.WS_KEY_MAP.unifiedPrivate,
exports.WS_KEY_MAP.contractUSDTPrivate,
exports.WS_KEY_MAP.contractInversePrivate,
];

@@ -149,2 +175,4 @@ exports.PUBLIC_WS_KEYS = [

exports.WS_KEY_MAP.unifiedPerpUSDCPublic,
exports.WS_KEY_MAP.contractUSDTPublic,
exports.WS_KEY_MAP.contractInversePublic,
];

@@ -185,2 +213,7 @@ /** Used to automatically determine if a sub request should be to the public or private ws (when there's two) */

'user.greeks.unifiedAccount',
// contract v3
'user.position.contractAccount',
'user.execution.contractAccount',
'user.order.contractAccount',
'user.wallet.contractAccount',
];

@@ -234,2 +267,12 @@ function getWsKeyForTopic(market, topic, isPrivate) {

}
case 'contractInverse': {
return isPrivateTopic
? exports.WS_KEY_MAP.contractInversePrivate
: exports.WS_KEY_MAP.contractInversePublic;
}
case 'contractUSDT': {
return isPrivateTopic
? exports.WS_KEY_MAP.contractUSDTPrivate
: exports.WS_KEY_MAP.contractUSDTPublic;
}
default: {

@@ -249,3 +292,5 @@ throw neverGuard(market, `getWsKeyForTopic(): Unhandled market`);

case 'unifiedPerp':
case 'spot': {
case 'spot':
case 'contractInverse':
case 'contractUSDT': {
return null;

@@ -252,0 +297,0 @@ }

@@ -84,5 +84,11 @@ /// <reference types="node" />

private ping;
/**
* Closes a connection, if it's even open. If open, this will trigger a reconnect asynchronously.
* If closed, trigger a reconnect immediately
*/
private executeReconnectableClose;
private clearTimers;
private clearPingTimer;
private clearPongTimer;
private clearReconnectTimer;
/**

@@ -100,3 +106,2 @@ * @private Use the `subscribe(topics)` method to subscribe to topics. Send WS message to subscribe to topics.

private onWsMessage;
private onWsError;
private onWsClose;

@@ -103,0 +108,0 @@ private getWs;

@@ -22,8 +22,9 @@ "use strict";

const spot_client_1 = require("./spot-client");
const usdc_option_client_1 = require("./usdc-option-client");
const usdc_perpetual_client_1 = require("./usdc-perpetual-client");
const unified_margin_client_1 = require("./unified-margin-client");
const contract_client_1 = require("./contract-client");
const node_support_1 = require("./util/node-support");
const WsStore_1 = __importDefault(require("./util/WsStore"));
const util_1 = require("./util");
const usdc_option_client_1 = require("./usdc-option-client");
const usdc_perpetual_client_1 = require("./usdc-perpetual-client");
const unified_margin_client_1 = require("./unified-margin-client");
const loggerCategory = { category: 'bybit-ws' };

@@ -38,2 +39,4 @@ class WebsocketClient extends events_1.EventEmitter {

this.prepareRESTClient();
// add default error handling so this doesn't crash node (if the user didn't set a handler)
this.on('error', () => { });
}

@@ -115,2 +118,7 @@ /**

}
case 'contractInverse':
case 'contractUSDT': {
this.restClient = new contract_client_1.ContractClient(this.options.restOptions, this.options.requestOptions);
break;
}
default: {

@@ -140,2 +148,3 @@ throw util_1.neverGuard(this.options.market, `prepareRESTClient(): Unhandled market`);

const keys = this.wsStore.getKeys();
this.logger.info(`Closing all ws connections: ${keys}`);
keys.forEach((key) => {

@@ -161,3 +170,5 @@ this.close(key, force);

case 'unifiedPerp':
case 'unifiedOption': {
case 'unifiedOption':
case 'contractUSDT':
case 'contractInverse': {
return [...this.connectPublic(), this.connectPrivate()];

@@ -199,2 +210,6 @@ }

}
case 'contractUSDT':
return [this.connect(util_1.WS_KEY_MAP.contractUSDTPublic)];
case 'contractInverse':
return [this.connect(util_1.WS_KEY_MAP.contractInversePublic)];
default: {

@@ -229,2 +244,6 @@ throw util_1.neverGuard(this.options.market, `connectPublic(): Unhandled market`);

}
case 'contractUSDT':
return this.connect(util_1.WS_KEY_MAP.contractUSDTPrivate);
case 'contractInverse':
return this.connect(util_1.WS_KEY_MAP.contractInversePrivate);
default: {

@@ -272,3 +291,10 @@ throw util_1.neverGuard(this.options.market, `connectPrivate(): Unhandled market`);

default:
this.logger.error(`${context} due to unexpected response error: "${(error === null || error === void 0 ? void 0 : error.msg) || (error === null || error === void 0 ? void 0 : error.message) || error}"`, Object.assign(Object.assign({}, loggerCategory), { wsKey, error }));
if (this.wsStore.getConnectionState(wsKey) !==
util_1.WsConnectionStateEnum.CLOSING) {
this.logger.error(`${context} due to unexpected response error: "${(error === null || error === void 0 ? void 0 : error.msg) || (error === null || error === void 0 ? void 0 : error.message) || error}"`, Object.assign(Object.assign({}, loggerCategory), { wsKey, error }));
this.executeReconnectableClose(wsKey, 'unhandled onWsError');
}
else {
this.logger.info(`${wsKey} socket forcefully closed. Will not reconnect.`);
}
break;

@@ -339,2 +365,3 @@ }

reconnectWithDelay(wsKey, connectionDelayMs) {
var _a;
this.clearTimers(wsKey);

@@ -345,4 +372,8 @@ if (this.wsStore.getConnectionState(wsKey) !==

}
if ((_a = this.wsStore.get(wsKey)) === null || _a === void 0 ? void 0 : _a.activeReconnectTimer) {
this.clearReconnectTimer(wsKey);
}
this.wsStore.get(wsKey, true).activeReconnectTimer = setTimeout(() => {
this.logger.info('Reconnecting to websocket', Object.assign(Object.assign({}, loggerCategory), { wsKey }));
this.clearReconnectTimer(wsKey);
this.connect(wsKey);

@@ -358,17 +389,28 @@ }, connectionDelayMs);

this.tryWsSend(wsKey, JSON.stringify({ op: 'ping' }));
this.wsStore.get(wsKey, true).activePongTimer = setTimeout(() => {
var _a;
this.logger.info('Pong timeout - closing socket to reconnect', Object.assign(Object.assign({}, loggerCategory), { wsKey }));
(_a = this.getWs(wsKey)) === null || _a === void 0 ? void 0 : _a.terminate();
delete this.wsStore.get(wsKey, true).activePongTimer;
}, this.options.pongTimeout);
this.wsStore.get(wsKey, true).activePongTimer = setTimeout(() => this.executeReconnectableClose(wsKey, 'Pong timeout'), this.options.pongTimeout);
}
clearTimers(wsKey) {
/**
* Closes a connection, if it's even open. If open, this will trigger a reconnect asynchronously.
* If closed, trigger a reconnect immediately
*/
executeReconnectableClose(wsKey, reason) {
var _a;
this.logger.info(`${reason} - closing socket to reconnect`, Object.assign(Object.assign({}, loggerCategory), { wsKey,
reason }));
const wasOpen = this.wsStore.isWsOpen(wsKey);
(_a = this.getWs(wsKey)) === null || _a === void 0 ? void 0 : _a.terminate();
delete this.wsStore.get(wsKey, true).activePongTimer;
this.clearPingTimer(wsKey);
this.clearPongTimer(wsKey);
const wsState = this.wsStore.get(wsKey);
if (wsState === null || wsState === void 0 ? void 0 : wsState.activeReconnectTimer) {
clearTimeout(wsState.activeReconnectTimer);
if (!wasOpen) {
this.logger.info(`${reason} - socket already closed - trigger immediate reconnect`, Object.assign(Object.assign({}, loggerCategory), { wsKey,
reason }));
this.reconnectWithDelay(wsKey, this.options.reconnectTimeout);
}
}
clearTimers(wsKey) {
this.clearPingTimer(wsKey);
this.clearPongTimer(wsKey);
this.clearReconnectTimer(wsKey);
}
// Send a ping at intervals

@@ -390,2 +432,9 @@ clearPingTimer(wsKey) {

}
clearReconnectTimer(wsKey) {
const wsState = this.wsStore.get(wsKey);
if (wsState === null || wsState === void 0 ? void 0 : wsState.activeReconnectTimer) {
clearTimeout(wsState.activeReconnectTimer);
wsState.activeReconnectTimer = undefined;
}
}
/**

@@ -465,3 +514,3 @@ * @private Use the `subscribe(topics)` method to subscribe to topics. Send WS message to subscribe to topics.

ws.onmessage = (event) => this.onWsMessage(event, wsKey);
ws.onerror = (event) => this.onWsError(event, wsKey);
ws.onerror = (event) => this.parseWsError('Websocket onWsError', event, wsKey);
ws.onclose = (event) => this.onWsClose(event, wsKey);

@@ -536,5 +585,2 @@ return ws;

}
onWsError(error, wsKey) {
this.parseWsError('Websocket error', error, wsKey);
}
onWsClose(event, wsKey) {

@@ -609,2 +655,14 @@ this.logger.info('Websocket connection closed', Object.assign(Object.assign({}, loggerCategory), { wsKey }));

}
case util_1.WS_KEY_MAP.contractInversePrivate: {
return util_1.WS_BASE_URL_MAP.contractInverse.private[networkKey];
}
case util_1.WS_KEY_MAP.contractInversePublic: {
return util_1.WS_BASE_URL_MAP.contractInverse.public[networkKey];
}
case util_1.WS_KEY_MAP.contractUSDTPrivate: {
return util_1.WS_BASE_URL_MAP.contractUSDT.private[networkKey];
}
case util_1.WS_KEY_MAP.contractUSDTPublic: {
return util_1.WS_BASE_URL_MAP.contractUSDT.public[networkKey];
}
default: {

@@ -611,0 +669,0 @@ this.logger.error('getWsUrl(): Unhandled wsKey: ', Object.assign(Object.assign({}, loggerCategory), { wsKey }));

{
"name": "bybit-api",
"version": "3.1.3",
"version": "3.2.0",
"description": "Complete & robust node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & integration tests.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -14,3 +14,3 @@ # Node.js & Typescript Bybit API SDK

- TypeScript support (with type declarations for most API requests & responses).
- Over 300 integration tests making real API calls & WebSocket connections, validating any changes before they reach npm.
- Over 300 end-to-end tests making real API calls & WebSocket connections, validating any changes before they reach npm.
- Robust WebSocket integration with configurable connection heartbeats & automatic reconnect then resubscribe workflows.

@@ -29,6 +29,6 @@ - Browser support (via webpack bundle - see "Browser Usage" below).

- Try my connectors:
- [ftx-api](https://www.npmjs.com/package/ftx-api)
- [binance](https://www.npmjs.com/package/binance)
- [bybit-api](https://www.npmjs.com/package/bybit-api)
- [binance](https://www.npmjs.com/package/binance)
- [okx-api](https://www.npmjs.com/package/okx-api)
- [ftx-api](https://www.npmjs.com/package/ftx-api)
- Try my misc utilities:

@@ -185,2 +185,4 @@ - [orderbooks](https://www.npmjs.com/package/orderbooks)

| USDC Options | `market: 'usdcOption'`| The [USDC options](https://bybit-exchange.github.io/docs/usdc/option/#t-websocket) category. |
| Contract v3 USDT | `market: 'contractUSDT'`| The [Contract V3](https://bybit-exchange.github.io/docs/derivativesV3/contract/#t-websocket) category (USDT perps) |
| Contract v3 Inverse | `market: 'contractInverse'`| The [Contract V3](https://bybit-exchange.github.io/docs/derivativesV3/contract/#t-websocket) category (inverse perps) |

@@ -187,0 +189,0 @@ ```javascript

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc