diff --git a/src/gasPriceOracle/adapters/polygon.ts b/src/gasPriceOracle/adapters/polygon.ts index b7d24b54..bfe52160 100644 --- a/src/gasPriceOracle/adapters/polygon.ts +++ b/src/gasPriceOracle/adapters/polygon.ts @@ -68,20 +68,19 @@ export class PolygonGasStation extends BaseHTTPAdapter { } } -export class MockPolygonGasStation extends PolygonGasStation { - constructor( - readonly baseFee: BigNumber, - readonly priorityFee: BigNumber, - readonly getFeeDataThrows = false - ) { - super(); +export class MockRevertingPolygonGasStation extends PolygonGasStation { + getFeeData(): Promise { + throw new Error(); } +} +export class MockPolygonGasStation extends PolygonGasStation { + public static BASE_FEE = parseUnits("12", 9); + public static PRIORITY_FEE = parseUnits("1", 9); getFeeData(): Promise { - if (this.getFeeDataThrows) throw new Error(); return Promise.resolve({ - maxPriorityFeePerGas: this.priorityFee, - maxFeePerGas: this.baseFee.add(this.priorityFee), + maxPriorityFeePerGas: MockPolygonGasStation.PRIORITY_FEE, + maxFeePerGas: MockPolygonGasStation.BASE_FEE.add(MockPolygonGasStation.PRIORITY_FEE), }); } } @@ -96,8 +95,15 @@ export async function gasStation( provider: providers.Provider, opts: GasPriceEstimateOptions ): Promise { - const { chainId, baseFeeMultiplier, polygonGasStation } = opts; - const gasStation = polygonGasStation ?? new PolygonGasStation({ chainId: chainId, timeout: 2000, retries: 0 }); + const { chainId, baseFeeMultiplier } = opts; + let gasStation: PolygonGasStation; + if (process.env.TEST_POLYGON_GAS_STATION === "true") { + gasStation = new MockPolygonGasStation(); + } else if (process.env.TEST_REVERTING_POLYGON_GAS_STATION === "true") { + gasStation = new MockRevertingPolygonGasStation(); + } else { + gasStation = new PolygonGasStation({ chainId: chainId, timeout: 2000, retries: 0 }); + } let maxPriorityFeePerGas: BigNumber; let maxFeePerGas: BigNumber; try { diff --git a/src/gasPriceOracle/oracle.ts b/src/gasPriceOracle/oracle.ts index 8a5388b0..d6aba829 100644 --- a/src/gasPriceOracle/oracle.ts +++ b/src/gasPriceOracle/oracle.ts @@ -10,7 +10,6 @@ import * as ethereum from "./adapters/ethereum"; import * as linea from "./adapters/linea"; import * as polygon from "./adapters/polygon"; import * as lineaViem from "./adapters/linea-viem"; -import { PolygonGasStation } from "./adapters/polygon"; export interface GasPriceEstimateOptions { // baseFeeMultiplier Multiplier applied to base fee for EIP1559 gas prices (or total fee for legacy). @@ -23,8 +22,6 @@ export interface GasPriceEstimateOptions { unsignedTx?: PopulatedTransaction; // transport Viem Transport object to use for querying gas fees used for testing. transport?: Transport; - // polygonGasStation Custom Polygon GasStation class used for testing. - polygonGasStation?: PolygonGasStation; } const GAS_PRICE_ESTIMATE_DEFAULTS = { diff --git a/test/GasPriceOracle.test.ts b/test/GasPriceOracle.test.ts index 4a3069cf..003072c4 100644 --- a/test/GasPriceOracle.test.ts +++ b/test/GasPriceOracle.test.ts @@ -181,27 +181,23 @@ describe("Gas Price Oracle", function () { expect(markedUpMaxPriorityFeePerGas).to.equal(0); }); it("Ethers Polygon GasStation", async function () { - const mockPolygonGasStation = new MockPolygonGasStation(stdLastBaseFeePerGas, stdMaxPriorityFeePerGas); const baseFeeMultiplier = 2.0; + process.env["TEST_POLYGON_GAS_STATION"] = "true"; const chainId = 137; - const { maxFeePerGas, maxPriorityFeePerGas } = await getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, - polygonGasStation: mockPolygonGasStation, }); - expect(maxFeePerGas).to.equal(stdLastBaseFeePerGas.mul(baseFeeMultiplier).add(stdMaxPriorityFeePerGas)); - expect(maxPriorityFeePerGas).to.equal(stdMaxPriorityFeePerGas); + expect(maxFeePerGas).to.equal( + MockPolygonGasStation.BASE_FEE.mul(baseFeeMultiplier).add(MockPolygonGasStation.PRIORITY_FEE) + ); + expect(maxPriorityFeePerGas).to.equal(MockPolygonGasStation.PRIORITY_FEE); + delete process.env["TEST_POLYGON_GAS_STATION"]; }); it("Ethers Polygon GasStation: Fallback", async function () { - const getFeeDataThrows = true; - const mockPolygonGasStation = new MockPolygonGasStation( - stdLastBaseFeePerGas, - stdMaxPriorityFeePerGas, - getFeeDataThrows - ); const baseFeeMultiplier = 2.0; + process.env["TEST_REVERTING_POLYGON_GAS_STATION"] = "true"; const chainId = 137; // If GasStation getFeeData throws, then the Polygon gas price oracle adapter should fallback to the @@ -209,7 +205,6 @@ describe("Gas Price Oracle", function () { const { maxFeePerGas, maxPriorityFeePerGas } = await getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, - polygonGasStation: mockPolygonGasStation, }); const minPolygonPriorityFee = parseUnits("30", 9); @@ -218,5 +213,6 @@ describe("Gas Price Oracle", function () { : minPolygonPriorityFee; expect(maxFeePerGas).to.equal(stdLastBaseFeePerGas.mul(baseFeeMultiplier).add(expectedPriorityFee)); expect(maxPriorityFeePerGas).to.equal(expectedPriorityFee); + delete process.env["TEST_REVERTING_POLYGON_GAS_STATION"]; }); });