forked from tiagosiebler/gateio-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-private-perp-futures.ts
114 lines (96 loc) · 3.15 KB
/
ws-private-perp-futures.ts
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
111
112
113
114
/* eslint-disable @typescript-eslint/no-unused-vars */
import { LogParams, WebsocketClient, WsTopicRequest } from '../src';
// import { LogParams, WebsocketClient, WsTopicRequest } from 'gateio-api'; // normally you should install this module via npm: `npm install gateio-api`
const account = {
key: process.env.API_KEY || 'apiKeyHere',
secret: process.env.API_SECRET || 'apiSecretHere',
};
// Define a custom logger object to handle logging at different levels
const customLogger = {
// Trace level logging: used for detailed debugging information
trace: (...params: LogParams): void => {
// Uncomment the line below to enable trace logging
// console.log(new Date(), 'trace', ...params);
},
// Info level logging: used for general informational messages
info: (...params: LogParams): void => {
console.log(new Date(), 'info', ...params);
},
// Error level logging: used for error messages
error: (...params: LogParams): void => {
console.error(new Date(), 'error', ...params);
},
};
async function start() {
const client = new WebsocketClient(
{
apiKey: account.key,
apiSecret: account.secret,
},
customLogger,
);
// console.log('auth with: ', account);
client.on('open', (data) => {
console.log('connected ', data?.wsKey);
});
// Data received
client.on('update', (data) => {
console.info('data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconnect
client.on('reconnect', (data) => {
console.log('reconnect: ', data);
});
// Reconnect successful
client.on('reconnected', (data) => {
console.log('reconnected: ', data);
});
// Connection closed. If unexpected, expect reconnect -> reconnected.
client.on('close', (data) => {
console.error('close: ', data);
});
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
client.on('response', (data) => {
console.info('server reply: ', JSON.stringify(data), '\n');
});
client.on('exception', (data) => {
console.error('exception: ', data);
});
client.on('authenticated', (data) => {
console.error('authenticated: ', data);
});
try {
// TODO: many private topics use your user ID
const myUserID = '20011';
const userBalances: WsTopicRequest = {
topic: 'futures.balances',
payload: [myUserID],
};
const userTrades: WsTopicRequest = {
topic: 'futures.usertrades',
payload: [myUserID, '!all'],
};
const userLiquidates: WsTopicRequest = {
topic: 'futures.liquidates',
payload: [myUserID, '!all'],
};
/**
* Either send one topic (with params) at a time
*/
// client.subscribe({
// topic: 'futures.usertrades',
// payload: [myUserID, '!all'],
// }, 'perpFuturesUSDTV4');
/**
* Or send multiple topics in a batch (grouped by ws connection (WsKey))
* You can also use strings for topics that don't have any parameters, even if you mix multiple requests into one function call:
*/
client.subscribe(
[userBalances, userTrades, userLiquidates],
'perpFuturesUSDTV4',
);
} catch (e) {
console.error(`Req error: `, e);
}
}
start();