-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.js
More file actions
86 lines (77 loc) · 2.22 KB
/
client.js
File metadata and controls
86 lines (77 loc) · 2.22 KB
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
const crypto = require('crypto');
const https = require('https');
const apiKey = "add api key here";
const privateKey = "add private key here";
const baseUrl = "api.btcmarkets.net";
function makeHttpCall(method, path, queryString, dataObj) {
var data = null;
if (dataObj) {
data = JSON.stringify(dataObj);
}
const headers = buildAuthHeaders(method, path, data);
let fullPath = path;
if (queryString != null) {
fullPath += '?' + queryString
}
const httpOptions = {host: baseUrl, path: fullPath, method: method, headers: headers};
var req = https.request(httpOptions, function(res) {
var output = '';
res.on('data', function (chunk) {
output += chunk;
});
res.on('end', function () {
console.log(output);
});
console.log("response code: " + res.statusCode);
});
if (data) {
req.write(data);
}
req.end();
}
function buildAuthHeaders(method, path, data) {
const now = Date.now();
let message = method + path + now;
if (data) {
message += data;
}
const signature = signMessage(privateKey, message);
const headers = {
"Accept": "application/json",
"Accept-Charset": "UTF-8",
"Content-Type": "application/json",
"BM-AUTH-APIKEY": apiKey,
"BM-AUTH-TIMESTAMP": now,
"BM-AUTH-SIGNATURE": signature
};
return headers;
}
function signMessage(secret, message) {
var buffer = Buffer.from(secret, 'base64');
var hmac = crypto.createHmac('sha512', buffer);
var signature = hmac.update(message).digest('base64');
return signature;
}
function getOpenOrders() {
const path = '/v3/orders';
makeHttpCall('GET', path, 'status=open', null);
}
function placeOrder() {
const data = {
marketId: 'XRP-AUD',
price: '0.1',
amount: "0.1",
side: "Bid",
type: "Limit"
}
const path = '/v3/orders';
makeHttpCall('POST', path, null, data);
}
function cancelOrder(id) {
const path = '/v3/orders/' + id ;
makeHttpCall('DELETE', path, null, null);
}
// please add your API keys and uncomment any of the following methods to use
//placeOrder();
getOpenOrders();
//cancelOrder("1");