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: support callback signature for fallback gas price option #23

Merged
merged 1 commit into from
Feb 23, 2023
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
73 changes: 72 additions & 1 deletion lib/networks/__tests__/getEthereumGasPrice.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";

import { mockFetch } from "./mockFetch";

Expand Down Expand Up @@ -282,4 +282,75 @@ describe("getEthereumGasPrice", () => {
},
});
});
it("should return the fallback gas price function return value if there an issue fetching from the Ethereum gas station", async () => {
mockFetch(undefined, { ok: false });

const gasPrice = 100;

const fallbackGasPrice = vi.fn(async () => {
return gasPrice;
});

const withFallbackFunctionValue = await getEthereumGasPrice("ethereum", {
fallbackGasPrice,
});

expect(fallbackGasPrice).toHaveBeenCalledTimes(1);

expect(withFallbackFunctionValue).toEqual({
low: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
average: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
high: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
asap: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
});
});
it("should return the fallback gas price function return value if there an error fetching from the Ethereum gas station", async () => {
mockFetch({
status: "0",
result: "Some error",
});

const gasPrice = 100;

const fallbackGasPrice = vi.fn(async () => {
return gasPrice;
});

const withFallbackFunctionValue = await getEthereumGasPrice("ethereum", {
fallbackGasPrice,
});

expect(fallbackGasPrice).toHaveBeenCalledTimes(1);

expect(withFallbackFunctionValue).toEqual({
low: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
average: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
high: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
asap: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
});
});
});
36 changes: 35 additions & 1 deletion lib/networks/__tests__/getPolygonGasPrice.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";

import { mockFetch } from "./mockFetch";

Expand Down Expand Up @@ -139,4 +139,38 @@ describe("getPolygonGasPrice", () => {
},
});
});
it("should return the fallback gas price function return value if there an issue fetching from the Polygon gas station", async () => {
mockFetch(undefined, { ok: false });

const gasPrice = 100;

const fallbackGasPrice = vi.fn(async () => {
return gasPrice;
});

const withFallbackFunctionValue = await getPolygonGasPrice("polygon", {
fallbackGasPrice,
});

expect(fallbackGasPrice).toHaveBeenCalledTimes(1);

expect(withFallbackFunctionValue).toEqual({
low: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
average: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
high: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
asap: {
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
});
});
});
23 changes: 14 additions & 9 deletions lib/networks/getEthereumGasPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface ResponseEthereumGasPrice {

interface EthereumOptions {
apiKey?: string;
fallbackGasPrice?: number;
fallbackGasPrice?: number | (() => Promise<number>);
}

export async function getEthereumGasPrice(
Expand Down Expand Up @@ -94,22 +94,27 @@ export async function getEthereumGasPrice(
asap: asapGasPriceLevel,
};
} catch (error) {
const gasPrice =
typeof fallbackGasPrice === "function"
? await fallbackGasPrice()
: fallbackGasPrice;

return {
low: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
average: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
high: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
asap: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
};
}
Expand Down
23 changes: 14 additions & 9 deletions lib/networks/getPolygonGasPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ResponsePolygonGasPrice {
}

interface PolygonOptions {
fallbackGasPrice?: number;
fallbackGasPrice?: number | (() => Promise<number>);
}

export async function getPolygonGasPrice(
Expand Down Expand Up @@ -66,22 +66,27 @@ export async function getPolygonGasPrice(
asap: asapGasPriceLevel,
};
} catch (error) {
const gasPrice =
typeof fallbackGasPrice === "function"
? await fallbackGasPrice()
: fallbackGasPrice;

return {
low: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
average: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
high: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
asap: {
maxPriorityFeePerGas: fallbackGasPrice,
maxFeePerGas: fallbackGasPrice,
maxPriorityFeePerGas: gasPrice,
maxFeePerGas: gasPrice,
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ export interface GasPrice {

export interface Options {
etherscanApiKey?: string;
fallbackGasPrice?: Partial<Record<Network, number>>;
fallbackGasPrice?: Partial<Record<Network, number | (() => Promise<number>)>>;
}