-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcoinfloor_api.js
315 lines (288 loc) · 8.07 KB
/
coinfloor_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
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
"use strict";
(function () {
var _worker = new Worker("coinfloor_worker.js");
var _worker_handlers = [];
_worker.onmessage = function (event) {
_worker_handlers.shift().call(this, event.data);
};
self.Coinfloor = function () {
var _self = this;
var _websocket;
var _server_nonce;
var _tag = 0;
var _idle_ping_timer_id;
var _event_handlers = {};
var _result_handlers = {};
this.on = function (type, handler) {
var handlers = _event_handlers[type];
if (handlers) {
handlers.push(handler);
}
else {
_event_handlers[type] = [ handler ];
}
};
this.trigger = function (type, data) {
var handlers = _event_handlers[type];
if (handlers) {
for (var i = 0; i < handlers.length; ++i) {
handlers[i].call(this, data);
}
}
};
this.request = function (request, callback) {
var tag = request.tag = ++_tag;
_websocket.send(JSON.stringify(request));
_result_handlers[tag] = callback;
reset_idle_ping_timer();
};
function reset_idle_ping_timer() {
if (_idle_ping_timer_id) {
clearTimeout(_idle_ping_timer_id);
}
_idle_ping_timer_id = setTimeout(function () {
_self.request({ }, null);
}, 45000);
}
/*
* Initiates a connection to a Coinfloor API server and returns the new
* WebSocket object. A websocket URL may be given to override the default.
* If a callback function is provided, it will be invoked after the
* connection is established.
*/
this.connect = function (url, callback) {
_websocket = new WebSocket(url || "wss://api.coinfloor.co.uk/");
var handler = function (event) {
reset_idle_ping_timer();
var msg = JSON.parse(event.data);
if (msg.tag !== undefined) {
var handler = _result_handlers[msg.tag];
delete _result_handlers[msg.tag];
if (handler) {
handler.call(_self, msg);
}
else if (handler === undefined) {
alert("Error code " + msg.error_code + ": " + msg.error_msg);
}
}
else if (msg.notice !== undefined) {
_self.trigger(msg.notice, msg);
}
};
_websocket.onmessage = function (event) {
reset_idle_ping_timer();
var msg = JSON.parse(event.data);
_server_nonce = atob(msg.nonce);
this.onmessage = handler;
if (callback) {
callback.call(_self, msg);
}
};
return _websocket;
};
this._authenticate = function (user_id, cookie, secret, callback) {
var packed_user_id = String.fromCharCode(0, 0, 0, 0, user_id >> 24 & 0xFF, user_id >> 16 & 0xFF, user_id >> 8 & 0xFF, user_id & 0xFF);
var client_nonce = "";
for (var i = 0; i < 16; ++i) {
client_nonce += String.fromCharCode(Math.random() * 256);
}
var data = {
op: "sign",
content: packed_user_id + _server_nonce + client_nonce
};
if (secret.privkey) {
data.privkey = secret.privkey;
}
else {
data.seed = packed_user_id + unescape(encodeURIComponent(secret.passphrase));
}
_worker.postMessage(data);
_worker_handlers.push(function (data) {
_self.request({
method: "Authenticate",
user_id: user_id,
cookie: cookie,
nonce: btoa(client_nonce),
signature: [ btoa(data[0]), btoa(data[1]) ]
}, callback);
});
};
/*
* Discrete event hooks are supported for backward compatibility.
* New code should register event handlers using on().
*/
var _hook_warned;
function define_hook(type) {
var _hook, _registered;
Object.defineProperty(_self, "on" + type, {
get: function () {
return _hook;
},
set: function (hook) {
if (!_hook_warned) {
console.warn("Discrete event hooks are deprecated.");
_hook_warned = true;
}
if (!_registered) {
_self.on(type, function (msg) {
if (_hook) {
_hook(msg);
}
});
_registered = true;
}
return _hook = hook;
},
});
}
define_hook("BalanceChanged");
define_hook("OrderOpened");
define_hook("OrdersMatched");
define_hook("OrderClosed");
define_hook("TickerChanged");
};
/*
* Authenticates as the specified user with the given authentication cookie
* and passphrase.
*/
Coinfloor.prototype.authenticate = function (user_id, cookie, passphrase, callback) {
this._authenticate(user_id, cookie, { passphrase: passphrase }, callback);
};
/*
* Retrieves all available balances of the authenticated user.
*/
Coinfloor.prototype.getBalances = function (callback) {
this.request({
method: "GetBalances"
}, callback);
};
/*
* Retrieves all open orders of the authenticated user.
*/
Coinfloor.prototype.getOrders = function (callback) {
this.request({
method: "GetOrders"
}, callback);
};
/*
* Estimates the total (in units of the counter asset) for a market order
* trading the specified quantity (in units of the base asset). The
* quantity should be positive for a buy order or negative for a sell
* order.
*/
Coinfloor.prototype.estimateBaseMarketOrder = function (base, counter, quantity, callback) {
this.request({
method: "EstimateMarketOrder",
base: base,
counter: counter,
quantity: quantity
}, callback);
};
/*
* Estimates the quantity (in units of the base asset) for a market order
* trading the specified total (in units of the counter asset). The total
* should be positive for a buy order or negative for a sell order.
*/
Coinfloor.prototype.estimateCounterMarketOrder = function (base, counter, total, callback) {
this.request({
method: "EstimateMarketOrder",
base: base,
counter: counter,
total: total
}, callback);
};
/*
* Places a limit order to trade the specified quantity (in units of the
* base asset) at the specified price or better. The quantity should be
* positive for a buy order or negative for a sell order. The price should
* be pre-multiplied by 10000.
*/
Coinfloor.prototype.placeLimitOrder = function (base, counter, quantity, price, callback) {
this.request({
method: "PlaceOrder",
base: base,
counter: counter,
quantity: quantity,
price: price
}, callback);
};
/*
* Executes a market order to trade up to the specified quantity (in units
* of the base asset). The quantity should be positive for a buy order or
* negative for a sell order.
*/
Coinfloor.prototype.executeBaseMarketOrder = function (base, counter, quantity, callback) {
this.request({
method: "PlaceOrder",
base: base,
counter: counter,
quantity: quantity
}, callback);
};
/*
* Executes a market order to trade up to the specified total (in units of
* the counter asset). The total should be positive for a buy order or
* negative for a sell order.
*/
Coinfloor.prototype.executeCounterMarketOrder = function (base, counter, total, callback) {
this.request({
method: "PlaceOrder",
base: base,
counter: counter,
total: total
}, callback);
};
/*
* Cancels the specified open order.
*/
Coinfloor.prototype.cancelOrder = function (id, callback) {
this.request({
method: "CancelOrder",
id: id
}, callback);
};
/*
* Cancels all open orders belonging to the authenticated user.
*/
Coinfloor.prototype.cancelAllOrders = function (callback) {
this.request({
method: "CancelAllOrders"
}, callback);
};
/*
* Retrieves the trailing 30-day trading volume of the authenticated user
* in the specified asset.
*/
Coinfloor.prototype.getTradeVolume = function (asset, callback) {
this.request({
method: "GetTradeVolume",
asset: asset
}, callback);
};
/*
* Subscribes to (or unsubscribes from) the orders feed of the specified
* order book. Subscribing to feeds does not require authentication.
*/
Coinfloor.prototype.watchOrders = function (base, counter, watch, callback) {
this.request({
method: "WatchOrders",
base: base,
counter: counter,
watch: watch
}, callback);
};
/*
* Subscribes to (or unsubscribes from) the ticker feed of the specified
* order book. Subscribing to feeds does not require authentication.
*/
Coinfloor.prototype.watchTicker = function (base, counter, watch, callback) {
this.request({
method: "WatchTicker",
base: base,
counter: counter,
watch: watch
}, callback);
};
// singleton instance for backward compatibility
Coinfloor = new Coinfloor();
})();