Repository navigation
Expand file tree
/
Copy pathupdateBlockByNumber.js
More file actions
144 lines (121 loc) · 3.47 KB
/
Copy pathupdateBlockByNumber.js
File metadata and controls
144 lines (121 loc) · 3.47 KB
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
const path = require("path");
const fs = require("fs");
const https = require("https");
const directoryPath = path.join(__dirname, "appchains");
fs.readdir(directoryPath, (err, files) => {
if (err) {
throw new Error("Unable to scan directory: " + files);
}
files.forEach(async (file) => {
const tmpAppChain = readFile(directoryPath + "/" + file);
updateHeight(tmpAppChain);
});
});
const readFile = (fileDir) => {
let rawdata = fs.readFileSync(fileDir);
return JSON.parse(rawdata);
};
const updateHeight = (chain) => {
if (chain.jsonRPC) {
try {
chain.ecosystem.toLowerCase() === "cosmos"
? handleCosmos(chain)
: handleEth(chain);
} catch (error) {
console.log("Error fetching block height for " + chain.shortName + ": ", error);
}
} else {
console.log("RPC is not provided for " + chain.shortName);
}
};
const handleCosmos = (chain) => {
options = {
hostname: chain.jsonRPC.split("/")[2],
path: "/status",
method: "GET",
headers: {
"Content-Type": "application/json",
},
};
const request = https
.request(options, (res) => {
let data = "";
res
.on("data", (chunk) => {
data += chunk;
})
.on("end", () => handleCosmosResponse(data, chain));
})
.on("error", (err) => {
throw new Error("No data is provided: " + err);
});
request.end();
};
const handleCosmosResponse = (data, chain) => {
console.log("Response fetched: ", chain.shortName);
try {
const result = getResult(data, chain.shortName);
chain.blockNumber = result.sync_info?.latest_block_height;
chain.blockNumberUpdated = result.sync_info?.latest_block_time;
writeJSONFile(chain);
} catch (error) {
console.log("Error fetching block height for " + chain.shortName + ": ", error);
}
};
const handleEth = (chain) => {
body = JSON.stringify({
jsonrpc: "2.0",
method: "eth_getBlockByNumber",
params: ["latest", false],
id: 0,
});
options = {
hostname: chain.jsonRPC.split("/")[2],
path: chain.jsonRPC.substring(chain.jsonRPC.indexOf("/", 8)),
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
const request = https
.request(options, (res) => {
let data = "";
res
.on("data", (chunk) => {
data += chunk;
})
.on("end", () => handleEthResponse(data, chain));
})
.on("error", (err) => {
throw new Error("No data is provided: " + err);
});
request.end(body);
};
const handleEthResponse = (data, chain) => {
console.log("Response ended: ", chain.shortName);
try {
const result = getResult(data, chain.shortName);
chain.blockNumber = parseInt(result.number, 16);
chain.blockNumberUpdated = new Date(result.timestamp * 1000).toISOString();
writeJSONFile(chain);
} catch (error) {
console.log("Error fetching block height for " + chain.shortName + ": ", error);
}
};
const getResult = (data, chainName) => {
const response = JSON.parse(data);
const result = response.result;
if (!result) {
throw new Error("No data is provided for" + chainName);
}
return result;
};
const writeJSONFile = (data) => {
const path = __dirname + "/appchains/" + data.name.toLowerCase() + ".json";
fs.writeFile(path, JSON.stringify(data), (err) => {
if (err) {
throw new Error("Unable to crate file: " + err);
}
console.log("JSON file for " + data.shortName + " generated successfully.");
});
};