Skip to content

Commit f8f11c7

Browse files
committed
Use provider-specified suggested priority fee when available, otherwise fallback onto existing logic of 1 gwei (#4463).
1 parent 4681b83 commit f8f11c7

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

src.ts/providers/abstract-provider.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,8 @@ export type PerformActionRequest = {
383383
} | {
384384
method: "getLogs",
385385
filter: PerformActionFilter
386+
} | {
387+
method: "getPriorityFee"
386388
} | {
387389
method: "getStorage",
388390
address: string, position: bigint, blockTag: BlockTag
@@ -906,14 +908,21 @@ export class AbstractProvider implements Provider {
906908
const network = await this.getNetwork();
907909

908910
const getFeeDataFunc = async () => {
909-
const { _block, gasPrice } = await resolveProperties({
911+
const { _block, gasPrice, priorityFee } = await resolveProperties({
910912
_block: this.#getBlock("latest", false),
911913
gasPrice: ((async () => {
912914
try {
913-
const gasPrice = await this.#perform({ method: "getGasPrice" });
914-
return getBigInt(gasPrice, "%response");
915+
const value = await this.#perform({ method: "getGasPrice" });
916+
return getBigInt(value, "%response");
915917
} catch (error) { }
916918
return null
919+
})()),
920+
priorityFee: ((async () => {
921+
try {
922+
const value = await this.#perform({ method: "getPriorityFee" });
923+
return getBigInt(value, "%response");
924+
} catch (error) { }
925+
return null;
917926
})())
918927
});
919928

@@ -923,7 +932,7 @@ export class AbstractProvider implements Provider {
923932
// These are the recommended EIP-1559 heuristics for fee data
924933
const block = this._wrapBlock(_block, network);
925934
if (block && block.baseFeePerGas) {
926-
maxPriorityFeePerGas = BigInt("1000000000");
935+
maxPriorityFeePerGas = (priorityFee != null) ? priorityFee: BigInt("1000000000");
927936
maxFeePerGas = (block.baseFeePerGas * BN_2) + maxPriorityFeePerGas;
928937
}
929938

src.ts/providers/network.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -346,26 +346,6 @@ function getGasStationPlugin(url: string) {
346346
});
347347
}
348348

349-
// Used by Optimism for a custom priority fee
350-
function getPriorityFeePlugin(maxPriorityFeePerGas: bigint) {
351-
return new FetchUrlFeeDataNetworkPlugin("data:", async (fetchFeeData, provider, request) => {
352-
const feeData = await fetchFeeData();
353-
354-
// This should always fail
355-
if (feeData.maxFeePerGas == null || feeData.maxPriorityFeePerGas == null) {
356-
return feeData;
357-
}
358-
359-
// Compute the corrected baseFee to recompute the updated values
360-
const baseFee = feeData.maxFeePerGas - feeData.maxPriorityFeePerGas;
361-
return {
362-
gasPrice: feeData.gasPrice,
363-
maxFeePerGas: (baseFee + maxPriorityFeePerGas),
364-
maxPriorityFeePerGas
365-
};
366-
});
367-
}
368-
369349
// See: https://chainlist.org
370350
let injected = false;
371351
function injectCommonNetworks(): void {
@@ -409,10 +389,10 @@ function injectCommonNetworks(): void {
409389
registerEth("kovan", 42, { ensNetwork: 42 });
410390
registerEth("sepolia", 11155111, { ensNetwork: 11155111 });
411391

412-
registerEth("classic", 61, { });
413-
registerEth("classicKotti", 6, { });
414392

415393

394+
registerEth("classic", 61, { });
395+
registerEth("classicKotti", 6, { });
416396

417397
registerEth("arbitrum", 42161, {
418398
ensNetwork: 1,
@@ -444,9 +424,7 @@ function injectCommonNetworks(): void {
444424

445425
registerEth("optimism", 10, {
446426
ensNetwork: 1,
447-
plugins: [
448-
getPriorityFeePlugin(BigInt("1000000"))
449-
]
427+
plugins: [ ]
450428
});
451429
registerEth("optimism-goerli", 420, { });
452430

src.ts/providers/provider-etherscan.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
hexlify, toQuantity,
2727
FetchRequest,
2828
assert, assertArgument, isError,
29+
// parseUnits,
2930
toUtf8String
3031
} from "../utils/index.js";
3132

@@ -421,6 +422,43 @@ export class EtherscanProvider extends AbstractProvider {
421422
case "getGasPrice":
422423
return this.fetch("proxy", { action: "eth_gasPrice" });
423424

425+
case "getPriorityFee":
426+
// This is temporary until Etherscan completes support
427+
if (this.network.name === "mainnet") {
428+
return "1000000000";
429+
} else if (this.network.name === "optimism") {
430+
return "1000000";
431+
} else {
432+
throw new Error("fallback onto the AbstractProvider default");
433+
}
434+
/* Working with Etherscan to get this added:
435+
try {
436+
const test = await this.fetch("proxy", {
437+
action: "eth_maxPriorityFeePerGas"
438+
});
439+
console.log(test);
440+
return test;
441+
} catch (e) {
442+
console.log("DEBUG", e);
443+
throw e;
444+
}
445+
*/
446+
/* This might be safe; but due to rounding neither myself
447+
or Etherscan are necessarily comfortable with this. :)
448+
try {
449+
const result = await this.fetch("gastracker", { action: "gasoracle" });
450+
console.log(result);
451+
const gasPrice = parseUnits(result.SafeGasPrice, "gwei");
452+
const baseFee = parseUnits(result.suggestBaseFee, "gwei");
453+
const priorityFee = gasPrice - baseFee;
454+
if (priorityFee < 0) { throw new Error("negative priority fee; defer to abstract provider default"); }
455+
return priorityFee;
456+
} catch (error) {
457+
console.log("DEBUG", error);
458+
throw error;
459+
}
460+
*/
461+
424462
case "getBalance":
425463
// Returns base-10 result
426464
return this.fetch("account", {

src.ts/providers/provider-fallback.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ export class FallbackProvider extends AbstractProvider {
458458
return await provider.getCode(req.address, req.blockTag);
459459
case "getGasPrice":
460460
return (await provider.getFeeData()).gasPrice;
461+
case "getPriorityFee":
462+
return (await provider.getFeeData()).maxPriorityFeePerGas;
461463
case "getLogs":
462464
return await provider.getLogs(req.filter);
463465
case "getStorage":
@@ -614,6 +616,7 @@ export class FallbackProvider extends AbstractProvider {
614616
}
615617

616618
case "getGasPrice":
619+
case "getPriorityFee":
617620
case "estimateGas":
618621
return getMedian(this.quorum, results);
619622

src.ts/providers/provider-jsonrpc.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
821821
case "getGasPrice":
822822
return { method: "eth_gasPrice", args: [] };
823823

824+
case "getPriorityFee":
825+
return { method: "eth_maxPriorityFeePerGas", args: [ ] };
826+
824827
case "getBalance":
825828
return {
826829
method: "eth_getBalance",

0 commit comments

Comments
 (0)