-
Notifications
You must be signed in to change notification settings - Fork 16
/
cexapi.js
145 lines (131 loc) · 3.26 KB
/
cexapi.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
var _username;
var _api_key;
var _api_secret;
var _nonce = '';
var _https = require('https');
var _crypto = require('crypto');
function create(username, api_key, api_secret) //Set variable
{
_username = username;
_api_key = api_key;
_api_secret = api_secret;
}
function __signature() //Generate signature
{
string = _nonce + _username + _api_key
hmac = _crypto.createHmac('sha256', _api_secret);
hmac.setEncoding('hex');
hmac.write(string);
hmac.end();
var temp = hmac.read();
return temp.toUpperCase()
}
function __nonce() //Get timestamp as nonce
{
_nonce = Math.round(new Date().getTime() / 1000);
}
function __post(url, param, callback) //Send post request via requstify
{
var post_data = '';
var body = '';
for (var key in param)
{
post_data += key + '=' + param[key] + '&'
}
if (post_data.length > 2)
{
post_data = post_data.substring(0, post_data.length-1 );
}
else
{
post_data ='';
}
var request = _https.request({
hostname:'cex.io',
path : url,
port : 443,
method : 'POST',
headers: {'User-Agent': 'cex.io_node.js_api',
'Content-Length': post_data.length,
'Content-Type' : 'application/x-www-form-urlencoded'}
}, function(res){
res.setEncoding('utf8');
res.on('data', function(chunk){
body +=chunk;
});
res.on('end', function() {
callback(JSON.parse(body));
});
});//Return answer as object in callback
request.write(post_data);
request.end();
return body;
}
function api_call(method, param, is_private, couple, callback) //Api call
{
var url = '/api/' + method +'/' //generate uri
if (couple == undefined) {var couple = ''} else {
if (couple.length > 5)
{
url = url + couple +'/' }
}
if (param == undefined) {var param = new Object()} //generate param in needed
if (is_private == undefined) {var is_private = 0}
else
{
if (is_private == 1)
{
__nonce();
param.key = _api_key;
param.signature = __signature();
param.nonce = _nonce;
}
}
__post(url,param, callback)
}
function ticker(couple,callback)
{
api_call('ticker', new Object(), 0, couple, callback)
}
function order_book(couple,callback)
{
api_call('order_book', new Object(), 0, couple, callback)
}
function trade_history(since,couple,callback)
{
param = new Object();
param.since = since;
api_call('trade_history', param, 0, couple, callback)
}
function balance(callback)
{
param = new Object();
api_call('balance', param, 1, '', callback)
}
function open_orders(couple, callback)
{
api_call('open_orders', new Object(), 1, couple, callback);
}
function cancel_order(id, callback)
{
param = new Object();
param.id = id;
api_call('cancel_order', param, 1, '', callback);
}
function place_order(type, amount, price, couple, callback)
{
param = new Object();
param.type = type;
param.amount = amount;
param.price = price;
api_call('place_order', param, 1, couple, callback)
}
exports.create = create;
exports.api_call = api_call;
exports.ticker = ticker;
exports.order_book = order_book;
exports.trade_history = trade_history;
exports.balance = balance;
exports.open_orders = open_orders;
exports.cancel_order = cancel_order;
exports.place_order = place_order;