-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
296 lines (236 loc) · 7.7 KB
/
utils.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
// const Web3 = require('web3');
function createTreeView(element, obj) {
for (let key in obj) {
// Create a container for each key-value pair
let pairContainer = document.createElement('div');
pairContainer.style.marginLeft = '20px';
// Create and append the key element
let keyElement = document.createElement('span');
keyElement.textContent = key + ': ';
pairContainer.appendChild(keyElement);
// Check if the value is an object, if so, recursively create its tree view
if (typeof obj[key] === 'object' && obj[key] !== null) {
let subTree = createTreeView(document.createElement('div'), obj[key]);
pairContainer.appendChild(subTree);
} else {
// If the value is not an object, just append the value
let valueElement = document.createElement('span');
valueElement.textContent = obj[key];
pairContainer.appendChild(valueElement);
}
// Append the key-value pair container to the element
element.appendChild(pairContainer);
}
return element;
}
function base64ToUint8Array(base64) {
const binaryString = atob(base64);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
function base64ToUtf8(base64String) {
if (base64String == null) {
return "null";
}
const binaryString = atob(base64String);
return binaryString;
}
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 toggleDropdown(dropdownId) {
var dropdown = document.getElementById(dropdownId);
dropdown.style.display = (dropdown.style.display === "block") ? "none" : "block";
}
// Function to add commas to a number string so "1234567" will
// turn into "1,234,567"
function addCommas(numberString) {
var parts = numberString.split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
// 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.
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
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;
}
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
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)
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
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
function fromDecimals(amount, decimals) {
return Number(BigInt(amount)) / Math.pow(10, decimals);
}