Skip to content

Commit

Permalink
chore(): updates for pre-V5 deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagosiebler committed Jan 24, 2025
1 parent 03169b3 commit a4945d1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 134 deletions.
74 changes: 3 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,14 @@ Node.js, JavaScript & TypeScript SDK for the Bybit REST APIs and WebSockets:
## Structure & Usage
- [Structure](#structure)
- [API Clients](#api-clients)
- [Usage](#usage)
- [REST API USAGE](#rest-api-usage)

## WebSocket Integration
- [WebSockets](#websockets)
- [WebSocket Subscriptions - Consuming Events](#websocket-subscriptions---consuming-events)
- [Websocket API - Sending Orders via WebSockets](#websocket-api---sending-orders-via-websockets)
- [Specifying Other Markets](#specifying-other-markets)
- [Load Balancing](#balancing-load-across-multiple-connections)
- [Older Websocket APIs](#older-websocket-apis)
- [Consumer Load Balancing](#balancing-load-across-multiple-connections)

## Additional Features
- [Logging](#logging)
Expand Down Expand Up @@ -154,7 +153,7 @@ Here are the available REST clients and the corresponding API groups described i
| [WebsocketClient](src/websocket-client.ts) | All WebSocket Events (Public & Private for all API categories) |


### Usage
## REST API Usage

Create API credentials on Bybit's website:

Expand Down Expand Up @@ -257,38 +256,6 @@ client.getOrderbook({ category: 'linear', symbol: 'BTCUSDT' })

---

### Deprecated/Obsolete REST APIs

The following API clients are for previous generation REST APIs and will be removed in the next major release. Some have already stopped working (because bybit stopped supporting them). You should use the V5 APIs for all new development.

<details>
<summary>Click me to read more</summary>

Each generation is labelled with the version number (e.g. v1/v2/v3/v5). New projects & developments should use the newest available API generation (e.g. use the V5 APIs instead of V3).


| Class | Description |
| :--------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------: |
| [ **Derivatives v3** ] | The Derivatives v3 APIs (successor to the Futures V2 APIs) |
| [UnifiedMarginClient](src/unified-margin-client.ts) |[Derivatives (v3) Unified Margin APIs](https://bybit-exchange.github.io/docs/derivatives/unified/place-order) |
| [ContractClient](src/contract-client.ts) | [Derivatives (v3) Contract APIs](https://bybit-exchange.github.io/docs/derivatives/contract/place-order). |
| [ **Futures v2** ] | The Futures v2 APIs |
| [~~InverseClient~~](src/inverse-client.ts) | [Inverse Perpetual Futures (v2) APIs](https://bybit-exchange.github.io/docs/futuresV2/inverse/) |
| [~~LinearClient~~](src/linear-client.ts) | [USDT Perpetual Futures (v2) APIs](https://bybit-exchange.github.io/docs/futuresV2/linear/#t-introduction) |
| [~~InverseFuturesClient~~](src/inverse-futures-client.ts) | [Inverse Futures (v2) APIs](https://bybit-exchange.github.io/docs/futuresV2/inverse_futures/#t-introduction) |
| [ **Spot** ] | The spot APIs |
| [SpotClientV3](src/spot-client-v3.ts) | [Spot Market (v3) APIs](https://bybit-exchange.github.io/docs/spot/public/instrument) |
| [~~SpotClient~~](src/spot-client.ts) (deprecated, SpotClientV3 recommended) | [Spot Market (v1) APIs](https://bybit-exchange.github.io/docs/spot/v1/#t-introduction) |
| [ **USDC Contract** ] | The USDC Contract APIs |
| [USDCPerpetualClient](src/usdc-perpetual-client.ts) | [USDC Perpetual APIs](https://bybit-exchange.github.io/docs/usdc/option/?console#t-querydeliverylog) |
| [USDCOptionClient](src/usdc-option-client.ts) | [USDC Option APIs](https://bybit-exchange.github.io/docs/usdc/option/#t-introduction) |
| [~~AccountAssetClient~~](src/account-asset-client.ts) | [Account Asset V1 APIs](https://bybit-exchange.github.io/docs/account_asset/v1/#t-introduction) |
| [ **Other** ] | Other standalone API groups |
| [CopyTradingClient](src/copy-trading-client.ts) | [Copy Trading APIs](https://bybit-exchange.github.io/docs/category/copy-trade) |
| [AccountAssetClientV3](src/account-asset-client-v3.ts) | [Account Asset V3 APIs](https://bybit-exchange.github.io/docs/account-asset/internal-transfer) |

</details>

## WebSockets

The WebsocketClient will automatically use the latest V5 WebSocket endpoints by default. To use a different endpoint, use the `market` parameter. Except for the WebSocket API - this can be accessed without any special configuration.
Expand Down Expand Up @@ -317,15 +284,6 @@ const wsConfig = {
The following parameters are optional:
*/

/**
* The API group this client should connect to. The V5 market is currently used by default.
*
* For the V3 APIs use `v3` as the market (spot/unified margin/usdc/account
* asset/copy trading).
* Note that older API groups are deprecated and may stop working soon.
*/
// market: 'v5',

/**
* Set to `true` to connect to Bybit's testnet environment.
* - If demo trading, `testnet` should be set to false!
Expand Down Expand Up @@ -553,32 +511,6 @@ Important: do not subscribe to the same topics on both clients or you will recei
---
### Older Websocket APIs
The following API groups are still available in the WebsocketClient but are deprecated and may no longer work. They will be removed in the next major release:
<details>
<summary>Click me to see the table</summary>
| API Category | Market | Description |
| :------------------------------: | :-------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ~~Unified Margin - Options~~ | `market: 'unifiedOption'` | The [derivatives v3](https://bybit-exchange.github.io/docs/derivativesV3/unified_margin/#t-websocket) category for unified margin. Note: public topics only support options topics. If you need USDC/USDT perps, use `unifiedPerp` instead. |
| ~~Unified Margin - Perps~~ | `market: 'unifiedPerp'` | The [derivatives v3](https://bybit-exchange.github.io/docs/derivativesV3/unified_margin/#t-websocket) category for unified margin. Note: public topics only support USDT/USDC perpetual topics - use `unifiedOption` if you need public options topics. |
| ~~Futures v2 - Inverse Perps~~ | `market: 'inverse'` | The [inverse v2 perps](https://bybit-exchange.github.io/docs/futuresV2/inverse/#t-websocket) category. |
| ~~Futures v2 - USDT Perps~~ | `market: 'linear'` | The [USDT/linear v2 perps](https://bybit-exchange.github.io/docs/futuresV2/linear/#t-websocket) category. |
| ~~Futures v2 - Inverse Futures~~ | `market: 'inverse'` | The [inverse futures v2](https://bybit-exchange.github.io/docs/futuresV2/inverse_futures/#t-websocket) category uses the same market as inverse perps. |
| ~~Spot v3~~ | `market: 'spotv3'` | The [spot v3](https://bybit-exchange.github.io/docs/spot/v3/#t-websocket) category. |
| ~~Spot v1~~ | `market: 'spot'` | The older [spot v1](https://bybit-exchange.github.io/docs/spot/v1/#t-websocket) category. Use the `spotv3` market if possible, as the v1 category does not have automatic re-subscribe if reconnected. |
| ~~Copy Trading~~ | `market: 'linear'` | The [copy trading](https://bybit-exchange.github.io/docs/copy_trading/#t-websocket) category. Use the linear market to listen to all copy trading topics. |
| ~~USDC Perps~~ | `market: 'usdcPerp` | The [USDC perps](https://bybit-exchange.github.io/docs/usdc/perpetual/#t-websocket) category. |
| ~~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) |
</details
---
## Logging
### Customise logging
Expand Down
38 changes: 1 addition & 37 deletions src/util/BaseRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ interface SignedRequestContext {
timestamp?: number;
api_key?: string;
recv_window?: number;
// spot v1 is diff from the rest...
recvWindow?: number;
}

interface SignedRequest<T> {
Expand All @@ -72,7 +70,7 @@ interface UnsignedRequest<T> {
recvWindow?: number;
}

type SignMethod = 'v2auth' | 'v5auth';
type SignMethod = 'v5auth';

export default abstract class BaseRestClient {
private timeOffset: number | null = null;
Expand Down Expand Up @@ -443,40 +441,6 @@ export default abstract class BaseRestClient {
return res;
}

// spot/v2 derivatives
if (signMethod === 'v2auth') {
res.originalParams.api_key = key;
res.originalParams.timestamp = timestamp;

// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
if (recvWindow) {
res.originalParams.recv_window = recvWindow;
}
const sortProperties = true;
const encodeValues = false;

res.serializedParams = serializeParams(
res.originalParams,
strictParamValidation,
sortProperties,
encodeValues,
);
res.sign = await this.signMessage(
res.serializedParams,
this.secret,
'hex',
'SHA-256',
);

// @ts-ignore
res.paramsWithSign = {
...res.originalParams,
sign: res.sign,
};

return res;
}

return res;
}

Expand Down
24 changes: 0 additions & 24 deletions src/util/requestUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { AxiosResponse } from 'axios';
import { APIRateLimit } from '../types';
import {
WebsocketSucceededTopicSubscriptionConfirmationEvent,
WebsocketTopicSubscriptionConfirmationEvent,
} from '../types/websockets/ws-confirmations';
import { WSAPIResponse, WS_API_Operations } from '../types/websockets/ws-api';

export interface RestClientOptions {
/** Your API key */
Expand Down Expand Up @@ -188,25 +183,6 @@ export function isWsPong(msg: any): boolean {
);
}

export function isTopicSubscriptionConfirmation(
msg: unknown,
): msg is WebsocketTopicSubscriptionConfirmationEvent {
if (typeof msg !== 'object') {
return false;
}
if (!msg) {
return false;
}
if (typeof msg['op'] !== 'string') {
return false;
}
if (msg['op'] !== 'subscribe') {
return false;
}

return true;
}

export const APIID = 'bybitapinode';

/**
Expand Down
25 changes: 23 additions & 2 deletions src/util/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
* Use type guards to narrow down types with minimal efforts.
*/

import { WebsocketSucceededTopicSubscriptionConfirmationEvent } from '../types';
import {
WebsocketSucceededTopicSubscriptionConfirmationEvent,
WebsocketTopicSubscriptionConfirmationEvent,
} from '../types';
import { WSAPIResponse, WS_API_Operations } from '../types/websockets/ws-api';
import {
WSAccountOrderEventV5,
WSExecutionEventV5,
WSOrderbookEventV5,
WSPositionEventV5,
} from '../types/websockets/ws-events';
import { isTopicSubscriptionConfirmation } from './requestUtils';

/**
* Type guard to detect a V5 orderbook event (delta & snapshots)
Expand Down Expand Up @@ -120,3 +122,22 @@ export function isTopicSubscriptionSuccess(
if (!isTopicSubscriptionConfirmation(msg)) return false;
return msg.success === true;
}

export function isTopicSubscriptionConfirmation(
msg: unknown,
): msg is WebsocketTopicSubscriptionConfirmationEvent {
if (typeof msg !== 'object') {
return false;
}
if (!msg) {
return false;
}
if (typeof msg['op'] !== 'string') {
return false;
}
if (msg['op'] !== 'subscribe') {
return false;
}

return true;
}

0 comments on commit a4945d1

Please sign in to comment.