Skip to content

Commit b21755d

Browse files
committed
compress l2 data
1 parent acf0628 commit b21755d

7 files changed

+160
-92
lines changed

assets_js_bitrequest_assets.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets_js_bitrequest_coin_settings.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,15 +1346,22 @@ function reset_coinsettings_function(currency) {
13461346
if (current_settings) {
13471347
const ln_settings = currency === "bitcoin" ? current_settings["Lightning network"] : false,
13481348
xpub_settings = current_settings.Xpub || false,
1349+
l2 = current_settings.layer2,
13491350
coinsettings = getcoinsettings(currency);
13501351
if (ln_settings) {
13511352
coinsettings["Lightning network"] = ln_settings; // don't reset lightning settings
13521353
}
13531354
if (xpub_settings) {
13541355
coinsettings.Xpub = xpub_settings; // don't reset xpub settings
13551356
}
1356-
br_set_local(currency + "_settings", coinsettings, true);
1357-
append_coinsetting(currency, coinsettings);
1357+
if (l2) {
1358+
const cs = compress_l2obj2(currency, null, true);
1359+
br_set_local(currency + "_settings", cs, true);
1360+
append_coinsetting(currency, cs);
1361+
} else {
1362+
br_set_local(currency + "_settings", coinsettings, true);
1363+
append_coinsetting(currency, coinsettings);
1364+
}
13581365
}
13591366
canceldialog();
13601367
notify(translate("resetnotify", {

assets_js_bitrequest_core.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ function finishfunctions() {
390390
//validateaddress_vk
391391
//set_xmr_node_access
392392
//validateaddress
393+
//compress_l2obj2
393394
//check_address
394395
//check_vk
395396
send_trigger();
@@ -2558,7 +2559,8 @@ function validateaddress(ad, vk) {
25582559
if (index === 1) {
25592560
if (iserc20 === true) {
25602561
buildpage(ad, true);
2561-
append_coinsetting(currency, get_erc20_settings());
2562+
append_coinsetting(currency, compress_l2obj2(currency, ccsymbol, true));
2563+
save_cc_settings(currency);
25622564
}
25632565
if (glob_const.body.hasClass("showstartpage")) {
25642566
const acountname = $("#eninput").val();
@@ -2586,6 +2588,35 @@ function validateaddress(ad, vk) {
25862588
clear_savedurl();
25872589
}
25882590

2591+
function compress_l2obj2(currency, ccsymbol, reset) {
2592+
// Initialize the result object with the base structure
2593+
const eth_settings = JSON.parse(JSON.stringify(getcoinsettings(currency))), // make a deep clone to prevent duplicates
2594+
cc_symbol = ccsymbol || q_obj(fetchsymbol(currency), "symbol"),
2595+
symbol = cc_symbol || "eth",
2596+
l2_settings = eth_settings.layer2,
2597+
ctracts = contracts(symbol),
2598+
result = {
2599+
"icon": "new-tab",
2600+
"selected": false,
2601+
"options": {}
2602+
};
2603+
// Get all networks from options
2604+
const networks = Object.entries(l2_settings.options);
2605+
2606+
// Filter and process only networks that have selected: true
2607+
networks.forEach(([network_name, network_data]) => {
2608+
if (ctracts[network_name] || currency === "ethereum") {
2609+
// Initialize this network in result if it's selected
2610+
result.options[network_name] = reset ? {} : {
2611+
"apis": network_data.apis.selected.name,
2612+
"websockets": network_data.websockets.selected.name
2613+
};
2614+
}
2615+
});
2616+
eth_settings.layer2 = result;
2617+
return eth_settings;
2618+
}
2619+
25892620
// Validates an address for a given currency using a regex pattern
25902621
function check_address(address, currency) {
25912622
const regex = getcoindata(currency).regex;

assets_js_bitrequest_ethl2.js

Lines changed: 108 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ $(document).ready(function() {
3333
//set_l2_status_init
3434
//set_l2_status
3535
//fertch_l2s
36+
//get_l2_node
3637
//omni_rdo
3738

3839
});
@@ -49,12 +50,12 @@ function init_l2_sockets(payment, address, ct, socket_node) {
4950
req_l2_arr = request.eth_l2s;
5051
let index = 0;
5152
$.each(l2_options, function(l2, l2_dat) {
52-
const set_select = is_request ? false : l2_dat.selected,
53+
const set_select = is_request ? false : !empty_obj(l2_dat),
5354
inarr = $.inArray(index, req_l2_arr) !== -1,
5455
selected = set_select || inarr;
5556
if (selected) {
5657
l2_arr.push(index);
57-
const sn = (socket_node && socket_node.network === l2) ? socket_node : q_obj(l2_dat, "websockets.selected");
58+
const sn = socket_node || get_l2_node(payment, l2, l2_dat, "websockets");
5859
init_layer2(sn, address, ctracts);
5960
}
6061
index++;
@@ -71,17 +72,21 @@ function init_l2_sockets(payment, address, ct, socket_node) {
7172

7273
// Init eth and erc20 L2 address scanning
7374
function init_layer2(socket_node, address, ctracts, retry) {
74-
const l2 = socket_node.network,
75-
contract = ctracts ? ctracts[l2] : false;
76-
socket_info(socket_node, true);
77-
const node_name = socket_node.name,
78-
ping_id = sha_sub(socket_node.url + l2, 15);
79-
glob_let.socket_attempt[ping_id] = true;
80-
if (node_name === "infura") {
81-
web3_erc20_websocket(socket_node, address, contract, ping_id);
75+
const l2 = q_obj(socket_node, "network");
76+
if (l2) {
77+
const contract = ctracts ? ctracts[l2] : false;
78+
socket_info(socket_node, true);
79+
const node_name = socket_node.name,
80+
ping_id = sha_sub(socket_node.url + l2, 15);
81+
glob_let.socket_attempt[ping_id] = true;
82+
if (node_name === "infura") {
83+
web3_erc20_websocket(socket_node, address, contract, ping_id);
84+
return
85+
}
86+
omni_scan(socket_node, contract, ping_id, retry);
8287
return
8388
}
84-
omni_scan(socket_node, contract, ping_id, retry);
89+
console.error("error", "missing api data");
8590
}
8691

8792
// Initiates Eth layer2 scanning
@@ -137,27 +142,28 @@ function query_ethl2_api(rd, rdo, api_dat, l2) {
137142
scan_ethl2_api(rd, rdo, api_dat, network);
138143
}
139144

140-
function scan_ethl2_api(rd, rdo, api_dat, l2) {
145+
function scan_ethl2_api(rd, rdo, api_dat, network) {
141146
glob_let.l2_fetched = {};
142147
const req_l2_arr = rd.eth_l2s;
143148
if (empty_obj(req_l2_arr)) { // No l2's
144149
api_callback(rdo);
145150
return
146151
}
147-
const l2_options = fertch_l2s(rd.payment);
152+
const currency = rd.payment,
153+
l2_options = fertch_l2s(currency);
148154
if (l2_options) {
149155
const ctracts = contracts(rd.currencysymbol);
150-
if (l2) { // l2 network is known so only scan this network
151-
const api_data = api_dat || q_obj(l2_options, l2 + ".apis.selected")
156+
if (network) { // l2 network is known so only scan this network
157+
const api_data = api_dat || get_l2_node(currency, network, l2_options[network], "apis");
152158
if (api_data) {
153-
const contract = ctracts[l2],
159+
const contract = ctracts[network],
154160
dat = {
155161
contract,
156162
rd,
157163
api_data,
158164
rdo
159165
};
160-
ethl2_networks(dat, l2);
166+
ethl2_networks(dat, network);
161167
}
162168
return
163169
}
@@ -173,7 +179,7 @@ function scan_ethl2_api(rd, rdo, api_dat, l2) {
173179
} else {
174180
const inarr = $.inArray(index, req_l2_arr) !== -1;
175181
if (inarr) {
176-
const api_data = api_dat || q_obj(l2_dat, "apis.selected");
182+
const api_data = api_dat || get_l2_node(currency, l2, l2_dat, "apis");
177183
if (api_data) {
178184
const contract = ctracts[l2],
179185
dat = {
@@ -209,7 +215,7 @@ function scan_ethl2_api(rd, rdo, api_dat, l2) {
209215

210216
function poll_ethl2_api(rd, rdo, api_dat, l2) {
211217
const l2_options = fertch_l2s(rd.payment),
212-
api_data = api_dat || q_obj(l2_options, l2 + ".apis.selected"),
218+
api_data = api_dat || get_l2_node(rd.payment, l2, l2_options[l2], "apis"),
213219
api_name = api_data.name,
214220
network = api_data.network;
215221
if (api_name && network) {
@@ -292,7 +298,11 @@ function bnb_apis(dat) {
292298
function edit_l2() {
293299
$(document).on("click", ".cc_settinglist li[data-id='layer2']", function() {
294300
const thiscurrency = $(this).children(".liwrap").attr("data-currency"),
295-
l2_options = fertch_l2s(thiscurrency);
301+
options = fertch_l2s(thiscurrency),
302+
old_format = q_obj(options, "arbitrum.selected"),
303+
l2_options = (old_format !== undefined) ? q_obj(compress_l2obj2(thiscurrency), "layer2.options") : options, // convert to compressed l2 format
304+
eth_settings = getcoinsettings(thiscurrency),
305+
eth_l2_settings = q_obj(eth_settings, "layer2.options");
296306
if (l2_options) {
297307
const ccsymbol = fetchsymbol(thiscurrency),
298308
symbol = ccsymbol.symbol,
@@ -301,66 +311,68 @@ function edit_l2() {
301311
polygon_contract = ctracts.polygon,
302312
bnb_contract = ctracts.bnb,
303313
networks = [];
304-
$.each(l2_options, function(l2, l2_dat) {
305-
if (l2 === "arbitrum" && !arb_contract && thiscurrency !== "ethereum") {} else if (l2 === "polygon" && !polygon_contract && thiscurrency !== "ethereum") {} else if (l2 === "bnb" && !bnb_contract && thiscurrency !== "ethereum") {} else {
314+
$.each(eth_l2_settings, function(l2, l2_dat) {
315+
if (l2_options.hasOwnProperty(l2)) {
306316
const nw_name = l2 === "bnb" ? "bnb smart chain" : l2,
307-
nw_selected = l2_dat.selected,
308-
s_boxes = []
317+
select = l2_options[l2],
318+
nw_selected = !empty_obj(select),
319+
s_boxes = [];
309320
$.each(l2_dat, function(k, v) {
310-
if (k === "selected") {} else {
311-
const selected = v.selected,
312-
apis = v.apis,
313-
api_push = [];
314-
$.each(apis, function(i, v2) {
315-
api_push.push({
316-
"span": {
317-
"data-pe": "none",
318-
"attr": add_prefix_to_keys(v2),
319-
"content": v2.name
320-
}
321-
});
321+
const selected = v.selected
322+
if (k === "selected") return;
323+
const apis = v.apis,
324+
select_name = k === "apis" ? select.apis : select.websockets,
325+
select_val = select_name || selected.name,
326+
api_push = [];
327+
$.each(apis, function(i, v2) {
328+
api_push.push({
329+
"span": {
330+
"data-pe": "none",
331+
"attr": add_prefix_to_keys(v2),
332+
"content": v2.name
333+
}
322334
});
323-
s_boxes.push({
324-
"div": {
325-
"class": "l2_apis",
326-
"attr": {
327-
"data-type": k
335+
});
336+
s_boxes.push({
337+
"div": {
338+
"class": "l2_apis",
339+
"attr": {
340+
"data-type": k
341+
},
342+
"content": [{
343+
"h3": {
344+
"content": k
328345
},
329-
"content": [{
330-
"h3": {
331-
"content": k
332-
},
333-
"div": {
334-
"class": "selectbox",
335-
"content": [{
336-
"input": {
337-
"attr": {
338-
"type": "text",
339-
"value": selected.name,
340-
"placeholder": translate("layer2"),
341-
"readonly": "readonly"
342-
},
343-
"close": true
346+
"div": {
347+
"class": "selectbox",
348+
"content": [{
349+
"input": {
350+
"attr": {
351+
"type": "text",
352+
"value": select_val,
353+
"placeholder": translate("layer2"),
354+
"readonly": "readonly"
344355
},
345-
"div": {
346-
"class": "selectarrows icon-menu2",
347-
"attr": {
348-
"data-pe": "none"
349-
}
350-
}
356+
"close": true
351357
},
352-
{
353-
"div": {
354-
"class": "options single",
355-
"content": api_push
358+
"div": {
359+
"class": "selectarrows icon-menu2",
360+
"attr": {
361+
"data-pe": "none"
356362
}
357363
}
358-
]
359-
}
360-
}]
361-
}
362-
});
363-
}
364+
},
365+
{
366+
"div": {
367+
"class": "options single",
368+
"content": api_push
369+
}
370+
}
371+
]
372+
}
373+
}]
374+
}
375+
});
364376
});
365377
networks.push({
366378
"div": {
@@ -380,6 +392,7 @@ function edit_l2() {
380392
}]
381393
}
382394
});
395+
383396
}
384397
});
385398
networks.push({
@@ -447,29 +460,27 @@ function submit_l2() {
447460
const payment = $(this).attr("data-currency"),
448461
csnode = cs_node(payment, "layer2");
449462
if (csnode) {
450-
const cs_node_dat = csnode.data("options"),
463+
const options = csnode.data("options"),
451464
nw2box = $("#l2_formbox").find(".popform > .nw2box");
452465
nw2box.each(function() {
453-
const this_box = $(this),
466+
const l2_type_obj = {},
467+
this_box = $(this),
454468
this_network = this_box.data("network"),
455469
this_switch = this_box.find(".switchpanel"),
456-
selected = this_switch.hasClass("true"),
457-
l2_apis = this_box.find(".l2_apis");
458-
cs_node_dat[this_network].selected = selected;
470+
l2_apis = this_box.find(".l2_apis"),
471+
select = this_switch.hasClass("true");
459472
l2_apis.each(function() {
460473
const this_nw = $(this),
461474
input = this_nw.find(".selectbox > input"),
462-
input_data = input.data();
475+
input_data = input.val();
463476
if (!empty_obj(input_data)) {
464-
const this_type = this_nw.data("type"),
465-
new_selected = q_obj(cs_node_dat, this_network + "." + this_type);
466-
if (new_selected) {
467-
new_selected.selected = input_data;
468-
}
477+
const this_type = this_nw.data("type");
478+
l2_type_obj[this_type] = input_data;
469479
}
470480
});
481+
options[this_network] = select ? l2_type_obj : {};
471482
});
472-
csnode.data("options", cs_node_dat).find("p").html("");
483+
csnode.data("options", options).find("p").html("");
473484
canceldialog();
474485
notify(translate("datasaved"));
475486
save_cc_settings(payment, true);
@@ -543,6 +554,19 @@ function fertch_l2s(currency) {
543554
return q_obj(l2_setting, "options");
544555
}
545556

557+
// get node data based on api name
558+
function get_l2_node(payment, network, l2_dat, type) {
559+
const selected = q_obj(l2_dat, type);
560+
if (selected) {
561+
const eth_settings = getcoinsettings(payment),
562+
eth_l2_settings = q_obj(eth_settings, "layer2.options." + network + "." + type + ".apis");
563+
if (eth_l2_settings) {
564+
return object_from_array(eth_l2_settings, "name", selected);
565+
}
566+
}
567+
return false;
568+
}
569+
546570
// get l2 request data object
547571
function omni_rdo(timeout, pending, contract, ping_id) {
548572
if (!request) return false;

0 commit comments

Comments
 (0)