kucoin-api 0.0.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +256 -40
- package/dist/cjs/FuturesClient.d.ts +246 -0
- package/dist/cjs/FuturesClient.js +261 -0
- package/dist/cjs/FuturesClient.js.map +1 -0
- package/dist/cjs/SpotClient.d.ts +692 -0
- package/dist/cjs/SpotClient.js +745 -0
- package/dist/cjs/SpotClient.js.map +1 -0
- package/dist/cjs/WebsocketClient.d.ts +77 -0
- package/dist/cjs/WebsocketClient.js +337 -0
- package/dist/cjs/WebsocketClient.js.map +1 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +39 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/lib/BaseRestClient.d.ts +42 -0
- package/dist/cjs/lib/BaseRestClient.js +268 -0
- package/dist/cjs/lib/BaseRestClient.js.map +1 -0
- package/dist/cjs/lib/BaseWSClient.d.ts +168 -0
- package/dist/cjs/lib/BaseWSClient.js +622 -0
- package/dist/cjs/lib/BaseWSClient.js.map +1 -0
- package/dist/cjs/lib/misc-util.d.ts +1 -0
- package/dist/cjs/lib/misc-util.js +8 -0
- package/dist/cjs/lib/misc-util.js.map +1 -0
- package/dist/cjs/lib/requestUtils.d.ts +35 -0
- package/dist/cjs/lib/requestUtils.js +54 -0
- package/dist/cjs/lib/requestUtils.js.map +1 -0
- package/dist/cjs/lib/webCryptoAPI.d.ts +10 -0
- package/dist/cjs/lib/webCryptoAPI.js +57 -0
- package/dist/cjs/lib/webCryptoAPI.js.map +1 -0
- package/dist/cjs/lib/websocket/WsStore.d.ts +63 -0
- package/dist/cjs/lib/websocket/WsStore.js +254 -0
- package/dist/cjs/lib/websocket/WsStore.js.map +1 -0
- package/dist/cjs/lib/websocket/WsStore.types.d.ts +50 -0
- package/dist/cjs/lib/websocket/WsStore.types.js +14 -0
- package/dist/cjs/lib/websocket/WsStore.types.js.map +1 -0
- package/dist/cjs/lib/websocket/logger.d.ts +6 -0
- package/dist/cjs/lib/websocket/logger.js +16 -0
- package/dist/cjs/lib/websocket/logger.js.map +1 -0
- package/dist/cjs/lib/websocket/websocket-util.d.ts +30 -0
- package/dist/cjs/lib/websocket/websocket-util.js +19 -0
- package/dist/cjs/lib/websocket/websocket-util.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/types/request/futures.types.d.ts +183 -0
- package/dist/cjs/types/request/futures.types.js +7 -0
- package/dist/cjs/types/request/futures.types.js.map +1 -0
- package/dist/cjs/types/request/spot-account.d.ts +56 -0
- package/dist/cjs/types/request/spot-account.js +3 -0
- package/dist/cjs/types/request/spot-account.js.map +1 -0
- package/dist/cjs/types/request/spot-earn.d.ts +27 -0
- package/dist/cjs/types/request/spot-earn.js +8 -0
- package/dist/cjs/types/request/spot-earn.js.map +1 -0
- package/dist/cjs/types/request/spot-funding.d.ts +88 -0
- package/dist/cjs/types/request/spot-funding.js +10 -0
- package/dist/cjs/types/request/spot-funding.js.map +1 -0
- package/dist/cjs/types/request/spot-margin-trading.d.ts +169 -0
- package/dist/cjs/types/request/spot-margin-trading.js +10 -0
- package/dist/cjs/types/request/spot-margin-trading.js.map +1 -0
- package/dist/cjs/types/request/spot-trading.d.ts +214 -0
- package/dist/cjs/types/request/spot-trading.js +10 -0
- package/dist/cjs/types/request/spot-trading.js.map +1 -0
- package/dist/cjs/types/response/futures.types.d.ts +501 -0
- package/dist/cjs/types/response/futures.types.js +7 -0
- package/dist/cjs/types/response/futures.types.js.map +1 -0
- package/dist/cjs/types/response/shared.types.d.ts +9 -0
- package/dist/cjs/types/response/shared.types.js +3 -0
- package/dist/cjs/types/response/shared.types.js.map +1 -0
- package/dist/cjs/types/response/spot-account.d.ts +140 -0
- package/dist/cjs/types/response/spot-account.js +3 -0
- package/dist/cjs/types/response/spot-account.js.map +1 -0
- package/dist/cjs/types/response/spot-earn.d.ts +74 -0
- package/dist/cjs/types/response/spot-earn.js +8 -0
- package/dist/cjs/types/response/spot-earn.js.map +1 -0
- package/dist/cjs/types/response/spot-funding.d.ts +176 -0
- package/dist/cjs/types/response/spot-funding.js +10 -0
- package/dist/cjs/types/response/spot-funding.js.map +1 -0
- package/dist/cjs/types/response/spot-margin-trading.d.ts +257 -0
- package/dist/cjs/types/response/spot-margin-trading.js +10 -0
- package/dist/cjs/types/response/spot-margin-trading.js.map +1 -0
- package/dist/cjs/types/response/spot-trading.d.ts +388 -0
- package/dist/cjs/types/response/spot-trading.js +10 -0
- package/dist/cjs/types/response/spot-trading.js.map +1 -0
- package/dist/cjs/types/response/spot-vip.d.ts +38 -0
- package/dist/cjs/types/response/spot-vip.js +10 -0
- package/dist/cjs/types/response/spot-vip.js.map +1 -0
- package/dist/cjs/types/response/ws.d.ts +11 -0
- package/dist/cjs/types/response/ws.js +3 -0
- package/dist/cjs/types/response/ws.js.map +1 -0
- package/dist/cjs/types/websockets/client.d.ts +46 -0
- package/dist/cjs/types/websockets/client.js +3 -0
- package/dist/cjs/types/websockets/client.js.map +1 -0
- package/dist/cjs/types/websockets/events.d.ts +5 -0
- package/dist/cjs/types/websockets/events.js +3 -0
- package/dist/cjs/types/websockets/events.js.map +1 -0
- package/dist/cjs/types/websockets/requests.d.ts +8 -0
- package/dist/cjs/types/websockets/requests.js +3 -0
- package/dist/cjs/types/websockets/requests.js.map +1 -0
- package/dist/cjs/types/websockets/wsAPI.d.ts +9 -0
- package/dist/cjs/types/websockets/wsAPI.js +3 -0
- package/dist/cjs/types/websockets/wsAPI.js.map +1 -0
- package/dist/mjs/FuturesClient.d.ts +246 -0
- package/dist/mjs/FuturesClient.js +257 -0
- package/dist/mjs/FuturesClient.js.map +1 -0
- package/dist/mjs/SpotClient.d.ts +692 -0
- package/dist/mjs/SpotClient.js +741 -0
- package/dist/mjs/SpotClient.js.map +1 -0
- package/dist/mjs/WebsocketClient.d.ts +77 -0
- package/dist/mjs/WebsocketClient.js +333 -0
- package/dist/mjs/WebsocketClient.js.map +1 -0
- package/dist/mjs/index.d.ts +22 -0
- package/dist/mjs/index.js +23 -0
- package/dist/mjs/index.js.map +1 -0
- package/dist/mjs/lib/BaseRestClient.d.ts +42 -0
- package/dist/mjs/lib/BaseRestClient.js +261 -0
- package/dist/mjs/lib/BaseRestClient.js.map +1 -0
- package/dist/mjs/lib/BaseWSClient.d.ts +168 -0
- package/dist/mjs/lib/BaseWSClient.js +615 -0
- package/dist/mjs/lib/BaseWSClient.js.map +1 -0
- package/dist/mjs/lib/misc-util.d.ts +1 -0
- package/dist/mjs/lib/misc-util.js +4 -0
- package/dist/mjs/lib/misc-util.js.map +1 -0
- package/dist/mjs/lib/requestUtils.d.ts +35 -0
- package/dist/mjs/lib/requestUtils.js +49 -0
- package/dist/mjs/lib/requestUtils.js.map +1 -0
- package/dist/mjs/lib/webCryptoAPI.d.ts +10 -0
- package/dist/mjs/lib/webCryptoAPI.js +52 -0
- package/dist/mjs/lib/webCryptoAPI.js.map +1 -0
- package/dist/mjs/lib/websocket/WsStore.d.ts +63 -0
- package/dist/mjs/lib/websocket/WsStore.js +249 -0
- package/dist/mjs/lib/websocket/WsStore.js.map +1 -0
- package/dist/mjs/lib/websocket/WsStore.types.d.ts +50 -0
- package/dist/mjs/lib/websocket/WsStore.types.js +11 -0
- package/dist/mjs/lib/websocket/WsStore.types.js.map +1 -0
- package/dist/mjs/lib/websocket/logger.d.ts +6 -0
- package/dist/mjs/lib/websocket/logger.js +13 -0
- package/dist/mjs/lib/websocket/logger.js.map +1 -0
- package/dist/mjs/lib/websocket/websocket-util.d.ts +30 -0
- package/dist/mjs/lib/websocket/websocket-util.js +15 -0
- package/dist/mjs/lib/websocket/websocket-util.js.map +1 -0
- package/dist/mjs/package.json +3 -0
- package/dist/mjs/types/request/futures.types.d.ts +183 -0
- package/dist/mjs/types/request/futures.types.js +6 -0
- package/dist/mjs/types/request/futures.types.js.map +1 -0
- package/dist/mjs/types/request/spot-account.d.ts +56 -0
- package/dist/mjs/types/request/spot-account.js +2 -0
- package/dist/mjs/types/request/spot-account.js.map +1 -0
- package/dist/mjs/types/request/spot-earn.d.ts +27 -0
- package/dist/mjs/types/request/spot-earn.js +7 -0
- package/dist/mjs/types/request/spot-earn.js.map +1 -0
- package/dist/mjs/types/request/spot-funding.d.ts +88 -0
- package/dist/mjs/types/request/spot-funding.js +9 -0
- package/dist/mjs/types/request/spot-funding.js.map +1 -0
- package/dist/mjs/types/request/spot-margin-trading.d.ts +169 -0
- package/dist/mjs/types/request/spot-margin-trading.js +9 -0
- package/dist/mjs/types/request/spot-margin-trading.js.map +1 -0
- package/dist/mjs/types/request/spot-trading.d.ts +214 -0
- package/dist/mjs/types/request/spot-trading.js +9 -0
- package/dist/mjs/types/request/spot-trading.js.map +1 -0
- package/dist/mjs/types/response/futures.types.d.ts +501 -0
- package/dist/mjs/types/response/futures.types.js +6 -0
- package/dist/mjs/types/response/futures.types.js.map +1 -0
- package/dist/mjs/types/response/shared.types.d.ts +9 -0
- package/dist/mjs/types/response/shared.types.js +2 -0
- package/dist/mjs/types/response/shared.types.js.map +1 -0
- package/dist/mjs/types/response/spot-account.d.ts +140 -0
- package/dist/mjs/types/response/spot-account.js +2 -0
- package/dist/mjs/types/response/spot-account.js.map +1 -0
- package/dist/mjs/types/response/spot-earn.d.ts +74 -0
- package/dist/mjs/types/response/spot-earn.js +7 -0
- package/dist/mjs/types/response/spot-earn.js.map +1 -0
- package/dist/mjs/types/response/spot-funding.d.ts +176 -0
- package/dist/mjs/types/response/spot-funding.js +9 -0
- package/dist/mjs/types/response/spot-funding.js.map +1 -0
- package/dist/mjs/types/response/spot-margin-trading.d.ts +257 -0
- package/dist/mjs/types/response/spot-margin-trading.js +9 -0
- package/dist/mjs/types/response/spot-margin-trading.js.map +1 -0
- package/dist/mjs/types/response/spot-trading.d.ts +388 -0
- package/dist/mjs/types/response/spot-trading.js +9 -0
- package/dist/mjs/types/response/spot-trading.js.map +1 -0
- package/dist/mjs/types/response/spot-vip.d.ts +38 -0
- package/dist/mjs/types/response/spot-vip.js +9 -0
- package/dist/mjs/types/response/spot-vip.js.map +1 -0
- package/dist/mjs/types/response/ws.d.ts +11 -0
- package/dist/mjs/types/response/ws.js +2 -0
- package/dist/mjs/types/response/ws.js.map +1 -0
- package/dist/mjs/types/websockets/client.d.ts +46 -0
- package/dist/mjs/types/websockets/client.js +2 -0
- package/dist/mjs/types/websockets/client.js.map +1 -0
- package/dist/mjs/types/websockets/events.d.ts +5 -0
- package/dist/mjs/types/websockets/events.js +2 -0
- package/dist/mjs/types/websockets/events.js.map +1 -0
- package/dist/mjs/types/websockets/requests.d.ts +8 -0
- package/dist/mjs/types/websockets/requests.js +2 -0
- package/dist/mjs/types/websockets/requests.js.map +1 -0
- package/dist/mjs/types/websockets/wsAPI.d.ts +9 -0
- package/dist/mjs/types/websockets/wsAPI.js +2 -0
- package/dist/mjs/types/websockets/wsAPI.js.map +1 -0
- package/package.json +70 -14
- package/LICENSE +0 -21
- package/index.js +0 -192
- package/test.js +0 -122
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.BaseRestClient = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const misc_util_js_1 = require("./misc-util.js");
|
|
9
|
+
const requestUtils_js_1 = require("./requestUtils.js");
|
|
10
|
+
const webCryptoAPI_js_1 = require("./webCryptoAPI.js");
|
|
11
|
+
const MISSING_API_KEYS_ERROR = 'API Key, Secret & API Passphrase are ALL required to use the authenticated REST client';
|
|
12
|
+
const ENABLE_HTTP_TRACE = typeof process === 'object' &&
|
|
13
|
+
typeof process.env === 'object' &&
|
|
14
|
+
process.env.KUCOINTRACE;
|
|
15
|
+
if (ENABLE_HTTP_TRACE) {
|
|
16
|
+
axios_1.default.interceptors.request.use((request) => {
|
|
17
|
+
console.log(new Date(), 'Starting Request', JSON.stringify({
|
|
18
|
+
url: request.url,
|
|
19
|
+
method: request.method,
|
|
20
|
+
params: request.params,
|
|
21
|
+
data: request.data,
|
|
22
|
+
}, null, 2));
|
|
23
|
+
return request;
|
|
24
|
+
});
|
|
25
|
+
axios_1.default.interceptors.response.use((response) => {
|
|
26
|
+
console.log(new Date(), 'Response:', {
|
|
27
|
+
// request: {
|
|
28
|
+
// url: response.config.url,
|
|
29
|
+
// method: response.config.method,
|
|
30
|
+
// data: response.config.data,
|
|
31
|
+
// headers: response.config.headers,
|
|
32
|
+
// },
|
|
33
|
+
response: {
|
|
34
|
+
status: response.status,
|
|
35
|
+
statusText: response.statusText,
|
|
36
|
+
headers: response.headers,
|
|
37
|
+
data: JSON.stringify(response.data, null, 2),
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
return response;
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
class BaseRestClient {
|
|
44
|
+
options;
|
|
45
|
+
baseUrl;
|
|
46
|
+
globalRequestOptions;
|
|
47
|
+
apiKey;
|
|
48
|
+
apiSecret;
|
|
49
|
+
apiPassphrase;
|
|
50
|
+
/**
|
|
51
|
+
* Create an instance of the REST client. Pass API credentials in the object in the first parameter.
|
|
52
|
+
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity
|
|
53
|
+
* @param {AxiosRequestConfig} [networkOptions={}] HTTP networking options for axios
|
|
54
|
+
*/
|
|
55
|
+
constructor(restClientOptions = {}, networkOptions = {}) {
|
|
56
|
+
this.options = {
|
|
57
|
+
/** Throw errors if any request params are empty */
|
|
58
|
+
strictParamValidation: false,
|
|
59
|
+
apiKeyVersion: 2,
|
|
60
|
+
...restClientOptions,
|
|
61
|
+
};
|
|
62
|
+
this.globalRequestOptions = {
|
|
63
|
+
/** in ms == 5 minutes by default */
|
|
64
|
+
timeout: 1000 * 60 * 5,
|
|
65
|
+
/** inject custom rquest options based on axios specs - see axios docs for more guidance on AxiosRequestConfig: https://github.com/axios/axios#request-config */
|
|
66
|
+
...networkOptions,
|
|
67
|
+
headers: {
|
|
68
|
+
'Content-Type': 'application/json',
|
|
69
|
+
locale: 'en-US',
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
this.baseUrl = (0, requestUtils_js_1.getRestBaseUrl)(false, restClientOptions, this.getClientType());
|
|
73
|
+
this.apiKey = this.options.apiKey;
|
|
74
|
+
this.apiSecret = this.options.apiSecret;
|
|
75
|
+
this.apiPassphrase = this.options.apiPassphrase;
|
|
76
|
+
// Throw if one of the 3 values is missing, but at least one of them is set
|
|
77
|
+
const credentials = [this.apiKey, this.apiSecret, this.apiPassphrase];
|
|
78
|
+
if (credentials.includes(undefined) &&
|
|
79
|
+
credentials.some((v) => typeof v === 'string')) {
|
|
80
|
+
throw new Error(MISSING_API_KEYS_ERROR);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Timestamp used to sign the request. Override this method to implement your own timestamp/sync mechanism
|
|
85
|
+
*/
|
|
86
|
+
getSignTimestampMs() {
|
|
87
|
+
return Date.now();
|
|
88
|
+
}
|
|
89
|
+
get(endpoint, params) {
|
|
90
|
+
return this._call('GET', endpoint, params, true);
|
|
91
|
+
}
|
|
92
|
+
post(endpoint, params) {
|
|
93
|
+
return this._call('POST', endpoint, params, true);
|
|
94
|
+
}
|
|
95
|
+
getPrivate(endpoint, params) {
|
|
96
|
+
return this._call('GET', endpoint, params, false);
|
|
97
|
+
}
|
|
98
|
+
postPrivate(endpoint, params) {
|
|
99
|
+
return this._call('POST', endpoint, params, false);
|
|
100
|
+
}
|
|
101
|
+
deletePrivate(endpoint, params) {
|
|
102
|
+
return this._call('DELETE', endpoint, params, false);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @private Make a HTTP request to a specific endpoint. Private endpoint API calls are automatically signed.
|
|
106
|
+
*/
|
|
107
|
+
async _call(method, endpoint, params, isPublicApi) {
|
|
108
|
+
// Sanity check to make sure it's only ever prefixed by one forward slash
|
|
109
|
+
const requestUrl = [this.baseUrl, endpoint].join(endpoint.startsWith('/') ? '' : '/');
|
|
110
|
+
// Build a request and handle signature process
|
|
111
|
+
const options = await this.buildRequest(method, endpoint, requestUrl, params, isPublicApi);
|
|
112
|
+
if (ENABLE_HTTP_TRACE) {
|
|
113
|
+
console.log('full request: ', options);
|
|
114
|
+
}
|
|
115
|
+
// Dispatch request
|
|
116
|
+
return (0, axios_1.default)(options)
|
|
117
|
+
.then((response) => {
|
|
118
|
+
if (response.status == 200) {
|
|
119
|
+
// Throw if API returns an error (e.g. insufficient balance)
|
|
120
|
+
if (typeof response.data?.code === 'string' &&
|
|
121
|
+
response.data?.code !== '200000') {
|
|
122
|
+
throw { response };
|
|
123
|
+
}
|
|
124
|
+
return response.data;
|
|
125
|
+
}
|
|
126
|
+
throw { response };
|
|
127
|
+
})
|
|
128
|
+
.catch((e) => this.parseException(e, { method, endpoint, requestUrl, params }));
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @private generic handler to parse request exceptions
|
|
132
|
+
*/
|
|
133
|
+
parseException(e, requestParams) {
|
|
134
|
+
if (this.options.parseExceptions === false) {
|
|
135
|
+
throw e;
|
|
136
|
+
}
|
|
137
|
+
// Something happened in setting up the request that triggered an error
|
|
138
|
+
if (!e.response) {
|
|
139
|
+
if (!e.request) {
|
|
140
|
+
throw e.message;
|
|
141
|
+
}
|
|
142
|
+
// request made but no response received
|
|
143
|
+
throw e;
|
|
144
|
+
}
|
|
145
|
+
// The request was made and the server responded with a status code
|
|
146
|
+
// that falls out of the range of 2xx
|
|
147
|
+
const response = e.response;
|
|
148
|
+
// console.error('err: ', response?.data);
|
|
149
|
+
throw {
|
|
150
|
+
code: response.status,
|
|
151
|
+
message: response.statusText,
|
|
152
|
+
body: response.data,
|
|
153
|
+
headers: response.headers,
|
|
154
|
+
requestOptions: {
|
|
155
|
+
...this.options,
|
|
156
|
+
// Prevent credentials from leaking into error messages
|
|
157
|
+
apiKey: 'omittedFromError',
|
|
158
|
+
apiSecret: 'omittedFromError',
|
|
159
|
+
apiPassphrase: 'omittedFromError',
|
|
160
|
+
},
|
|
161
|
+
requestParams,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @private sign request and set recv window
|
|
166
|
+
*/
|
|
167
|
+
async signRequest(data, endpoint, method, signMethod) {
|
|
168
|
+
const timestamp = this.getSignTimestampMs();
|
|
169
|
+
const res = {
|
|
170
|
+
originalParams: {
|
|
171
|
+
...data,
|
|
172
|
+
},
|
|
173
|
+
sign: '',
|
|
174
|
+
timestamp,
|
|
175
|
+
recvWindow: 0,
|
|
176
|
+
serializedParams: '',
|
|
177
|
+
queryParamsWithSign: '',
|
|
178
|
+
};
|
|
179
|
+
if (!this.apiKey || !this.apiSecret) {
|
|
180
|
+
return res;
|
|
181
|
+
}
|
|
182
|
+
const strictParamValidation = this.options.strictParamValidation;
|
|
183
|
+
const encodeQueryStringValues = true;
|
|
184
|
+
if (signMethod === 'kucoin') {
|
|
185
|
+
const signRequestParams = method === 'GET' || method === 'DELETE'
|
|
186
|
+
? (0, requestUtils_js_1.serializeParams)(data, strictParamValidation, encodeQueryStringValues, '?')
|
|
187
|
+
: JSON.stringify(data) || '';
|
|
188
|
+
const paramsStr = `${timestamp}${method}/${endpoint}${signRequestParams}`;
|
|
189
|
+
res.sign = await (0, webCryptoAPI_js_1.signMessage)(paramsStr, this.apiSecret, 'base64', 'SHA-256');
|
|
190
|
+
res.queryParamsWithSign = signRequestParams;
|
|
191
|
+
return res;
|
|
192
|
+
}
|
|
193
|
+
console.error(new Date(), (0, misc_util_js_1.neverGuard)(signMethod, `Unhandled sign method: "${webCryptoAPI_js_1.signMessage}"`));
|
|
194
|
+
return res;
|
|
195
|
+
}
|
|
196
|
+
async prepareSignParams(method, endpoint, signMethod, params, isPublicApi) {
|
|
197
|
+
if (isPublicApi) {
|
|
198
|
+
return {
|
|
199
|
+
originalParams: params,
|
|
200
|
+
paramsWithSign: params,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (!this.apiKey || !this.apiSecret || !this.apiPassphrase) {
|
|
204
|
+
throw new Error(MISSING_API_KEYS_ERROR);
|
|
205
|
+
}
|
|
206
|
+
return this.signRequest(params, endpoint, method, signMethod);
|
|
207
|
+
}
|
|
208
|
+
/** Returns an axios request object. Handles signing process automatically if this is a private API call */
|
|
209
|
+
async buildRequest(method, endpoint, url, params, isPublicApi) {
|
|
210
|
+
const options = {
|
|
211
|
+
...this.globalRequestOptions,
|
|
212
|
+
url: url,
|
|
213
|
+
method: method,
|
|
214
|
+
};
|
|
215
|
+
for (const key in params) {
|
|
216
|
+
if (typeof params[key] === 'undefined') {
|
|
217
|
+
delete params[key];
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (isPublicApi || !this.apiKey || !this.apiSecret) {
|
|
221
|
+
return {
|
|
222
|
+
...options,
|
|
223
|
+
params: params,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const signResult = await this.prepareSignParams(method, endpoint, 'kucoin', params, isPublicApi);
|
|
227
|
+
const authHeaders = {
|
|
228
|
+
'KC-API-KEY': this.apiKey,
|
|
229
|
+
'KC-API-PARTNER': this.getClientType() === requestUtils_js_1.REST_CLIENT_TYPE_ENUM.main
|
|
230
|
+
? requestUtils_js_1.APIIDMain
|
|
231
|
+
: requestUtils_js_1.APIIDFutures,
|
|
232
|
+
'KC-API-TIMESTAMP': signResult.timestamp,
|
|
233
|
+
'KC-API-KEY-VERSION': this.options.apiKeyVersion,
|
|
234
|
+
};
|
|
235
|
+
const partnerSignParam = `${authHeaders['KC-API-TIMESTAMP']}${authHeaders['KC-API-PARTNER']}${authHeaders['KC-API-KEY']}`;
|
|
236
|
+
const partnerSign = this.getClientType() === requestUtils_js_1.REST_CLIENT_TYPE_ENUM.main
|
|
237
|
+
? requestUtils_js_1.APIIDMainSign
|
|
238
|
+
: requestUtils_js_1.APIIDFuturesSign;
|
|
239
|
+
const partnerSignResult = await (0, webCryptoAPI_js_1.signMessage)(partnerSignParam, partnerSign, 'base64', 'SHA-256');
|
|
240
|
+
const signedPassphrase = await (0, webCryptoAPI_js_1.signMessage)(this.apiPassphrase, this.apiSecret, 'base64', 'SHA-256');
|
|
241
|
+
if (method === 'GET') {
|
|
242
|
+
return {
|
|
243
|
+
...options,
|
|
244
|
+
headers: {
|
|
245
|
+
...authHeaders,
|
|
246
|
+
...options.headers,
|
|
247
|
+
'KC-API-SIGN': signResult.sign,
|
|
248
|
+
'KC-API-PARTNER-SIGN': partnerSignResult,
|
|
249
|
+
'KC-API-PASSPHRASE': signedPassphrase,
|
|
250
|
+
},
|
|
251
|
+
url: options.url + signResult.queryParamsWithSign,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
...options,
|
|
256
|
+
headers: {
|
|
257
|
+
...authHeaders,
|
|
258
|
+
...options.headers,
|
|
259
|
+
'KC-API-SIGN': signResult.sign,
|
|
260
|
+
'KC-API-PARTNER-SIGN': partnerSignResult,
|
|
261
|
+
'KC-API-PASSPHRASE': signedPassphrase,
|
|
262
|
+
},
|
|
263
|
+
data: params,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
exports.BaseRestClient = BaseRestClient;
|
|
268
|
+
//# sourceMappingURL=BaseRestClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseRestClient.js","sourceRoot":"","sources":["../../../src/lib/BaseRestClient.ts"],"names":[],"mappings":";;;;;;AAAA,kDAAyE;AAEzE,iDAA4C;AAC5C,uDAU2B;AAC3B,uDAAgD;AAEhD,MAAM,sBAAsB,GAC1B,wFAAwF,CAAC;AAmB3F,MAAM,iBAAiB,GACrB,OAAO,OAAO,KAAK,QAAQ;IAC3B,OAAO,OAAO,CAAC,GAAG,KAAK,QAAQ;IAC/B,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;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AmB,EAAE,gBAAgB;aACtC;YACD,IAAI,EAAE,MAAM;SACb,CAAC;IACJ,CAAC;CACF;AAtWD,wCAsWC"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
import WebSocket from 'isomorphic-ws';
|
|
4
|
+
import { WebsocketClientOptions, WSClientConfigurableOptions } from '../types/websockets/client.js';
|
|
5
|
+
import { WsOperation } from '../types/websockets/requests.js';
|
|
6
|
+
import { DefaultLogger } from './websocket/logger.js';
|
|
7
|
+
import { MessageEventLike, WsTopicRequest, WsTopicRequestOrStringTopic } from './websocket/websocket-util.js';
|
|
8
|
+
import { WsStore } from './websocket/WsStore.js';
|
|
9
|
+
import { WSConnectedResult } from './websocket/WsStore.types.js';
|
|
10
|
+
interface WSClientEventMap<WsKey extends string> {
|
|
11
|
+
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
|
|
12
|
+
open: (evt: {
|
|
13
|
+
wsKey: WsKey;
|
|
14
|
+
event: any;
|
|
15
|
+
}) => void;
|
|
16
|
+
/** Reconnecting a dropped connection */
|
|
17
|
+
reconnect: (evt: {
|
|
18
|
+
wsKey: WsKey;
|
|
19
|
+
event: any;
|
|
20
|
+
}) => void;
|
|
21
|
+
/** Successfully reconnected a connection that dropped */
|
|
22
|
+
reconnected: (evt: {
|
|
23
|
+
wsKey: WsKey;
|
|
24
|
+
event: any;
|
|
25
|
+
}) => void;
|
|
26
|
+
/** Connection closed */
|
|
27
|
+
close: (evt: {
|
|
28
|
+
wsKey: WsKey;
|
|
29
|
+
event: any;
|
|
30
|
+
}) => void;
|
|
31
|
+
/** Received reply to websocket command (e.g. after subscribing to topics) */
|
|
32
|
+
response: (response: any & {
|
|
33
|
+
wsKey: WsKey;
|
|
34
|
+
}) => void;
|
|
35
|
+
/** Received data for topic */
|
|
36
|
+
update: (response: any & {
|
|
37
|
+
wsKey: WsKey;
|
|
38
|
+
}) => void;
|
|
39
|
+
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
|
|
40
|
+
exception: (response: any & {
|
|
41
|
+
wsKey: WsKey;
|
|
42
|
+
}) => void;
|
|
43
|
+
error: (response: any & {
|
|
44
|
+
wsKey: WsKey;
|
|
45
|
+
}) => void;
|
|
46
|
+
/** Confirmation that a connection successfully authenticated */
|
|
47
|
+
authenticated: (event: {
|
|
48
|
+
wsKey: WsKey;
|
|
49
|
+
event: any;
|
|
50
|
+
}) => void;
|
|
51
|
+
}
|
|
52
|
+
export interface EmittableEvent<TEvent = any> {
|
|
53
|
+
eventType: 'response' | 'update' | 'exception' | 'authenticated' | 'connectionReady';
|
|
54
|
+
event: TEvent;
|
|
55
|
+
}
|
|
56
|
+
export interface BaseWebsocketClient<TWSKey extends string> {
|
|
57
|
+
on<U extends keyof WSClientEventMap<TWSKey>>(event: U, listener: WSClientEventMap<TWSKey>[U]): this;
|
|
58
|
+
emit<U extends keyof WSClientEventMap<TWSKey>>(event: U, ...args: Parameters<WSClientEventMap<TWSKey>[U]>): boolean;
|
|
59
|
+
}
|
|
60
|
+
type WSTopic = string;
|
|
61
|
+
export declare abstract class BaseWebsocketClient<TWSKey extends string> extends EventEmitter {
|
|
62
|
+
private wsStore;
|
|
63
|
+
protected logger: typeof DefaultLogger;
|
|
64
|
+
protected options: WebsocketClientOptions;
|
|
65
|
+
private wsApiRequestId;
|
|
66
|
+
constructor(options?: WSClientConfigurableOptions, logger?: typeof DefaultLogger);
|
|
67
|
+
protected abstract sendPingEvent(wsKey: TWSKey, ws: WebSocket): void;
|
|
68
|
+
protected abstract sendPongEvent(wsKey: TWSKey, ws: WebSocket): void;
|
|
69
|
+
protected abstract isWsPong(data: any): boolean;
|
|
70
|
+
protected abstract isWsPing(data: any): boolean;
|
|
71
|
+
protected abstract getWsAuthRequestEvent(wsKey: TWSKey): Promise<object>;
|
|
72
|
+
protected abstract isPrivateTopicRequest(request: WsTopicRequest<WSTopic>, wsKey: TWSKey): boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Returns a list of string events that can be individually sent upstream to complete subscribing/unsubscribing/etc to these topics
|
|
75
|
+
*/
|
|
76
|
+
protected abstract getWsOperationEventsForTopics(topics: WsTopicRequest<WSTopic>[], wsKey: TWSKey, operation: WsOperation): Promise<string[]>;
|
|
77
|
+
protected abstract getPrivateWSKeys(): TWSKey[];
|
|
78
|
+
protected abstract getWsUrl(wsKey: TWSKey): Promise<string>;
|
|
79
|
+
protected abstract getMaxTopicsPerSubscribeEvent(wsKey: TWSKey): number | null;
|
|
80
|
+
/**
|
|
81
|
+
* Abstraction called to sort ws events into emittable event types (response to a request, data update, etc)
|
|
82
|
+
*/
|
|
83
|
+
protected abstract resolveEmittableEvents(wsKey: TWSKey, event: MessageEventLike): EmittableEvent[];
|
|
84
|
+
/**
|
|
85
|
+
* Request connection of all dependent (public & private) websockets, instead of waiting for automatic connection by library
|
|
86
|
+
*/
|
|
87
|
+
protected abstract connectAll(): Promise<(WSConnectedResult | undefined)[]>;
|
|
88
|
+
protected isPrivateWsKey(wsKey: TWSKey): boolean;
|
|
89
|
+
/** Returns auto-incrementing request ID, used to track promise references for async requests */
|
|
90
|
+
protected getNewRequestId(): string;
|
|
91
|
+
protected abstract sendWSAPIRequest(wsKey: TWSKey, channel: WSTopic, params?: any): Promise<unknown>;
|
|
92
|
+
protected abstract sendWSAPIRequest(wsKey: TWSKey, channel: WSTopic, params: any): Promise<unknown>;
|
|
93
|
+
/**
|
|
94
|
+
* Subscribe to one or more topics on a WS connection (identified by WS Key).
|
|
95
|
+
*
|
|
96
|
+
* - Topics are automatically cached
|
|
97
|
+
* - Connections are automatically opened, if not yet connected
|
|
98
|
+
* - Authentication is automatically handled
|
|
99
|
+
* - Topics are automatically resubscribed to, if something happens to the connection, unless you call unsubsribeTopicsForWsKey(topics, key).
|
|
100
|
+
*
|
|
101
|
+
* @param wsTopicRequests array of topics to subscribe to
|
|
102
|
+
* @param wsKey ws key referring to the ws connection these topics should be subscribed on
|
|
103
|
+
*/
|
|
104
|
+
subscribeTopicsForWsKey(wsTopicRequests: WsTopicRequestOrStringTopic<WSTopic>[], wsKey: TWSKey): false | Promise<WSConnectedResult | undefined> | undefined;
|
|
105
|
+
protected unsubscribeTopicsForWsKey(wsTopicRequests: WsTopicRequestOrStringTopic<string>[], wsKey: TWSKey): void;
|
|
106
|
+
/**
|
|
107
|
+
* Splits topic requests into two groups, public & private topic requests
|
|
108
|
+
*/
|
|
109
|
+
private sortTopicRequestsIntoPublicPrivate;
|
|
110
|
+
/** Get the WsStore that tracks websockets & topics */
|
|
111
|
+
getWsStore(): WsStore<TWSKey, WsTopicRequest<string>>;
|
|
112
|
+
close(wsKey: TWSKey, force?: boolean): void;
|
|
113
|
+
closeAll(force?: boolean): void;
|
|
114
|
+
isConnected(wsKey: TWSKey): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Request connection to a specific websocket, instead of waiting for automatic connection.
|
|
117
|
+
*/
|
|
118
|
+
connect(wsKey: TWSKey): Promise<WSConnectedResult | undefined>;
|
|
119
|
+
private connectToWsUrl;
|
|
120
|
+
private parseWsError;
|
|
121
|
+
/** Get a signature, build the auth request and send it */
|
|
122
|
+
private sendAuthRequest;
|
|
123
|
+
private reconnectWithDelay;
|
|
124
|
+
private ping;
|
|
125
|
+
private clearTimers;
|
|
126
|
+
private clearPingTimer;
|
|
127
|
+
private clearPongTimer;
|
|
128
|
+
/**
|
|
129
|
+
* Simply builds and sends subscribe events for a list of topics for a ws key
|
|
130
|
+
*
|
|
131
|
+
* @private Use the `subscribe(topics)` or `subscribeTopicsForWsKey(topics, wsKey)` method to subscribe to topics. Send WS message to subscribe to topics.
|
|
132
|
+
*/
|
|
133
|
+
private requestSubscribeTopics;
|
|
134
|
+
/**
|
|
135
|
+
* Simply builds and sends unsubscribe events for a list of topics for a ws key
|
|
136
|
+
*
|
|
137
|
+
* @private Use the `unsubscribe(topics)` method to unsubscribe from topics. Send WS message to unsubscribe from topics.
|
|
138
|
+
*/
|
|
139
|
+
private requestUnsubscribeTopics;
|
|
140
|
+
/**
|
|
141
|
+
* Try sending a string event on a WS connection (identified by the WS Key)
|
|
142
|
+
*/
|
|
143
|
+
tryWsSend(wsKey: TWSKey, wsMessage: string): void;
|
|
144
|
+
private onWsOpen;
|
|
145
|
+
/**
|
|
146
|
+
* Called automatically once a connection is ready.
|
|
147
|
+
* - Some exchanges are ready immediately after the connections open.
|
|
148
|
+
* - Some exchanges send an event to confirm the connection is ready for us.
|
|
149
|
+
*
|
|
150
|
+
* This method is called to act when the connection is ready. Use `requireConnectionReadyConfirmation` to control how this is called.
|
|
151
|
+
*/
|
|
152
|
+
private onWsReadyForEvents;
|
|
153
|
+
/**
|
|
154
|
+
* Handle subscription to private topics _after_ authentication successfully completes asynchronously.
|
|
155
|
+
*
|
|
156
|
+
* Only used for exchanges that require auth before sending private topic subscription requests
|
|
157
|
+
*/
|
|
158
|
+
private onWsAuthenticated;
|
|
159
|
+
private onWsMessage;
|
|
160
|
+
private onWsClose;
|
|
161
|
+
private getWs;
|
|
162
|
+
private setWsState;
|
|
163
|
+
/**
|
|
164
|
+
* Promise-driven method to assert that a ws has successfully connected (will await until connection is open)
|
|
165
|
+
*/
|
|
166
|
+
protected assertIsConnected(wsKey: TWSKey): Promise<unknown>;
|
|
167
|
+
}
|
|
168
|
+
export {};
|