-
Notifications
You must be signed in to change notification settings - Fork 0
/
WLA.cpp
executable file
·636 lines (572 loc) · 15.1 KB
/
WLA.cpp
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Written on ubuntu environment
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <string.h>
#include <chrono>
#include <openssl/hmac.h>
#include <iomanip>
#include <chrono>
#include <sstream>
#include <fstream>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <cstdlib>
#include <unistd.h>
using namespace std;
using json = nlohmann::json;
using namespace std::chrono;
string baseUrl = "https://api.binance.com";
const string apiKey = "9YpTJujwgAA9HcB167yjhQlXyzOcIqpDLQtJURT617hJvSxwEkKwSnpwmVGY7TfI";
const string apiSecret = "IWZkv78zFn1BIene4ukwtewW9n4gVVsdVepREE4mgfCdChylCbwv905fvEjqn6ZQ";
static string gs_strLastResponse, str4;
json JsonForm;
string TempRes;
/*This is our callback function which will be executed by curl on perform.
here we will copy the data received to our struct
ptr : points to the data received by curl.
size : is the count of data items received.
nmemb : number of bytes
data : our pointer to hold the data.
*/
string getTimestamp()
{
long long ms_since_epoch = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
return to_string(ms_since_epoch);
}
string encryptWithHMAC(const char *key, const char *data)
{
unsigned char *result;
static char res_hexstring[64];
int result_len = 32;
string signature;
result = HMAC(EVP_sha256(), key, strlen((char *)key), const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(data)), strlen((char *)data), NULL, NULL);
for (int i = 0; i < result_len; i++)
{
sprintf(&(res_hexstring[i * 2]), "%02x", result[i]);
}
for (int i = 0; i < 64; i++)
{
signature += res_hexstring[i];
}
return signature;
}
string getSignature(string query)
{
string signature = encryptWithHMAC(apiSecret.c_str(), query.c_str());
return "&signature=" + signature;
}
/* concatenate the query parameters to &<key>=<value> */
string joinQueryParameters(const unordered_map<string, string> ¶meters)
{
string queryString = "";
for (auto it = parameters.cbegin(); it != parameters.cend(); ++it)
{
if (it == parameters.cbegin())
{
queryString += it->first + '=' + it->second;
}
else
{
queryString += '&' + it->first + '=' + it->second;
}
}
return queryString;
}
/*Sending the HTTP Request and printing the response*/
void executeHTTPRequest(CURL *curl)
{
CURLcode res;
gs_strLastResponse = "";
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
// cout << gs_strLastResponse << endl;
}
/*Write the server response to string for display*/
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
gs_strLastResponse += (const char *)contents;
gs_strLastResponse += '\n';
return size * nmemb;
}
void sendPublicRequest(CURL *curl, string urlPath, unordered_map<string, string> ¶meters)
{
string url = baseUrl + urlPath + '?';
if (!parameters.empty())
{
url += joinQueryParameters(parameters);
}
// cout << "url:" << url << endl;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
parameters.clear();
executeHTTPRequest(curl);
}
void sendSignedRequest(CURL *curl, string httpMethod, string urlPath, unordered_map<string, string> ¶meters)
{
string url = baseUrl + urlPath + '?';
string queryString = "";
string timeStamp = "timestamp=" + getTimestamp();
if (!parameters.empty())
{
string res = joinQueryParameters(parameters) + '&' + timeStamp;
url += res;
queryString += res;
}
else
{
url += timeStamp;
queryString += timeStamp;
}
string signature = getSignature(queryString);
url += signature;
queryString += signature;
// cout << "url:" << url << endl;
if (httpMethod == "POST")
{
curl_easy_setopt(curl, CURLOPT_URL, (baseUrl + urlPath).c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, queryString.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
parameters.clear();
executeHTTPRequest(curl);
}
else
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
executeHTTPRequest(curl);
}
}
bool ErrorHandler(string message)
{
bool error = true;
json data = message;
if (!json::accept(message)) // prevents wierd nlohmann parse error due to binance reaspones
{
error = true;
usleep(9000000);
cout << "parse error\n";
}
else if (message[9] == '1' && message[10] == '2' && message[11] == '0' && message[12] == '1' && message[13] == '4') // no data(forbidden)
{
error = true;
cout << "binance throuwn forbidden error....exiting" << endl;
exit(0);
}
else if (message[9] == '1' && message[10] == '2' && message[11] == '0' && message[12] == '0' && message[13] == '1') // below min transaction
{
error = true;
exit(0);
}
else if (message[9] == '2' && message[10] == '0' && message[11] == '1' && message[12] == '5') // invalid api or ip
{
error = true;
exit(0);
}
else if (message[9] == '1' && message[10] == '0' && message[11] == '2' && message[12] == '1') // timestamp expired
{
error = true;
}
else if (message[2] == 'c' && message[3] == 'o' && message[4] == 'd' && message[5] == 'e') // general errors(unknown by me)
{
error = true;
usleep(1000000);
exit(0);
}
else if (message[9] == '9' && message[10] == '0' && message[11] == '0' && message[12] == '0')
{ // Enter an amount to {0} decimals.
error = true;
cout << "Enter an amount to {0} decimals." << endl;
exit(0);
}
else if (message[9] == '1' && message[10] == '2' && message[11] == '0' && message[12] == '0' && message[13] == '1')
{ // Below the minimum transaction quantity
error = true;
cout << "Below the minimum transaction quantity." << endl;
exit(0);
}
else
{
error = false;
}
return error;
}
string SignedResp[5];
string UnsignedResp[5];
string *APIcall(string protocol, string path, unordered_map<string, string> par, string key[], int size, int round, int iter)
{
CURL *curl;
string queryString;
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
/* Adding API key to header */
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, ("X-MBX-APIKEY:" + apiKey).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
int NumOfKey = size; // length calculation
do
{
sendSignedRequest(curl, protocol, path, par);
// cout << " size is " << gs_strLastResponse.size() << endl;
cout << "dumping round "<<round<<" iter "<<iter<<" "<< gs_strLastResponse << endl;
} while (ErrorHandler(gs_strLastResponse));
TempRes = gs_strLastResponse;
auto APIresponse = json::parse(gs_strLastResponse);
JsonForm = APIresponse;
if (APIresponse.is_array()) // for array response
{
// cout << " yes it is array\n";
for (int i = 0; i < NumOfKey; i++)
{
if (APIresponse[0][key[i]].is_number())
{
auto p = APIresponse[0][key[i]].get<int>();
int TempNum = p;
SignedResp[i] = to_string(TempNum);
}
else
{
// cout << "\n arrived here\n";
SignedResp[i] = APIresponse[0][key[i]];
}
}
}
else if (!APIresponse.is_array()) // for non-array reponse
{
// cout << "\n no array\n";
for (int i = 0; i < NumOfKey; i++)
{
if (APIresponse[key[i]].is_number())
{
auto p = APIresponse[key[i]].get<int>();
int TempNum = p;
SignedResp[i] = to_string(TempNum);
}
else
{
SignedResp[i] = APIresponse[key[i]];
}
}
}
/* always cleanup */
curl_easy_cleanup(curl);
curl_global_cleanup();
gs_strLastResponse.clear();
return SignedResp;
}
string *PublicAPI(string path, unordered_map<string, string> par, string key[], int size)
{
CURL *curl;
string queryString;
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
/* Adding API key to header */
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, ("X-MBX-APIKEY:" + apiKey).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
int NumOfKey = size; // length calculation
do
{
sendPublicRequest(curl, path, par);
// cout << "dumping " << gs_strLastResponse << endl;
} while (ErrorHandler(gs_strLastResponse));
auto UnResp = json::parse(gs_strLastResponse);
JsonForm = UnResp;
if (UnResp.is_array()) // for array response
{
// cout << " yes it is array\n"<<endl;
for (int i = 0; i < NumOfKey; i++)
{
if (UnResp[0][key[i]].is_number())
{
auto p = UnResp[0][key[i]].get<int>();
int TempNum = p;
UnsignedResp[i] = to_string(TempNum);
}
else
{
UnsignedResp[i] = UnResp[0][key[i]];
}
}
}
else if (!UnResp.is_array()) // for non-array reponse
{
// cout << "" no array\n"<<endl;
for (int i = 0; i < NumOfKey; i++)
{
if (UnResp[key[i]].is_number())
{
auto p = UnResp[key[i]].get<int>();
int TempNum = p;
UnsignedResp[i] = to_string(TempNum);
}
else
{
UnsignedResp[i] = UnResp[key[i]];
}
}
}
/* always cleanup */
curl_easy_cleanup(curl);
curl_global_cleanup();
return UnsignedResp;
}
string dref(string *str, int index)
{
string value = str[index];
return value;
}
string ReadBalance(string asset)
{
unordered_map<string, string> par;
string *res;
string balance;
/*------read "from" asset------*/
par.insert(
{
{"asset", asset} // asset
});
string key[] = {"free"};
res = APIcall("POST", "/sapi/v3/asset/getUserAsset", par, key,1,1,1);
balance = dref(res, 0);
// cout << "balance is " << balance << endl;
return balance;
}
void CombinationMaker()
{
unordered_map<string, string> par, s;
string *res, key[] = {"poolId"};
vector<string> pairs, USDTpairs, SinglePairs, NonUSDT, graph;
string Lpair, Rpair;
int separator;
/*---------read tradabel pair on bswap------------*/
par.insert({});
res = APIcall("GET", "/sapi/v1/bswap/pools", par, key, 1,1,1);
/*---------separating USDT pairs only------------*/
for (const auto &item : JsonForm)
{
string tradeSymbol = string(item["poolName"]);
pairs.push_back(tradeSymbol);
}
for (const string &value : pairs)
{
for (int i = 0; i < value.size(); i++) // collect all pairs to vector pair
{
if (value[i] == '/')
{
separator = i;
break;
}
}
for (int i = 0; i < separator; i++) // constracting left side pair name
{
Lpair += value[i];
}
for (int i = separator + 1; i < value.size(); i++) // constracting right side pair name
{
Rpair += value[i];
}
string temp;
if (Lpair == "USDT" || Rpair == "USDT") // check wether or not it is usdt pair or non usdt pair
{
temp = Rpair + "/" + Lpair;
USDTpairs.push_back(value);
if (Lpair == "USDT")
SinglePairs.push_back(Rpair);
else
SinglePairs.push_back(Lpair);
}
else
{
NonUSDT.push_back(value);
NonUSDT.push_back(temp);
}
Lpair.clear();
Rpair.clear();
}
/*arbitrage path builder using only possible paths in Bswap*/
string TempHolder;
// cout << "checking singlepairlist\n";
for (const string &Lside : SinglePairs)
{
for (const string &Rside : SinglePairs)
{
TempHolder = Lside + "/" + Rside;
// cout << h << endl;
for (const string &Source : NonUSDT)
{
if (TempHolder == Source)
{
string temp = "USDT " + Lside + " " + Rside;
graph.push_back(temp);
}
}
}
}
ofstream file("graph.dat");
if (file)
{
for (const string &data : graph)
{
file << data << "\n";
}
file.close();
}
} // end of combination maker
bool order(string from, string to)
{
unordered_map<string, string> par;
string *res;
string key[5];
bool result;
string FromBalance = ReadBalance(from);
// swap order
if (stod(FromBalance) > 0)
{
// cout << "qauantity is " << FromBalance << "\n";
key[0] = {"swapId"};
par.insert(
{{"quoteAsset", from}, // asset to sell
{"baseAsset", to}, // asset to buy
{"quoteQty", FromBalance}, // quoteAsset
{"timeInForce", "GTC"}});
res = APIcall("POST", "/sapi/v1/bswap/swap", par, key, 1,1,1);
string SwapRes = TempRes;
// cout<<"the string being checked is"<<SwapRes<<endl;
if (SwapRes[2] == 's' && SwapRes[3] == 'w' && SwapRes[4] == 'a' && SwapRes[5] == 'p' && SwapRes[6] == 'I' && SwapRes[7] == 'd')
{
result = true;
// cout << "yaa contains swap id" << endl;
}
else
{
result = false;
// cout << "no swap id" << endl;
}
}
else
{
cout << "no asset. amount of asset is 0" << endl;
exit;
}
return result;
}
double price[6];
void ArbFinder() // algo efficiency is -- 20 round of 134 iterration in 14 mins
{
string a[144], b[144], c[144];
ifstream input("graph.dat");
while (!input)
{
CombinationMaker();
}
string pair1, pair2, pair3;
int k = 0;
while (input >> pair1 >> pair2 >> pair3)
{
a[k] = pair1;
b[k] = pair2;
c[k] = pair3;
k++;
}
input.close();
cout << "\n\n\t\tversion 1.3 min 0.18 only 7 orders\n"
<< endl;
string searched;
ifstream command;
int RoundCounter = 1;
double amount = 50;
int OrderExe = 0;
while (RoundCounter < 1500)
{
for (int i = 0; i <= 132; i++)
{
int foundarb = 0;
again:
/*---------let path a-b-c------------*/
unordered_map<string, string> pathA, pathB, pathC;
string *res, key[] = {"baseQty"};
string amt = to_string(amount);
/*---------path a-b------------*/
pathA.insert(
{{"quoteAsset", a[i]}, // asset to sell
{"baseAsset", b[i]}, // asset to buy
{"quoteQty", amt}, // quoteAsset
{"timeInForce", "GTC"}});
res = APIcall("GET", "/sapi/v1/bswap/quote", pathA, key, 1,RoundCounter,i);
string SwapPrice1 = dref(res, 0);
/*---------path b-c------------*/
pathB.insert(
{{"quoteAsset", b[i]}, // asset to sell
{"baseAsset", c[i]}, // asset to buy
{"quoteQty", SwapPrice1}, // quoteAsset
{"timeInForce", "GTC"}});
res = APIcall("GET", "/sapi/v1/bswap/quote", pathB, key, 1,RoundCounter,i);
string SwapPrice2 = dref(res, 0);
/*---------path c-a------------*/
pathC.insert(
{{"quoteAsset", c[i]}, // asset to sell
{"baseAsset", a[i]}, // asset to buy
{"quoteQty", SwapPrice2}, // quoteAsset
{"timeInForce", "GTC"}});
res = APIcall("GET", "/sapi/v1/bswap/quote", pathC, key, 1,RoundCounter,i);
string SwapPrice3 = dref(res, 0);
/*calculation part*/
double total;
total = stod(SwapPrice3); // --swaptoswap with uncertainity of (+-)0.017524065 USDT
searched = a[i] + "-" + b[i] + "-" + c[i];
cout << "\t\tgraph is " << searched << endl<< endl;
double net = total - amount;
if (net > 0)
{
if (foundarb > 1)
{
if (order(a[i], b[i]))
{
usleep(2500000);
if (order(b[i], c[i]))
{
usleep(2500000);
if (order(c[i], a[i]))
{
cout << "\nall succss" << endl;
OrderExe++;
cout<<"no of exe orders are "<<OrderExe<<endl;
}
else
{
cout << "failed at third" << endl;
}
}
else
{
cout << "failed at sec" << endl;
}
usleep(2100000);
}
else
{
cout << "failed at first" << endl;
}
}
cout << "\t\tnet is " << net << "\n\n";
foundarb++;
goto again;
}
}
RoundCounter++;
cout << "\n\n\t\t round----- " << RoundCounter << " ------" << endl;
}
cout << "runned the estimation hour of 24...stopped for maintainance or analysis" << endl;
exit(3);
}
int main()
{
ArbFinder();
}