-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
263 lines (259 loc) · 6.66 KB
/
api.js
File metadata and controls
263 lines (259 loc) · 6.66 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
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
/**
* BTC-e JavaScript Trading API
* https://btc-e.com/api/documentation
*
* Author: jsCoin
* BTC : 151vumzopVBZMV9CtswFiumQBbEHcULPnG
* LTC : Laoq3qsLvQFCnnbfcFGpQyjy5kcK58bpen
*
* Dependencies:
* jQuery - http://jquery.com/
* CryptoJS - http://code.google.com/p/crypto-js/
* CryptoJS HMAC SHA512 rollup -
* http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha512.js
**/
/**
* On object instantiation the getInfo method should be called to initialize
* member variables.
*/
function API(key,secret,startNonce){
self.key = key;
self.secret = secret;
self.headers = {
Key : self.key,
Sign : ''
};
this.lastNonce = startNonce;
}
API.prototype.key;
API.prototype.secret;
API.prototype.url = 'https://btc-e.com/tapi';
API.prototype.headers;
API.prototype.funds;
API.prototype.openOrders;
API.prototype.rights;
API.prototype.transactionCount;
/**
* Send the request to the server asynchronously.
*/
API.prototype.send = function(params, success){
this.lastNonce = this.lastNonce+1;
localStorage['lastNonce'] = this.lastNonce;
params.nonce = this.lastNonce;
var query = $.param(params);
self.headers.sign = CryptoJS.HmacSHA512(query,self.secret).toString();
$.ajax({
async : true,
type : 'POST',
url : this.url,
headers : self.headers,
dataType : 'json',
data : params,
success : success
});
};
/**
* Retrieve account information and API key permissions.
*/
API.prototype.getInfo = function(callback){
var self = this;
var params = {
method : "getInfo",
};
var success = function(data,text){
if(data.success===1){
self.funds = data.return.funds;
self.openOrders = data.return.open_orders;
self.rights = data.return.rights;
self.transactionCount = data.return.transaction_count;
if(callback)
callback(data.return);
}else{
if(callback)
console.log(data);
callback(data.error);
}
};
this.send(params, success);
};
/**
* Retrieve your transaction history. There are 7 possible parameters for this
* function instead of having a terrible method signature this will take an
* object of the desired parameters.
*
* @param paramObj -
* object containing 0 or members
*
* NOTE: Possible argument members are:
* from,count,from_id,end_id,order,since,end Refer to BTC-e documentation for
* parameter explanation.
*/
API.prototype.transHistory = function(paramObj,callback){
var params = {
method : "TransHistory"
};
$.extend(params,paramObj);
var success = function(data,text){
if(data.success===1){
callback(data.return);
}else{
callback(data.error);
}
};
this.send(params, success);
};
/**
* Retrieve your trade history. There are eight possible parameters for this
* function instead of having a terrible method signature this will take an
* object of the desired parameters.
*
* @param paramObj -
* object containing 0 or members
*
* NOTE: Possible argument members are:
* pair,from,count,from_id,end_id,order,since,end Refer to BTC-e documentation
* for parameter explanation.
*/
API.prototype.tradeHistory = function(paramObj,callback){
var params = {
method : "TradeHistory"
};
$.extend(params,paramObj);
var success = function(data,text){
if(data.success===1){
callback(data.return);
}else{
callback(data.error);
}
};
this.send(params,success);
};
/**
* Retrieve your open order list. There are nine possible parameters for this
* function instead of having a terrible method signature this will take an
* object of the desired parameters.
*
* @param paramObj -
* object containing 0 or members
*
* NOTE: Possible argument members are:
* pair,active,from,count,from_id,end_id,order,since,end Refer to BTC-e
* documentation for parameter explanation.
*/
API.prototype.orderList = function(paramObj,callback){
var params = {
method : "OrderList"
};
$.extend(params,paramObj);
var success = function(data,text){
if(data.success===1){
callback(data.return);
}else{
callback(data.error);
}
};
this.send(params,success);
};
/**
* Create a new trade. All parameters are required.
*
* @param pair -
* currency pair in form btc_usd
* @param type -
* buy or sell
* @param rate -
* the price you would like to trade at
* @param rate -
* how many coins you want
* @return order stats or error
*/
API.prototype.trade = function(pair,type,rate,amount,callback){
var self = this;
var params = {
method : "Trade",
pair : pair,
type : type,
rate : rate,
amount : amount
};
var success = function(data,text){
if(data.success===1){
self.funds = data.return.funds;
callback(data.return);
}else{
callback(data.error);
}
};
this.send(params,success);
};
/**
* Cancel the argument order.
*
* @param order_id -
* order id of the desired order
*/
API.prototype.cancelOrder = function(order_id,callback){
var self = this;
var params = {
method : "CancelOrder",
order_id : order_id
};
var success = function(data,text){
if(data.success===1){
self.funds = data.return.funds;
callback(data.return)
}else{
calback(data.error);
}
};
this.send(params,success);
};
// ********************** Public API **************************
/*
* Quick implementation for Public API for completeness.
*/
API.prototype.fee = function(pair,callback){
$.ajax({
async : true,
type : 'GET',
url : 'https://btc-e.com/api/2/'+pair+'/fee',
success : function(data){
callback(data);
}
});
};
API.prototype.ticker = function(pair,callback){
$.ajax({
async : true,
type : 'GET',
url : 'https://btc-e.com/api/2/'+pair+'/ticker',
dataType : 'json',
success : function(data){
if(data!==undefined){
callback(pair,data);
}
}
});
};
API.prototype.trades = function(pair,callback){
$.ajax({
async : true,
type : 'GET',
url : 'https://btc-e.com/api/2/'+pair+'/trades',
dataType : 'json',
success : function(data){
callback(data);
}
});
};
API.prototype.depth = function(pair,callback){
$.ajax({
async : true,
type : 'GET',
url : 'https://btc-e.com/api/2/'+pair+'/depth',
dataType : 'json',
success : function(data){
callback(data);
}
});
};