Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Sepolia support #8

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
23 changes: 23 additions & 0 deletions lib/__tests__/getNetworkGasPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion lib/getNetworkGasPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ export function getNetworkGasPrice(
network: Network,
options: Options = {}
): Promise<GasPrice> {
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],
Expand Down
47 changes: 47 additions & 0 deletions lib/networks/__tests__/getEthereumGasPrice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions lib/networks/getEthereumGasPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const GAS_STATION_URL_BY_NETWORK: Record<EthereumNetwork, string> = {
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",
};
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down