Skip to content

Commit bb3130f

Browse files
committed
feat(): refactor rest client cache out of ws client
1 parent 722a0e1 commit bb3130f

File tree

6 files changed

+184
-106
lines changed

6 files changed

+184
-106
lines changed

examples/rest-usdm-order-sl.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
12
import { USDMClient } from '../src/index';
23

34
// or
@@ -26,8 +27,8 @@ async function start() {
2627
*/
2728
const [
2829
{ positionAmt: longAmount, ...long },
29-
{ positionAmt: shortAmount, ...short }
30-
]: any = await client.getPositions({ symbol });
30+
{ positionAmt: shortAmount, ...short },
31+
]: any = await client.getPositionsV3({ symbol });
3132

3233
// if longAmount is bigger than 0 means we have open long position and if shortAmount is below 0 means we have open short position
3334
const hasLong = parseFloat(longAmount) > 0;
@@ -36,16 +37,17 @@ async function start() {
3637

3738
// if we have any open position then we continue
3839
if (hasOpen) {
39-
4040
// we get ourstop loss here
4141
const orders = await client.getAllOpenOrders({ symbol });
42-
const stopOrders = orders.filter(({ type }) => type === 'STOP_MARKET') ?? {};
42+
const stopOrders =
43+
orders.filter(({ type }) => type === 'STOP_MARKET') ?? [];
4344

44-
// we want to modify our long position SL here
45+
// we want to modify our long position SL here
4546
if (hasLong) {
46-
4747
// we get the StopLoss order which is realted to long
48-
const { orderId }: any = stopOrders.find(({ positionSide: ps }) => ps == 'LONG');
48+
const { orderId }: any = stopOrders.find(
49+
({ positionSide: ps }) => ps == 'LONG',
50+
);
4951

5052
// if it exists, cancel it.
5153
if (orderId) {
@@ -62,8 +64,8 @@ async function start() {
6264
timeInForce: 'GTC',
6365
type: 'STOP_MARKET',
6466
closePosition: 'true', // this is here because we don't have the position quantity value, and it means closee all quantity
65-
stopPrice: parseFloat((markPrice * .99).toFixed(3)), // set sl price 1% below current price
66-
workingType: 'MARK_PRICE'
67+
stopPrice: parseFloat((markPrice * 0.99).toFixed(3)), // set sl price 1% below current price
68+
workingType: 'MARK_PRICE',
6769
});
6870
console.log('SL Modifiled sell result: ', result);
6971
}

examples/ws-userdata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
isWsFormattedUserDataEvent,
77
WsUserDataEvents,
88
} from '../src';
9-
import { WebsocketClientV1 } from '../src/websocket-client-legacy';
9+
import { WebsocketClientV1 } from '../src';
1010

1111
// or
1212
// import { DefaultLogger, WebsocketClient } from 'binance';
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { AxiosRequestConfig } from 'axios';
2+
3+
import { CoinMClient } from '../../coinm-client';
4+
import { MainClient } from '../../main-client';
5+
import { USDMClient } from '../../usdm-client';
6+
import { RestClientOptions } from '../requestUtils';
7+
8+
interface RestClientStore {
9+
spot: MainClient;
10+
margin: MainClient;
11+
usdmFutures: USDMClient;
12+
usdmFuturesTestnet: USDMClient;
13+
coinmFutures: CoinMClient;
14+
coinmFuturesTestnet: CoinMClient;
15+
// options: MainClient;
16+
}
17+
18+
export class RestClientCache {
19+
private restClients: Partial<RestClientStore>;
20+
21+
public getSpotRestClient(
22+
restOptions: RestClientOptions,
23+
requestOptions?: AxiosRequestConfig,
24+
): MainClient {
25+
if (!this.restClients.spot) {
26+
this.restClients.spot = new MainClient(restOptions, requestOptions);
27+
}
28+
return this.restClients.spot;
29+
}
30+
31+
public getUSDMRestClient(
32+
restOptions: RestClientOptions,
33+
requestOptions?: AxiosRequestConfig,
34+
isTestnet?: boolean,
35+
): USDMClient {
36+
if (isTestnet) {
37+
if (!this.restClients.usdmFuturesTestnet) {
38+
this.restClients.usdmFuturesTestnet = new USDMClient(
39+
restOptions,
40+
requestOptions,
41+
isTestnet, // TODO: migrate to using restOptions param instead, deprecate this flag
42+
);
43+
}
44+
return this.restClients.usdmFuturesTestnet;
45+
}
46+
47+
if (!this.restClients.usdmFutures) {
48+
this.restClients.usdmFutures = new USDMClient(
49+
restOptions,
50+
requestOptions,
51+
);
52+
}
53+
return this.restClients.usdmFutures;
54+
}
55+
56+
public getCOINMRestClient(
57+
restOptions: RestClientOptions,
58+
requestOptions?: AxiosRequestConfig,
59+
isTestnet?: boolean,
60+
): CoinMClient {
61+
if (isTestnet) {
62+
if (!this.restClients.coinmFuturesTestnet) {
63+
this.restClients.coinmFuturesTestnet = new CoinMClient(
64+
restOptions,
65+
requestOptions,
66+
isTestnet, // TODO: migrate to using restOptions param instead, deprecate this flag
67+
);
68+
}
69+
return this.restClients.coinmFuturesTestnet;
70+
}
71+
72+
if (!this.restClients.coinmFutures) {
73+
this.restClients.coinmFutures = new CoinMClient(
74+
restOptions,
75+
requestOptions,
76+
);
77+
}
78+
return this.restClients.coinmFutures;
79+
}
80+
}

src/util/websockets/websocket-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export const WS_AUTH_ON_CONNECT_KEYS: WsKey[] = [
145145
// ] as WsKey[];
146146

147147
/** Used to automatically determine if a sub request should be to the public or private ws (when there's two) */
148-
const PRIVATE_TOPICS = [];
148+
const PRIVATE_TOPICS: string[] = [];
149149

150150
/**
151151
* Normalised internal format for a request (subscribe/unsubscribe/etc) on a topic, with optional parameters.

0 commit comments

Comments
 (0)