-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathassets_js_bitrequest_monitors.js
1368 lines (1309 loc) · 57.4 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() {
// ** Core Request Management: **
update_request_trigger();
refresh_request_states();
//scan_pending_requests
//process_request_updates
//process_transaction_data
//finalize_request_state
// ** Network and API Handling: **
//route_network_request
//route_api_request
//route_crypto_api
//route_blockchain_rpc
//process_scan_results
// ** Error Handling: **
//handle_scan_failure
//mark_network_failure
//handle_api_failure
//show_api_error
//extract_error_details
//fail_dialogs
//no_results
// ** API Management: **
//get_next_layer2_api
//get_next_rpc
//switch_rpc_endpoint
//update_api_source
//display_api_source
// ** Transaction Processing: **
//create_transaction_item
//wrap_historic_data
//format_transaction_details
//validate_payment_amounts
//update_transaction_count
// ** Historical Data Processing: **
//init_fiat_history
//fetch_fiat_rates
//get_historic_fiatprice_api_payload
//form_date
//fetch_crypto_rates
//get_payload_historic_coingecko
//get_payload_historic_coinpaprika
//get_payload_historic_coincodex
//cx_date
//match_price_timestamps
//get_historic_object_coingecko
//get_historic_object_coinpaprika
//get_historic_object_coincodex
// ** Utility Functions: **
//is_scanning
//clearscan
//check_api
//tx_data
});
// ** Fetch incoming transactions **
// Attaches event listener to trigger request state updates when .requestsbttn.self is clicked
function update_request_trigger() {
$(document).on("click", ".requestsbttn .self", function() {
if (is_scanning()) return
scan_pending_requests(true);
})
}
// Triggers request state updates with delay if URL parameter indicates requests page
function refresh_request_states() {
const url_params = get_urlparameters();
if (url_params.xss) {
return
}
if (url_params.p === "requests") { // only trigger on "requests page"
setTimeout(function() {
scan_pending_requests("delay");
}, 300);
}
}
// Initializes request state update process by resetting global state and marking active requests
function scan_pending_requests(trigger, active_req_list) {
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 requests = active_req_list || $("#requestlist .rqli").filter(function() {
return $(this).data("pending") !== "unknown";
});
requests.addClass("open");
process_request_updates(trigger, requests);
}
// Manages request state cache, updates, and UI synchronization based on transaction status
function process_request_updates(trigger, active_requests) {
glob_let.apikey_fails = false; // reset apikey fails
const request_data = $("#requestlist li.rqli.open").first().data();
if (request_data) {
if (trigger === "loop") {
process_transaction_data(request_data);
return
}
const is_delayed = trigger === "delay",
status_cache = br_get_session("txstatus", true);
if (status_cache) {
const cache_age = now() - status_cache.timestamp,
request_states = status_cache.requeststates;
if (cache_age > 30000 || empty_obj(request_states)) { //check if cached crypto rates are expired (check every 30 seconds on page refresh or when opening request page)
active_requests.addClass("scan");
process_transaction_data(request_data, is_delayed);
return
}
if (trigger === true) {
return
}
// Only update on page refresh
// parse cached transaction data
$.each(request_states, function(index, state) {
const current_list = $("#" + state.requestid),
current_data = current_list.data();
if (current_data) {
const pending_status = current_data.pending;
if (pending_status === "scanning" || pending_status === "polling") {
const status_panel = current_list.find(".pmetastatus"),
transaction_list = current_list.find(".transactionlist");
status_panel.text(state.status);
transaction_list.empty();
add_historical_data(transaction_list, state.transactions);
current_list.addClass("pmstatloaded");
}
}
});
return
}
active_requests.addClass("scan");
process_transaction_data(request_data, is_delayed);
return
}
if (!empty_obj(glob_let.statuspush)) {
const tx_states = br_get_session("txstatus", true);
if (tx_states) {
const txs = tx_states.requeststates,
txid = sha_sub(JSON.stringify(txs), 15), // hash arrays to compare for changes
new_tx_id = sha_sub(JSON.stringify(glob_let.statuspush), 15);
if (txid !== new_tx_id) { // check for updates
// Only save on status updates
save_requests();
}
} else {
save_requests();
}
const status_object = {
"timestamp": now(),
"requeststates": glob_let.statuspush
};
br_set_session("txstatus", status_object, true);
clearscan();
}
}
// Processes transaction data and initiates API calls with rate limiting protection
function process_transaction_data(rd, dl) {
const rdo = tx_data(rd);
if (rdo.pending === "scanning" || rdo.pending === "polling") {
if (dl) {
const delay = 10000,
monitor_timer_cache = br_get_session("monitor_timer"),
monitor_timer = monitor_timer_cache ? parseInt(monitor_timer_cache, 10) : delay,
time_since_last = now() - monitor_timer;
if (time_since_last < delay) { // prevent over scanning
play_audio(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");
update_transaction_count(rdo.statuspanel, "reset");
route_network_request(rd, api_data, rdo);
return
}
const transaction_list = rdo.transactionlist;
if (transaction_list) {
transaction_list.find("li").each(function(index) {
glob_let.tx_list.push($(this).data("txhash")); //index transaction id's
});
finalize_request_state(rdo);
}
}
// Processes final state updates and transaction data after API request completion
function finalize_request_state(rdo) {
const src = rdo.source;
if (!src === "requests") {
return
}
// src === "requests"
const current_list = rdo.thislist;
if (current_list && current_list.hasClass("scan")) {
current_list.removeClass("scan open").addClass("pmstatloaded");
const request_id = rdo.requestid,
transaction_list = rdo.transactionlist,
transaction_items = transaction_list.children("li");
if (transaction_items.length) {
const transaction_push = [];
transaction_items.each(function() {
const current_node = $(this),
current_data = current_node.data();
if (current_data) {
transaction_push.push(current_data);
if (current_node.attr("title")) {} else {
const history_string = format_transaction_details(current_data);
if (history_string) {
current_node.append(wrap_historic_data(history_string)).attr("title", history_string);
}
}
}
});
const status_panel = current_list.find(".pmetastatus"),
status_box = {
"requestid": request_id,
"status": status_panel.attr("data-count"),
"transactions": transaction_push
};
glob_let.statuspush.push(status_box);
} else {
const status_box = {
"requestid": request_id,
"status": 0
};
glob_let.statuspush.push(status_box);
}
process_request_updates("loop");
}
}
// ** Network and API Handling: **
// Routes requests to appropriate handlers based on payment type and network availability
function route_network_request(rd, api_data, rdo) {
if (api_data) {
const src = rdo.source;
if (src === "requests") {
const current_list = rdo.thislist;
if (current_list) {
current_list.removeClass("no_network");
const transaction_list = rdo.transactionlist;
if (transaction_list) {
transaction_list.empty();
}
}
}
if (rd.eth_layer2) {
route_layer2_api_request(rd, rdo);
return
}
if (rd.lightning && rdo.source === "requests") {
process_lightning_payment(rd, api_data, rdo);
return
}
route_api_request(rd, api_data, rdo);
return
}
console.error("error", "no api data available");
}
// Tracks API attempts and routes to specific API or RPC handlers based on availability
function route_api_request(rd, api_data, rdo) {
const request_id = rd.requestid || "";
glob_let.rpc_attempts[sha_sub(request_id + api_data.url, 15)] = true;
if (api_data.api) {
route_crypto_api(rd, api_data, rdo);
return
}
route_blockchain_rpc(rd, api_data, rdo);
}
// Routes cryptocurrency requests to specific API endpoints based on provider capabilities
function route_crypto_api(rd, api_data, rdo) {
const provider = api_data.name;
if (provider === "mymonero api" || provider === "blockchair_xmr") {
initialize_monero_scan(rd, api_data, rdo);
return
}
if (provider === "mempool.space") {
mempoolspace_rpc_init(rd, api_data, rdo, false);
return
}
if (provider === "blockchain.info") {
initialize_bitcoin_scan(rd, api_data, rdo);
return
}
if (provider === "blockcypher") {
process_blockcypher_transactions(rd, api_data, rdo);
return
}
if (provider === "etherscan") {
scan_layer2_transactions(rd, api_data, rdo, null, 1);
return
}
if (provider === "ethplorer") {
process_ethereum_transactions(rd, api_data, rdo);
return
}
if (provider === "blockchair") {
blockchair_fetch(rd, api_data, rdo);
return
}
if (provider === "nimiq.watch" || provider === "mopsus.com") {
process_nimiq_transactions(rd, api_data, rdo);
return
}
if (rd.payment === "kaspa") {
initialize_kaspa_scan(rd, api_data, rdo);
return
}
if (provider === "dash.org") {
process_dash_transactions(rd, api_data, rdo);
return
}
finalize_request_state(rdo);
}
// Routes requests to appropriate RPC endpoints for Bitcoin, Ethereum, and Nano networks
function route_blockchain_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
}
finalize_request_state(rdo);
}
// Processes transaction scan results and handles Ethereum L2, confirmations, and UI updates based on match type
function process_scan_results(rd, api_data, rdo, counter, tx_details, l2) {
glob_let.apikey_fails = false;
const src = rdo.source;
if (src === "requests") {
if (counter) {
update_transaction_count(rdo.statuspanel, counter);
}
const current_list = rdo.thislist;
if (current_list) {
current_list.removeClass("no_network");
}
}
if (tx_details) {
const tx_hash = rd.txhash || tx_details.txhash;
if (!tx_hash) return;
const eth_layer2 = tx_details.eth_layer2;
if (src === "requests") {
if (eth_layer2) {
const has_l2 = 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
}
}
validate_payment_amounts(rd, rdo);
return
}
if (eth_layer2) { // Eth layer 2
initialize_network_status(api_data, "paid");
}
tx_details.txhash = tx_hash;
if (src !== "after_scan") {
const status = validate_confirmations(tx_details);
if (status === "paid") {
force_close_socket();
return
}
}
if (rdo.pending === "scanning") { // scanning
// After scan
if (src === "after_scan") {
glob_const.html.addClass("blurmain_payment");
glob_const.paymentpopup.addClass("active");
closeloader();
}
force_close_socket();
start_transaction_monitor(tx_details, api_data);
return
}
if (src === "tx_polling" || src === "l2_polling") {
if (block_overflow("polling")) return; // prevent overflow
route_transaction_monitor(tx_details, api_data);
}
return
}
if (src === "requests") {
if (rd.erc20 || rd.payment === "ethereum") {
if (!l2) {
// Init eth layer 2's
route_layer2_api_request(rd, rdo);
return
}
}
finalize_request_state(rdo);
return
}
if (src === "after_scan") {
cancel_after_scan();
return
}
}
// ** Error Handling: **
// Processes API scan failures and routes to appropriate error handlers based on network type
function handle_scan_failure(error_obj, rd, api_data, rdo, l2) {
const src = rdo.source;
if (src === "requests") {
const current_list = rdo.thislist;
if (current_list) {
mark_network_failure(current_list, rdo.statuspanel);
}
}
if (api_data === "ln") {
extract_error_details(error_obj);
return
}
if (l2 && src === "l2_scanning") {
handle_socket_fails(api_data, rd.address, rdo.ping_id, l2);
return
}
handle_api_failure(rd, rdo, error_obj, api_data, l2);
return
}
// Updates UI elements to indicate network connectivity failure
function mark_network_failure(current_list, status_panel) {
current_list.addClass("no_network");
status_panel.attr("data-count", 0).text("0");
}
// Manages API failure scenarios by attempting fallback options and proxy switching
function handle_api_failure(rd, rdo, error_obj, api_data, l2) {
const src = rdo.source,
error_details = extract_error_details(error_obj),
timeout = rdo.timeout,
cache_time = rdo.cachetime;
function next_proxy(type) { // try next proxy
if (type === "api_fail" && (error_details.apikey || glob_let.apikey_fails)) return false; // only try next proxy if api key is expired or missing
if (get_next_proxy()) {
if (src === "addr_polling") {
start_address_monitor(timeout, api_data, true);
return true;
}
if (src === "tx_polling" || src === "l2_polling") {
if (request) {
start_transaction_monitor({
"txhash": request.txhash,
"setconfirmations": request.set_confirmations,
"eth_layer2": request.eth_layer2
}, api_data, true);
return true;
}
}
if (l2) {
route_layer2_api_request(rd, rdo, api_data, l2);
return true;
}
route_api_request(rd, api_data, rdo);
return true;
}
return false;
}
if (error_obj && error_obj.is_proxy) { // Try next proxy if proxy fails
if (next_proxy("proxy_fail")) {
return
}
no_results(rdo, src, api_data, error_details);
return
}
if (!api_data) {
show_api_error(false, error_details);
if (src === "requests") {
finalize_request_state(rdo);
return
}
stop_monitors();
socket_info(api_data, false);
notify(tl("websocketoffline"), 500000, "yes");
return
}
const request_id = rd.requestid,
payment = rd.payment;
if (l2) {
const next_l2_api = get_next_layer2_api(payment, api_data, request_id, l2);
if (next_l2_api) {
// Scan eth layer 2
if (src === "requests") {
route_layer2_api_request(rd, rdo, next_l2_api, l2);
return
}
if (src === "l2_polling") {
monitor_layer2_chain(l2, next_l2_api, true);
return
}
}
} else {
const next_rpc = get_next_rpc(payment, api_data, request_id);
if (next_rpc) {
switch_rpc_endpoint(rd, rdo, next_rpc, timeout);
return
}
if (rd.erc20 || payment === "ethereum") {
// Init eth layer 2
route_layer2_api_request(rd, rdo);
return
}
}
if (next_proxy("api_fail")) { // Try next proxy after trying all api's
return
}
no_results(rdo, src, api_data, error_details);
}
// Renders API error messages with optional API key management UI
function show_api_error(api_source, error_obj) {
if (!error_obj) return;
const error_data = error_obj || {
"errorcode": null,
"errormessage": "errormessage"
};
if ($("#dialogbody .doselect").length) {
return
}
if (api_source) {
const key_fail = error_data.apikey === true,
error_message = error_data.errormessage,
error_code = error_data.errorcode !== undefined ? "Error: " + error_data.errorcode : "",
api_button = key_fail ? "<div id='add_api' data-api='" + api_source + "' class='button'>" + tl("addapikey", {
"apisrc": api_source
}) + "</div>" : "",
try_other = api_source ? "<span id='proxy_dialog' class='ref'>" + tl("tryotherproxy") + "</span>" : "",
content = "<h2 class='icon-blocked'>" + error_code + "</h2><p class='doselect'><strong>" + tl("error") + ": " + error_message + "<br/><br/>" + try_other + "</p>" + api_button;
popdialog(content, "canceldialog");
}
}
// Extracts and normalizes error information from various API response formats
function extract_error_details(error_obj) {
const error = q_obj(error_obj, "error");
if (!error) return {
"errorcode": 0,
"errormessage": "unknown"
};
const result = error.result,
error_code = error.code ?? error.status ?? error.error_code ?? error ?? 0,
error_message = error.message ?? error.error_message ?? error.responseText ?? error.statusText ?? error.type ?? error.error ?? error,
api_key_check = (typeof error === "string") ? (error.indexOf("API calls limits have been reached") > -1 || error.indexOf("Limits reached") > -1) : false, // blockcypher
api_key_check2 = (result) ? result.indexOf("API Key") > -1 : null, // etherscan
api_key = (
error_code === 101 || // fixer
error_code === 402 || // blockchair
error_code === 403 || error_code === 1 || // ethplorer => invalid or missing API key
error_code === 1001 || // coinmarketcap => invalid API key
error_code === 1002 || // coinmarketcap => missing API key
api_key_check || api_key_check2
),
error_details = {
"errorcode": error_code,
"errormessage": error_message,
api_key
},
is_proxy = (error_obj.is_proxy) ? "proxy " : "";
console.error("API " + is_proxy + "error:", error_details);
if (api_key) {
glob_let.apikey_fails = true;
}
return error_details;
}
// Triggers error dialogs for API-related failures
function fail_dialogs(api_source, error_obj) {
const error_details = extract_error_details(error_obj);
show_api_error(api_source, error_details);
}
// Displays error notifications when all API and proxy attempts fail
function no_results(rdo, src, api_data, error_details) {
const rpc_id = api_data.name || api_data.url || "unknown";
show_api_error(rpc_id, error_details);
if (src === "requests") {
finalize_request_state(rdo);
return
}
stop_monitors();
socket_info(api_data, false);
notify(tl("websocketoffline"), 500000, "yes");
}
// ** API Management: **
// Retrieves next available Layer 2 API endpoint while preventing overflow
function get_next_layer2_api(this_payment, api_data, request_id, l2) {
if (block_overflow("l2")) return false; // prevent overflow
const api_settings = q_obj(get_coinsettings(this_payment), "layer2.options." + l2 + ".apis");
if (api_settings) {
const api_list = api_settings.apis,
list_length = api_list.length;
if (api_list && list_length) {
const current_index = api_list.findIndex(option => option.name === api_data.name),
next_api = api_list[(current_index + 1) % list_length],
rq_id = request_id || "";
if (glob_let.rpc_attempts[sha_sub(rq_id + next_api.url + l2, 15)] !== true) {
return next_api;
}
}
}
return false;
}
// Retrieves next available RPC endpoint while preventing overflow
function get_next_rpc(this_payment, api_data, request_id, l2) {
if (block_overflow("rpc")) return false; // prevent overflow
const api_settings = cs_node(this_payment, "apis", true);
if (api_settings) {
const api_list = api_settings.apis,
rpc_list = api_settings.options,
combined_list = (rpc_list && rpc_list.length) ? $.merge(api_list, rpc_list) : api_list,
list_length = combined_list.length;
if (combined_list && list_length) {
const next_index = combined_list.findIndex(option => option.url === api_data.url),
next_rpc = combined_list[(next_index + 1) % list_length],
rq_id = request_id || "",
l2_prefix = l2 || "";
if (glob_let.rpc_attempts[sha_sub(rq_id + next_rpc.url + l2_prefix, 15)] !== true) {
return next_rpc;
}
}
}
return false;
}
// Routes requests to next available RPC endpoint based on request source type
function switch_rpc_endpoint(rd, rdo, next_rpc, timeout) {
const src = rdo.source;
if (src === "addr_polling") {
start_address_monitor(timeout, next_rpc, true);
return
}
if (src === "tx_polling") {
monitor_main_chain(rdo.txdat, next_rpc, true);
return
}
if (src === "after_scan") {
after_scan(rd, next_rpc, rdo);
return
}
route_api_request(rd, next_rpc, rdo);
}
// Updates request data with current API source information
function update_api_source(rdo, api_data) {
if (rdo.source === "requests") {
display_api_source(rdo.thislist, api_data);
}
}
// Updates UI with API source details and connection status indicators
function display_api_source(current_list, api_data) {
const api_url = api_data.url,
api_url_short = api_url ? (api_url.length > 40 ? api_url.slice(0, 40) + "..." : api_url) : "",
provider_name = api_data.name,
api_title = provider_name === "mempool.space" ? api_url : provider_name,
api_source = api_title || api_url_short;
current_list.data("source", api_source).find(".api_source").html("<span class='src_txt' title='" + api_url_short + "'>" + tl("source") + ": " + api_source + "</span><span class='icon-wifi-off'></span><span class='icon-connection'></span>");
}
// ** Transaction Processing: **
// Creates and returns a formatted transaction list item with confirmation status
function create_transaction_item(tx_details, request_type) {
const tx_hash = tx_details.txhash;
if (!tx_hash) return null;
const crypto_value = tx_details.ccval,
crypto_value_rounded = trimdecimals(crypto_value, 6),
transaction_time = tx_details.transactiontime,
confirmations = tx_details.confirmations,
set_confirmations = tx_details.setconfirmations,
crypto_symbol = tx_details.ccsymbol,
formatted_crypto_symbol = crypto_symbol ? crypto_symbol.toUpperCase() : "",
is_lightning = tx_hash && tx_hash.slice(0, 9) === "lightning",
lightning_icon = is_lightning ? " <span class='icon-power'></span>" : "",
value_string = (is_lightning && !confirmations) ? "" : crypto_value_rounded + " " + formatted_crypto_symbol + lightning_icon,
formatted_date = transaction_time ? short_date(transaction_time) : "",
no_confirmation_requirement = set_confirmations === false,
instant_lock = tx_details.instant_lock,
is_confirmed = instant_lock || no_confirmation_requirement || (confirmations && confirmations >= set_confirmations),
confirmation_count = no_confirmation_requirement ? "" : confirmations + " / " + set_confirmations + " " + tl("confirmations"),
instant_lock_text = instant_lock ? " (instant_lock)" : "",
confirmation_title = instant_lock ? "instant_lock" : confirmation_count,
unconfirmed_text = tl("unconfirmedtx"),
checked_span = "<span class='icon-checkmark' title='" + confirmation_title + "'></span>",
confirmation_span = is_confirmed ? checked_span : confirmations ? "<div class='txli_conf' title='" + confirmation_title + "'><div class='confbar'></div><span>" + confirmation_title + "</span></div>" :
"<div class='txli_conf' title='" + unconfirmed_text + "'><div class='confbar'></div><span>" + unconfirmed_text + "</span></div>",
tx_list_item = $("<li><div class='txli_content'>" + formatted_date + confirmation_span + "<div class='txli_conf txl_canceled'><span class='icon-blocked'></span>Canceled</div><span class='tx_val'> + " + value_string + " <span class='icon-eye show_tx' title='view on blockexplorer'></span></span></div></li>");
if (glob_let.tx_list.includes(tx_hash)) { // check for indexed transaction id's
return request_type === "outgoing" ? null : tx_list_item;
}
glob_let.tx_list.push(tx_hash);
return tx_list_item;
}
// Generates HTML wrapper for historic transaction metadata
function wrap_historic_data(data) {
return "<div class='historic_meta'>" + data.split("\n").join("<br/>") + "</div>";
}
// Generates detailed transaction information string for tooltip display
function format_transaction_details(data) {
const historic = data.historic;
let historic_details = "";
if (historic && historic.price) {
const timestamp = historic.timestamp,
price = historic.price,
fiat_source = historic.fiatapisrc,
crypto_source = historic.apisrc,
local_symbol = historic.lcsymbol,
local_eur_rate = historic.lcrate,
usd_eur_rate = historic.usdrate,
local_usd_rate = 1 / (local_eur_rate / usd_eur_rate),
local_crypto_rate = price / local_usd_rate,
local_value = data.ccval * local_crypto_rate,
crypto_symbol_upper = data.ccsymbol ? data.ccsymbol.toUpperCase() : data.ccsymbol,
local_symbol_upper = local_symbol ? local_symbol.toUpperCase() : local_symbol,
local_rate_info = local_symbol_upper === "USD" ? "" : crypto_symbol_upper + "-" + local_symbol_upper + ": " + local_crypto_rate.toFixed(6) + "\n" + local_symbol_upper + "-USD: " + local_usd_rate.toFixed(2);
// set historic data
historic_details = "Historic data (" + fulldateformat(new Date(timestamp - glob_const.timezone), langcode) + "):\n" +
"Fiatvalue: " + local_value.toFixed(2) + " " + local_symbol_upper + "\n" +
crypto_symbol_upper + "-USD: " + price.toFixed(6) + "\n" +
local_rate_info + "\n" +
"Source: " + fiat_source + "/" + crypto_source + "\n";
}
const confirmation_details = data.confirmations ? data.confirmations + "/" + data.setconfirmations : tl("unconfirmedtx"),
confirmation_var = data.instant_lock ? "(instant_lock)" : confirmation_details,
confirmation_info = data.setconfirmations === false ? "" : "Confirmations: " + confirmation_var,
layer2_source = data.l2 ? "\nLayer: " + data.l2 : "",
title_string = historic_details + confirmation_info + layer2_source;
return title_string.length ? title_string : false;
}
// Validates received cryptocurrency or fiat amounts against requested amounts and updates transaction status
function validate_payment_amounts(rd, rdo) {
const request_id = rd.requestid,
current_list = rdo.thislist,
transaction_items = current_list.find(".transactionlist li"),
transaction_count = transaction_items.length;
if (transaction_count) {
const last_transaction = transaction_items.last(),
first_transaction_time = last_transaction.data("transactiontime");
if (first_transaction_time) {
const is_crypto = rd.iscrypto,
pending_status = rd.pending,
required_confirmations = rd.set_confirmations,
confirmation_interval = required_confirmations === false ? 0 : (required_confirmations ? parseInt(required_confirmations) : 1),
minimum_confirmations = rd.lightning ? 1 : confirmation_interval, // set minimum confirmations to 1
first_transaction = transaction_items.first(),
first_confirmations = first_transaction.data("confirmations"),
latest_transaction_time = first_transaction.data("transactiontime"),
time_offset = Math.abs(now() - (first_transaction_time - glob_const.timezone)),
single_transaction = transaction_count === 1,
is_recent = time_offset < 300000 && single_transaction,
crypto_amount = parseFloat(rd.cc_amount),
amount_margin = 0.95;
let recent_data = false,
transaction_counter = 0,
crypto_status = "pending",
crypto_pending = pending_status,
is_crypto_confirmed = false,
crypto_confirmations = 0,
crypto_payment_timestamp,
crypto_transaction_hash,
total_crypto_amount = 0,
fiat_value = rd.fiatvalue;
if (is_crypto || is_recent) {
const transaction_reverse = transaction_count > 1 ? transaction_items.get().reverse() : transaction_items;
$(transaction_reverse).each(function(index) {
transaction_counter++;
const current_transaction = $(this),
transaction_data = current_transaction.data(),
correction_confirmations = transaction_data.instant_lock ? 0 : minimum_confirmations; // correction if dash instant_lock
crypto_confirmations = transaction_data.confirmations,
crypto_payment_timestamp = transaction_data.transactiontime,
crypto_transaction_hash = transaction_data.txhash,
total_crypto_amount += parseFloat(transaction_data.ccval) || 0; // sum of outputs
if (crypto_confirmations >= correction_confirmations || rd.no_conf || transaction_data.setconfirmations === false) { // check all confirmations + whitelist for currencies unable to fetch confirmations
is_crypto_confirmed = true;
if (total_crypto_amount >= crypto_amount * amount_margin) { // compensation for small fluctuations in rounding amount
current_transaction.addClass("exceed").nextAll().addClass("exceed");
return
}
} else {
is_crypto_confirmed = false;
}
const confirmation_bar = current_transaction.find(".confbar");
if (confirmation_bar.length > 0) {
confirmation_bar.each(function(bar_index) {
animate_confbar($(this), bar_index);
});
};
});
if (total_crypto_amount >= crypto_amount * amount_margin) { // compensation for small fluctuations in rounding amount
if (is_crypto_confirmed === false) { // check confirmations outside the loop
crypto_status = "pending",
crypto_pending = single_transaction && crypto_transaction_hash ? "polling" : pending_status; // switch to tx polling if there's only one transaction and txhash is known
} else {
crypto_status = "paid",
crypto_pending = "no";
}
} else {
crypto_status = "insufficient",
crypto_pending = "scanning";
}
}
if (is_recent && !is_crypto) { // get local fiat rates when request is less then 15 minutes old
const crypto_symbol = rd.currencysymbol,
exchange_rates = br_get_session("exchangerates", true),
crypto_exchange_rates = br_get_session("xrates_" + crypto_symbol, true);
if (exchange_rates && crypto_exchange_rates) {
const fiat_exchange_rates = exchange_rates.fiat_exchangerates,
local_exchange_rate = fiat_exchange_rates ? fiat_exchange_rates[rd.fiatcurrency] : null,
usd_eur_exchange_rate = fiat_exchange_rates ? fiat_exchange_rates.usd : null;
if (local_exchange_rate && usd_eur_exchange_rate) {
const usd_rate = crypto_exchange_rates ? crypto_exchange_rates.ccrate : null;
if (usd_rate) {
const usd_value = total_crypto_amount * usd_rate,
eur_value = usd_value / usd_eur_exchange_rate;
fiat_value = eur_value * local_exchange_rate,
recent_data = true;
}
}
} else {
init_fiat_history(rd, rdo, first_confirmations, latest_transaction_time, first_transaction_time);
return
}
}
if (is_crypto || recent_data) {
update_request({
"requestid": rd.requestid,
"status": crypto_status,
"receivedamount": total_crypto_amount,
"fiatvalue": fiat_value,
"paymenttimestamp": crypto_payment_timestamp,
"txhash": crypto_transaction_hash,
"confirmations": crypto_confirmations,
"pending": crypto_pending,
"lightning": rd.lightning
}, false);
finalize_request_state(rdo);
return
}
init_fiat_history(rd, rdo, first_confirmations, latest_transaction_time, first_transaction_time);
return
}
}
finalize_request_state(rdo);
}
// Updates and displays the transaction count in the UI status panel with reset functionality
function update_transaction_count(status_panel, count) {
if (count === "reset") {
status_panel.attr("data-count", 0).text("+ " + 0);
return
}
const current_count = parseInt(status_panel.attr("data-count")),
new_count = current_count + count;
if (!new_count) return;
status_panel.attr("data-count", new_count).text("+ " + new_count);
if (new_count > 1) {
status_panel.closest(".rqli").find(".metalist .show_tx").hide();
}
}
// ** Historical Data Processing: **
// Triggers historical fiat data retrieval for transactions based on confirmation status
function init_fiat_history(rd, rdo, conf, latestinput, firstinput) {
const confirm_level = conf || 0,
no_confirm = rd.no_conf || conf === false,
latest_confirm = no_confirm ? 0 : confirm_level, // only update on change
cache_prefix = "historic_" + rd.requestid,
historic_cache = br_get_session(cache_prefix),
cache_value = parseInt(latestinput) + parseInt(latest_confirm);
if ((latest_confirm || no_confirm) && cache_value > historic_cache) { //new input detected; call historic api
br_remove_session(cache_prefix); // remove historic price cache
const historic_payload = $.extend(rd, {
"latestinput": latestinput,
"latestconf": latest_confirm,
"firstinput": firstinput
}),
api_list = "historic_fiat_price_apis",
fiat_api = $("#fiatapisettings").data("selected"),
fiat_api_default = (fiat_api === "coingecko" || fiat_api === "coinbase") ? "fixer" : fiat_api; // exclude coingecko api"
glob_let.api_attempt[api_list] = {}; // reset global historic fiat price api attempt
fetch_fiat_rates(historic_payload, rdo, api_list, fiat_api_default);
return
}
finalize_request_state(rdo);
}
// Retrieves historical fiat exchange rates from specified API with fallback options
function fetch_fiat_rates(rd, rdo, api_list, fiat_api) {
glob_let.api_attempt[api_list][fiat_api] = true;
const fiat_currency = rd.fiatcurrency;
if (fiat_currency) {
const currency_symbol = fiat_currency.toUpperCase(),
payload = get_historic_fiatprice_api_payload(fiat_api, currency_symbol, rd.latestinput);
api_proxy({
"api": fiat_api,
"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(api_list, fiat_api);
if (next_historic) {
fetch_fiat_rates(rd, rdo, api_list, next_historic);
return
}
fail_dialogs(fiat_api, {
"error": data.error
});
finalize_request_state(rdo);
return
}
let usd_eur = false,
usd_local = false,
usd_rate = false,
local_rate = false;
if (fiat_api === "currencylayer") {
usd_eur = q_obj(data, "quotes.USDEUR"),
usd_local = q_obj(data, "quotes.USD" + currency_symbol);
if (usd_eur && usd_local) {
usd_rate = 1 / usd_eur,
local_rate = usd_local * usd_rate;
}
} else {
usd_rate = q_obj(data, "rates.USD"),
local_rate = q_obj(data, "rates." + currency_symbol);
}
if (usd_rate && local_rate) {
const currency_rate = currency_symbol === "EUR" ? 1 : local_rate,
historic_api = $("#cmcapisettings").data("selected"),
picked_historic_api = historic_api === "coinmarketcap" ? "coingecko" : historic_api, // default to "coingecko api"
init_api_list = "historic_crypto_price_apis";
glob_let.api_attempt[init_api_list] = {};
fetch_crypto_rates(rd, rdo, fiat_api, init_api_list, picked_historic_api, currency_rate, usd_rate, currency_symbol);
return
}
const next_historic = try_next_api(api_list, fiat_api);
if (next_historic) {
fetch_fiat_rates(rd, rdo, api_list, next_historic);
return
}
}
fail_dialogs(fiat_api, {
"error": "unable to fetch " + currency_symbol + " exchange rate"
});
finalize_request_state(rdo);
}).fail(function(xhr, stat, err) {
function next_proxy() { // try next proxy
if (get_next_proxy()) {
fetch_fiat_rates(rd, rdo, api_list, fiat_api);
return true
}
return false
}
if (is_proxy_fail(this.url) && next_proxy()) { // Try next proxy if proxy fails
return
}