-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
privateWs.ts
136 lines (119 loc) · 4.54 KB
/
privateWs.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
// DefaultLogger,
WebsocketClient,
// WS_KEY_MAP,
WsTopicRequest,
} from '../../../src/index.js';
/**
* import { WebsocketClient } from 'coinbase-api';
* const { WebsocketClient } = require('coinbase-api');
*/
async function start() {
// key name & private key, as returned by coinbase when creating your API keys.
// Note: the below example is a dummy key and won't actually work
const advancedTradeCdpAPIKey = {
name: 'organizations/13232211d-d7e2-d7e2-d7e2-d7e2d7e2d7e2/apiKeys/d7e2d7e2-d7e2-d7e2-d7e2-d7e2d7e2d7e2',
privateKey:
'-----BEGIN EC PRIVATE KEY-----\nADFGHmkgnjdfg16k165kuu1kdtyudtyjdtyjytj/ADFGHmkgnjdfg16k165kuu1kdtyudtyjdtyjytj+oAoGCCqGSM49\nAwEHoUQDQgAEhtAep/ADFGHmkgnjdfg16k165kuu1kdtyudtyjdtyjytj+bzduY3iYXEmj/KtCk\nADFGHmkgnjdfg16k165kuu1kdtyudtyjdtyjytj\n-----END EC PRIVATE KEY-----\n',
};
// Optional: fully customise the logging experience by injecting a custom logger
// const logger: typeof DefaultLogger = {
// ...DefaultLogger,
// trace: (...params) => {
// if (
// [
// 'Sending ping',
// 'Sending upstream ws message: ',
// 'Received pong, clearing pong timer',
// 'Received ping, sending pong frame',
// ].includes(params[0])
// ) {
// return;
// }
// console.log('trace', params);
// },
// };
const client = new WebsocketClient(
{
// Either pass the full JSON object that can be downloaded when creating your API keys
// cdpApiKey: advancedTradeCdpAPIKey,
// Or use the key name as "apiKey" and private key (WITH the "begin/end EC PRIVATE KEY" comment) as "apiSecret"
apiKey: advancedTradeCdpAPIKey.name,
apiSecret: advancedTradeCdpAPIKey.privateKey,
},
// logger,
);
client.on('open', (data) => {
console.log('open: ', data?.wsKey);
});
// Data received
client.on('update', (data) => {
console.info(new Date(), 'data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconenct
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('response: ', JSON.stringify(data, null, 2));
// throw new Error('res?');
});
client.on('exception', (data) => {
console.error('exception: ', data);
});
try {
/**
* Use the client subscribe(topic, market) pattern to subscribe to any websocket topic.
*
* You can subscribe to topics one at a time or many one one request.
*
* Topics can be sent as simple strings, if no parameters are required.
*
* Any subscribe requests on the "advTradeUserData" market are automatically authenticated with the available credentials
*/
// client.subscribe('heartbeats', 'advTradeUserData');
client.subscribe('futures_balance_summary', 'advTradeUserData');
// This is the same as above, but uses WS_KEY_MAP as an enum (do this if you're not sure what value to put)
// client.subscribe('futures_balance_summary', WS_KEY_MAP.advTradeUserData);
// Subscribe to the user feed for the advanced trade websocket
client.subscribe('user', 'advTradeUserData');
// /**
// * Or, as an array of simple strings.
// *
// * Any requests sent to the "advTradeUserData" wsKey are
// * automatically authenticated, if API keys are avaiable:
// */
// client.subscribe(
// ['futures_balance_summary', 'user'],
// 'advTradeUserData',
// );
/**
* Or send a more structured object with parameters, e.g. if parameters are required
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const tickerSubscribeRequest: WsTopicRequest = {
topic: 'futures_balance_summary',
/**
* Anything in the payload will be merged into the subscribe "request",
* allowing you to send misc parameters supported by the exchange (such as `product_ids: string[]`)
*/
payload: {
// In this case, the "futures_balance_summary" channel doesn't support any parameters
// product_ids: ['ETH-USD', 'BTC-USD'],
},
};
client.subscribe(tickerSubscribeRequest, 'advTradeUserData');
} catch (e) {
console.error(`Subscribe exception: `, e);
}
}
start();