-
Notifications
You must be signed in to change notification settings - Fork 0
/
onchaindata.js
101 lines (83 loc) · 2.92 KB
/
onchaindata.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
const testnet_config_files = [
'wardenprotocol.json',
'allora.json',
];
const mainnet_config_files = [
'osmosis.json',
'nolus.json',
'neutron.json',
];
const typeChains= ['testnet', 'mainnet'];
const chainsUrl = `https://raw.githubusercontent.com/0ndrec/explorer/master/chains/`;
const tendermintUrl = `/cosmos/base/tendermint/v1beta1/blocks/latest`;
async function fetchJSON(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
// Function who return dictionary values from json files for current network
async function getValues(chainsUrl, chainType, configFile) {
const fileUrl = `${chainsUrl}${chainType}/${configFile}`;
console.log(fileUrl)
const jsonData = await fetchJSON(fileUrl);
data = {
name : jsonData.chain_name,
rpc : jsonData.rpc[0],
api : jsonData.api[0],
}
console.log(data)
return data
}
async function generateDivs(configFiles, chainType, containerClassTarget) {
for (config_f of configFiles) {
const data = await getValues(chainsUrl, chainType, config_f);
function selectEndpoint(endpoint) {
if (typeof endpoint === 'string') {
return endpoint
} else {
return endpoint.address;
}
}
const ApiEndpoint = selectEndpoint(data.api);
const RpcEndpoint = selectEndpoint(data.rpc);
const ChainName = data.name;
async function fetchBlockHeight() {
try {
const response = await fetch(`${ApiEndpoint}${tendermintUrl}`);
const height_data = await response.json();
return height_data.block.header.height;
} catch (error) {
return 'Loading...';
}
}
blockHeight = await fetchBlockHeight();
const div = document.createElement('div');
if (chainType === 'testnet') {
div.id = `${data.name}`;
div.classList.add('testnet');
}
else {
div.id = `${data.name}`;
div.classList.add('mainnet');
}
// Convert ChainName to uppercase
div.innerHTML = `
<h3>${ChainName.toUpperCase()}</h3>
<div class="block_height">
<p class="title">Block Height</p>
<p class="value">${blockHeight}</p>
</div>
<div class="endpoints">
<div class="button" href="${ApiEndpoint}" target="_blank">
<a href="${ApiEndpoint}" target="_blank">API</a>
</div>
<div class="button" href="${RpcEndpoint}" target="_blank">
<a href="${RpcEndpoint}" target="_blank">RPC</a>
</div>
</div>
`;
document.querySelector(containerClassTarget).appendChild(div);
}
}