Use provider-specified suggested priority fee when available, otherwise fallback onto existing logic of 1 gwei (#4463).

This commit is contained in:
Richard Moore 2023-11-27 06:04:52 -05:00
parent 4681b83d51
commit f8f11c754a
5 changed files with 60 additions and 29 deletions

@ -383,6 +383,8 @@ export type PerformActionRequest = {
} | {
method: "getLogs",
filter: PerformActionFilter
} | {
method: "getPriorityFee"
} | {
method: "getStorage",
address: string, position: bigint, blockTag: BlockTag
@ -906,14 +908,21 @@ export class AbstractProvider implements Provider {
const network = await this.getNetwork();
const getFeeDataFunc = async () => {
const { _block, gasPrice } = await resolveProperties({
const { _block, gasPrice, priorityFee } = await resolveProperties({
_block: this.#getBlock("latest", false),
gasPrice: ((async () => {
try {
const gasPrice = await this.#perform({ method: "getGasPrice" });
return getBigInt(gasPrice, "%response");
const value = await this.#perform({ method: "getGasPrice" });
return getBigInt(value, "%response");
} catch (error) { }
return null
})()),
priorityFee: ((async () => {
try {
const value = await this.#perform({ method: "getPriorityFee" });
return getBigInt(value, "%response");
} catch (error) { }
return null;
})())
});
@ -923,7 +932,7 @@ export class AbstractProvider implements Provider {
// These are the recommended EIP-1559 heuristics for fee data
const block = this._wrapBlock(_block, network);
if (block && block.baseFeePerGas) {
maxPriorityFeePerGas = BigInt("1000000000");
maxPriorityFeePerGas = (priorityFee != null) ? priorityFee: BigInt("1000000000");
maxFeePerGas = (block.baseFeePerGas * BN_2) + maxPriorityFeePerGas;
}

@ -346,26 +346,6 @@ 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 {
@ -409,11 +389,11 @@ function injectCommonNetworks(): void {
registerEth("kovan", 42, { ensNetwork: 42 });
registerEth("sepolia", 11155111, { ensNetwork: 11155111 });
registerEth("classic", 61, { });
registerEth("classicKotti", 6, { });
registerEth("arbitrum", 42161, {
ensNetwork: 1,
});
@ -444,9 +424,7 @@ function injectCommonNetworks(): void {
registerEth("optimism", 10, {
ensNetwork: 1,
plugins: [
getPriorityFeePlugin(BigInt("1000000"))
]
plugins: [ ]
});
registerEth("optimism-goerli", 420, { });

@ -26,6 +26,7 @@ import {
hexlify, toQuantity,
FetchRequest,
assert, assertArgument, isError,
// parseUnits,
toUtf8String
} from "../utils/index.js";
@ -421,6 +422,43 @@ export class EtherscanProvider extends AbstractProvider {
case "getGasPrice":
return this.fetch("proxy", { action: "eth_gasPrice" });
case "getPriorityFee":
// This is temporary until Etherscan completes support
if (this.network.name === "mainnet") {
return "1000000000";
} else if (this.network.name === "optimism") {
return "1000000";
} else {
throw new Error("fallback onto the AbstractProvider default");
}
/* Working with Etherscan to get this added:
try {
const test = await this.fetch("proxy", {
action: "eth_maxPriorityFeePerGas"
});
console.log(test);
return test;
} catch (e) {
console.log("DEBUG", e);
throw e;
}
*/
/* This might be safe; but due to rounding neither myself
or Etherscan are necessarily comfortable with this. :)
try {
const result = await this.fetch("gastracker", { action: "gasoracle" });
console.log(result);
const gasPrice = parseUnits(result.SafeGasPrice, "gwei");
const baseFee = parseUnits(result.suggestBaseFee, "gwei");
const priorityFee = gasPrice - baseFee;
if (priorityFee < 0) { throw new Error("negative priority fee; defer to abstract provider default"); }
return priorityFee;
} catch (error) {
console.log("DEBUG", error);
throw error;
}
*/
case "getBalance":
// Returns base-10 result
return this.fetch("account", {

@ -458,6 +458,8 @@ export class FallbackProvider extends AbstractProvider {
return await provider.getCode(req.address, req.blockTag);
case "getGasPrice":
return (await provider.getFeeData()).gasPrice;
case "getPriorityFee":
return (await provider.getFeeData()).maxPriorityFeePerGas;
case "getLogs":
return await provider.getLogs(req.filter);
case "getStorage":
@ -614,6 +616,7 @@ export class FallbackProvider extends AbstractProvider {
}
case "getGasPrice":
case "getPriorityFee":
case "estimateGas":
return getMedian(this.quorum, results);

@ -821,6 +821,9 @@ export abstract class JsonRpcApiProvider extends AbstractProvider {
case "getGasPrice":
return { method: "eth_gasPrice", args: [] };
case "getPriorityFee":
return { method: "eth_maxPriorityFeePerGas", args: [ ] };
case "getBalance":
return {
method: "eth_getBalance",