-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgasCost.js
70 lines (58 loc) · 2.36 KB
/
gasCost.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
const axios = require("axios");
const fs = require("fs");
const ethGasApi = "https://ethgasstation.info/api/ethgasAPI";
const ethPriceApi = "https://api.gemini.com/v1/pubticker/ethusd";
const maticGasApi = "https://gasstation.polygon.technology/v2";
const maticPriceApi = "https://api.gemini.com/v1/pubticker/maticusd";
const getFileData = () => {
try {
const content = fs.readFileSync("./gas-report.txt");
return content.toString();
} catch (error) {
console.log("Error occured while reading content of gas-report.txt file");
}
};
const getGasCost = () => {
const gasReportContent = getFileData();
const lines = gasReportContent.split("\n");
let gasCost;
lines.forEach((line) => {
if (line.indexOf("setBlock") > 0) {
const avgGasCost = line.split("·")[4];
gasCost = Number(avgGasCost);
return;
}
});
return gasCost;
};
const main = async () => {
const gasCost = getGasCost();
console.log(`Gas consumption of setBlock is ${gasCost}`);
if (gasCost && gasCost > 0) {
// * Ethereum
// console.log("----------------- ETHEREUM ------------------------");
// let ethGasPrice = await axios.get(ethGasApi);
// ethGasPrice = Number(ethGasPrice.data.average) / 10;
// let ethPrice = await axios.get(ethPriceApi);
// ethPrice = Number(ethPrice.data.last);
// console.log("Gas price in Gwei on Ethereum:", ethGasPrice);
// console.log("Eth price in USD:", ethPrice);
// const costOnEthereum =
// Math.floor(((ethPrice * ethGasPrice * gasCost) / 1e9) * 100) / 100;
// console.log(`cost of setBlock on Ethereum is $${costOnEthereum}`);
// console.log(`cost per day if epoch = 20min: $${costOnEthereum * 3 * 24}`);
// * Polygon
console.log("\n----------------- POLYGON -------------------------");
let maticGasPrice = await axios.get(maticGasApi);
maticGasPrice = Number(maticGasPrice.data.standard.maxFee);
let maticPrice = await axios.get(maticPriceApi);
maticPrice = Number(maticPrice.data.last);
console.log("Gas price in Gwei on Polygon:", maticGasPrice);
console.log("Polygon price in USD:", maticPrice);
const costOnMatic =
Math.floor(((maticPrice * maticGasPrice * gasCost) / 1e9) * 100) / 100;
console.log(`cost of setBlock on Polygon is $${costOnMatic}`);
console.log(`cost per day if epoch = 20min: $${costOnMatic * 3 * 24}`);
}
};
main();