-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
336 lines (286 loc) · 15.6 KB
/
index.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
// Sample Node JS
const CryptoJS = require('crypto-js');
const request = require('superagent');
const querystring = require('querystring');
const HttpMethod = {POST:1, GET:2, DELETE:3};
const public_api_key = 'MYPUBKEY';
const private_api_key = 'MYPRIVKEY';
const HOST_URL = "https://api.coss.io/v1";
//WRAPPER STARTS HERE (Commenting out stuff that coss wrote which should be part of bot.js (the actual trading bot) rather than index.js (the wrapper core functionality))
let Coss = () => {
//HTTPS Private Request
async function transmit_encrypted_request(method, url, payload){
//var payload_json_str = urllib.parse.urlencode(payload) if method == HttpMethod.GET else json.dumps(payload, separators=(',', ':'))
var payload_json_str = method == HttpMethod.GET ? querystring.stringify(payload) : JSON.stringify(payload)
var sign = transmit_payload_signature_get(payload_json_str)
var req_headers = transmit_signed_header_get(sign)
console.log(`sending ${payload_json_str} to ${url}`)
if (method == HttpMethod.POST) {
let response = await request.post(url).set(req_headers).send(payload_json_str)
return(response.body);
} else if (method == HttpMethod.DELETE) {
let response = await request.delete(url).set(req_headers).send(payload_json_str)
return(response.body);
} else if (method == HttpMethod.GET) {
let response = await request.get(url).set(req_headers).query(payload_json_str)
return(response.body);
}
}
function transmit_signed_header_get(sign) {
var req_headers = {
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
'Authorization': public_api_key,
'Signature': sign,
}
return req_headers
}
//AUTHBUILDER
function transmit_payload_signature_get(payload_json_str){
//var sign = hmac.new(self.private_api_key, payload_json_str.encode('utf8'), hashlib.sha256).hexdigest()
var sign = CryptoJS.HmacSHA256(payload_json_str, private_api_key).toString(CryptoJS.enc.Hex)
return sign
}
//HTTPS Public Request
async function publicRequest(url) {
let response = await request.get(url)
return response.body;
}
return {
//PLACE LIMIT ORDER
placeLimitOrder: async (params = {}) => {
if (!params.Symbol) {
return Promise.reject("placeLimitOrder(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("placeLimitOrder(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
} else if (!params.Side) {
return Promise.reject("placeLimitOrder(), You must supply a valid Side, e.g. 'BUY' or 'SELL'!");
} else if (params.Side && params.Side !== 'Buy' && params.Side !== 'Sell') {
return Promise.reject("placeLimitOrder(), You must supply a valid Side, e.g. 'BUY' or 'SELL'!");
} else if (!params.Price || !params.Amount) {
return Promise.reject("placeLimitOrder(), You must supply a valid Price and Amount, e.g. Price: '0.00000034' or Amount: '123.00000000'!");
} else if (typeof params.Price !== 'number' || typeof params.Amount !== 'number') {
return Promise.reject("placeLimitOrder(), You must supply a valid Price and Amount as a number, e.g. Price: '0.00000034' or Amount: '123.00000000'!");
}
var now = new Date().getTime()
var query_params = {
'order_symbol': params.Symbol,
'order_price': params.Price.toString(),
'order_side': params.Side,
'order_size': params.Amount.toString(),
'type': 'limit',
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/add";
return transmit_encrypted_request(HttpMethod.POST, url, query_params)
},
//PLACE MARKET ORDER
placeMarketOrder: async (params = {}) => {
if (!params.Symbol) {
return Promise.reject("placeMarketOrder(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("placeMarketOrder(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
} else if (!params.Side) {
return Promise.reject("placeMarketOrder(), You must supply a valid Side, e.g. 'BUY' or 'SELL'!");
} else if (params.Side && params.Side !== 'Buy' && params.Side !== 'Sell') {
return Promise.reject("placeMarketOrder(), You must supply a valid Side, e.g. 'BUY' or 'SELL'!");
} else if (!params.Amount) {
return Promise.reject("placeMarketOrder(), You must supply a valid Amount, e.g. Amount: '123.00000000'!");
} else if (typeof params.Amount !== 'number') {
return Promise.reject("placeMarketOrder(), You must supply a valid Amount as a number, e.g. Amount: '123.00000000'!");
}
var now = new Date().getTime()
var query_params = {
'order_symbol': params.Symbol,
'order_side': params.Side,
'order_size': params.Amount.toString(),
'type': 'market',
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/add";
return transmit_encrypted_request(HttpMethod.POST, url, query_params)
},
//Get Account Balances
getAccountBalances: async (params = {}) => {
var now = new Date().getTime()
var query_params = {
'recvWindow': 5000,
'timestamp': now
}
let url = HOST_URL + "/account/balances";
return transmit_encrypted_request(HttpMethod.GET, url, query_params)
},
//Get Account Details
getAccountDetails: async (params = {}) => {
var now = new Date().getTime()
var query_params = {
'recvWindow': 5000,
'timestamp': now
}
let url = HOST_URL + "/account/details";
return transmit_encrypted_request(HttpMethod.GET, url, query_params)
},
//CANCEL ORDER BY ID #
cancelOrder: async (params = {}) => {
if (!params.ID) {
return Promise.reject("cancelOrder(), You must supply a valid order_ID to cancel, e.g. '9e5ae4dd-3369-401d-81f5-dff985e1c4e7'!");
} else if (params.ID && typeof params.ID !== 'string') {
return Promise.reject("cancelOrder(), You must supply a valid order_ID to cancel as a string, e.g. '9e5ae4dd-3369-401d-81f5-dff985e1c4e7'!");
} else if (!params.Symbol) {
return Promise.reject("cancelOrder(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("cancelOrder(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
var now = new Date().getTime()
var query_params = {
'order_id': params.ID,
'order_symbol': params.Symbol,
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/cancel";
return transmit_encrypted_request(HttpMethod.DELETE, url, query_params)
},
//GET ORDER DETAILS
getOrderDetails: async (params = {}) => {
if (!params.ID) {
return Promise.reject("getOrderDetails(), You must supply a valid order_ID to cancel, e.g. '9e5ae4dd-3369-401d-81f5-dff985e1c4e7'!");
} else if (params.ID && typeof params.ID !== 'string') {
return Promise.reject("getOrderDetails(), You must supply a valid order_ID to cancel as a string, e.g. '9e5ae4dd-3369-401d-81f5-dff985e1c4e7'!");
}
var now = new Date().getTime()
var query_params = {
'order_id': params.ID,
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/details";
return transmit_encrypted_request(HttpMethod.POST, url, query_params)
},
//GET OPEN ORDERS
getOpenOrders: async (params = {}) => {
if (!params.Limit) {
return Promise.reject("getOpenOrders(), You must supply a valid Limit for your open orders, e.g. '10'!");
} else if (params.Limit && typeof params.Limit !== 'number') {
return Promise.reject("getOpenOrders(), You must supply a valid Limit as a number for your open orders, e.g. '10'!");
} else if (!params.Symbol) {
return Promise.reject("getOpenOrders(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getOpenOrders(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
var now = new Date().getTime()
var query_params = {
'limit': params.Limit.toString(),
'page': 0,
'symbol': params.Symbol,
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/list/open";
return transmit_encrypted_request(HttpMethod.POST, url, query_params)
},
//GET COMPLETED ORDERS
getCompletedOrders: async (params = {}) => {
if (!params.Limit) {
return Promise.reject("getCompletedOrders(), You must supply a valid Limit for your open orders, e.g. '10'!");
} else if (params.Limit && typeof params.Limit !== 'number') {
return Promise.reject("getCompletedOrders(), You must supply a valid Limit as a number for your open orders, e.g. '10'!");
} else if (!params.Symbol) {
return Promise.reject("getCompletedOrders(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getCompletedOrders(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
var now = new Date().getTime()
var query_params = {
'limit': params.Limit.toString(),
'page': 0,
'symbol': params.Symbol,
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/list/completed";
return transmit_encrypted_request(HttpMethod.POST, url, query_params)
},
//GET ALL USER ORDERS
getAllOrders: async (params = {}) => {
if (!params.Limit) {
return Promise.reject("getAllOrders(), You must supply a valid Limit for your open orders, e.g. '10'!");
} else if (params.Limit && typeof params.Limit !== 'number') {
return Promise.reject("getAllOrders(), You must supply a valid Limit as a number for your open orders, e.g. '10'!");
} else if (!params.Symbol) {
return Promise.reject("getAllOrders(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getAllOrders(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
} else if (!params.ID) {
return Promise.reject("getAllOrders(), You must supply a valid account ID");
} else if (params.ID && typeof params.ID !== 'string') {
return Promise.reject("getAllOrders(), You must supply a valid account ID as a string");
}
var now = new Date().getTime()
var query_params = {
'symbol': params.Symbol,
'from_id': params.ID,
'limit': params.Limit,
'timestamp': now,
'recvWindow': 5000
}
let url = HOST_URL + "/order/list/all";
return transmit_encrypted_request(HttpMethod.POST, url, query_params)
},
//Public API Calls (don't need to be signed)
//get Market Price
getMarketPrice: async (params = {}) => {
if (!params.Symbol) {
return Promise.reject("getMarketPrice(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getMarketPrice(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
let url = HOST_URL + "/market-price/" + '?symbol=' + params.Symbol;
return publicRequest(url);
},
//get Market Price
getPairDepth: async (params = {}) => {
if (!params.Symbol) {
return Promise.reject("getPairDepth(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getPairDepth(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
console.log(HOST_URL + "/dp/" + '?symbol=' + params.Symbol);
let url = HOST_URL + "/dp/" + '?symbol=' + params.Symbol;
return publicRequest(url);
},
//get Market Price
getMarketSides: async (params = {}) => {
if (!params.Symbol) {
return Promise.reject("getMarketSides(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getMarketSides(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
console.log(HOST_URL + "/dp/" + '?symbol=' + params.Symbol);
let url = HOST_URL + "/dp/" + '?symbol=' + params.Symbol;
let pairDepth = await publicRequest(url);
let marketSides = [[pairDepth.bids[0][0], pairDepth.bids[0][1]],[pairDepth.asks[0][0], pairDepth.asks[0][1]]];
return(marketSides)
},
//get Exchange Info
getExchangeInfo: async () => {
console.log(HOST_URL + "/exchange-info");
let url = HOST_URL + "/exchange-info";
return publicRequest(url);
},
//get Market Summary (NOT WORKING ATM)
getMarketSummary: async (params = {}) => {
if (!params.Symbol) {
return Promise.reject("getMarketSummary(), You must supply a valid trading pair Symbol, e.g. 'eth-btc'!");
} else if (params.Symbol && typeof params.Symbol !== 'string') {
return Promise.reject("getMarketSummary(), You must supply a valid trading pair Symbol as a string, e.g. 'eth-btc'!");
}
console.log(HOST_URL + "/getmarketsummaries" + '?symbol=' + params.Symbol);
let url = HOST_URL + "/getmarketsummaries" + '?symbol=' + params.Symbol;
return publicRequest(url);
},
}
};
module.exports = exports = Coss;