Skip to content

Commit

Permalink
Added custom priority fee to Optimism chain (via telegram).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Aug 2, 2023
1 parent f3c46f2 commit ff80b04
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src.ts/providers/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,16 @@ function parseUnits(_value: number | string, decimals: number): bigint {
while (comps[1].length < decimals) { comps[1] += "0"; }

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

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

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

Expand All @@ -339,6 +342,26 @@ function getGasStationPlugin(url: string) {
});
}

// Used by Optimism for a custom priority fee
function getPriorityFeePlugin(maxPriorityFeePerGas: bigint) {
return new FetchUrlFeeDataNetworkPlugin("data:", async (fetchFeeData, provider, request) => {
const feeData = await fetchFeeData();

// This should always fail
if (feeData.maxFeePerGas == null || feeData.maxPriorityFeePerGas == null) {
return feeData;
}

// Compute the corrected baseFee to recompute the updated values
const baseFee = feeData.maxFeePerGas - feeData.maxPriorityFeePerGas;
return {
gasPrice: feeData.gasPrice,
maxFeePerGas: (baseFee + maxPriorityFeePerGas),
maxPriorityFeePerGas
};
});
}

// See: https://chainlist.org
let injected = false;
function injectCommonNetworks(): void {
Expand Down Expand Up @@ -413,6 +436,9 @@ function injectCommonNetworks(): void {

registerEth("optimism", 10, {
ensNetwork: 1,
plugins: [
getPriorityFeePlugin(BigInt("1000000"))
]
});
registerEth("optimism-goerli", 420, { });

Expand Down

0 comments on commit ff80b04

Please sign in to comment.