forked from andreihq/binance-dumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
127 lines (108 loc) · 3.97 KB
/
api.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
const crypto = require('crypto');
const request = require('request');
const moment = require('moment');
const { delay } = require('./utils');
const API_ENDPOINTS = {
'SERVER_TIME': 'https://api.binance.com/api/v1/time',
'EXCHANGE_INFO': 'https://api.binance.com/api/v1/exchangeInfo',
'GET_ORDER_BOOK': 'https://api.binance.com/api/v3/ticker/bookTicker',
'PLACE_ORDER': 'https://api.binance.com/api/v3/order',
'CANCEL_ORDER': 'https://api.binance.com/api/v3/order',
'TEST_ORDER': 'https://api.binance.com/api/v3/order/test'
};
const api = (key, secret) => {
let apiSecret = secret;
let apiKey = key;
let requestDelay = 0;
const sendRequest = async (options) => {
if (requestDelay > 0 ) {
await delay(requestDelay);
}
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
let success = (!error && response.statusCode == 200) ? true : false;
if (response.statusCode == 429 || response.statusCode == 418) {
requestDelay = (response.headers['Retry-After'] || 0) * 1000;
} else {
requestDelay = 0;
}
resolve(
{
success: success,
httpCode: response.statusCode,
data: JSON.parse(body)
}
);
});
});
}
return {
getExchangeInfo: async () => {
const options = {
url: API_ENDPOINTS['EXCHANGE_INFO'],
method: 'GET',
headers: {
'X-MBX-APIKEY': apiKey
},
forever: true
};
return await sendRequest(options);
},
/*
order: {
symbol: String,
side: String,
type: String,
quantity: String,
price: String
}
*/
placeOrder: async (order, isTest) => {
const timestamp = moment().valueOf();
let requestBody = `symbol=${order.symbol}&side=${order.side}&type=${order.type}&quantity=${order.quantity}&price=${order.price}&timeInForce=GTC×tamp=${timestamp}`;
const sign = crypto.createHmac('sha256', apiSecret).update(requestBody).digest('hex');
requestBody = `${requestBody}&signature=${sign}`;
const options = {
url: isTest ? API_ENDPOINTS['TEST_ORDER'] : API_ENDPOINTS['PLACE_ORDER'],
method: 'POST',
headers: {
'X-MBX-APIKEY': apiKey
},
forever: true,
form: requestBody
};
return await sendRequest(options);
},
getOrderBook: async (symbol) => {
const options = {
url: `${API_ENDPOINTS['GET_ORDER_BOOK']}?symbol=${symbol}`,
method: 'GET',
headers: {
'X-MBX-APIKEY': apiKey
},
forever: true
};
return await sendRequest(options);
},
cancelOrder: async (symbol, orderId) => {
const timestamp = moment().valueOf();
let requestBody = `symbol=${symbol}&orderId=${orderId}×tamp=${timestamp}`;
const sign = crypto.createHmac('sha256', apiSecret).update(requestBody).digest('hex');
requestBody = `${requestBody}&signature=${sign}`;
const options = {
url: API_ENDPOINTS['CANCEL_ORDER'],
method: 'DELETE',
headers: {
'X-MBX-APIKEY': apiKey
},
forever: true,
form: requestBody
};
//console.log(options);
return await sendRequest(options);
}
};
};
module.exports = {
api: api
};