Skip to content

Commit

Permalink
feat(GasPriceAdapter): Allow caller to set a minimum priority fee for…
Browse files Browse the repository at this point in the history
… eip1559Raw adapter (#821)
  • Loading branch information
nicholaspai authored Jan 9, 2025
1 parent 3d4d839 commit 40888c4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@across-protocol/sdk",
"author": "UMA Team",
"version": "3.4.6",
"version": "3.4.7",
"license": "AGPL-3.0",
"homepage": "https://docs.across.to/reference/sdk",
"files": [
Expand Down
8 changes: 6 additions & 2 deletions src/gasPriceOracle/adapters/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { providers } from "ethers";
import { BigNumber, bnZero, fixedPointAdjustment, getNetworkName } from "../../utils";
import { BigNumber, bnZero, fixedPointAdjustment, getNetworkName, parseUnits } from "../../utils";
import { GasPriceEstimate } from "../types";
import { gasPriceError } from "../util";
import { GasPriceEstimateOptions } from "../oracle";
Expand Down Expand Up @@ -43,7 +43,11 @@ export async function eip1559Raw(
provider.getBlock("pending"),
(provider as providers.JsonRpcProvider).send("eth_maxPriorityFeePerGas", []),
]);
const maxPriorityFeePerGas = BigNumber.from(_maxPriorityFeePerGas);
let maxPriorityFeePerGas = BigNumber.from(_maxPriorityFeePerGas);
const flooredPriorityFeePerGas = parseUnits(process.env[`MIN_PRIORITY_FEE_PER_GAS_${chainId}`] || "0", 9);
if (maxPriorityFeePerGas.lt(flooredPriorityFeePerGas)) {
maxPriorityFeePerGas = BigNumber.from(flooredPriorityFeePerGas);
}
assert(BigNumber.isBigNumber(baseFeePerGas), `No baseFeePerGas received on ${getNetworkName(chainId)}`);

const scaledPriorityFee = maxPriorityFeePerGas.mul(priorityFeeMultiplier).div(fixedPointAdjustment);
Expand Down
57 changes: 57 additions & 0 deletions test/GasPriceOracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,63 @@ describe("Gas Price Oracle", function () {
expect(markedUpMaxPriorityFeePerGas).to.equal(expectedMarkedUpPriorityFee);
delete process.env[chainKey];
});
it("Ethers EIP1559 Raw: min priority fee is respected", async function () {
const baseFeeMultiplier = toBNWei("2.0");
const priorityFeeMultiplier = toBNWei("1.5");
const minPriorityFeeScaler = "1.5";
const minPriorityFee = parseUnits(minPriorityFeeScaler, 9);
const chainId = 1;
const chainKey = `GAS_PRICE_EIP1559_RAW_${chainId}`;
const minPriorityFeeKey = `MIN_PRIORITY_FEE_PER_GAS_${chainId}`;
process.env[chainKey] = "true";
process.env[minPriorityFeeKey] = minPriorityFeeScaler;

const { maxFeePerGas: markedUpMaxFeePerGas, maxPriorityFeePerGas: markedUpMaxPriorityFeePerGas } =
await getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, priorityFeeMultiplier });

// Base fee should be multiplied by multiplier. Returned max fee includes priority fee
// so back it out before scaling.
const expectedMarkedUpPriorityFee = minPriorityFee.mul(priorityFeeMultiplier).div(fixedPointAdjustment);
const expectedMarkedUpMaxFeePerGas = stdLastBaseFeePerGas
.mul(baseFeeMultiplier)
.div(fixedPointAdjustment)
.add(expectedMarkedUpPriorityFee);
expect(markedUpMaxFeePerGas).to.equal(expectedMarkedUpMaxFeePerGas);

// Priority fees should be scaled.
expect(markedUpMaxPriorityFeePerGas).to.equal(expectedMarkedUpPriorityFee);
delete process.env[chainKey];
delete process.env[minPriorityFeeKey];
});
it("Ethers EIP1559 Raw: min priority fee is ignored", async function () {
const baseFeeMultiplier = toBNWei("2.0");
const priorityFeeMultiplier = toBNWei("1.5");
const minPriorityFeeScaler = "0.5";
const minPriorityFee = parseUnits(minPriorityFeeScaler, 9);
expect(minPriorityFee.lt(stdMaxPriorityFeePerGas)).to.be.true;
const chainId = 1;
const chainKey = `GAS_PRICE_EIP1559_RAW_${chainId}`;
const minPriorityFeeKey = `MIN_PRIORITY_FEE_PER_GAS_${chainId}`;
process.env[chainKey] = "true";
process.env[minPriorityFeeKey] = minPriorityFeeScaler.toString();

const { maxFeePerGas: markedUpMaxFeePerGas, maxPriorityFeePerGas: markedUpMaxPriorityFeePerGas } =
await getGasPriceEstimate(provider, { chainId, baseFeeMultiplier, priorityFeeMultiplier });

// Base fee should be multiplied by multiplier. Returned max fee includes priority fee
// so back it out before scaling.
const expectedMarkedUpPriorityFee = stdMaxPriorityFeePerGas.mul(priorityFeeMultiplier).div(fixedPointAdjustment);
const expectedMarkedUpMaxFeePerGas = stdLastBaseFeePerGas
.mul(baseFeeMultiplier)
.div(fixedPointAdjustment)
.add(expectedMarkedUpPriorityFee);
expect(markedUpMaxFeePerGas).to.equal(expectedMarkedUpMaxFeePerGas);

// Priority fees should be scaled.
expect(markedUpMaxPriorityFeePerGas).to.equal(expectedMarkedUpPriorityFee);
delete process.env[chainKey];
delete process.env[minPriorityFeeKey];
});
it("Ethers EIP1559 Bad", async function () {
// This test should return identical results to the Raw test but it makes different
// provider calls, so we're really testing that the expected provider functions are called.
Expand Down

0 comments on commit 40888c4

Please sign in to comment.