diff --git a/README.md b/README.md index 91f9d8f..221f4fd 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ const networkGasPrice = await getNetworkGasPrice("ethereum"); > 💡 The price level ("low", "average", "high", "asap") you should use depends on your type of application. Most use cases will do okay with "average" or "high". If your application has background transactions support and execution is not time critical, you could use "low". If your application has a strong need to execute transactions as soon as possible, we recommend using the > "asap" option that adds 20% on top of the "high" reported gas price. -> 🌐 The package currently supports the Ethereum (`"ethereum"`), Polygon (`"polygon"`), Goerli (`"goerli"`), Rinkeby (`"rinkeby"`), and Mumbai (`"mumbai"`) networks. If you would like to add support for a new network, please open an issue or pull request. +> 🌐 The package currently supports the Ethereum (`"ethereum"`), Polygon (`"polygon"`), Goerli (`"goerli"`), Sepolia (`"sepolia"`), Rinkeby (`"rinkeby"`), and Mumbai (`"mumbai"`) networks. If you would like to add support for a new network, please open an issue or pull request. 3. Use gas prices (with [Ethers.js](https://github.com/ethers-io/ethers.js/) as an example): diff --git a/lib/__tests__/getNetworkGasPrice.test.ts b/lib/__tests__/getNetworkGasPrice.test.ts index 68c52c6..819c760 100644 --- a/lib/__tests__/getNetworkGasPrice.test.ts +++ b/lib/__tests__/getNetworkGasPrice.test.ts @@ -29,6 +29,16 @@ describe("getNetworkGasPrice", () => { expect(result).toBe(gasPrice); }); + it("should return the Ethereum gas price for Sepolia", async () => { + const gasPrice = 1; + (getEthereumGasPrice as Mock).mockImplementationOnce(async () => { + return gasPrice; + }); + + const result = await getNetworkGasPrice("sepolia"); + + expect(result).toBe(gasPrice); + }); it("should return the Ethereum gas price for Rinkeby", async () => { const gasPrice = 1; (getEthereumGasPrice as Mock).mockImplementationOnce(async () => { @@ -85,6 +95,19 @@ describe("getNetworkGasPrice", () => { fallbackGasPrice: options.fallbackGasPrice.goerli, }); }); + it("should pass the Sepolia options", async () => { + const options = { + etherscanApiKey: "testApiKey", + fallbackGasPrice: { sepolia: 1 }, + }; + + await getNetworkGasPrice("sepolia", options); + + expect(getEthereumGasPrice).toHaveBeenCalledWith("sepolia", { + apiKey: options.etherscanApiKey, + fallbackGasPrice: options.fallbackGasPrice.sepolia, + }); + }); it("should pass the Rinkeby options", async () => { const options = { etherscanApiKey: "testApiKey", diff --git a/lib/getNetworkGasPrice.ts b/lib/getNetworkGasPrice.ts index 245c086..32de9e5 100644 --- a/lib/getNetworkGasPrice.ts +++ b/lib/getNetworkGasPrice.ts @@ -6,7 +6,12 @@ export function getNetworkGasPrice( network: Network, options: Options = {} ): Promise { - if (network === "ethereum" || network === "goerli" || network === "rinkeby") { + if ( + network === "ethereum" || + network === "goerli" || + network === "sepolia" || + network === "rinkeby" + ) { return getEthereumGasPrice(network, { apiKey: options.etherscanApiKey, fallbackGasPrice: options.fallbackGasPrice?.[network], diff --git a/lib/networks/__tests__/getEthereumGasPrice.test.ts b/lib/networks/__tests__/getEthereumGasPrice.test.ts index 3d8219e..1a0c176 100644 --- a/lib/networks/__tests__/getEthereumGasPrice.test.ts +++ b/lib/networks/__tests__/getEthereumGasPrice.test.ts @@ -84,6 +84,43 @@ describe("getEthereumGasPrice", () => { }, }); }); + it("should return the Sepolia gas prices per level based on the Ethereum gas station", async () => { + const lowGasPrice = 100; + const averageGasPrice = 110; + const fastGasPrice = 120; + + const mock = mockFetch({ + result: { + SafeGasPrice: lowGasPrice, + ProposeGasPrice: averageGasPrice, + FastGasPrice: fastGasPrice, + }, + }); + + const result = await getEthereumGasPrice("sepolia"); + + expect(mock).toHaveBeenCalledTimes(1); + expect(mock).toHaveBeenCalledWith(GAS_STATION_URL_BY_NETWORK.sepolia); + + expect(result).toEqual({ + low: { + maxPriorityFeePerGas: lowGasPrice, + maxFeePerGas: lowGasPrice, + }, + average: { + maxPriorityFeePerGas: averageGasPrice, + maxFeePerGas: averageGasPrice, + }, + high: { + maxPriorityFeePerGas: fastGasPrice, + maxFeePerGas: fastGasPrice, + }, + asap: { + maxPriorityFeePerGas: (fastGasPrice * ASAP_PERCENTAGE) / 100, + maxFeePerGas: (fastGasPrice * ASAP_PERCENTAGE) / 100, + }, + }); + }); it("should return the Rinkeby gas prices per level based on the Ethereum gas station", async () => { const lowGasPrice = 100; const averageGasPrice = 110; @@ -141,6 +178,16 @@ describe("getEthereumGasPrice", () => { `${GAS_STATION_URL_BY_NETWORK.goerli}&apiKey=testApiKey` ); }); + it("should include the API key if provided for Sepolia", async () => { + const mock = mockFetch(null); + + await getEthereumGasPrice("sepolia", { apiKey: "testApiKey" }); + + expect(mock).toHaveBeenCalledTimes(1); + expect(mock).toHaveBeenCalledWith( + `${GAS_STATION_URL_BY_NETWORK.sepolia}&apiKey=testApiKey` + ); + }); it("should include the API key if provided for Rinkeby", async () => { const mock = mockFetch(null); diff --git a/lib/networks/getEthereumGasPrice.ts b/lib/networks/getEthereumGasPrice.ts index 674318c..92573df 100644 --- a/lib/networks/getEthereumGasPrice.ts +++ b/lib/networks/getEthereumGasPrice.ts @@ -6,6 +6,8 @@ export const GAS_STATION_URL_BY_NETWORK: Record = { ethereum: "https://api.etherscan.io/api?module=gastracker&action=gasoracle", goerli: "https://api-goerli.etherscan.io/api?module=gastracker&action=gasoracle", + sepolia: + "https://api-sepolia.etherscan.io/api?module=gastracker&action=gasoracle", rinkeby: "https://api-rinkeby.etherscan.io/api?module=gastracker&action=gasoracle", }; diff --git a/lib/types.ts b/lib/types.ts index 4b0d003..5ec30f6 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,4 +1,4 @@ -export type EthereumNetwork = "ethereum" | "goerli" | "rinkeby"; +export type EthereumNetwork = "ethereum" | "goerli" | "sepolia" | "rinkeby"; export type PolygonNetwork = "polygon" | "mumbai"; export type Network = EthereumNetwork | PolygonNetwork;