forked from catsats/backpack_exchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backpack_client.js
406 lines (406 loc) · 15.1 KB
/
backpack_client.js
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BackpackClient = void 0;
const got_1 = __importDefault(require("got"));
const crypto_1 = __importDefault(require("crypto"));
const qs_1 = __importDefault(require("qs"));
const ws_1 = __importDefault(require("ws"));
const BACKOFF_EXPONENT = 1.5;
const DEFAULT_TIMEOUT_MS = 5000;
const BASE_URL = "https://api.backpack.exchange/";
// const BASE_URL = "https://api.cf.backpack.exchange/";
// 执行对应操作的命令
const instructions = {
public: new Map([
["assets", { url: `${BASE_URL}api/v1/assets`, method: "GET" }],
["markets", { url: `${BASE_URL}api/v1/markets`, method: "GET" }],
["ticker", { url: `${BASE_URL}api/v1/ticker`, method: "GET" }],
["tickers", { url: `${BASE_URL}api/v1/tickers`, method: "GET" }],
["depth", { url: `${BASE_URL}api/v1/depth`, method: "GET" }],
["klines", { url: `${BASE_URL}api/v1/klines`, method: "GET" }],
["status", { url: `${BASE_URL}api/v1/status`, method: "GET" }],
["ping", { url: `${BASE_URL}api/v1/ping`, method: "GET" }],
["time", { url: `${BASE_URL}api/v1/time`, method: "GET" }],
["trades", { url: `${BASE_URL}api/v1/trades`, method: "GET" }],
[
"tradesHistory",
{ url: `${BASE_URL}api/v1/trades/history`, method: "GET" },
],
]),
private: new Map([
["balanceQuery", { url: `${BASE_URL}api/v1/capital`, method: "GET" }],
[
"depositAddressQuery",
{ url: `${BASE_URL}wapi/v1/capital/deposit/address`, method: "GET" },
],
[
"depositQueryAll",
{ url: `${BASE_URL}wapi/v1/capital/deposits`, method: "GET" },
],
[
"fillHistoryQueryAll",
{ url: `${BASE_URL}wapi/v1/history/fills`, method: "GET" },
],
["orderCancel", { url: `${BASE_URL}api/v1/order`, method: "DELETE" }],
["orderCancelAll", { url: `${BASE_URL}api/v1/orders`, method: "DELETE" }],
["orderExecute", { url: `${BASE_URL}api/v1/order`, method: "POST" }],
[
"orderHistoryQueryAll",
{ url: `${BASE_URL}wapi/v1/history/orders`, method: "GET" },
],
["orderQuery", { url: `${BASE_URL}api/v1/order`, method: "GET" }],
["orderQueryAll", { url: `${BASE_URL}api/v1/orders`, method: "GET" }],
[
"withdraw",
{ url: `${BASE_URL}wapi/v1/capital/withdrawals`, method: "POST" },
],
[
"withdrawalQueryAll",
{ url: `${BASE_URL}wapi/v1/capital/withdrawals`, method: "GET" },
],
]),
};
//解码私钥成pkcs8编码的私钥 因为crypto.sign需要的私钥格式是pkcs8格式
// https://stackoverflow.com/questions/71916954/crypto-sign-function-to-sign-a-message-with-given-private-key
const toPkcs8der = (rawB64) => {
var rawPrivate = Buffer.from(rawB64, "base64").subarray(0, 32);
var prefixPrivateEd25519 = Buffer.from("302e020100300506032b657004220420", "hex");
var der = Buffer.concat([prefixPrivateEd25519, rawPrivate]);
return crypto_1.default.createPrivateKey({ key: der, format: "der", type: "pkcs8" });
};
//解码公钥成spki编码的私钥 因为crypto.sign需要的公钥格式是spki格式
// https://stackoverflow.com/questions/68612396/sign-and-verify-jws-json-web-signature-with-ed25519-keypair
const toSpki = (rawB64) => {
var rawPublic = Buffer.from(rawB64, "base64");
var prefixPublicEd25519 = Buffer.from("302a300506032b6570032100", "hex");
var der = Buffer.concat([prefixPublicEd25519, rawPublic]);
return crypto_1.default.createPublicKey({ key: der, format: "der", type: "spki" });
};
/**
* 生成签名方法 getMessageSignature
* https://docs.backpack.exchange/#section/Authentication/Signing-requests
* @param {Object} request params as an object
* @param {UInt8Array} privateKey
* @param {number} timestamp Unix time in ms that the request was sent
* @param {string} instruction
* @param {number} window Time window in milliseconds that the request is valid for
* @return {string} base64 encoded signature to include on request
*/
const getMessageSignature = (request, privateKey, timestamp, instruction, window) => {
function alphabeticalSort(a, b) {
return a.localeCompare(b);
}
const message = qs_1.default.stringify(request, { sort: alphabeticalSort });
const headerInfo = { timestamp, window: window ?? DEFAULT_TIMEOUT_MS };
const headerMessage = qs_1.default.stringify(headerInfo);
const messageToSign = "instruction=" +
instruction +
"&" +
(message ? message + "&" : "") +
headerMessage;
const signature = crypto_1.default.sign(null, Buffer.from(messageToSign), toPkcs8der(privateKey));
return signature.toString("base64");
};
// 请求方法 rawRequest(命令,请求头,请求参数)
const rawRequest = async (instruction, headers, data) => {
const { url, method } = instructions.private.has(instruction)
? instructions.private.get(instruction)
: instructions.public.get(instruction);
let fullUrl = url;
headers["User-Agent"] = "Backpack Typescript API Client";
headers["Content-Type"] =
method == "GET"
? "application/x-www-form-urlencoded"
: "application/json; charset=utf-8";
const options = { headers };
if (method == "GET") {
Object.assign(options, { method });
fullUrl =
url + (Object.keys(data).length > 0 ? "?" + qs_1.default.stringify(data) : "");
}
else if (method == "POST" || method == "DELETE") {
Object.assign(options, {
method,
body: JSON.stringify(data),
});
}
const response = await (0, got_1.default)(fullUrl, options);
const contentType = response.headers["content-type"];
if (contentType?.includes("application/json")) {
const parsed = JSON.parse(response.body, function (_key, value) {
if (value instanceof Array && value.length == 0) {
return value;
}
if (isNaN(Number(value))) {
return value;
}
return Number(value);
});
if (parsed.error && parsed.error.length) {
const error = parsed.error
.filter((e) => e.startsWith("E"))
.map((e) => e.substr(1));
if (!error.length) {
throw new Error("Backpack API returned an unknown error");
}
throw new Error(`url=${url} body=${options["body"]} err=${error.join(", ")}`);
}
return parsed;
}
else if (contentType?.includes("text/plain")) {
return response.body;
}
else {
return response;
}
};
/**
* 初始化BackpackClient 填入私钥和公钥
* BackpackClient connects to the Backpack API
* @param {string} privateKey base64 encoded
* @param {string} publicKey base64 encoded
*/
class BackpackClient {
constructor(privateKey, publicKey) {
this.config = { privateKey, publicKey };
// Verify that the keys are a correct pair before sending any requests. Ran
// into errors before with that which were not obvious.
const pubkeyFromPrivateKey = crypto_1.default
.createPublicKey(toPkcs8der(privateKey))
.export({ format: "der", type: "spki" })
.toString("base64");
const pubkey = toSpki(publicKey)
.export({ format: "der", type: "spki" })
.toString("base64");
if (pubkeyFromPrivateKey != pubkey) {
throw new Error("错误的秘钥对,请检查私钥公钥是否匹配");
}
}
/**
* 发送公共或私有API请求
* This method makes a public or private API request.
* @param {String} method 方法名 The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @param {Number} retrysLeft
* @return {Object} The response object 重试请求的次数
*/
async api(method, params, retrysLeft = 10) {
try {
if (instructions.public.has(method)) {
return await this.publicMethod(method, params);
}
else if (instructions.private.has(method)) {
return await this.privateMethod(method, params);
}
}
catch (e) {
if (retrysLeft > 0) {
const numTry = 11 - retrysLeft;
const backOff = Math.pow(numTry, BACKOFF_EXPONENT);
console.warn("BPX api error", {
method,
numTry,
backOff,
}, e.toString(), e.response && e.response.body ? e.response.body : '');
await new Promise((resolve) => setTimeout(resolve, backOff * 1000));
return await this.api(method, params, retrysLeft - 1);
}
else {
throw e;
}
}
throw new Error(method + " is not a valid API method.");
}
/**
* 发送公共API请求
* This method makes a public API request.
* @param {String} instruction The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @return {Object} The response object
*/
async publicMethod(instruction, params = {}) {
const response = await rawRequest(instruction, {}, params);
return response;
}
/**
* 发送私有API请求
* This method makes a private API request.
* @param {String} instruction The API method (public or private)
* @param {Object} params Arguments to pass to the api call
* @return {Object} The response object
*/
async privateMethod(instruction, params = {}) {
const timestamp = Date.now();
const signature = getMessageSignature(params, this.config.privateKey, timestamp, instruction);
const headers = {
"X-Timestamp": timestamp,
"X-Window": this.config.timeout ?? DEFAULT_TIMEOUT_MS,
"X-API-Key": this.config.publicKey,
"X-Signature": signature,
};
const response = await rawRequest(instruction, headers, params);
return response;
}
/**
* https://docs.backpack.exchange/#tag/Capital/operation/get_balances
*/
async Balance() {
return this.api("balanceQuery");
}
/**
* https://docs.backpack.exchange/#tag/Capital/operation/get_deposits
*/
async Deposits(params) {
return this.api("depositQueryAll", params);
}
/**
* https://docs.backpack.exchange/#tag/Capital/operation/get_deposit_address
*/
async DepositAddress(params) {
return this.api("depositAddressQuery", params);
}
/**
* https://docs.backpack.exchange/#tag/Capital/operation/get_withdrawals
*/
async Withdrawals(params) {
return this.api("withdrawalQueryAll", params);
}
/**
* https://docs.backpack.exchange/#tag/Capital/operation/request_withdrawal
*/
async Withdraw(params) {
this.api("withdraw", params);
}
/**
* https://docs.backpack.exchange/#tag/History/operation/get_order_history
*/
async OrderHistory(params) {
return this.api("orderHistoryQueryAll", params);
}
/**
* https://docs.backpack.exchange/#tag/History/operation/get_fills
*/
async FillHistory(params) {
return this.api("fillHistoryQueryAll", params);
}
/**
* https://docs.backpack.exchange/#tag/Markets/operation/get_assets
*/
async Assets() {
return this.api("assets");
}
/**
* https://docs.backpack.exchange/#tag/Markets/operation/get_markets
*/
async Markets() {
return this.api("markets");
}
async Tickers(params) {
return this.api("tickers", params);
}
/**
* https://docs.backpack.exchange/#tag/Markets/operation/get_ticker
*/
async Ticker(params) {
return this.api("ticker", params);
}
/**
* https://docs.backpack.exchange/#tag/Markets/operation/get_depth
*/
async Depth(params) {
return this.api("depth", params);
}
/**
* https://docs.backpack.exchange/#tag/Markets/operation/get_klines
*/
async KLines(params) {
return this.api("klines", params);
}
/**
* https://docs.backpack.exchange/#tag/Order/operation/get_order
*/
async GetOrder(params) {
return this.api("orderQuery", params);
}
/**
* https://docs.backpack.exchange/#tag/Order/operation/execute_order
*/
async ExecuteOrder(params) {
return this.api("orderExecute", params, 0);
}
/**
* https://docs.backpack.exchange/#tag/Order/operation/cancel_order
*/
async CancelOrder(params) {
return this.api("orderCancel", params);
}
/**
* https://docs.backpack.exchange/#tag/Order/operation/get_open_orders
*/
async GetOpenOrders(params) {
return this.api("orderQueryAll", params);
}
/**
* https://docs.backpack.exchange/#tag/Order/operation/cancel_open_orders
*/
async CancelOpenOrders(params) {
return this.api("orderCancelAll", params);
}
/**
* https://docs.backpack.exchange/#tag/System/operation/get_status
*/
async Status() {
return this.api("status");
}
/**
* https://docs.backpack.exchange/#tag/System/operation/ping
*/
async Ping() {
return this.api("ping");
}
/**
* https://docs.backpack.exchange/#tag/System/operation/get_time
*/
async Time() {
return this.api("time");
}
/**
* https://docs.backpack.exchange/#tag/Trades/operation/get_recent_trades
*/
async RecentTrades(params) {
return this.api("trades", params);
}
/**
* https://docs.backpack.exchange/#tag/Trades/operation/get_historical_trades
*/
async HistoricalTrades(params) {
return this.api("tradesHistory", params);
}
/**
* https://docs.backpack.exchange/#tag/Streams/Private
* @return {Object} Websocket Websocket connecting to order update stream
*/
subscribeOrderUpdate() {
const privateStream = new ws_1.default('wss://ws.backpack.exchange');
const timestamp = Date.now();
const window = 5000;
const signature = getMessageSignature({}, this.config.privateKey, timestamp, "subscribe", window);
const subscriptionData = {
method: 'SUBSCRIBE',
params: ["account.orderUpdate"],
"signature": [this.config.publicKey, signature, timestamp.toString(), window.toString()]
};
privateStream.onopen = (_) => {
console.log('Connected to BPX Websocket');
privateStream.send(JSON.stringify(subscriptionData));
};
privateStream.onerror = (error) => {
console.log(`Websocket Error ${error}`);
};
return privateStream;
}
}
exports.BackpackClient = BackpackClient;