From c02a2f0c759882664dbea545e3d7fa451b1abfb7 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Sat, 17 Sep 2022 11:33:49 +0100 Subject: [PATCH] clean extract linear req types --- src/linear-client.ts | 216 ++++++++++++--------------------- src/types/request/index.ts | 1 + src/types/request/linear.ts | 190 +++++++++++++++++++++++++++++ src/types/request/usdt-perp.ts | 27 ----- 4 files changed, 267 insertions(+), 167 deletions(-) create mode 100644 src/types/request/linear.ts delete mode 100644 src/types/request/usdt-perp.ts diff --git a/src/linear-client.ts b/src/linear-client.ts index 32b6754..ca8e52c 100644 --- a/src/linear-client.ts +++ b/src/linear-client.ts @@ -4,7 +4,26 @@ import { APIResponseWithTime, AssetExchangeRecordsReq, CoinParam, + LinearCancelConditionalOrderRequest, + LinearCancelOrderRequest, + LinearConditionalOrderRequest, + LinearGetClosedPnlRequest, + LinearGetConditionalOrderRequest, + LinearGetOrderRequest, + LinearGetOrdersRequest, + LinearGetTradeRecordsRequest, LinearOrder, + LinearQueryConditionalOrderRequest, + LinearReplaceConditionalOrderRequest, + LinearReplaceOrderRequest, + LinearSetAddReduceMarginRequest, + LinearSetAutoAddMarginRequest, + LinearSetMarginSwitchRequest, + LinearSetPositionModeRequest, + LinearSetPositionTpSlModeRequest, + LinearSetRiskLimitRequest, + LinearSetTradingStopRequest, + LinearSetUserLeverageRequest, NewLinearOrder, PerpPosition, PerpPositionRoot, @@ -178,23 +197,15 @@ export class LinearClient extends BaseRestClient { return this.postPrivate('private/linear/order/create', params); } - getActiveOrderList(params: { - order_id?: string; - order_link_id?: string; - symbol: string; - order?: string; - page?: number; - limit?: number; - order_status?: string; - }): Promise> { + getActiveOrderList( + params: LinearGetOrdersRequest + ): Promise> { return this.getPrivate('private/linear/order/list', params); } - cancelActiveOrder(params: { - symbol: string; - order_id?: string; - order_link_id?: string; - }): Promise> { + cancelActiveOrder( + params: LinearCancelOrderRequest + ): Promise> { return this.postPrivate('private/linear/order/cancel', params); } @@ -204,25 +215,15 @@ export class LinearClient extends BaseRestClient { return this.postPrivate('private/linear/order/cancel-all', params); } - replaceActiveOrder(params: { - order_id?: string; - order_link_id?: string; - symbol: string; - p_r_qty?: number; - p_r_price?: number; - take_profit?: number; - stop_loss?: number; - tp_trigger_by?: string; - sl_trigger_by?: string; - }): Promise> { + replaceActiveOrder( + params: LinearReplaceOrderRequest + ): Promise> { return this.postPrivate('private/linear/order/replace', params); } - queryActiveOrder(params: { - order_id?: string; - order_link_id?: string; - symbol: string; - }): Promise> { + queryActiveOrder( + params: LinearGetOrderRequest + ): Promise> { return this.getPrivate('private/linear/order/search', params); } @@ -230,44 +231,21 @@ export class LinearClient extends BaseRestClient { * Conditional orders */ - placeConditionalOrder(params: { - side: string; - symbol: string; - order_type: string; - qty: number; - price?: number; - base_price: number; - stop_px: number; - time_in_force: string; - trigger_by?: string; - close_on_trigger?: boolean; - order_link_id?: string; - reduce_only: boolean; - take_profit?: number; - stop_loss?: number; - tp_trigger_by?: string; - sl_trigger_by?: string; - }): Promise> { + placeConditionalOrder( + params: LinearConditionalOrderRequest + ): Promise> { return this.postPrivate('private/linear/stop-order/create', params); } - getConditionalOrder(params: { - stop_order_id?: string; - order_link_id?: string; - symbol: string; - stop_order_status?: string; - order?: string; - page?: number; - limit?: number; - }): Promise> { + getConditionalOrder( + params: LinearGetConditionalOrderRequest + ): Promise> { return this.getPrivate('private/linear/stop-order/list', params); } - cancelConditionalOrder(params: { - symbol: string; - stop_order_id?: string; - order_link_id?: string; - }): Promise> { + cancelConditionalOrder( + params: LinearCancelConditionalOrderRequest + ): Promise> { return this.postPrivate('private/linear/stop-order/cancel', params); } @@ -277,26 +255,15 @@ export class LinearClient extends BaseRestClient { return this.postPrivate('private/linear/stop-order/cancel-all', params); } - replaceConditionalOrder(params: { - stop_order_id?: string; - order_link_id?: string; - symbol: string; - p_r_qty?: number; - p_r_price?: number; - p_r_trigger_price?: number; - take_profit?: number; - stop_loss?: number; - tp_trigger_by?: string; - sl_trigger_by?: string; - }): Promise> { + replaceConditionalOrder( + params: LinearReplaceConditionalOrderRequest + ): Promise> { return this.postPrivate('private/linear/stop-order/replace', params); } - queryConditionalOrder(params: { - symbol: string; - stop_order_id?: string; - order_link_id?: string; - }): Promise> { + queryConditionalOrder( + params: LinearQueryConditionalOrderRequest + ): Promise> { return this.getPrivate('private/linear/stop-order/search', params); } @@ -315,33 +282,27 @@ export class LinearClient extends BaseRestClient { return this.getPrivate('private/linear/position/list', params); } - setAutoAddMargin(params?: { - symbol: string; - side: string; - auto_add_margin: boolean; - }): Promise> { + setAutoAddMargin( + params?: LinearSetAutoAddMarginRequest + ): Promise> { return this.postPrivate( 'private/linear/position/set-auto-add-margin', params ); } - setMarginSwitch(params?: { - symbol: string; - is_isolated: boolean; - buy_leverage: number; - sell_leverage: number; - }): Promise> { + setMarginSwitch( + params?: LinearSetMarginSwitchRequest + ): Promise> { return this.postPrivate('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]; - }): Promise> { + setPositionMode( + params: LinearSetPositionModeRequest + ): Promise> { return this.postPrivate('private/linear/position/switch-mode', params); } @@ -349,62 +310,39 @@ export class LinearClient extends BaseRestClient { * 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]; - }): Promise> { + setPositionTpSlMode( + params: LinearSetPositionTpSlModeRequest + ): Promise> { return this.postPrivate('private/linear/tpsl/switch-mode', params); } - setAddReduceMargin(params?: { - symbol: string; - side: string; - margin: number; - }): Promise> { + setAddReduceMargin( + params?: LinearSetAddReduceMarginRequest + ): Promise> { return this.postPrivate('private/linear/position/add-margin', params); } - setUserLeverage(params: { - symbol: string; - buy_leverage: number; - sell_leverage: number; - }): Promise> { + setUserLeverage( + params: LinearSetUserLeverageRequest + ): Promise> { return this.postPrivate('private/linear/position/set-leverage', params); } - setTradingStop(params: { - symbol: string; - side: string; - take_profit?: number; - stop_loss?: number; - trailing_stop?: number; - tp_trigger_by?: string; - sl_trigger_by?: string; - sl_size?: number; - tp_size?: number; - }): Promise> { + setTradingStop( + params: LinearSetTradingStopRequest + ): Promise> { return this.postPrivate('private/linear/position/trading-stop', params); } - getTradeRecords(params: { - symbol: string; - start_time?: number; - end_time?: number; - exec_type?: string; - page?: number; - limit?: number; - }): Promise> { + getTradeRecords( + params: LinearGetTradeRecordsRequest + ): Promise> { return this.getPrivate('private/linear/trade/execution/list', params); } - getClosedPnl(params: { - symbol: string; - start_time?: number; - end_time?: number; - exec_type?: string; - page?: number; - limit?: number; - }): Promise> { + getClosedPnl( + params: LinearGetClosedPnlRequest + ): Promise> { return this.getPrivate('private/linear/trade/closed-pnl/list', params); } @@ -416,11 +354,9 @@ export class LinearClient extends BaseRestClient { return this.getPrivate('public/linear/risk-limit', params); } - setRiskLimit(params: { - symbol: string; - side: string; - risk_id: number; - }): Promise> { + setRiskLimit( + params: LinearSetRiskLimitRequest + ): Promise> { return this.postPrivate('private/linear/position/set-risk', params); } diff --git a/src/types/request/index.ts b/src/types/request/index.ts index 88721cd..de89375 100644 --- a/src/types/request/index.ts +++ b/src/types/request/index.ts @@ -1,5 +1,6 @@ export * from './account-asset'; export * from './copy-trading'; +export * from './linear'; export * from './inverse'; export * from './spot'; export * from './usdt-perp'; diff --git a/src/types/request/linear.ts b/src/types/request/linear.ts new file mode 100644 index 0000000..6901a24 --- /dev/null +++ b/src/types/request/linear.ts @@ -0,0 +1,190 @@ +import { + LinearPositionIdx, + linearPositionModeEnum, + positionTpSlModeEnum, +} from '../../constants/enum'; +import { OrderSide } from '../shared'; + +export interface LinearGetOrdersRequest { + order_id?: string; + order_link_id?: string; + symbol: string; + order?: string; + page?: number; + limit?: number; + order_status?: string; +} + +export interface LinearCancelOrderRequest { + symbol: string; + order_id?: string; + order_link_id?: string; +} + +export interface LinearReplaceOrderRequest { + order_id?: string; + order_link_id?: string; + symbol: string; + p_r_qty?: number; + p_r_price?: number; + take_profit?: number; + stop_loss?: number; + tp_trigger_by?: string; + sl_trigger_by?: string; +} + +export interface LinearGetOrderRequest { + order_id?: string; + order_link_id?: string; + symbol: string; +} + +export type LinearOrderType = 'Limit' | 'Market'; + +export type LinearTimeInForce = + | 'GoodTillCancel' + | 'ImmediateOrCancel' + | 'FillOrKill' + | 'PostOnly'; + +export interface NewLinearOrder { + side: OrderSide; + symbol: string; + order_type: LinearOrderType; + qty: number; + price?: number; + time_in_force: LinearTimeInForce; + take_profit?: number; + stop_loss?: number; + tp_trigger_by?: string; + sl_trigger_by?: string; + reduce_only: boolean; + close_on_trigger: boolean; + order_link_id?: string; + position_idx?: LinearPositionIdx; +} + +export interface LinearConditionalOrderRequest { + side: string; + symbol: string; + order_type: string; + qty: number; + price?: number; + base_price: number; + stop_px: number; + time_in_force: string; + trigger_by?: string; + close_on_trigger?: boolean; + order_link_id?: string; + reduce_only: boolean; + take_profit?: number; + stop_loss?: number; + tp_trigger_by?: string; + sl_trigger_by?: string; +} + +export interface LinearGetConditionalOrderRequest { + stop_order_id?: string; + order_link_id?: string; + symbol: string; + stop_order_status?: string; + order?: string; + page?: number; + limit?: number; +} + +export interface LinearCancelConditionalOrderRequest { + symbol: string; + stop_order_id?: string; + order_link_id?: string; +} + +export interface LinearReplaceConditionalOrderRequest { + stop_order_id?: string; + order_link_id?: string; + symbol: string; + p_r_qty?: number; + p_r_price?: number; + p_r_trigger_price?: number; + take_profit?: number; + stop_loss?: number; + tp_trigger_by?: string; + sl_trigger_by?: string; +} + +export interface LinearQueryConditionalOrderRequest { + symbol: string; + stop_order_id?: string; + order_link_id?: string; +} + +export interface LinearSetAutoAddMarginRequest { + symbol: string; + side: string; + auto_add_margin: boolean; +} + +export interface LinearSetMarginSwitchRequest { + symbol: string; + is_isolated: boolean; + buy_leverage: number; + sell_leverage: number; +} + +export interface LinearSetPositionModeRequest { + symbol: string; + mode: typeof linearPositionModeEnum[keyof typeof linearPositionModeEnum]; +} + +export interface LinearSetPositionTpSlModeRequest { + symbol: string; + tp_sl_mode: typeof positionTpSlModeEnum[keyof typeof positionTpSlModeEnum]; +} + +export interface LinearSetAddReduceMarginRequest { + symbol: string; + side: string; + margin: number; +} + +export interface LinearSetUserLeverageRequest { + symbol: string; + buy_leverage: number; + sell_leverage: number; +} + +export interface LinearSetTradingStopRequest { + symbol: string; + side: string; + take_profit?: number; + stop_loss?: number; + trailing_stop?: number; + tp_trigger_by?: string; + sl_trigger_by?: string; + sl_size?: number; + tp_size?: number; +} + +export interface LinearGetTradeRecordsRequest { + symbol: string; + start_time?: number; + end_time?: number; + exec_type?: string; + page?: number; + limit?: number; +} + +export interface LinearGetClosedPnlRequest { + symbol: string; + start_time?: number; + end_time?: number; + exec_type?: string; + page?: number; + limit?: number; +} + +export interface LinearSetRiskLimitRequest { + symbol: string; + side: string; + risk_id: number; +} diff --git a/src/types/request/usdt-perp.ts b/src/types/request/usdt-perp.ts deleted file mode 100644 index f9bf6e7..0000000 --- a/src/types/request/usdt-perp.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { LinearPositionIdx } from '../../constants/enum'; -import { OrderSide } from '../shared'; - -export type LinearOrderType = 'Limit' | 'Market'; - -export type LinearTimeInForce = - | 'GoodTillCancel' - | 'ImmediateOrCancel' - | 'FillOrKill' - | 'PostOnly'; - -export interface NewLinearOrder { - side: OrderSide; - symbol: string; - order_type: LinearOrderType; - qty: number; - price?: number; - time_in_force: LinearTimeInForce; - take_profit?: number; - stop_loss?: number; - tp_trigger_by?: string; - sl_trigger_by?: string; - reduce_only: boolean; - close_on_trigger: boolean; - order_link_id?: string; - position_idx?: LinearPositionIdx; -}