-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
640 lines (559 loc) · 18.4 KB
/
common.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
export var nodeURL;
export var evmURL;
export var evmTxURL;
export var corsProxyURL;
export var checkURL;
export var tmURL;
export var hashServerURL;
export var RPCByChainID;
export var AddressExplorerByChainID;
export var esploraAPIURL;
export var externalChainIDs;
export var Chains;
export var zetaclientIPs;
export var bitcoinChainID;
export var groupAdminAddresses;
var zetaIP;
var auxIP = "46.4.15.110";
var zetaChainID;
// Step 0: Inject HTML
const widgetContainer = document.createElement("div");
const selectHTML = `
<label for="networkSelector">Choose a network:</label>
<select id="networkSelector">
<option value="athens3">Athens 3</option>
<option value="mockmain">Mock Main</option>
<option value="mainnet">Mainnet</option>
</select>
`;
widgetContainer.innerHTML = selectHTML;
// add widgetConainter to the body of the page
document.body.prepend(widgetContainer);
// Step 1: Get the select element by its ID
const networkSelector = document.getElementById("networkSelector");
// Step 2: Load saved network choice from localStorage if it exists
const savedNetwork = localStorage.getItem("network");
if (savedNetwork) {
networkSelector.value = savedNetwork;
}
// Step 3: Add event listener for when the selection changes
networkSelector.addEventListener("change", function() {
const selectedNetwork = networkSelector.value;
// Step 4: Save the selection to localStorage
localStorage.setItem("network", selectedNetwork);
alert("refresh your page to take effect");
});
export var network = localStorage.getItem("network");
if (network == "mockmain") {
bitcoinChainID = 8332;
zetaIP = "100.113.213.95";
zetaChainID = 70000;
RPCByChainID = {
1: "https://eth.llamarpc.com",
56: "https://binance.llamarpc.com",
8332: "https://blockstream.info/api",
70000: evmURL,
};
AddressExplorerByChainID = {
1: "https://www.etherscan.io/address",
56: "https://www.bscscan.com/address",
8332: "https://blockstream.info/address",
};
esploraAPIURL = "https://blockstream.info/api";
externalChainIDs = [1,56,8332];
groupAdminAddresses = {
"Network Admin": "zeta1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzsxn0x73",
};
Chains = {
1: {
"chainId": "1",
"chainName": "Ethereum Mainnet",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
},
56: {
"chainId": "56",
"chainName": "Binance Smart Chain",
"nativeCurrency": {
"name": "BNB",
"symbol": "BNB",
"decimals": 18
},
},
70000: {
"chainId": "70000",
"chainName": "ZetaChain",
"nativeCurrency": {
"name": "Zeta",
"symbol": "ZETA",
"decimals": 18
},
},
8332: {
"chainId": "8332",
"chainName": "Bitcoin Mainnet",
"nativeCurrency": {
"name": "Bitcoin",
"symbol": "tBTC",
"decimals": 8
},
}
};
zetaclientIPs = ["50.16.78.24", "44.218.42.109","44.216.230.163"];
} else if (network == "mainnet") {
bitcoinChainID = 8332;
zetaIP = "46.4.15.110";
zetaChainID = 7000;
RPCByChainID = {
1: "https://rpc.ankr.com/eth",
56: "https://bsc-dataseed1.bnbchain.org",
137: "https://polygon.llamarpc.com",
8332: "https://blockstream.info/api",
7000: evmURL,
};
AddressExplorerByChainID = {
1: "https://www.etherscan.io/address",
56: "https://www.bscscan.com/address",
137: "https://polygonscan.com/address",
8332: "https://blockstream.info/address",
};
esploraAPIURL = "https://blockstream.info/api";
externalChainIDs = [1,56,137,8332];
groupAdminAddresses = {
"Network Operations": "zeta1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzsxn0x73",
"Network Admin": "zeta1dlszg2sst9r69my4f84l3mj66zxcf3umcgujys30t84srg95dgvs5wguxq",
"Validator Admin": "zeta1c799jddmlz7segvg6jrw6w2k6svwafganjdznard3tc74n7td7rqzgjegn",
"Validator Operations": "zeta17pmq7hp4upvmmveqexzuhzu64v36re3w3447n7dt46uwp594wtps03v8vg",
};
Chains = {
1: {
"chainId": "1",
"chainName": "Ethereum Mainnet",
"nativeCurrency": {
"name": "Ether",
"symbol": "ETH",
"decimals": 18
},
},
56: {
"chainId": "56",
"chainName": "Binance Smart Chain",
"nativeCurrency": {
"name": "BNB",
"symbol": "BNB",
"decimals": 18
},
},
7000: {
"chainId": "7000",
"chainName": "ZetaChain",
"nativeCurrency": {
"name": "Zeta",
"symbol": "ZETA",
"decimals": 18
},
},
8332: {
"chainId": "8332",
"chainName": "Bitcoin Mainnet",
"nativeCurrency": {
"name": "Bitcoin",
"symbol": "tBTC",
"decimals": 8
},
}
};
// zetaclientIPs = ["50.16.78.24", "44.218.42.109","44.216.230.163"];
zetaclientIPs = [
"34.225.36.174",
"52.45.59.77",
"52.35.128.130",
"35.87.167.186",
"54.212.38.135",
"108.171.216.154",
"192.69.210.202",
"208.91.106.108",
"15.235.10.84"
];
evmTxURL = "https://zetachain-evm.blockpi.network:443/v1/rpc/public";
} else { // default to athens3
bitcoinChainID = 18332;
network = "athens3";
zetaIP = '100.88.13.140';
zetaChainID = 7001
RPCByChainID = {
// 5: "https://rpc.ankr.com/eth_goerli",
11155111: "https://ethereum-sepolia.blockpi.network/v1/rpc/public",
97: "https://data-seed-prebsc-1-s1.binance.org:8545",
// 80001: "https://rpc.ankr.com/polygon_mumbai",
80002: "https://rpc.ankr.com/polygon_amoy",
18332: "https://blockstream.info/testnet/api",
7001: evmURL,
};
AddressExplorerByChainID = {
11155111: "https://sepolia.etherscan.io/address",
97: "https://testnet.bscscan.com/address",
80002: "https://amoy.polygonscan.com/address",
18332: "https://blockstream.info/testnet/address",
};
esploraAPIURL = "https://blockstream.info/testnet/api";
externalChainIDs = [11155111, 97, 80002, 18332, 901];
groupAdminAddresses = {
"Network Admin": "zeta1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzsxn0x73",
"Network Operations": "zeta1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzsxn0x73",
"Validator Admin": "zeta1dlszg2sst9r69my4f84l3mj66zxcf3umcgujys30t84srg95dgvs5wguxq",
};
Chains = {
5: {
"chainId": "5",
"chainName": "Goerli Testnet",
"nativeCurrency": {
"name": "Goerli Ether",
"symbol": "gETH",
"decimals": 18
},
},
97: {
"chainId": "97",
"chainName": "Binance Smart Chain Testnet",
"nativeCurrency": {
"name": "Testnet BNB",
"symbol": "tBNB",
"decimals": 18
},
},
80001: {
"chainId": "80001",
"chainName": "Polygon Mumbai Testnet",
"nativeCurrency": {
"name": "Matic",
"symbol": "tMATIC",
"decimals": 18
},
},
7001: {
"chainId": "7001",
"chainName": "ZetaChain",
"nativeCurrency": {
"name": "Zeta",
"symbol": "ZETA",
"decimals": 18
},
},
18332: {
"chainId": "18332",
"chainName": "Bitcoin Testnet",
"nativeCurrency": {
"name": "Bitcoin",
"symbol": "tBTC",
"decimals": 8
},
}
};
zetaclientIPs = ["52.42.64.63", "150.136.176.81",
// "202.8.10.137",
// "35.210.142.91",
"bd-validator-01.testnet.zetachain.bdnodes.net",
"54.39.18.86",
"34.239.99.239",
"3.218.170.198",
"18.210.106.52",
"44.236.174.26",
"35.162.231.114",
"54.77.180.134",
"34.253.137.241",
"18.143.71.236",
"54.254.133.239",
];
nodeURL = "https://athens.rpc.zetachain.com/D0608595-63FF-4EA9-91FC-0587BB0D968A/internal";
evmURL = "https://athens.rpc.zetachain.com/D0608595-63FF-4EA9-91FC-0587BB0D968A/evm";
tmURL = "https://athens.rpc.zetachain.com/D0608595-63FF-4EA9-91FC-0587BB0D968A/rpc";
}
nodeURL = nodeURL ?? `http://${zetaIP}:1317`;
evmURL = evmURL ?? `http://${zetaIP}:8545`;
evmTxURL = evmTxURL ?? `http://${zetaIP}:8545`;
corsProxyURL = `http://${auxIP}:8088`;
checkURL = `http://${auxIP}:8888`; // remote server that tests port 6668 p2p nodes
tmURL = tmURL ?? `http://${zetaIP}:26657`;
hashServerURL = `http://${auxIP}:9001`;
RPCByChainID[zetaChainID] = evmURL;
export async function getForegienCoins() {
const url = `${nodeURL}/zeta-chain/fungible/foreign_coins`;
const p1 = await fetch(url, { method: 'GET', });
const data = await p1.json();
return data?.foreignCoins;
}
if (window.location.protocol === 'https:') {
alert('HTTPS does not work; force your browser to use HTTP instead.');
}
export function addDetails(sum, det) {
return DIV(
DETAILS(
SUMMARY(TEXT(sum)),
PRE(TEXT(det))
));
}
export function addDetails2(sum,detElement) {
return DIV(
DETAILS(
SUMMARY(TEXT(sum)),
detElement
));
}
export function timeSince(date) {
const now = new Date();
const secondsPast = (now.getTime() - date.getTime()) / 1000;
if (secondsPast < 60) {
return `${parseInt(secondsPast)}s ago`;
}
if (secondsPast < 3600) {
return `${parseInt(secondsPast / 60)}m ago`;
}
if (secondsPast <= 86400) {
return `${parseInt(secondsPast / 3600)}h${parseInt((secondsPast % 3600) / 60)}m ago`;
}
// Handle cases for more than one day here if needed
}
export function msToTime(duration) {
let seconds = parseInt((duration / 1000) % 60),
minutes = parseInt((duration / (1000 * 60)) % 60),
hours = parseInt((duration / (1000 * 60 * 60)) % 24),
days = parseInt((duration / (1000 * 60 * 60 * 24)));
let result = "";
if(days) { result += days + "d"; }
if(hours) { result += hours + "h"; }
if(minutes) { result += minutes + "m"; }
if(seconds) { result += seconds + "s"; }
return result.trim();
}
// const Web3 = require('web3');
export function base64ToUtf8(base64String) {
if (base64String == null) {
return "null";
}
const binaryString = atob(base64String);
return binaryString;
}
export function parseHexString(hexString) {
var bytes = [];
for (var i = 0; i < hexString.length; i += 2) {
bytes.push(parseInt(hexString.substring(i, i + 2), 16));
}
return bytes;
}
// Function to add commas to a number string so "1234567" will
// turn into "1,234,567"
export function addCommas(numberString) {
var parts = numberString.split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
export function makeTableElementNew(json) {
const body = TBODY();
for (const field in json) {
const row = TR(
TD(TEXT(field)),
TD(TEXT(json[field]))
);
body.appendChild(row);
}
return TABLE(body);
}
// simple utility to turn a JSON object into a HTML table element;
// two columns: "JSON key", "JSON value";
// each row is a field in JSON object.
export function makeTableElement(json) {
// create a table
var table = document.createElement('table');
// create table header
var thead = document.createElement('thead');
table.appendChild(thead);
// create table body
var tbody = document.createElement('tbody');
// iterate over the JSON object
for (var field in json) {
// create a row for each field
var tr = document.createElement('tr');
// create a cell for the field name
var td1 = document.createElement('td');
td1.textContent = field;
tr.appendChild(td1);
// create a cell for the field value
var td2 = document.createElement('td');
td2.textContent = json[field];
tr.appendChild(td2);
// add the row to the table body
tbody.appendChild(tr);
}
// append the body to the table
table.appendChild(tbody);
// append the table to the body of the page
return table;
}
// given a JSON array, turn it into a table; each row is
// one element in JSON array; each column is a field;
// the list of interested fields are specified by the fields
// string array
export function makeTableElement2(jsonArray, fields) {
// create a table
var table = document.createElement('table');
// create table header
var thead = document.createElement('thead');
var headerRow = document.createElement('tr');
fields.forEach(header => {
var th = document.createElement('th');
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
// create table body
var tbody = document.createElement('tbody');
// iterate over the JSON object
jsonArray.forEach( row => {
// console.log("row", row);
// create a row for each field
var tr = document.createElement('tr');
fields.forEach( field => {
var td = document.createElement('td');
td.textContent = row[field];
tr.appendChild(td);
});
table.appendChild(tr);
});
// append the body to the table
table.appendChild(tbody);
// append the table to the body of the page
return table;
}
export function utcToLocal(utcString) {
let utcDate = new Date(utcString);
return utcDate.toLocaleString();
}
// tx_resp is JSON array of tx_responses
// tx is JSON array of tx
export function txResponsesToTable(tx_resp) {
if (tx_resp.length == 0) {
console.log("error: tx.length == 0", tx.length);
return;
}
// create a table
var table = document.createElement('table');
// create table header
var thead = document.createElement('thead');
var headerRow = document.createElement('tr');
let fields = ["tx_hash", "code", "log"];
fields.forEach(header => {
var th = document.createElement('th');
th.textContent = header;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
// create table body
var tbody = document.createElement('tbody');
for (var i = 0; i < tx_resp.length; i++) {
var tr = document.createElement('tr');
var row = {};
let tx = tx_resp[i].tx;
let msg_type = tx["body"].messages[0]["msgs"][0]["@type"];
let txhash = tx_resp[i].txhash;
row["tx_hash"] = `${txhash}<br>${msg_type}`
row["code"] = tx_resp[i].code;
var raw_log = tx_resp[i].raw_log;
// console.log(JSON.parse(raw_log));
var log_elm;
try {
var log = JSON.parse(raw_log);
log_elm = document.createElement('pre');
log_elm.textContent = JSON.stringify(log, null, 2);
} catch (e) {
log_elm = document.createElement('div');
log_elm.textContent = raw_log;
}
let div = document.createElement('div');
div.classList = "tooltip";
div.appendChild(document.createTextNode("hover"));
// span.classList = "tooltiptext";
log_elm.classList = "tooltiptext";
div.appendChild(log_elm);
row["log"] = div.outerHTML;
// row["log"] = tx_resp[i].raw_log;
fields.forEach( field => {
var td = document.createElement('td');
td.innerHTML = row[field];
tr.appendChild(td);
});
tbody.appendChild(tr);
}
// iterate over the JSON object
// jsonArray.forEach( row => {
// // console.log("row", row);
// // create a row for each field
// var tr = document.createElement('tr');
// fields.forEach( field => {
// var td = document.createElement('td');
// td.textContent = row[field];
// tr.appendChild(td);
// });
// table.appendChild(tr);
// });
// append the body to the table
table.appendChild(tbody);
// append the table to the body of the page
return table;
}
// array of int8 (length 20) to hex string (eth address format)
export function int8ArrayToHex(array) {
if (array.length != 20) {
console.log("error: array.length != 20", array.length);
return;
}
let hexString = array.map(num => {
let hex = num.toString(16);
if (hex.length < 2) {
hex = '0' + hex;
}
return hex;
}).join('');
hexString = '0x' + hexString;
return Web3.utils.toChecksumAddress(hexString);
}
// array of int8 (length any) to hex string
export function int8ArrayToHexRelaxed(array) {
let hexString = array.map(num => {
let hex = num.toString(16);
if (hex.length < 2) {
hex = '0' + hex;
}
return hex;
}).join('');
hexString = '0x' + hexString;
return hexString;
}
// amount in smallest denomination (wei, satoshi, etc), convert into (eth, btc, etc)
// amount is string; decimals is Number; return Number
export function fromDecimals(amount, decimals) {
return Number(BigInt(amount)) / Math.pow(10, decimals);
}
export function renderHeader() {
var headerHTML = `
<div id="links" style="text-align: center;font-size: 20px;display: flex;justify-content: space-between;">
<span class="flexible-filler"></span>
<a href="./index.html">Basics</a> <span class="flexible-filler"></span>
<a href="./zevm.html">zEVM</a> <span class="flexible-filler"></span>
<a href="./zetaclients.html">ZetaClients</a> <span class="flexible-filler"></span>
<a href="./crosschain.html">Cross-Chain Module</a><span class="flexible-filler"></span>
<a href="./params.html">Params</a><span class="flexible-filler"></span>
<a href="./consensus.html">Consensus</a> <span class="flexible-filler"></span>
<a href="./wallet.html">Wallet</a> <span class="flexible-filler"></span>
<a href="./audit.html">Audit</a> <span class="flexible-filler"></span>
<a href="./staking.html">Staking</a>
<a href="./group-proposals.html">Admin Proposals</a>
</div>
`
document.body.insertAdjacentHTML('afterbegin', headerHTML);
}