Skip to content

Commit ff80b04

Browse files
committed
Added custom priority fee to Optimism chain (via telegram).
1 parent f3c46f2 commit ff80b04

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src.ts/providers/network.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,16 @@ function parseUnits(_value: number | string, decimals: number): bigint {
311311
while (comps[1].length < decimals) { comps[1] += "0"; }
312312

313313
// Too many decimals and some non-zero ending, take the ceiling
314-
if (comps[1].length > 9 && !comps[1].substring(9).match(/^0+$/)) {
315-
comps[1] = (BigInt(comps[1].substring(0, 9)) + BigInt(1)).toString();
314+
if (comps[1].length > 9) {
315+
let frac = BigInt(comps[1].substring(0, 9));
316+
if (!comps[1].substring(9).match(/^0+$/)) { frac++; }
317+
comps[1] = frac.toString();
316318
}
317319

318320
return BigInt(comps[0] + comps[1]);
319321
}
320322

323+
// Used by Polygon to use a gas station for fee data
321324
function getGasStationPlugin(url: string) {
322325
return new FetchUrlFeeDataNetworkPlugin(url, async (fetchFeeData, provider, request) => {
323326

@@ -339,6 +342,26 @@ function getGasStationPlugin(url: string) {
339342
});
340343
}
341344

345+
// Used by Optimism for a custom priority fee
346+
function getPriorityFeePlugin(maxPriorityFeePerGas: bigint) {
347+
return new FetchUrlFeeDataNetworkPlugin("data:", async (fetchFeeData, provider, request) => {
348+
const feeData = await fetchFeeData();
349+
350+
// This should always fail
351+
if (feeData.maxFeePerGas == null || feeData.maxPriorityFeePerGas == null) {
352+
return feeData;
353+
}
354+
355+
// Compute the corrected baseFee to recompute the updated values
356+
const baseFee = feeData.maxFeePerGas - feeData.maxPriorityFeePerGas;
357+
return {
358+
gasPrice: feeData.gasPrice,
359+
maxFeePerGas: (baseFee + maxPriorityFeePerGas),
360+
maxPriorityFeePerGas
361+
};
362+
});
363+
}
364+
342365
// See: https://chainlist.org
343366
let injected = false;
344367
function injectCommonNetworks(): void {
@@ -413,6 +436,9 @@ function injectCommonNetworks(): void {
413436

414437
registerEth("optimism", 10, {
415438
ensNetwork: 1,
439+
plugins: [
440+
getPriorityFeePlugin(BigInt("1000000"))
441+
]
416442
});
417443
registerEth("optimism-goerli", 420, { });
418444

0 commit comments

Comments
 (0)