-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitfinex.js
193 lines (155 loc) · 4.14 KB
/
bitfinex.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
/*
Author: Nathaniel Pawelczyk
A node.js wrapper for the Bitfinex API
*/
var request = require("request");
var crypto = require("crypto");
function Bitfinex(key, secret) {
var settings = {
base: "https://api.bitfinex.com",
version: "/v1",
key: key,
secret: secret,
_nonce: Date.now()
};
function nonce() {
return settings._nonce++;
}
function unauthenticated_call(path, callback) {
var url = settings.base + settings.version + path;
request({url: url, method: "GET"}, callback);
}
function authenticated_call(path, parameters, callback) {
var options = {
request: settings.version + path,
nonce: JSON.stringify(nonce()),
};
for(key in parameters) {
options[key] = parameters[key];
}
var payload = new Buffer(JSON.stringify(options)).toString("base64");
var signature = crypto.createHmac("sha384", options).update(payload).digest("hex");
var headers = {
"X-BFX-APIKEY": key,
"X-BFX-PAYLOAD": payload,
"X-BFX-SIGNATURE": signature
}
request({url: url, method: "POST", headers: headers}, callback);
}
//unauthenticated methods
function ticker(symbol, callback) {
var path = "/ticker/" + symbol;
unauthenticated_call(path, callback);
}
function lendbook(currency, callback) {
var path = "/lendbook/" + currency;
unauthenticated_call(path, callback);
}
function orderbook(symbol, callback) {
var path = "/orderbook/" + symbol;
unauthenticated_call(path, callback);
}
function trades(symbol, callback) {
var path = "/trades/" + symbol;
unauthenticated_call(path, callback);
}
function lends(currency, callback) {
var path = "/lends/" + currency;
unauthenticated_call(path, callback);
}
function symbols(callback) {
var path = "/symbols";
unauthenticated_call(callback);
}
//authenticated methods
function new_order(options, callback) {
var path = "/order/new";
authenticated_call(path, options, callback);
}
function multi_order(options, callback)
{
var path = "/order/new/multi";
authenticated_call(path, options, callback);
}
function cancel_order(options, callback)
{
var path = "/order/cancel";
authenticated_call(path, options, callback);
}
function cancel_multi(options, callback)
{
var path = "/order/cancel/multi";
authenticated_call(path, options, callback);
}
function order_status(options, callback)
{
var path = "/order/status";
authenticated_call(path, options, callback);
}
function active_orders(options, callback)
{
var path = "/orders";
authenticated_call(path, options, callback);
}
function active_positions(options, callback)
{
var path = "/positions";
authenticated_call(path, options, callback);
}
function past_trades(options, callback)
{
var path = "/mytrades";
authenticated_call(path, options, callback);
}
function new_offer(options, callback)
{
var path = "/offer/new";
authenticated_call(path, options, callback);
}
function cancel_offer(options, callback)
{
var path = "/offer/cancel";
authenticated_call(path, options, callback);
}
function offer_status(options, callback)
{
var path = "/offer/status";
authenticated_call(path, options, callback);
}
function active_offers(options, callback)
{
var path = "/offers";
authenticated_call(path, options, callback);
}
function active_credits(options, callback)
{
var path = "/credits";
authenticated_call(path, options, callback);
}
function balances(options, callback)
{
var path = "/balances";
authenticated_call(path, options, callback);
}
this.ticker = ticker;
this.lendbook = lendbook;
this.orderbook = orderbook;
this.trades = trades;
this.lends = lends;
this.symbols = symbols;
this.new_order = new_order;
this.multi_order = multi_order;
this.cancel_order = cancel_order;
this.cancel_multi = cancel_multi;
this.order_status = order_status;
this.active_orders = active_orders;
this.active_positions = active_positions;
this.past_trades = past_trades;
this.new_offer = new_offer;
this.cancel_offer = cancel_offer;
this.offer_status = offer_status;
this.active_offers = active_offers;
tihs.active_credits = active_credits;
this.balances = balances;
}
module.exports = Bitfinex;