Skip to content

Commit

Permalink
Use provider-specified suggested priority fee when available, otherwi…
Browse files Browse the repository at this point in the history
…se fallback onto existing logic of 1 gwei (ethers-io#4463).
  • Loading branch information
ricmoo authored and Woodpile37 committed Jan 14, 2024
1 parent 034ef1a commit 0b45931
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
17 changes: 13 additions & 4 deletions src.ts/providers/abstract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ export type PerformActionRequest = {
} | {
method: "getLogs",
filter: PerformActionFilter
} | {
method: "getPriorityFee"
} | {
method: "getStorage",
address: string, position: bigint, blockTag: BlockTag
Expand Down Expand Up @@ -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;
})())
});

Expand All @@ -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;
}

Expand Down
28 changes: 3 additions & 25 deletions src.ts/providers/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -409,10 +389,10 @@ function injectCommonNetworks(): void {
registerEth("kovan", 42, { ensNetwork: 42 });
registerEth("sepolia", 11155111, { ensNetwork: 11155111 });

registerEth("classic", 61, { });
registerEth("classicKotti", 6, { });


registerEth("classic", 61, { });
registerEth("classicKotti", 6, { });

registerEth("arbitrum", 42161, {
ensNetwork: 1,
Expand Down Expand Up @@ -444,9 +424,7 @@ function injectCommonNetworks(): void {

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

Expand Down
38 changes: 38 additions & 0 deletions src.ts/providers/provider-etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
hexlify, toQuantity,
FetchRequest,
assert, assertArgument, isError,
// parseUnits,
toUtf8String
} from "../utils/index.js";

Expand Down Expand Up @@ -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", {
Expand Down
3 changes: 3 additions & 0 deletions src.ts/providers/provider-fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -614,6 +616,7 @@ export class FallbackProvider extends AbstractProvider {
}

case "getGasPrice":
case "getPriorityFee":
case "estimateGas":
return getMedian(this.quorum, results);

Expand Down
3 changes: 3 additions & 0 deletions src.ts/providers/provider-jsonrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0b45931

Please sign in to comment.