-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathBittrex.java
More file actions
393 lines (238 loc) · 12.2 KB
/
Copy pathBittrex.java
File metadata and controls
393 lines (238 loc) · 12.2 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;
import javax.net.ssl.HttpsURLConnection;
public class Bittrex {
public static final String ORDERBOOK_BUY = "buy", ORDERBOOK_SELL = "sell", ORDERBOOK_BOTH = "both";
public static final int DEFAULT_RETRY_ATTEMPTS = 1;
public static final int DEFAULT_RETRY_DELAY = 15;
private static final Exception InvalidStringListException = new Exception("Must be in key-value pairs");
private final String API_VERSION = "1.1", INITIAL_URL = "https://bittrex.com/api/";
private final String PUBLIC = "public", MARKET = "market", ACCOUNT = "account";
private final String encryptionAlgorithm = "HmacSHA512";
private String apikey;
private String secret;
private final int retryAttempts;
private int retryAttemptsLeft;
private final int retryDelaySeconds;
public Bittrex(String apikey, String secret, int retryAttempts, int retryDelaySeconds) {
this.apikey = apikey;
this.secret = secret;
this.retryAttempts = retryAttempts;
this.retryDelaySeconds = retryDelaySeconds;
retryAttemptsLeft = retryAttempts;
}
public Bittrex(int retryAttempts, int retryDelaySeconds) {
this.retryAttempts = retryAttempts;
this.retryDelaySeconds = retryDelaySeconds;
retryAttemptsLeft = retryAttempts;
}
public Bittrex() {
this(DEFAULT_RETRY_ATTEMPTS, DEFAULT_RETRY_DELAY);
}
public void setAuthKeysFromTextFile(String textFile) { // Add the text file containing the key & secret in the same path as the source code
try (Scanner scan = new Scanner(getClass().getResourceAsStream(textFile))) {
String apikeyLine = scan.nextLine(), secretLine = scan.nextLine();
apikey = apikeyLine.substring(apikeyLine.indexOf("\"") + 1, apikeyLine.lastIndexOf("\""));
secret = secretLine.substring(secretLine.indexOf("\"") + 1, secretLine.lastIndexOf("\""));
} catch (NullPointerException | IndexOutOfBoundsException e) {
System.err.println("Text file not found or corrupted - please attach key & secret in the format provided.");
}
}
public String getMarkets() { // Returns all markets with their metadata
return getJson(API_VERSION, PUBLIC, "getmarkets");
}
public String getCurrencies() { // Returns all currencies currently on Bittrex with their metadata
return getJson(API_VERSION, PUBLIC, "getcurrencies");
}
public String getTicker(String market) { // Returns current tick values for a specific market
return getJson(API_VERSION, PUBLIC, "getticker", returnCorrectMap("market", market));
}
public String getMarketSummaries() { // Returns a 24-hour summary of all markets
return getJson(API_VERSION, PUBLIC, "getmarketsummaries");
}
public String getMarketSummary(String market) { // Returns a 24-hour summar for a specific market
return getJson(API_VERSION, PUBLIC, "getmarketsummary", returnCorrectMap("market", market));
}
public String getOrderBook(String market, String type) { // Returns the orderbook for a specific market
return getJson(API_VERSION, PUBLIC, "getorderbook", returnCorrectMap("market", market, "type", type));
}
public String getMarketHistory(String market) { // Returns latest trades that occurred for a specific market
return getJson(API_VERSION, PUBLIC, "getmarkethistory", returnCorrectMap("market", market));
}
public String buyLimit(String market, String quantity, String rate) { // Places a limit buy in a specific market; returns the UUID of the order
return getJson(API_VERSION, MARKET, "buylimit", returnCorrectMap("market", market, "quantity", quantity, "rate", rate));
}
public String buyMarket(String market, String quantity) { // Places a market buy in a specific market; returns the UUID of the order
return getJson(API_VERSION, MARKET, "buymarket", returnCorrectMap("market", market, "quantity", quantity));
}
public String sellLimit(String market, String quantity, String rate) { // Places a limit sell in a specific market; returns the UUID of the order
return getJson(API_VERSION, MARKET, "selllimit", returnCorrectMap("market", market, "quantity", quantity, "rate", rate));
}
public String sellMarket(String market, String quantity) { // Places a market sell in a specific market; returns the UUID of the order
return getJson(API_VERSION, MARKET, "sellmarket", returnCorrectMap("market", market, "quantity", quantity));
}
public String cancelOrder(String uuid) { // Cancels a specific order based on its UUID
return getJson(API_VERSION, MARKET, "cancel", returnCorrectMap("uuid", uuid));
}
public String getOpenOrders(String market) { // Returns your currently open orders in a specific market
String method = "getopenorders";
if(market.equals(""))
return getJson(API_VERSION, MARKET, method);
return getJson(API_VERSION, MARKET, method, returnCorrectMap("market", market));
}
public String getOpenOrders() { // Returns all your currently open orders
return getOpenOrders("");
}
public String getBalances() { // Returns all balances in your account
return getJson(API_VERSION, ACCOUNT, "getbalances");
}
public String getBalance(String currency) { // Returns a specific balance in your account
return getJson(API_VERSION, ACCOUNT, "getbalance", returnCorrectMap("currency", currency));
}
public String getDepositAddres(String currency) { // Returns the deposit address for a specific currency - if one is not found, it will be generated
return getJson(API_VERSION, ACCOUNT, "getdepositaddress", returnCorrectMap("currency", currency));
}
public String withdraw(String currency, String quantity, String address, String paymentId) { // Withdraw a certain amount of a specific coin to an address, and add a payment id
String method = "withdraw";
if(paymentId.equals(""))
return getJson(API_VERSION, ACCOUNT, method, returnCorrectMap("currency", currency, "quantity", quantity, "address", address));
return getJson(API_VERSION, ACCOUNT, method, returnCorrectMap("currency", currency, "quantity", quantity, "address", address, "paymentid", paymentId));
}
public String withdraw(String currency, String quantity, String address) { // Withdraw a certain amount of a specific coin to an address
return withdraw(currency, quantity, address, "");
}
public String getOrder(String uuid) { // Returns information about a specific order (by UUID)
return getJson(API_VERSION, ACCOUNT, "getorder", returnCorrectMap("uuid", uuid));
}
public String getOrderHistory(String market) { // Returns your order history for a specific market
String method = "getorderhistory";
if(market.equals(""))
return getJson(API_VERSION, ACCOUNT, method);
return getJson(API_VERSION, ACCOUNT, method, returnCorrectMap("market", market));
}
public String getOrderHistory() { // Returns all of your order history
return getOrderHistory("");
}
public String getWithdrawalHistory(String currency) { // Returns your withdrawal history for a specific currency
String method = "getwithdrawalhistory";
if(currency.equals(""))
return getJson(API_VERSION, ACCOUNT, method);
return getJson(API_VERSION, ACCOUNT, method, returnCorrectMap("currency", currency));
}
public String getWithdrawalHistory() { // Returns all of your withdrawal history
return getWithdrawalHistory("");
}
public String getDepositHistory(String currency) { // Returns your deposit history for a specific currency
String method = "getdeposithistory";
if(currency.equals(""))
return getJson(API_VERSION, ACCOUNT, method);
return getJson(API_VERSION, ACCOUNT, method, returnCorrectMap("currency", currency));
}
public String getDepositHistory() { // Returns all of your deposit history
return getDepositHistory("");
}
private HashMap<String, String> returnCorrectMap(String...parameters) { // Handles the exception of the generateHashMapFromStringList() method gracefully as to not have an excess of try-catch statements
HashMap<String, String> map = null;
try {
map = generateHashMapFromStringList(parameters);
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
private HashMap<String, String> generateHashMapFromStringList(String...strings) throws Exception { // Method to easily create a HashMap from a list of Strings
if(strings.length % 2 != 0)
throw InvalidStringListException;
HashMap<String, String> map = new HashMap<>();
for(int i = 0; i < strings.length; i += 2) // Each key will be i, with the following becoming its value
map.put(strings[i], strings[i + 1]);
return map;
}
private String getJson(String apiVersion, String type, String method) {
return getResponseBody(generateUrl(apiVersion, type, method));
}
private String getJson(String apiVersion, String type, String method, HashMap<String, String> parameters) {
return getResponseBody(generateUrl(apiVersion, type, method, parameters));
}
private String generateUrl(String apiVersion, String type, String method) {
return generateUrl(apiVersion, type, method, new HashMap<String, String>());
}
private String generateUrl(String apiVersion, String type, String method, HashMap<String, String> parameters) {
String url = INITIAL_URL;
url += "v" + apiVersion + "/";
url += type + "/";
url += method;
url += generateUrlParameters(parameters);
return url;
}
private String generateUrlParameters(HashMap<String, String> parameters) { // Returns a String with the key-value pairs formatted for URL
String urlAttachment = "?";
Object[] keys = parameters.keySet().toArray();
for(Object key : keys)
urlAttachment += key.toString() + "=" + parameters.get(key) + "&";
return urlAttachment;
}
public static List<HashMap<String, String>> getMapsFromResponse(String response) {
final List<HashMap<String, String>> maps = new ArrayList<>();
if(!response.contains("[")) {
maps.add(jsonMapToHashMap(response.substring(response.lastIndexOf("\"result\":") + "\"result\":".length(), response.indexOf("}") + 1))); // Sorry.
} else {
final String resultArray = response.substring(response.indexOf("\"result\":") + "\"result\":".length() + 1, response.lastIndexOf("]"));
final String[] jsonMaps = resultArray.split(",(?=\\{)");
for(String map : jsonMaps)
maps.add(jsonMapToHashMap(map));
}
return maps;
}
private static HashMap<String, String> jsonMapToHashMap(String jsonMap) {
final HashMap<String, String> map = new HashMap<>();
final String[] keyValuePairs = jsonMap.replaceAll("[{}]", "").split(",");
for(String pair : keyValuePairs) {
pair = pair.replaceAll("\"", "");
final String[] pairValues = pair.split(":",2);
map.put(pairValues[0], pairValues[1]);
}
return map;
}
private String getResponseBody(final String baseUrl) {
String result = null;
final String urlString = baseUrl + "apikey=" + apikey + "&nonce=" + EncryptionUtility.generateNonce();
try {
URL url = new URL(urlString);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setRequestProperty("apisign", EncryptionUtility.calculateHash(secret, urlString, encryptionAlgorithm));
BufferedReader reader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
StringBuffer resultBuffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null)
resultBuffer.append(line);
result = resultBuffer.toString();
} catch (UnknownHostException | SocketException e) {
if(retryAttemptsLeft-- > 0) {
System.err.printf("Could not connect to host - retrying in %d seconds... [%d/%d]%n", retryDelaySeconds, retryAttempts - retryAttemptsLeft, retryAttempts);
try {
Thread.sleep(retryDelaySeconds * 1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
result = getResponseBody(baseUrl);
} else {
throw new ReconnectionAttemptsExceededException("Maximum amount of attempts to connect to host exceeded.");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
retryAttemptsLeft = retryAttempts;
}
return result;
}
}