-
-
Notifications
You must be signed in to change notification settings - Fork 268
/
BaseRestClient.ts
381 lines (321 loc) · 10.1 KB
/
BaseRestClient.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import axios, { AxiosError, AxiosRequestConfig, Method } from 'axios';
import { BinanceBaseUrlKey } from '../types/shared';
import Beautifier from './beautifier';
import {
GenericAPIResponse,
getRequestSignature,
getRestBaseUrl,
RestClientOptions,
serialiseParams,
} from './requestUtils';
type ApiLimitHeader =
| 'x-mbx-used-weight'
| 'x-mbx-used-weight-1m'
| 'x-sapi-used-ip-weight-1m'
| 'x-mbx-order-count-1s'
| 'x-mbx-order-count-1m'
| 'x-mbx-order-count-1h'
| 'x-mbx-order-count-1d';
export default abstract class BaseRestClient {
private timeOffset: number = 0;
private syncTimePromise: null | Promise<void>;
private options: RestClientOptions;
private baseUrl: string;
private globalRequestOptions: AxiosRequestConfig;
private key: string | undefined;
private secret: string | undefined;
private baseUrlKey: BinanceBaseUrlKey;
private beautifier: Beautifier | undefined;
public apiLimitTrackers: Record<ApiLimitHeader, number>;
public apiLimitLastUpdated: number;
constructor(
baseUrlKey: BinanceBaseUrlKey,
options: RestClientOptions = {},
requestOptions: AxiosRequestConfig = {},
) {
this.options = {
recvWindow: 5000,
// how often to sync time drift with binance servers
syncIntervalMs: 3600000,
// if true, we'll throw errors if any params are undefined
strictParamValidation: false,
// disable the time sync mechanism by default
disableTimeSync: true,
...options,
};
this.globalRequestOptions = {
// in ms == 5 minutes by default
timeout: 1000 * 60 * 5,
headers: {
// 'content-type': 'application/x-www-form-urlencoded';
},
// custom request options based on axios specs - see: https://github.com/axios/axios#request-config
...requestOptions,
};
this.key = options.api_key;
this.secret = options.api_secret;
if (this.key) {
if (!this.globalRequestOptions.headers) {
this.globalRequestOptions.headers = {};
}
this.globalRequestOptions.headers['X-MBX-APIKEY'] = this.key;
}
this.baseUrlKey = this.options.baseUrlKey || baseUrlKey;
this.baseUrl = getRestBaseUrl(this.baseUrlKey, this.options);
if (this.key && !this.secret) {
throw new Error(
'API Key & Secret are both required for private enpoints',
);
}
if (this.options.disableTimeSync !== true) {
this.syncTime();
setInterval(this.syncTime.bind(this), +this.options.syncIntervalMs!);
}
if (this.options.beautifyResponses) {
this.beautifier = new Beautifier();
}
this.syncTimePromise = null;
this.apiLimitTrackers = {
'x-mbx-used-weight': 0,
'x-mbx-used-weight-1m': 0,
'x-sapi-used-ip-weight-1m': 0,
'x-mbx-order-count-1s': 0,
'x-mbx-order-count-1m': 0,
'x-mbx-order-count-1h': 0,
'x-mbx-order-count-1d': 0,
};
}
abstract getServerTime(
baseUrlKeyOverride?: BinanceBaseUrlKey,
): Promise<number>;
public getBaseUrlKey(): BinanceBaseUrlKey {
return this.baseUrlKey;
}
public getRateLimitStates() {
return {
...this.apiLimitTrackers,
lastUpdated: this.apiLimitLastUpdated,
};
}
/**
* Return time sync offset, automatically set if time sync is enabled. A higher offset means system clock is behind server time.
*/
public getTimeOffset(): number {
return this.timeOffset;
}
public setTimeOffset(value: number) {
this.timeOffset = value;
}
public get(endpoint: string, params?: any): GenericAPIResponse {
return this._call('GET', endpoint, params);
}
public getForBaseUrl(
endpoint: string,
baseUrlKey: BinanceBaseUrlKey,
params?: any,
) {
const baseUrl = getRestBaseUrl(baseUrlKey, {});
return this._call('GET', endpoint, params, false, baseUrl);
}
public getPrivate(endpoint: string, params?: any): GenericAPIResponse {
return this._call('GET', endpoint, params, true);
}
public post(endpoint: string, params?: any): GenericAPIResponse {
return this._call('POST', endpoint, params);
}
public postPrivate(endpoint: string, params?: any): GenericAPIResponse {
return this._call('POST', endpoint, params, true);
}
public put(endpoint: string, params?: any): GenericAPIResponse {
return this._call('PUT', endpoint, params);
}
public putPrivate(endpoint: string, params?: any): GenericAPIResponse {
return this._call('PUT', endpoint, params, true);
}
public delete(endpoint: string, params?: any): GenericAPIResponse {
return this._call('DELETE', endpoint, params);
}
public deletePrivate(endpoint: string, params?: any): GenericAPIResponse {
return this._call('DELETE', endpoint, params, true);
}
/**
* @private Make a HTTP request to a specific endpoint. Private endpoints are automatically signed.
*/
public async _call(
method: Method,
endpoint: string,
params?: any,
isPrivate?: boolean,
baseUrlOverride?: string,
): GenericAPIResponse {
const timestamp = Date.now() + (this.getTimeOffset() || 0);
if (isPrivate && (!this.key || !this.secret)) {
throw new Error(
'Private endpoints require api and private keys to be set',
);
}
// Handles serialisation of params into query string (url?key1=value1&key2=value2), handles encoding of values, adds timestamp and signature to request.
const { serialisedParams, signature, requestBody } =
await getRequestSignature(
params,
this.key,
this.secret,
this.options.recvWindow,
timestamp,
this.options.strictParamValidation,
this.options.filterUndefinedParams,
);
const baseUrl = baseUrlOverride || this.baseUrl;
const options = {
...this.globalRequestOptions,
url: [baseUrl, endpoint].join('/'),
method: method,
json: true,
};
if (isPrivate) {
options.url +=
'?' + [serialisedParams, 'signature=' + signature].join('&');
} else if (method === 'GET' || method === 'DELETE') {
options.params = params;
} else {
options.data = serialiseParams(
requestBody,
this.options.strictParamValidation,
true,
);
}
// console.log(
// 'sending request: ',
// JSON.stringify(
// {
// serialisedParams,
// requestBody,
// signature,
// reqOptions: options,
// reqParams: params,
// },
// null,
// 2,
// ),
// );
return axios(options)
.then((response) => {
this.updateApiLimitState(response.headers);
if (response.status == 200) {
return response.data;
}
throw response;
})
.then((response) => {
if (!this.options.beautifyResponses || !this.beautifier) {
return response;
}
// Fallback to original response if beautifier fails
try {
return this.beautifier.beautify(response, endpoint) || response;
} catch (e) {
console.error(
'BaseRestClient response beautify failed: ',
JSON.stringify({ response: response, error: e }),
);
}
return response;
})
.catch((e) => this.parseException(e, options.url));
}
/**
* @private generic handler to parse request exceptions
*/
private parseException(
e: AxiosError<{ code: string; msg: string }>,
url: string,
): unknown {
const { response, request, message } = e;
if (response && response.headers) {
this.updateApiLimitState(response.headers);
}
if (this.options.parseExceptions === false) {
throw e;
}
// Something happened in setting up the request that triggered an Error
if (!response) {
if (!request) {
throw message;
}
// request made but no response received
throw e;
}
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
throw {
code: response.data?.code,
message: response.data?.msg,
body: response.data,
headers: response.headers,
requestUrl: url,
requestBody: request.body,
requestOptions: {
...this.options,
api_key: undefined,
api_secret: undefined,
},
};
}
private updateApiLimitState(responseHeaders: Record<string, any>) {
const delta: Record<string, any> = {};
for (const headerKey in this.apiLimitTrackers) {
const headerValue = responseHeaders[headerKey];
const value = parseInt(headerValue);
if (headerValue !== undefined && !isNaN(value)) {
// TODO: track last seen by key? insetad of all? some keys not returned by some endpoints more useful in estimating whether reset should've happened
this.apiLimitTrackers[headerKey] = value;
delta[headerKey] = {
updated: true,
valueParsed: value,
valueRaw: headerValue,
};
} else {
delta[headerKey] = {
updated: false,
valueParsed: value,
valueRaw: headerValue,
};
}
}
// console.log('responseHeaders: ', requestedUrl);
// console.table(responseHeaders);
// console.table(delta);
this.apiLimitLastUpdated = new Date().getTime();
}
/**
* Trigger time sync and store promise
*/
public syncTime(): Promise<void> {
if (this.options.disableTimeSync === true) {
return Promise.resolve();
}
if (this.syncTimePromise !== null) {
return this.syncTimePromise;
}
this.syncTimePromise = this.fetchTimeOffset().then((offset) => {
this.timeOffset = offset;
this.syncTimePromise = null;
});
return this.syncTimePromise;
}
/**
* Estimate drift based on client<->server latency
*/
async fetchTimeOffset(): Promise<number> {
try {
const start = Date.now();
const serverTime = await this.getServerTime();
const end = Date.now();
const avgDrift = (end - start) / 2;
return Math.ceil(serverTime - end + avgDrift);
} catch (e) {
console.error('Failed to fetch get time offset: ', e);
return 0;
}
}
}