This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { JsonRpcProvider, formatUnits } from "ethers"; | |
const POLYGON_RPC_URL = process.env.POLYGON_RPC_URL || "https://polygon-rpc.com"; | |
async function getPolygonGasPriceGwei() { | |
const provider = new JsonRpcProvider(POLYGON_RPC_URL); | |
const feeData = await provider.getFeeData(); | |
const wei = feeData.gasPrice ?? feeData.maxFeePerGas ?? feeData.lastBaseFeePerGas; | |
if (wei == null) { | |
throw new Error("No gas price available from provider"); |