-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
37 lines (32 loc) · 1.06 KB
/
test.ts
File metadata and controls
37 lines (32 loc) · 1.06 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
async function getBaseFeeGwei() {
const rpcUrl = "https://cronos.blockpi.network/v1/rpc/8c000aa3c116d991def800b5baf5193372ca0445";
const payload = {
jsonrpc: "2.0",
id: 1,
method: "eth_getBlockByNumber",
params: ["latest", false]
};
try {
const response = await fetch(rpcUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
const data = await response.json();
if (data.result && data.result.baseFeePerGas) {
const baseFeeWei = BigInt(data.result.baseFeePerGas);
console.log(`Base Fee: ${baseFeeWei} Wei`);
const baseFeeGwei = baseFeeWei / BigInt(1e9); // Convert Wei to Gwei
console.log(`Base Fee: ${baseFeeGwei} Gwei`);
return baseFeeGwei;
} else {
console.error("Base fee not found in response.");
return null;
}
} catch (error) {
console.error("Error fetching base fee:", error);
return null;
}
}
// Call the function
getBaseFeeGwei();