Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Dec 24, 2024
1 parent 4961d48 commit bda4843
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/GasPriceOracle.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
});

0 comments on commit bda4843

Please sign in to comment.