-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathassets_js_bitrequest_monitors.js
1328 lines (1272 loc) · 51.2 KB
/
assets_js_bitrequest_monitors.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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
$(document).ready(function() {
updaterequeststatestrigger();
updaterequeststatesrefresh();
//trigger_requeststates
//get_requeststates
//getinputs
//select_rpc_init
//select_rpc
//continue_select
//continue_select_api
//continue_select_rpc
//fail_dialogs
//scan_match
//tx_count
//tx_api_scan_fail
//tx_api_fail
//handle_rpc_fails
//pick_next_rpc
//no_results
//get_next_l2
//get_next_rpc
//api_eror_msg
//get_api_error_data
//set_api_src
//api_src
//api_callback
//scan_tx_li
//append_tx_li
//hs_for
//data_title
//compareamounts
//init_historical_fiat_data
//get_historical_fiat_data
//get_historic_fiatprice_api_payload
//form_date
//get_historical_crypto_data
//get_payload_historic_coingecko
//get_payload_historic_coinpaprika
//get_payload_historic_coincodex
//cx_date
//compare_historic_prices
//get_historic_object_coingecko
//get_historic_object_coinpaprika
// ** Helpers **
//is_scanning
//clearscan
//check_api
//tx_data
});
// ** Fetch incoming transactions **
// Attaches a click event listener to update request states
function updaterequeststatestrigger() {
$(document).on("click", ".requestsbttn .self", function() {
if (is_scanning()) return
trigger_requeststates(true);
})
}
// Updates request states based on URL parameters
function updaterequeststatesrefresh() {
const gets = geturlparameters();
if (gets.xss) {
return
}
if (gets.p === "requests") { // only trigger on "requests page"
setTimeout(function() {
trigger_requeststates("delay");
}, 300);
}
}
// Triggers the update of request states
function trigger_requeststates(trigger) {
if (glob_const.offline === true) {
return // do nothing when offline
}
glob_let.rpc_attempts = {}, // reset cache and index
glob_let.proxy_attempts = {},
glob_let.tx_list = [], // reset transaction index
glob_let.statuspush = [],
glob_let.l2_fetched = {};
const active_requests = $("#requestlist .rqli").filter(function() {
return $(this).data("pending") !== "unknown";
});
active_requests.addClass("open");
get_requeststates(trigger, active_requests);
}
// Retrieves and processes request states
function get_requeststates(trigger, active_requests) {
const request_data = $("#requestlist li.rqli.open").first().data();
if (request_data) {
if (trigger === "loop") {
getinputs(request_data);
return
}
const d_lay = trigger === "delay",
statuscache = br_get_session("txstatus", true);
if (statuscache) {
const cachetime = now() - statuscache.timestamp,
requeststates = statuscache.requeststates;
if (cachetime > 30000 || empty_obj(requeststates)) { //check if cached crypto rates are expired (check every 30 seconds on page refresh or when opening request page)
active_requests.addClass("scan");
br_remove_session("txstatus"); // remove cached transactions
getinputs(request_data, d_lay);
return
}
if (trigger === true) {
return
}
// Only update on page refresh
// parse cached transaction data
$.each(requeststates, function(i, value) {
const thislist = $("#" + value.requestid),
thisdata = thislist.data();
if (thisdata) {
const pendingstatus = thisdata.pending;
if (pendingstatus === "scanning" || pendingstatus === "polling") {
const statuspanel = thislist.find(".pmetastatus"),
transactionlist = thislist.find(".transactionlist");
statuspanel.text(value.status);
transactionlist.empty();
add_historical_data(transactionlist, value.transactions);
thislist.addClass("pmstatloaded");
}
}
});
return
}
active_requests.addClass("scan");
getinputs(request_data, d_lay);
return
}
if (!empty_obj(glob_let.statuspush)) {
const statusobject = {
"timestamp": now(),
"requeststates": glob_let.statuspush
};
br_set_session("txstatus", statusobject, true);
saverequests();
clearscan();
}
}
// Retrieves input data for requests
function getinputs(rd, dl) {
const rdo = tx_data(rd); // fetchblocks.js
if (rdo.pending === "scanning" || rdo.pending === "polling") {
if (dl) {
const delay = 10000,
mtlc = br_get_session("monitor_timer"),
monitor_timer = mtlc ? parseInt(mtlc, 10) : delay,
timelapsed = now() - monitor_timer;
if (timelapsed < delay) { // prevent over scanning
playsound(glob_const.funk);
clearscan();
return
}
br_set_session("monitor_timer", now());
}
const api_info = check_api(rd.payment),
api_data = api_info.data;
rdo.thislist.removeClass("pmstatloaded");
tx_count(rdo.statuspanel, "reset");
select_rpc_init(rd, api_data, rdo);
return
}
const transactionlist = rdo.transactionlist;
if (transactionlist) {
transactionlist.find("li").each(function(i) {
glob_let.tx_list.push($(this).data("txhash"));
});
api_callback(rdo);
}
}
// Initializes API input retrieval
function select_rpc_init(rd, api_data, rdo) {
if (api_data) {
const rq_id = rd.requestid || "",
l2 = q_obj(api_data, "network") || rd.eth_layer2,
l2_prefix = l2 || "";
glob_let.rpc_attempts[sha_sub(rq_id + api_data.url + l2_prefix, 15)] = null, // reset api attempts
glob_let.rpc_overflow = 0; // reset overflow limits
select_rpc(rd, api_data, rdo);
return
}
console.error("error", "no api data available");
}
// Selects the appropriate API based on the request data and API information
function select_rpc(rd, api_data, rdo) {
if (rd.lightning && rdo.source === "list") {
lightning_fetch(rd, api_data, rdo);
return
}
continue_select(rd, api_data, rdo);
}
// Continue after scanning lightning transaction
function continue_select(rd, api_data, rdo) {
const src = rdo.source;
if (src === "list") {
const thislist = rdo.thislist;
if (thislist) {
thislist.removeClass("no_network");
const transactionlist = rdo.transactionlist;
if (transactionlist) {
transactionlist.empty();
}
}
}
const rq_id = rd.requestid || "";
glob_let.rpc_attempts[sha_sub(rq_id + api_data.url, 15)] = true;
if (api_data.api) {
continue_select_api(rd, api_data, rdo);
return
}
continue_select_rpc(rd, api_data, rdo);
}
function continue_select_api(rd, api_data, rdo) {
const api_name = api_data.name;
if (api_name === "mymonero api") {
monero_fetch(rd, api_data, rdo);
return
}
if (api_name === "blockchair_xmr") {
monero_fetch(rd, api_data, rdo);
return
}
if (api_name === "mempool.space") {
mempoolspace_rpc_init(rd, api_data, rdo, false);
return
}
if (api_name === "blockchain.info") {
blockchaininfo_fetch_init(rd, api_data, rdo);
return
}
if (api_name === "blockcypher") {
blockcypher_fetch(rd, api_data, rdo);
return
}
if (api_name === "etherscan") {
omniscan_fetch(rd, api_data, rdo, null, 1);
return
}
if (api_name === "ethplorer") {
ethplorer_fetch(rd, api_data, rdo);
return
}
if (api_name === "blockchair") {
blockchair_fetch(rd, api_data, rdo);
return
}
if (api_name === "nimiq.watch" || api_name === "mopsus.com") {
nimiq_fetch(rd, api_data, rdo);
return
}
if (rd.payment === "kaspa") {
kaspa_fetch_init(rd, api_data, rdo);
return
}
if (api_name === "dash.org") {
insight_fetch_dash(rd, api_data, rdo);
return
}
if (rdo.source === "addr_polling") {
glob_let.rpc_attempts = {}
select_rpc(rd, api_data, rdo);
return
}
api_callback(rdo);
}
function continue_select_rpc(rd, api_data, rdo) {
if (is_btchain(rd.payment) === true) {
mempoolspace_rpc_init(rd, api_data, rdo, true);
return
}
if (rd.payment === "ethereum" || rd.erc20 === true) {
infura_txd_rpc(rd, api_data, rdo);
return
}
if (rd.payment === "nano") {
nano_rpc(rd, api_data, rdo);
return
}
api_callback(rdo);
}
// API error handling
// Handles API error dialogs
function fail_dialogs(apisrc, error) {
const error_data = get_api_error_data(error);
api_eror_msg(apisrc, error_data)
}
// Processes the scan results and performs appropriate actions based on the match
function scan_match(rd, api_data, rdo, counter, txdat, l2) {
const src = rdo.source;
if (src === "list") {
tx_count(rdo.statuspanel, counter);
const thislist = rdo.thislist;
if (thislist) {
thislist.removeClass("no_network");
}
}
if (txdat) {
const txhash = rd.txhash || txdat.txhash;
if (!txhash) return;
const eth_layer2 = txdat.eth_layer2;
if (src === "list") {
if (eth_layer2) {
const hasl2 = rd.eth_layer2;
if (!rd.eth_layer2) {
// save eth l2 chain
rd.eth_layer2 = eth_layer2;
// block l2 scanning on match
glob_let.l2_fetched.id = rd.requestid;
glob_let.l2_fetched.l2 = eth_layer2;
return
}
}
compareamounts(rd, rdo);
return
}
if (isopenrequest()) { // only when request is visible
txdat.txhash = txhash;
const status = confirmations(txdat);
if (status === "paid") {
forceclosesocket();
return
}
if (rdo.pending === "scanning") { // scanning
// After scan
if (src === "after_scan") {
glob_const.html.addClass("blurmain_payment");
glob_const.paymentpopup.addClass("active");
closeloader();
}
clearpinging();
tx_polling_init(txdat, api_data);
return
}
if (src === "tx_polling" || src === "l2_polling") {
glob_let.polling_overflow++;
if (glob_let.polling_overflow > glob_const.overflow_limit) return false; // prevent overflow
if (eth_layer2) { // Eth layer 2
glob_let.l2s = {};
set_l2_status(api_data, true);
}
tx_polling(txdat, api_data);
}
}
return
}
if (src === "list") {
if (rd.erc20 || rd.payment === "ethereum") {
if (!l2) {
// Init eth layer 2's
query_ethl2_api(rd, rdo);
return
}
}
api_callback(rdo);
return
}
if (src === "after_scan") {
cancel_after_scan();
return
}
}
// Updates the transaction count in the status panel
function tx_count(statuspanel, count) {
if (count === "reset") {
statuspanel.attr("data-count", 0).text("+ " + 0);
return
}
const current_count = parseInt(statuspanel.attr("data-count")),
new_count = current_count + count;
if (!new_count) return;
statuspanel.attr("data-count", new_count).text("+ " + new_count);
if (new_count > 1) {
statuspanel.closest(".rqli").find(".metalist .show_tx").hide();
}
}
// Handles API scan failures
function tx_api_scan_fail(rd, api_data, rdo, error_data, is_proxy, l2) {
if (api_data === "ln") {
api_eror_msg("lightning", error_data);
return
}
const src = rdo.source;
if (l2 && src === "l2_scanning") {
handle_socket_fails(api_data, rd.address, rdo.ping_id, l2);
return
}
if (src === "list") {
const thislist = rdo.thislist;
if (thislist) {
tx_api_fail(thislist, rdo.statuspanel);
}
}
handle_rpc_fails(rd, rdo, error_data, api_data, is_proxy, l2);
return
}
// Updates UI elements to reflect an API failure
function tx_api_fail(thislist, statuspanel) {
thislist.addClass("no_network");
statuspanel.attr("data-count", 0).text("0");
}
// Handles API failures and attempts to use alternative APIs or RPCs
function handle_rpc_fails(rd, rdo, error, api_data, is_proxy, l2) {
const src = rdo.source,
error_data = get_api_error_data(error, is_proxy),
timeout = rdo.timeout,
cachetime = rdo.cachetime;
function next_proxy() { // try next proxy
if (get_next_proxy()) {
if (src === "addr_polling") {
address_polling_init(timeout, api_data, true);
} else {
select_rpc(rd, api_data, rdo);
}
return true
}
return false
}
if (is_proxy) { // Try next proxy if proxy fails
if (next_proxy()) {
return
}
no_results(rdo, src, api_data, error_data);
return
}
if (!api_data) {
api_eror_msg(false, error_data);
if (src === "list") {
api_callback(rdo);
return
}
clearpinging();
socket_info(api_data, false);
notify(translate("websocketoffline"), 500000, "yes");
return
}
const requestid = rd.requestid,
payment = rd.payment;
if (l2) {
const next_l2_api = get_next_l2(payment, api_data, requestid, l2);
if (next_l2_api) {
// Scan eth layer 2
if (src === "list") {
query_ethl2_api(rd, rdo, next_l2_api, l2);
return
}
if (src === "l2_polling") {
tx_polling_l2(l2, next_l2_api);
return
}
}
} else {
const nextrpc = get_next_rpc(payment, api_data, requestid);
if (nextrpc) {
pick_next_rpc(rd, rdo, nextrpc, timeout);
return
}
if (rd.erc20 || payment === "ethereum") {
// Init eth layer 2
query_ethl2_api(rd, rdo);
return
}
}
if (next_proxy()) { // Try next proxy after trying all api's
return
}
no_results(rdo, src, api_data, error_data);
}
// Pick next api rpx.
function pick_next_rpc(rd, rdo, next_rpc, timeout) {
const src = rdo.source;
if (src === "addr_polling") {
address_polling_init(timeout, next_rpc, true);
return
}
if (src === "tx_polling") {
tx_polling_l1(rdo.txdat, next_rpc);
return
}
if (src === "after_scan") {
after_scan(next_rpc);
return
}
continue_select(rd, next_rpc, rdo);
}
// Show error message if all proxies / apis fail.
function no_results(rdo, src, api_data, error_data) {
const rpc_id = api_data.name || api_data.url || "unknown";
api_eror_msg(rpc_id, error_data);
if (src === "list") {
api_callback(rdo);
return
}
clearpinging();
socket_info(api_data, false);
notify(translate("websocketoffline"), 500000, "yes");
}
function get_next_l2(this_payment, api_data, requestid, l2) {
glob_let.rpc_overflow++;
if (glob_let.rpc_overflow > glob_const.overflow_limit) return false; // prevent overflow
const api_settings = q_obj(getcoinsettings(this_payment), "layer2.options." + l2 + ".apis");
if (api_settings) {
const apilist = api_settings.apis,
al_length = apilist.length;
if (apilist && al_length) {
const currentIndex = apilist.findIndex(option => option.name === api_data.name),
next_api = apilist[(currentIndex + 1) % al_length],
rq_id = requestid || "";
if (glob_let.rpc_attempts[sha_sub(rq_id + next_api.url + l2, 15)] !== true) {
return next_api;
}
}
}
return false;
}
function get_next_rpc(this_payment, api_data, requestid, l2) {
glob_let.rpc_overflow++;
if (glob_let.rpc_overflow > glob_const.overflow_limit) return false; // prevent overflow
const api_settings = cs_node(this_payment, "apis", true);
if (api_settings) {
const apilist = api_settings.apis,
rpclist = api_settings.options,
restlist = (rpclist && rpclist.length) ? $.merge(apilist, rpclist) : apilist,
rl_length = restlist.length;
if (restlist && rl_length) {
const next_scan = restlist.findIndex(option => option.url === api_data.url),
next_rpc = restlist[(next_scan + 1) % rl_length],
rq_id = requestid || "",
l2_prefix = l2 || "";
if (glob_let.rpc_attempts[sha_sub(rq_id + next_rpc.url + l2_prefix, 15)] !== true) {
return next_rpc;
}
}
}
return false;
}
// Displays error messages for API-related issues
function api_eror_msg(apisrc, error) {
if (!error) return;
const error_dat = error || {
"errormessage": "errormessage",
"errorcode": null
};
if (error.console) {
console.error("API error:", error_dat);
return
}
if ($("#dialogbody .doselect").length) {
return
}
if (apisrc) {
const keyfail = error.apikey === true,
errormessage = error_dat.errormessage,
errorcode = error_dat.errorcode ? "Error: " + error_dat.errorcode : "",
api_bttn = keyfail ? "<div id='add_api' data-api='" + apisrc + "' class='button'>" + translate("addapikey", {
"apisrc": apisrc
}) + "</div>" : "",
t_op = apisrc ? "<span id='proxy_dialog' class='ref'>" + translate("tryotherproxy") + "</span>" : "",
content = "<h2 class='icon-blocked'>" + errorcode + "</h2><p class='doselect'><strong>" + translate("error") + ": " + errormessage + "<br/><br/>" + t_op + "</p>" + api_bttn;
popdialog(content, "canceldialog");
}
}
// Extracts and formats error data from various API responses
function get_api_error_data(error, proxy) {
if (!error) return;
const errorcode = error.code || error.status || error.error_code || 0,
errormessage = error.error || error.message || error.type || error.error_message || error.statusText || error,
cons = error.console,
ak_check = (typeof error === "string") ? (error.indexOf("API calls limits have been reached") > -1 || error.indexOf("Limits reached") > -1) : false, // blockcypher
apikey = (
errorcode === 101 || // fixer
errorcode === 402 || // blockchair
errorcode === 403 || errorcode === 1 || // ethplorer => invalid or missing API key
errorcode === 1001 || // coinmarketcap => invalid API key
errorcode === 1002 || // coinmarketcap => missing API key
ak_check
);
const error_dat = {
"errorcode": errorcode,
"errormessage": errormessage,
"apikey": apikey,
"console": cons
},
is_proxy = (proxy) ? "proxy " : "";
console.error("API " + is_proxy + "error:", error_dat);
return error_dat;
}
// Sets the API source for a given request
function set_api_src(rdo, api_data) {
if (rdo.source === "list") {
api_src(rdo.thislist, api_data);
}
}
// Updates the UI with API source information
function api_src(thislist, api_data) {
const api_url = api_data.url,
api_url_short = api_url ? (api_url.length > 40 ? api_url.slice(0, 40) + "..." : api_url) : "",
aoi_name = api_data.name,
api_title = aoi_name === "mempool.space" ? api_url : aoi_name,
api_source = api_title || api_url_short;
thislist.data("source", api_source).find(".api_source").html("<span class='src_txt' title='" + api_url_short + "'>" + translate("source") + ": " + api_source + "</span><span class='icon-wifi-off'></span><span class='icon-connection'></span>");
}
// Handles the callback after an API request is completed
function api_callback(rdo) {
const src = rdo.source;
if (!src === "list") {
return
}
// src === "list"
const thislist = rdo.thislist;
if (thislist && thislist.hasClass("scan")) {
thislist.removeClass("scan open").addClass("pmstatloaded");
const requestid = rdo.requestid,
transactionli = rdo.transactionlist,
txli = transactionli.children("li");
if (txli.length) {
const transactionpush = [];
txli.each(function() {
const thisnode = $(this),
thisdata = thisnode.data();
if (thisdata) {
transactionpush.push(thisdata);
if (thisnode.attr("title")) {} else {
const h_string = data_title(thisdata);
if (h_string) {
thisnode.append(hs_for(h_string)).attr("title", h_string);
}
}
}
});
const statuspanel = thislist.find(".pmetastatus"),
statusbox = {
"requestid": requestid,
"status": statuspanel.attr("data-count"),
"transactions": transactionpush
};
glob_let.statuspush.push(statusbox);
} else {
const statusbox = {
"requestid": requestid,
"status": 0
};
glob_let.statuspush.push(statusbox);
}
get_requeststates("loop");
}
}
// Appends a transaction list item with given transaction data
function append_tx_li(txd, rqtype) {
const txhash = txd.txhash;
if (!txhash) return null;
const ccval = txd.ccval,
ccval_rounded = trimdecimals(ccval, 6),
transactiontime = txd.transactiontime,
conf = txd.confirmations,
setconfirmations = txd.setconfirmations,
ccsymbol = txd.ccsymbol,
set_ccsymbol = ccsymbol ? ccsymbol.toUpperCase() : "",
ln = txhash && txhash.slice(0, 9) === "lightning",
lnstr = ln ? " <span class='icon-power'></span>" : "",
valstr = (ln && !conf) ? "" : ccval_rounded + " " + set_ccsymbol + lnstr,
date_format = transactiontime ? short_date(transactiontime) : "",
no_conf = setconfirmations === false,
instant_lock = txd.instant_lock,
confirmed = instant_lock || no_conf || (conf && conf >= setconfirmations),
conf_count = no_conf ? "" : conf + " / " + setconfirmations + " " + translate("confirmations"),
instant_lock_text = instant_lock ? " (instant_lock)" : "",
conf_title = instant_lock ? "instant_lock" : conf_count,
unconf_text = translate("unconfirmedtx"),
checked_span = "<span class='icon-checkmark' title='" + conf_title + "'></span>",
confspan = confirmed ? checked_span : conf ? "<div class='txli_conf' title='" + conf_title + "'><div class='confbar'></div><span>" + conf_title + "</span></div>" :
"<div class='txli_conf' title='" + unconf_text + "'><div class='confbar'></div><span>" + unconf_text + "</span></div>",
tx_listitem = $("<li><div class='txli_content'>" + date_format + confspan + "<div class='txli_conf txl_canceled'><span class='icon-blocked'></span>Canceled</div><span class='tx_val'> + " + valstr + " <span class='icon-eye show_tx' title='view on blockexplorer'></span></span></div></li>");
if (glob_let.tx_list.includes(txhash)) { // check for indexed transaction id's
return rqtype === "outgoing" ? null : tx_listitem;
}
glob_let.tx_list.push(txhash);
return tx_listitem;
}
// Creates HTML for historic data
function hs_for(dat) {
return "<div class='historic_meta'>" + dat.split("\n").join("<br/>") + "</div>";
}
// Generates a title string with transaction data
function data_title(dat) {
const historic = dat.historic;
let historic_dat = "";
if (historic && historic.price) {
const timestamp = historic.timestamp,
price = historic.price,
fiatsrc = historic.fiatapisrc,
src = historic.apisrc,
lcsymbol = historic.lcsymbol,
lc_eur_rate = historic.lcrate,
usd_eur_rate = historic.usdrate,
fetched = historic.fetched,
lc_usd_rate = 1 / (lc_eur_rate / usd_eur_rate),
lc_ccrate = price / lc_usd_rate,
lc_val = dat.ccval * lc_ccrate,
cc_upper = dat.ccsymbol ? dat.ccsymbol.toUpperCase() : dat.ccsymbol,
lc_upper = lcsymbol ? lcsymbol.toUpperCase() : lcsymbol,
localrate = lc_upper === "USD" ? "" : cc_upper + "-" + lc_upper + ": " + lc_ccrate.toFixed(6) + "\n" + lc_upper + "-USD: " + lc_usd_rate.toFixed(2);
historic_dat = "Historic data (" + fulldateformat(new Date(timestamp - glob_const.timezone), langcode) + "):\n" +
"Fiatvalue: " + lc_val.toFixed(2) + " " + lc_upper + "\n" +
cc_upper + "-USD: " + price.toFixed(6) + "\n" +
localrate + "\n" +
"Source: " + fiatsrc + "/" + src + "\n";
}
const conf_ratio = dat.confirmations ? dat.confirmations + "/" + dat.setconfirmations : translate("unconfirmedtx"),
conf_var = dat.instant_lock ? "(instant_lock)" : conf_ratio,
cf_info = dat.setconfirmations === false ? "" : "Confirmations: " + conf_var,
l2src = dat.l2 ? "\nLayer: " + dat.l2 : "",
title_string = historic_dat + cf_info + l2src;
return title_string.length ? title_string : false;
}
// Compares received amounts with requested amounts and updates request status
function compareamounts(rd, rdo) {
const thisrequestid = rd.requestid,
requestli = rdo.thislist,
txlist = requestli.find(".transactionlist li"),
txlist_length = txlist.length;
if (txlist_length) {
const lastlist = txlist.last(),
firstinput = lastlist.data("transactiontime");
if (firstinput) {
const iscrypto = rd.iscrypto,
pendingstatus = rd.pending,
getconfirmations = rd.set_confirmations,
getconfint = getconfirmations === false ? 0 : (getconfirmations ? parseInt(getconfirmations) : 1),
setconfirmations = rd.lightning ? 1 : getconfint, // set minimum confirmations to 1
firstlist = txlist.first(),
conf = firstlist.data("confirmations"),
latestinput = firstlist.data("transactiontime"),
offset = Math.abs(now() - (firstinput - glob_const.timezone)),
one_tx = txlist_length === 1,
recent = offset < 300000 && one_tx,
cc_amount = parseFloat(rd.cc_amount),
margin = 0.95;
let recent_dat = false,
tx_counter = 0,
status_cc = "pending",
pending_cc = pendingstatus,
confirmed_cc = false,
confirmations_cc = 0,
paymenttimestamp_cc,
txhash_cc,
thissum_cc = 0,
fiatvalue = rd.fiatvalue;
if (iscrypto || recent) {
const txreverse = txlist_length > 1 ? txlist.get().reverse() : txlist;
$(txreverse).each(function(i) {
tx_counter++;
const thisnode = $(this),
tn_dat = thisnode.data(),
conf_correct = tn_dat.instant_lock ? 0 : setconfirmations; // correction if dash instant_lock
confirmations_cc = tn_dat.confirmations,
paymenttimestamp_cc = tn_dat.transactiontime,
txhash_cc = tn_dat.txhash,
thissum_cc += parseFloat(tn_dat.ccval) || 0; // sum of outputs
if (confirmations_cc >= conf_correct || rd.no_conf === true || tn_dat.setconfirmations === false) { // check all confirmations + whitelist for currencies unable to fetch confirmations
confirmed_cc = true;
if (thissum_cc >= cc_amount * margin) { // compensation for small fluctuations in rounding amount
thisnode.addClass("exceed").nextAll().addClass("exceed");
return
}
} else {
confirmed_cc = false;
}
const confbar = thisnode.find(".confbar");
if (confbar.length > 0) {
confbar.each(function(i) {
animate_confbar($(this), i);
});
};
});
if (thissum_cc >= cc_amount * margin) { // compensation for small fluctuations in rounding amount
if (confirmed_cc === false) { // check confirmations outside the loop
status_cc = "pending",
pending_cc = one_tx && txhash_cc ? "polling" : pendingstatus; // switch to tx polling if there's only one transaction and txhash is known
} else {
status_cc = "paid",
pending_cc = "no";
}
} else {
status_cc = "insufficient",
pending_cc = "scanning";
}
}
if (recent && !iscrypto) { // get local fiat rates when request is less then 15 minutes old
const ccsymbol = rd.currencysymbol,
exchangerates = br_get_session("exchangerates", true),
cc_xrates = br_get_session("xrates_" + ccsymbol, true);
if (exchangerates && cc_xrates) {
const fiat_exchangerates = exchangerates.fiat_exchangerates,
local_xrate = fiat_exchangerates ? fiat_exchangerates[rd.fiatcurrency] : null,
usd_eur_xrate = fiat_exchangerates ? fiat_exchangerates.usd : null;
if (local_xrate && usd_eur_xrate) {
const usd_rate = cc_xrates ? cc_xrates.ccrate : null;
if (usd_rate) {
const usdval = thissum_cc * usd_rate,
eurval = usdval / usd_eur_xrate;
fiatvalue = eurval * local_xrate,
recent_dat = true;
}
}
} else {
init_historical_fiat_data(rd, rdo, conf, latestinput, firstinput);
return
}
}
if (iscrypto || recent_dat) {
updaterequest({
"requestid": rd.requestid,
"status": status_cc,
"receivedamount": thissum_cc,
"fiatvalue": fiatvalue,
"paymenttimestamp": paymenttimestamp_cc,
"txhash": txhash_cc,
"confirmations": confirmations_cc,
"pending": pending_cc,
"lightning": rd.lightning
}, false);
api_callback(rdo);
return
}
init_historical_fiat_data(rd, rdo, conf, latestinput, firstinput);
return
}
}
api_callback(rdo);
}
// get historic crypto rates
// Initializes the process of fetching historical fiat data for a request
function init_historical_fiat_data(rd, rdo, conf, latestinput, firstinput) {
const confcor = conf || 0,
latestconf = rd.no_conf === true ? 0 : confcor, // only update on change
hc_prefix = "historic_" + rd.requestid,
historiccache = br_get_session(hc_prefix),
cacheval = latestinput + latestconf;
if ((cacheval - historiccache) > 0) { //new input detected; call historic api
br_remove_session(hc_prefix); // remove historic price cache
const historic_payload = $.extend(rd, {
"latestinput": latestinput,
"latestconf": latestconf,
"firstinput": firstinput
}),
apilist = "historic_fiat_price_apis",
fiatapi = $("#fiatapisettings").data("selected"),
fiatapi_default = (fiatapi === "coingecko" || fiatapi === "coinbase") ? "fixer" : fiatapi; // exclude coingecko api"
glob_let.api_attempt[apilist] = {}; // reset global historic fiat price api attempt
get_historical_fiat_data(historic_payload, rdo, apilist, fiatapi_default);
return
}
api_callback(rdo);
}
// Fetches historical fiat data from a specified API
function get_historical_fiat_data(rd, rdo, apilist, fiatapi) {
glob_let.api_attempt[apilist][fiatapi] = true;
const fiatcurrency = rd.fiatcurrency;
if (fiatcurrency) {
const lcsymbol = fiatcurrency.toUpperCase(),
payload = get_historic_fiatprice_api_payload(fiatapi, lcsymbol, rd.latestinput);
api_proxy({
"api": fiatapi,
"search": payload,
"cachetime": 86400,
"cachefolder": "1d",
"params": {
"method": "GET"
}
}).done(function(e) {
const data = br_result(e).result;
if (data) {
if (data.error) {
const next_historic = try_next_api(apilist, fiatapi);
if (next_historic) {
get_historical_fiat_data(rd, rdo, apilist, next_historic);
return
}
fail_dialogs(fiatapi, data.error);
api_callback(rdo);
return
}
let usdeur = false,
usdloc = false,
usdrate = false,
get_lcrate = false;
if (fiatapi === "currencylayer") {
usdeur = q_obj(data, "quotes.USDEUR"),
usdloc = q_obj(data, "quotes.USD" + lcsymbol);
if (usdeur && usdloc) {
usdrate = 1 / usdeur,
get_lcrate = usdloc * usdrate;
}
} else {
usdrate = q_obj(data, "rates.USD"),
get_lcrate = q_obj(data, "rates." + lcsymbol);
}
if (usdrate && get_lcrate) {
const lcrate = lcsymbol === "EUR" ? 1 : get_lcrate,
historic_api = $("#cmcapisettings").data("selected"),
picked_historic_api = historic_api === "coinmarketcap" ? "coingecko" : historic_api, // default to "coingecko api"
init_apilist = "historic_crypto_price_apis";
glob_let.api_attempt[init_apilist] = {};
get_historical_crypto_data(rd, rdo, fiatapi, init_apilist, picked_historic_api, lcrate, usdrate, lcsymbol);
return
}
const next_historic = try_next_api(apilist, fiatapi);
if (next_historic) {
get_historical_fiat_data(rd, rdo, apilist, next_historic);
return
}
}
fail_dialogs(fiatapi, "unable to fetch " + lcsymbol + " exchange rate");
api_callback(rdo);
}).fail(function(xhr, stat, err) {
function next_proxy() { // try next proxy
if (get_next_proxy()) {
get_historical_fiat_data(rd, rdo, apilist, fiatapi);
return true
}
return false
}
if (is_proxy_fail(this.url) && next_proxy()) { // Try next proxy if proxy fails
return
}
const next_historic = try_next_api(apilist, fiatapi);
if (next_historic) {
get_historical_fiat_data(rd, rdo, apilist, next_historic);
return
}
if (next_proxy()) { // Try next proxy after trying all api's
return
}
const error_object = xhr || stat || err;
fail_dialogs(fiatapi, error_object);
api_callback(rdo);
});
return
}
api_callback(rdo);
}
// Generates the payload for historical fiat price API requests
function get_historic_fiatprice_api_payload(fiatapi, lcsymbol, latestinput) {
const dateformat = form_date(latestinput),
payload = (fiatapi === "fixer") ? dateformat + "?symbols=" + lcsymbol + ",USD" :
(fiatapi === "currencylayer") ? "historical?date=" + dateformat :
dateformat + "?base=EUR"; // <- exchangeratesapi
return payload;
}
// Formats a date for API requests
function form_date(latestinput) {
const dateobject = new Date(parseFloat(latestinput)),
getmonth = dateobject.getUTCMonth() + 1,
getday = dateobject.getUTCDate(),
year = dateobject.getUTCFullYear(),
month = getmonth < 10 ? "0" + getmonth : getmonth,
day = getday < 10 ? "0" + getday : getday;
return year + "-" + month + "-" + day;