Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ stream.latestTradeDetail$.subscribe((v) => {})
- [x] Listen Key expired push
- [x] Account balance and position update push
- [x] Order update push
- [ ] Configuration updates such as leverage and margin mode
- [x] Configuration updates such as leverage and margin mode
86 changes: 86 additions & 0 deletions src/bingx-socket/bingx-account-socket-config-update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { Server, WebSocket } from 'ws';
import { getPortFree } from 'bingx-api/get-port';
import { ApiAccount } from 'bingx-api/bingx/account/api-account';
import { BingxAccountSocketStream } from 'bingx-api/bingx-socket/bingx-account-socket-stream';
import * as zlib from 'zlib';
import { AccountWebsocketEventType } from 'bingx-api/bingx-socket/events';

describe('bingx account socket configuration update', () => {
let wss: Server;
let port: number;
let stream: BingxAccountSocketStream | undefined;
const sockets: WebSocket[] = [];

const sendToSocket = (socket: WebSocket, msg: string) => {
zlib.gzip(msg, (err, result) => {
socket.send(result);
});
};

beforeEach(async () => {
port = await getPortFree();
wss = new Server({ port });
await new Promise<void>((resolve) => {
wss.on('listening', resolve);
wss.on('connection', (ws) => {
sockets[0] = ws;
ws.on('close', () => {
sockets.splice(0, 1);
});
});
});
});

afterEach((done) => {
stream?.disconnect();
wss.close(() => {
sockets.splice(0, sockets.length);
done();
});
});

it('emits account configuration update events', (done) => {
const requestExecutorMock = {
execute: jest.fn().mockResolvedValueOnce({ listenKey: '123' }),
};
const account = new ApiAccount('xxx', 'xxx');

wss.once('connection', (socket) => {
setTimeout(() => {
sendToSocket(
socket,
JSON.stringify({
e: AccountWebsocketEventType.ACCOUNT_CONFIG_UPDATE,
E: 1676603102163,
ac: {
s: 'BTC-USDT',
l: 12,
S: 9,
mt: 'cross',
},
}),
);
}, 10);
});

stream = new BingxAccountSocketStream(account, {
requestExecutor: requestExecutorMock,
url: new URL('', `ws://0.0.0.0:${port}`),
});

stream.accountConfigurationUpdate$.subscribe((event) => {
expect(event).toStrictEqual({
e: AccountWebsocketEventType.ACCOUNT_CONFIG_UPDATE,
E: '1676603102163',
ac: {
s: 'BTC-USDT',
l: '12',
S: '9',
mt: 'cross',
},
});
expect(requestExecutorMock.execute).toHaveBeenCalledTimes(1);
done();
});
});
});
8 changes: 8 additions & 0 deletions src/bingx-socket/bingx-account-socket-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BingxWebsocketSerializer } from 'bingx-api/bingx-socket/bingx-websocket
import { HeartbeatInterface } from 'bingx-api/bingx-socket/interfaces/heartbeat.interface';
import { filterAndEmitToSubject } from 'bingx-api/bingx-socket/operators/filter-and-emit-to-subject';
import {
AccountConfigurationUpdateEvent,
AccountBalanceAndPositionPushEvent,
AccountOrderUpdatePushEvent,
AccountWebSocketEvent,
Expand All @@ -36,6 +37,8 @@ export class BingxAccountSocketStream {
new Subject<AccountBalanceAndPositionPushEvent>();
public readonly accountOrderUpdatePushEvent$ =
new Subject<AccountOrderUpdatePushEvent>();
public readonly accountConfigurationUpdate$ =
new Subject<AccountConfigurationUpdateEvent>();

constructor(
private readonly account: AccountInterface,
Expand Down Expand Up @@ -98,6 +101,11 @@ export class BingxAccountSocketStream {
event.e === AccountWebsocketEventType.ORDER_TRADE_UPDATE,
this.accountOrderUpdatePushEvent$,
),
filterAndEmitToSubject(
(event): event is AccountConfigurationUpdateEvent =>
event.e === AccountWebsocketEventType.ACCOUNT_CONFIG_UPDATE,
this.accountConfigurationUpdate$,
),
)
.subscribe();

Expand Down
14 changes: 14 additions & 0 deletions src/bingx-socket/events/account-websocket-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type EntryPrice = number;
export type UnrealizedProfitAndLossPosition = number;
export type MarginMode = string;
export type IsolatedPositionMargin = number;
export type AccountConfigLeverage = string | number;

/**
* Relates to order update events
Expand Down Expand Up @@ -140,3 +141,16 @@ export interface AccountOrderUpdatePushEvent extends AccountWebSocketEvent {
E: EventTimeInMilliseconds;
o: AccountWebSocketOrder;
}

export interface AccountConfiguration {
s: TradingPair;
l: AccountConfigLeverage;
S: AccountConfigLeverage;
mt: MarginMode;
}

export interface AccountConfigurationUpdateEvent extends AccountWebSocketEvent {
e: AccountWebsocketEventType.ACCOUNT_CONFIG_UPDATE;
E: EventTimeInMilliseconds;
ac: AccountConfiguration;
}