const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);
const getFeeHistory = async () => {
// Get fee history for last 10 blocks with 25th, 50th, 75th percentiles
const feeHistory = await provider.send("eth_feeHistory", [
"0xa",
"latest",
[25, 50, 75]
]);
console.log(`Oldest block: ${parseInt(feeHistory.oldestBlock, 16)}`);
console.log(`\nBase fees (gwei):`);
for (let i = 0; i < feeHistory.baseFeePerGas.length; i++) {
const baseFee = ethers.formatUnits(feeHistory.baseFeePerGas[i], "gwei");
const gasRatio = feeHistory.gasUsedRatio[i] ? (feeHistory.gasUsedRatio[i] * 100).toFixed(2) : "N/A";
console.log(` Block +${i}: ${baseFee} gwei (${gasRatio}% full)`);
}
if (feeHistory.reward) {
console.log(`\nPriority fee percentiles for latest block (gwei):`);
const latest = feeHistory.reward[feeHistory.reward.length - 1];
console.log(` 25th: ${ethers.formatUnits(latest[0], "gwei")}`);
console.log(` 50th: ${ethers.formatUnits(latest[1], "gwei")}`);
console.log(` 75th: ${ethers.formatUnits(latest[2], "gwei")}`);
}
};
getFeeHistory();