forked from singlesly/bingx-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingx-account-socket-stream-pool.ts
More file actions
110 lines (97 loc) · 3.24 KB
/
Copy pathbingx-account-socket-stream-pool.ts
File metadata and controls
110 lines (97 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { AccountInterface, HttpRequestExecutor } from 'bingx-api/bingx';
import {
BingxAccountSocketStream,
BingxAccountSocketStreamConfiguration,
} from 'bingx-api/bingx-socket/bingx-account-socket-stream';
import { map, ReplaySubject, Subject } from 'rxjs';
import { HeartbeatInterface } from 'bingx-api/bingx-socket/interfaces/heartbeat.interface';
import {
AccountBalanceAndPositionPushEvent,
AccountOrderUpdatePushEvent,
ListenKeyExpiredEvent,
} from 'bingx-api/bingx-socket/events/account-websocket-events';
export class BingxAccountSocketStreamPool {
private readonly configuration: Required<BingxAccountSocketStreamConfiguration>;
private readonly accounts: Record<
string,
[AccountInterface, BingxAccountSocketStream]
> = {};
public readonly heartbeat$ = new ReplaySubject<
[AccountInterface, HeartbeatInterface]
>(1);
public readonly listenKeyExpired$ = new Subject<
[AccountInterface, ListenKeyExpiredEvent]
>();
public readonly accountBalanceAndPositionPush$ = new Subject<
[AccountInterface, AccountBalanceAndPositionPushEvent]
>();
public readonly accountOrderUpdatePushEvent$ = new Subject<
[AccountInterface, AccountOrderUpdatePushEvent]
>();
constructor(
accounts: AccountInterface[],
configuration: BingxAccountSocketStreamConfiguration = {},
) {
this.configuration = {
requestExecutor:
configuration.requestExecutor ?? new HttpRequestExecutor(),
url:
configuration.url ??
new URL('/swap-market', 'wss://open-api-swap.bingx.com'),
};
accounts.forEach((account) => this.addAccount(account));
}
public addAccount(account: AccountInterface) {
const stream = new BingxAccountSocketStream(account, this.configuration);
stream.heartbeat$
.pipe(
map<HeartbeatInterface, [AccountInterface, HeartbeatInterface]>((v) => [
account,
v,
]),
)
.subscribe((value) => {
this.heartbeat$.next(value);
});
stream.listenKeyExpired$
.pipe(
map<ListenKeyExpiredEvent, [AccountInterface, ListenKeyExpiredEvent]>(
(v) => [account, v],
),
)
.subscribe((value) => {
this.listenKeyExpired$.next(value);
});
stream.accountBalanceAndPositionPush$
.pipe(
map<
AccountBalanceAndPositionPushEvent,
[AccountInterface, AccountBalanceAndPositionPushEvent]
>((v) => [account, v]),
)
.subscribe((value) => {
this.accountBalanceAndPositionPush$.next(value);
});
stream.accountOrderUpdatePushEvent$
.pipe(
map<
AccountOrderUpdatePushEvent,
[AccountInterface, AccountOrderUpdatePushEvent]
>((v) => [account, v]),
)
.subscribe((value) => {
this.accountOrderUpdatePushEvent$.next(value);
});
if (this.accounts[account.getApiKey()]) {
this.accounts[account.getApiKey()][1].disconnect();
}
this.accounts[account.getApiKey()] = [account, stream];
}
public removeAccount(account: AccountInterface) {
if (this.accounts[account.getApiKey()]) {
const [sacrifice, stream] = this.accounts[account.getApiKey()];
stream.disconnect();
delete this.accounts[sacrifice.getApiKey()];
}
}
}