Skip to content

Commit

Permalink
Remove polygonGasStation from type
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Jan 2, 2025
1 parent 20461a6 commit 576dac6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
30 changes: 18 additions & 12 deletions src/gasPriceOracle/adapters/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<GasPriceEstimate> {
throw new Error();
}
}

export class MockPolygonGasStation extends PolygonGasStation {
public static BASE_FEE = parseUnits("12", 9);
public static PRIORITY_FEE = parseUnits("1", 9);
getFeeData(): Promise<GasPriceEstimate> {
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),
});
}
}
Expand All @@ -96,8 +95,15 @@ export async function gasStation(
provider: providers.Provider,
opts: GasPriceEstimateOptions
): Promise<GasPriceEstimate> {
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 {
Expand Down
3 changes: 0 additions & 3 deletions src/gasPriceOracle/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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 = {
Expand Down
20 changes: 8 additions & 12 deletions test/GasPriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,35 +181,30 @@ 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
// ethereum EIP1559 logic. There should be logic to ensure the priority fee gets floored at 30 gwei.
const { maxFeePerGas, maxPriorityFeePerGas } = await getGasPriceEstimate(provider, {
chainId,
baseFeeMultiplier,
polygonGasStation: mockPolygonGasStation,
});

const minPolygonPriorityFee = parseUnits("30", 9);
Expand All @@ -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"];
});
});

0 comments on commit 576dac6

Please sign in to comment.