From 81b8a34522fe108d5383752398c6332b58f1a5e6 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Wed, 6 Apr 2022 10:57:56 +0100 Subject: [PATCH] v2.1.10: feat(#144): add missing endpoint for linear position mode. Type improvements & linting. --- .prettierrc | 4 ++ package.json | 2 +- src/constants/enum.ts | 11 ++++ src/index.ts | 18 ++++--- src/linear-client.ts | 123 +++++++++++++++++++++++++++++++++--------- 5 files changed, 125 insertions(+), 33 deletions(-) create mode 100644 .prettierrc create mode 100644 src/constants/enum.ts diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..1fb73bc --- /dev/null +++ b/.prettierrc @@ -0,0 +1,4 @@ +{ + "tabWidth": 2, + "singleQuote": true +} diff --git a/package.json b/package.json index 5df792a..7751980 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bybit-api", - "version": "2.1.9", + "version": "2.1.10", "description": "Node.js connector for Bybit's REST APIs and WebSockets, with TypeScript & integration tests.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/constants/enum.ts b/src/constants/enum.ts new file mode 100644 index 0000000..06aee36 --- /dev/null +++ b/src/constants/enum.ts @@ -0,0 +1,11 @@ +export const linearPositionModeEnum = { + OneWayMode: 'MergedSingle', + HedgeMode: 'BothSide', +} as const; + +export const positionTpSlModeEnum = { + /** Full take profit/stop loss mode (a single TP order and a single SL order can be placed, covering the entire position) */ + Full: 'Full', + /** Partial take profit/stop loss mode (multiple TP and SL orders can be placed, covering portions of the position) */ + Partial: 'Partial', +} as const; diff --git a/src/index.ts b/src/index.ts index ab33042..df2faad 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,10 @@ -export * from './inverse-client'; -export * from './inverse-futures-client'; -export * from './linear-client'; -export * from './spot-client'; -export * from './websocket-client'; -export * from './logger'; -export * from './types/shared'; -export * from './types/spot'; +export * from "./inverse-client"; +export * from "./inverse-futures-client"; +export * from "./linear-client"; +export * from "./spot-client"; +export * from "./websocket-client"; +export * from "./logger"; +export * from "./types/shared"; +export * from "./types/spot"; +export * from "./util/WsStore"; +export * from "./constants/enum"; diff --git a/src/linear-client.ts b/src/linear-client.ts index 9b90ae8..88dff20 100644 --- a/src/linear-client.ts +++ b/src/linear-client.ts @@ -1,8 +1,17 @@ import { AxiosRequestConfig } from 'axios'; -import { GenericAPIResponse, getRestBaseUrl, RestClientOptions } from './util/requestUtils'; +import { + GenericAPIResponse, + getRestBaseUrl, + RestClientOptions, +} from './util/requestUtils'; import RequestWrapper from './util/requestWrapper'; import SharedEndpoints from './shared-endpoints'; -import { SymbolIntervalFromLimitParam, SymbolLimitParam, SymbolParam } from '.'; +import { + SymbolIntervalFromLimitParam, + SymbolLimitParam, + SymbolParam, +} from './types/shared'; +import { linearPositionModeEnum, positionTpSlModeEnum } from './constants/enum'; export class LinearClient extends SharedEndpoints { protected requestWrapper: RequestWrapper; @@ -46,11 +55,17 @@ export class LinearClient extends SharedEndpoints { } getTrades(params: SymbolLimitParam): GenericAPIResponse { - return this.requestWrapper.get('public/linear/recent-trading-records', params); + return this.requestWrapper.get( + 'public/linear/recent-trading-records', + params + ); } getLastFundingRate(params: SymbolParam): GenericAPIResponse { - return this.requestWrapper.get('public/linear/funding/prev-funding-rate', params); + return this.requestWrapper.get( + 'public/linear/funding/prev-funding-rate', + params + ); } getMarkPriceKline(params: SymbolIntervalFromLimitParam): GenericAPIResponse { @@ -61,7 +76,9 @@ export class LinearClient extends SharedEndpoints { return this.requestWrapper.get('public/linear/index-price-kline', params); } - getPremiumIndexKline(params: SymbolIntervalFromLimitParam): GenericAPIResponse { + getPremiumIndexKline( + params: SymbolIntervalFromLimitParam + ): GenericAPIResponse { return this.requestWrapper.get('public/linear/premium-index-kline', params); } @@ -90,7 +107,7 @@ export class LinearClient extends SharedEndpoints { return this.requestWrapper.post('private/linear/order/create', params); } - getActiveOrderList(params: { + getActiveOrderList(params: { order_id?: string; order_link_id?: string; symbol: string; @@ -137,8 +154,8 @@ export class LinearClient extends SharedEndpoints { } /** - * Conditional orders - */ + * Conditional orders + */ placeConditionalOrder(params: { side: string; @@ -182,7 +199,10 @@ export class LinearClient extends SharedEndpoints { } cancelAllConditionalOrders(params: SymbolParam): GenericAPIResponse { - return this.requestWrapper.post('private/linear/stop-order/cancel-all', params); + return this.requestWrapper.post( + 'private/linear/stop-order/cancel-all', + params + ); } replaceConditionalOrder(params: { @@ -197,7 +217,10 @@ export class LinearClient extends SharedEndpoints { tp_trigger_by?: string; sl_trigger_by?: string; }): GenericAPIResponse { - return this.requestWrapper.post('private/linear/stop-order/replace', params); + return this.requestWrapper.post( + 'private/linear/stop-order/replace', + params + ); } queryConditionalOrder(params: { @@ -221,7 +244,10 @@ export class LinearClient extends SharedEndpoints { side: string; auto_add_margin: boolean; }): GenericAPIResponse { - return this.requestWrapper.post('private/linear/position/set-auto-add-margin', params); + return this.requestWrapper.post( + 'private/linear/position/set-auto-add-margin', + params + ); } setMarginSwitch(params?: { @@ -230,12 +256,40 @@ export class LinearClient extends SharedEndpoints { buy_leverage: number; sell_leverage: number; }): GenericAPIResponse { - return this.requestWrapper.post('private/linear/position/switch-isolated', params); + return this.requestWrapper.post( + 'private/linear/position/switch-isolated', + params + ); } + /** + * Switch between one-way vs hedge mode. Use `linearPositionModeEnum` for the mode parameter. + */ + setPositionMode(params: { + symbol: string; + mode: typeof linearPositionModeEnum[keyof typeof linearPositionModeEnum]; + }): GenericAPIResponse { + return this.requestWrapper.post( + 'private/linear/position/switch-mode', + params + ); + } + + /** @deprecated use setPositionTpSlMode() instead */ setSwitchMode(params?: { symbol: string; - tp_sl_mode: string; + tp_sl_mode: typeof positionTpSlModeEnum[keyof typeof positionTpSlModeEnum]; + }): GenericAPIResponse { + return this.requestWrapper.post('private/linear/tpsl/switch-mode', params); + } + + /** + * Switch TP/SL mode between full or partial. When set to Partial, TP/SL orders may have a quantity less than the position size. + * This is set with the setTradingStop() method. Use `positionTpSlModeEnum` for the tp_sl_mode parameter. + */ + setPositionTpSlMode(params: { + symbol: string; + tp_sl_mode: typeof positionTpSlModeEnum[keyof typeof positionTpSlModeEnum]; }): GenericAPIResponse { return this.requestWrapper.post('private/linear/tpsl/switch-mode', params); } @@ -245,7 +299,10 @@ export class LinearClient extends SharedEndpoints { side: string; margin: number; }): GenericAPIResponse { - return this.requestWrapper.post('private/linear/position/add-margin', params); + return this.requestWrapper.post( + 'private/linear/position/add-margin', + params + ); } setUserLeverage(params: { @@ -253,7 +310,10 @@ export class LinearClient extends SharedEndpoints { buy_leverage: number; sell_leverage: number; }): GenericAPIResponse { - return this.requestWrapper.post('private/linear/position/set-leverage', params); + return this.requestWrapper.post( + 'private/linear/position/set-leverage', + params + ); } setTradingStop(params: { @@ -267,7 +327,10 @@ export class LinearClient extends SharedEndpoints { sl_size?: number; tp_size?: number; }): GenericAPIResponse { - return this.requestWrapper.post('private/linear/position/trading-stop', params); + return this.requestWrapper.post( + 'private/linear/position/trading-stop', + params + ); } getTradeRecords(params: { @@ -278,7 +341,10 @@ export class LinearClient extends SharedEndpoints { page?: number; limit?: number; }): GenericAPIResponse { - return this.requestWrapper.get('private/linear/trade/execution/list', params); + return this.requestWrapper.get( + 'private/linear/trade/execution/list', + params + ); } getClosedPnl(params: { @@ -289,12 +355,15 @@ export class LinearClient extends SharedEndpoints { page?: number; limit?: number; }): GenericAPIResponse { - return this.requestWrapper.get('private/linear/trade/closed-pnl/list', params); + return this.requestWrapper.get( + 'private/linear/trade/closed-pnl/list', + params + ); } /** - * Risk Limit - */ + * Risk Limit + */ getRiskLimitList(params: SymbolParam): GenericAPIResponse { return this.requestWrapper.get('public/linear/risk-limit', params); @@ -309,14 +378,20 @@ export class LinearClient extends SharedEndpoints { } /** - * Funding - */ + * Funding + */ getPredictedFundingFee(params: SymbolParam): GenericAPIResponse { - return this.requestWrapper.get('private/linear/funding/predicted-funding', params); + return this.requestWrapper.get( + 'private/linear/funding/predicted-funding', + params + ); } getLastFundingFee(params: SymbolParam): GenericAPIResponse { - return this.requestWrapper.get('private/linear/funding/prev-funding', params); + return this.requestWrapper.get( + 'private/linear/funding/prev-funding', + params + ); } }