diff --git a/test/GasPriceOracle.test.ts b/test/GasPriceOracle.test.ts new file mode 100644 index 00000000..7a6fae91 --- /dev/null +++ b/test/GasPriceOracle.test.ts @@ -0,0 +1,61 @@ +// @note: This test is _not_ run automatically as part of git hooks or CI. +import dotenv from "dotenv"; +import { providers } from "ethers"; +import { getGasPriceEstimate } from "../src/gasPriceOracle"; +import { BigNumber, bnZero, parseUnits } from "../src/utils"; +import { expect, makeCustomTransport } from "../test/utils"; +dotenv.config({ path: ".env" }); + +const stdLastBaseFeePerGas = parseUnits("12", 9); +const stdMaxPriorityFeePerGas = parseUnits("1", 9); // EIP-1559 chains only +const chainIds = [1, 10, 137, 324, 8453, 42161, 534352]; + +const customTransport = makeCustomTransport({ stdLastBaseFeePerGas, stdMaxPriorityFeePerGas }); + +const provider = new providers.StaticJsonRpcProvider("https://eth.llamarpc.com"); + +describe("Gas Price Oracle", function () { + it("Gas Price Retrieval", async function () { + for (const chainId of chainIds) { + const chainKey = `NEW_GAS_PRICE_ORACLE_${chainId}`; + process.env[chainKey] = "true"; + const { maxFeePerGas, maxPriorityFeePerGas } = await getGasPriceEstimate(provider, { + chainId, + transport: customTransport, + }); + + expect(BigNumber.isBigNumber(maxFeePerGas)).to.be.true; + expect(BigNumber.isBigNumber(maxPriorityFeePerGas)).to.be.true; + + if (chainId === 137) { + // The Polygon gas station isn't mocked, so just ensure that the fees have a valid relationship. + expect(maxFeePerGas.gt(0)).to.be.true; + expect(maxPriorityFeePerGas.gt(0)).to.be.true; + expect(maxPriorityFeePerGas.lt(maxFeePerGas)).to.be.true; + } else if (chainId === 42161) { + // Arbitrum priority fees are refunded, so drop the priority fee from estimates. + // Expect a 1.2x multiplier on the last base fee. + expect(maxFeePerGas.eq(stdLastBaseFeePerGas.mul("120").div("100").add(1))).to.be.true; + expect(maxPriorityFeePerGas.eq(1)).to.be.true; + } else { + expect(maxFeePerGas.gt(bnZero)).to.be.true; + expect(maxPriorityFeePerGas.gt(bnZero)).to.be.true; + } + + delete process.env[chainKey]; + } + }); + it("Ethers: applies markup to maxFeePerGas", async function () { + for (const chainId of chainIds) { + const { maxFeePerGas: markedUpMaxFeePerGas, maxPriorityFeePerGas: markedUpMaxPriorityFeePerGas } = + await getGasPriceEstimate(provider, { chainId, baseFeeMultiplier: 2, transport: customTransport }); + const { maxFeePerGas, maxPriorityFeePerGas } = await getGasPriceEstimate(provider, { + chainId, + baseFeeMultiplier: 1, + transport: customTransport, + }); + expect(markedUpMaxFeePerGas.gt(maxFeePerGas)).to.be.true; + expect(markedUpMaxPriorityFeePerGas).to.equal(maxPriorityFeePerGas); + } + }); +});