spot v3 ws support with tests
This commit is contained in:
124
test/spot/ws.private.v3.test.ts
Normal file
124
test/spot/ws.private.v3.test.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import {
|
||||
WebsocketClient,
|
||||
WSClientConfigurableOptions,
|
||||
WS_ERROR_ENUM,
|
||||
WS_KEY_MAP,
|
||||
} from '../../src';
|
||||
import {
|
||||
silentLogger,
|
||||
waitForSocketEvent,
|
||||
WS_OPEN_EVENT_PARTIAL,
|
||||
} from '../ws.util';
|
||||
|
||||
describe('Private Spot V3 Websocket Client', () => {
|
||||
const API_KEY = process.env.API_KEY_COM;
|
||||
const API_SECRET = process.env.API_SECRET_COM;
|
||||
|
||||
const wsClientOptions: WSClientConfigurableOptions = {
|
||||
market: 'spotv3',
|
||||
key: API_KEY,
|
||||
secret: API_SECRET,
|
||||
};
|
||||
const wsTopic = `outboundAccountInfo`;
|
||||
|
||||
describe('with invalid credentials', () => {
|
||||
it('should reject private subscribe if keys/signature are incorrect', async () => {
|
||||
const badClient = new WebsocketClient(
|
||||
{
|
||||
...wsClientOptions,
|
||||
key: 'bad',
|
||||
secret: 'bad',
|
||||
},
|
||||
silentLogger
|
||||
);
|
||||
|
||||
// const wsOpenPromise = waitForSocketEvent(badClient, 'open');
|
||||
const wsResponsePromise = waitForSocketEvent(badClient, 'response');
|
||||
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
|
||||
|
||||
badClient.connectPrivate();
|
||||
badClient.subscribe(wsTopic);
|
||||
|
||||
expect(wsResponsePromise).rejects.toMatchObject({
|
||||
ret_code: WS_ERROR_ENUM.BAD_API_KEY_SPOT_V3,
|
||||
ret_msg: expect.any(String),
|
||||
type: 'error',
|
||||
});
|
||||
|
||||
try {
|
||||
await Promise.all([wsResponsePromise]);
|
||||
} catch (e) {
|
||||
// console.error()
|
||||
}
|
||||
badClient.closeAll();
|
||||
});
|
||||
});
|
||||
|
||||
describe('with valid API credentails', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
it('should have api credentials to test with', () => {
|
||||
expect(API_KEY).toStrictEqual(expect.any(String));
|
||||
expect(API_SECRET).toStrictEqual(expect.any(String));
|
||||
});
|
||||
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
|
||||
wsClient.connectPrivate();
|
||||
// logAllEvents(wsClient);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
wsClient.closeAll();
|
||||
});
|
||||
|
||||
it('should open a private ws connection', async () => {
|
||||
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
|
||||
|
||||
expect(wsOpenPromise).resolves.toMatchObject({
|
||||
event: WS_OPEN_EVENT_PARTIAL,
|
||||
wsKey: WS_KEY_MAP.spotV3Private,
|
||||
});
|
||||
|
||||
try {
|
||||
await Promise.all([wsOpenPromise]);
|
||||
} catch (e) {
|
||||
expect(e).toBeFalsy();
|
||||
}
|
||||
|
||||
try {
|
||||
expect(await wsResponsePromise).toMatchObject({
|
||||
op: 'auth',
|
||||
success: true,
|
||||
req_id: `${WS_KEY_MAP.spotV3Private}-auth`,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(`Wait for "${wsTopic}" event exception: `, e);
|
||||
expect(e).toBeFalsy();
|
||||
}
|
||||
});
|
||||
|
||||
it('should subscribe to private outboundAccountInfo events', async () => {
|
||||
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
|
||||
|
||||
// expect(wsUpdatePromise).resolves.toStrictEqual('');
|
||||
wsClient.subscribe(wsTopic);
|
||||
|
||||
try {
|
||||
expect(await wsResponsePromise).toMatchObject({
|
||||
op: 'subscribe',
|
||||
success: true,
|
||||
ret_msg: '',
|
||||
req_id: wsTopic,
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(
|
||||
`Wait for "${wsTopic}" subscription response exception: `,
|
||||
e
|
||||
);
|
||||
expect(e).toBeFalsy();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import {
|
||||
logAllEvents,
|
||||
silentLogger,
|
||||
fullLogger,
|
||||
waitForSocketEvent,
|
||||
WS_OPEN_EVENT_PARTIAL,
|
||||
} from '../ws.util';
|
||||
@@ -20,7 +21,7 @@ describe('Public Spot V1 Websocket Client', () => {
|
||||
beforeAll(() => {
|
||||
wsClient = new WebsocketClient(wsClientOptions, silentLogger);
|
||||
wsClient.connectPublic();
|
||||
// logAllEvents(wsClient);
|
||||
logAllEvents(wsClient);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import {
|
||||
logAllEvents,
|
||||
silentLogger,
|
||||
fullLogger,
|
||||
waitForSocketEvent,
|
||||
WS_OPEN_EVENT_PARTIAL,
|
||||
} from '../ws.util';
|
||||
@@ -14,7 +15,7 @@ describe('Public Spot V3 Websocket Client', () => {
|
||||
let wsClient: WebsocketClient;
|
||||
|
||||
const wsClientOptions: WSClientConfigurableOptions = {
|
||||
market: 'spotV3',
|
||||
market: 'spotv3',
|
||||
};
|
||||
|
||||
beforeAll(() => {
|
||||
@@ -42,15 +43,27 @@ describe('Public Spot V3 Websocket Client', () => {
|
||||
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
|
||||
const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
|
||||
|
||||
const wsTopic = 'orderbook.40.BTCUSDT';
|
||||
const symbol = 'BTCUSDT';
|
||||
const wsTopic = `orderbook.40.${symbol}`;
|
||||
|
||||
expect(wsResponsePromise).resolves.toMatchObject({
|
||||
request: {
|
||||
args: [wsTopic],
|
||||
op: 'subscribe',
|
||||
},
|
||||
op: 'subscribe',
|
||||
success: true,
|
||||
ret_msg: 'subscribe',
|
||||
req_id: wsTopic,
|
||||
});
|
||||
|
||||
expect(wsUpdatePromise).resolves.toMatchObject({
|
||||
data: {
|
||||
a: expect.any(Array),
|
||||
b: expect.any(Array),
|
||||
s: symbol,
|
||||
t: expect.any(Number),
|
||||
},
|
||||
topic: wsTopic,
|
||||
ts: expect.any(Number),
|
||||
type: 'delta',
|
||||
});
|
||||
expect(wsUpdatePromise).resolves.toStrictEqual('');
|
||||
|
||||
wsClient.subscribe(wsTopic);
|
||||
|
||||
|
||||
@@ -5,8 +5,17 @@ export const silentLogger = {
|
||||
debug: () => {},
|
||||
notice: () => {},
|
||||
info: () => {},
|
||||
warning: () => {},
|
||||
error: () => {},
|
||||
warning: (...params) => console.warn('warning', ...params),
|
||||
error: (...params) => console.error('error', ...params),
|
||||
};
|
||||
|
||||
export const fullLogger = {
|
||||
silly: (...params) => console.log('silly', ...params),
|
||||
debug: (...params) => console.log('debug', ...params),
|
||||
notice: (...params) => console.log('notice', ...params),
|
||||
info: (...params) => console.info('info', ...params),
|
||||
warning: (...params) => console.warn('warning', ...params),
|
||||
error: (...params) => console.error('error', ...params),
|
||||
};
|
||||
|
||||
export const WS_OPEN_EVENT_PARTIAL = {
|
||||
@@ -26,20 +35,29 @@ export function waitForSocketEvent(
|
||||
);
|
||||
}, timeoutMs);
|
||||
|
||||
function cleanup() {
|
||||
clearTimeout(timeout);
|
||||
resolvedOnce = true;
|
||||
wsClient.removeListener(event, (e) => resolver(e));
|
||||
wsClient.removeListener('error', (e) => rejector(e));
|
||||
}
|
||||
|
||||
let resolvedOnce = false;
|
||||
|
||||
wsClient.on(event, (event) => {
|
||||
clearTimeout(timeout);
|
||||
function resolver(event) {
|
||||
resolve(event);
|
||||
resolvedOnce = true;
|
||||
});
|
||||
cleanup();
|
||||
}
|
||||
|
||||
wsClient.on('error', (event) => {
|
||||
clearTimeout(timeout);
|
||||
function rejector(event) {
|
||||
if (!resolvedOnce) {
|
||||
reject(event);
|
||||
}
|
||||
});
|
||||
cleanup();
|
||||
}
|
||||
|
||||
wsClient.on(event, (e) => resolver(e));
|
||||
wsClient.on('error', (e) => rejector(e));
|
||||
|
||||
// if (event !== 'close') {
|
||||
// wsClient.on('close', (event) => {
|
||||
|
||||
Reference in New Issue
Block a user