add example and test for private contract v3 websocket topics
This commit is contained in:
@@ -13,10 +13,13 @@ import { WebsocketClient, WS_KEY_MAP, DefaultLogger } from '../src';
|
|||||||
const secret = process.env.API_SECRET;
|
const secret = process.env.API_SECRET;
|
||||||
|
|
||||||
// USDT Perps:
|
// USDT Perps:
|
||||||
const market = 'linear';
|
// const market = 'linear';
|
||||||
// Inverse Perp
|
// Inverse Perp
|
||||||
// const market = 'inverse';
|
// const market = 'inverse';
|
||||||
// const market = 'spotv3';
|
// const market = 'spotv3';
|
||||||
|
// Contract v3
|
||||||
|
const market = 'contractUSDT';
|
||||||
|
// const market = 'contractInverse';
|
||||||
|
|
||||||
// Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets.
|
// Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets.
|
||||||
const wsClient = new WebsocketClient(
|
const wsClient = new WebsocketClient(
|
||||||
@@ -48,8 +51,19 @@ import { WebsocketClient, WS_KEY_MAP, DefaultLogger } from '../src';
|
|||||||
wsClient.on('reconnected', (data) => {
|
wsClient.on('reconnected', (data) => {
|
||||||
console.log('ws has reconnected ', data?.wsKey);
|
console.log('ws has reconnected ', data?.wsKey);
|
||||||
});
|
});
|
||||||
|
wsClient.on('error', (data) => {
|
||||||
|
console.error('ws exception: ', data);
|
||||||
|
});
|
||||||
|
|
||||||
// subscribe to private endpoints
|
// subscribe to private endpoints
|
||||||
// check the api docs in your api category to see the available topics
|
// check the api docs in your api category to see the available topics
|
||||||
wsClient.subscribe(['position', 'execution', 'order', 'wallet']);
|
// wsClient.subscribe(['position', 'execution', 'order', 'wallet']);
|
||||||
|
|
||||||
|
// Contract v3
|
||||||
|
wsClient.subscribe([
|
||||||
|
'user.position.contractAccount',
|
||||||
|
'user.execution.contractAccount',
|
||||||
|
'user.order.contractAccount',
|
||||||
|
'user.wallet.contractAccount',
|
||||||
|
]);
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src';
|
|||||||
wsClient.on('reconnected', (data) => {
|
wsClient.on('reconnected', (data) => {
|
||||||
console.log('ws has reconnected ', data?.wsKey);
|
console.log('ws has reconnected ', data?.wsKey);
|
||||||
});
|
});
|
||||||
|
// wsClient.on('error', (data) => {
|
||||||
|
// console.error('ws exception: ', data);
|
||||||
|
// });
|
||||||
|
|
||||||
// Inverse
|
// Inverse
|
||||||
// wsClient.subscribe('trade');
|
// wsClient.subscribe('trade');
|
||||||
|
|||||||
@@ -221,6 +221,11 @@ const PRIVATE_TOPICS = [
|
|||||||
'user.order.unifiedAccount',
|
'user.order.unifiedAccount',
|
||||||
'user.wallet.unifiedAccount',
|
'user.wallet.unifiedAccount',
|
||||||
'user.greeks.unifiedAccount',
|
'user.greeks.unifiedAccount',
|
||||||
|
// contract v3
|
||||||
|
'user.position.contractAccount',
|
||||||
|
'user.execution.contractAccount',
|
||||||
|
'user.order.contractAccount',
|
||||||
|
'user.wallet.contractAccount',
|
||||||
];
|
];
|
||||||
|
|
||||||
export function getWsKeyForTopic(
|
export function getWsKeyForTopic(
|
||||||
|
|||||||
74
test/contract/ws.private.test.ts
Normal file
74
test/contract/ws.private.test.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import {
|
||||||
|
WebsocketClient,
|
||||||
|
WSClientConfigurableOptions,
|
||||||
|
WS_KEY_MAP,
|
||||||
|
} from '../../src';
|
||||||
|
import {
|
||||||
|
logAllEvents,
|
||||||
|
getSilentLogger,
|
||||||
|
waitForSocketEvent,
|
||||||
|
WS_OPEN_EVENT_PARTIAL,
|
||||||
|
fullLogger,
|
||||||
|
} from '../ws.util';
|
||||||
|
|
||||||
|
describe('Private Contract Websocket Client', () => {
|
||||||
|
const API_KEY = process.env.API_KEY_COM;
|
||||||
|
const API_SECRET = process.env.API_SECRET_COM;
|
||||||
|
|
||||||
|
let wsClient: WebsocketClient;
|
||||||
|
|
||||||
|
const wsClientOptions: WSClientConfigurableOptions = {
|
||||||
|
market: 'contractUSDT',
|
||||||
|
key: API_KEY,
|
||||||
|
secret: API_SECRET,
|
||||||
|
};
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
wsClient = new WebsocketClient(
|
||||||
|
wsClientOptions,
|
||||||
|
getSilentLogger('expectSuccessNoAuth')
|
||||||
|
// fullLogger
|
||||||
|
);
|
||||||
|
// logAllEvents(wsClient);
|
||||||
|
wsClient.connectPrivate();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
wsClient.closeAll(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should open a private ws connection', async () => {
|
||||||
|
const wsOpenPromise = waitForSocketEvent(wsClient, 'open');
|
||||||
|
try {
|
||||||
|
expect(await wsOpenPromise).toMatchObject({
|
||||||
|
event: WS_OPEN_EVENT_PARTIAL,
|
||||||
|
wsKey: WS_KEY_MAP.contractUSDTPrivate,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
expect(e).toBeFalsy();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
it('should authenticate successfully', async () => {
|
||||||
|
const wsResponsePromise = waitForSocketEvent(wsClient, 'response');
|
||||||
|
// const wsUpdatePromise = waitForSocketEvent(wsClient, 'update');
|
||||||
|
|
||||||
|
try {
|
||||||
|
expect(await wsResponsePromise).toMatchObject({
|
||||||
|
op: 'auth',
|
||||||
|
req_id: 'contractUSDTPrivate-auth',
|
||||||
|
success: true,
|
||||||
|
wsKey: WS_KEY_MAP.contractUSDTPrivate,
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
// sub failed
|
||||||
|
expect(e).toBeFalsy();
|
||||||
|
}
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// expect(await wsUpdatePromise).toStrictEqual('');
|
||||||
|
// } catch (e) {
|
||||||
|
// // no data
|
||||||
|
// expect(e).toBeFalsy();
|
||||||
|
// }
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -12,10 +12,15 @@ import {
|
|||||||
} from '../ws.util';
|
} from '../ws.util';
|
||||||
|
|
||||||
describe('Private Unified Margin Websocket Client', () => {
|
describe('Private Unified Margin Websocket Client', () => {
|
||||||
|
const API_KEY = process.env.API_KEY_COM;
|
||||||
|
const API_SECRET = process.env.API_SECRET_COM;
|
||||||
|
|
||||||
let wsClient: WebsocketClient;
|
let wsClient: WebsocketClient;
|
||||||
|
|
||||||
const wsClientOptions: WSClientConfigurableOptions = {
|
const wsClientOptions: WSClientConfigurableOptions = {
|
||||||
market: 'unifiedPerp',
|
market: 'unifiedPerp',
|
||||||
|
key: API_KEY,
|
||||||
|
secret: API_SECRET,
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user