Skip to content

Commit

Permalink
feat: support zero gas fee chains
Browse files Browse the repository at this point in the history
Private chains like besu can be run in a zero gas fee mode. We support this by
altering the gas fee calculation to detect whether we are on such a chain and
return zeroed values.
  • Loading branch information
jimthematrix authored and kanej committed May 1, 2024
1 parent 82fb651 commit e54cb62
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/internal/execution/jsonrpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,15 @@ export class EIP1193JsonRpcClient implements JsonRpcClient {
// We prioritize EIP-1559 fees over legacy gasPrice fees, however,
// polygon (chainId 137) requires legacy gasPrice fees so we skip EIP-1559 logic in that case
if (latestBlock.baseFeePerGas !== undefined && chainId !== 137) {
if (latestBlock.baseFeePerGas === 0n) {
// Support zero gas fee chains, such as a private instances
// of blockchains using Besu.
return {
maxFeePerGas: 0n,
maxPriorityFeePerGas: 0n,
};
}

const maxPriorityFeePerGas = await this._resolveMaxFeePerGas();

// Logic copied from ethers v6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,37 @@ describe("JSON-RPC client", function () {
assert.equal(fees.gasPrice, 1n);
});

it("Should return zero gas fees when deploying to a network with a zero base fee per gas (e.g. private Besu instances)", async function () {
const besuClient = new EIP1193JsonRpcClient({
request: async (req) => {
if (req.method === "eth_chainId") {
return "0x42";
}

if (req.method === "eth_getBlockByNumber") {
return {
number: "0x0",
hash: "0x0",
baseFeePerGas: "0x0", // Set the base fee to zero
};
}

if (req.method === "eth_gasPrice") {
return "0x1";
}

throw new Error(`Unimplemented mock for ${req.method}`);
},
});

const fees = await besuClient.getNetworkFees();

assert.deepStrictEqual(fees, {
maxFeePerGas: 0n,
maxPriorityFeePerGas: 0n,
});
});

it("Should use the `maxPriorityFeePerGas` from the node if `eth_maxPriorityFeePerGas` is present (and there is no config)", async function () {
// TODO: Hardhat does not support `eth_maxPriorityFeePerGas` yet, when it does, this
// can be removed.
Expand Down

0 comments on commit e54cb62

Please sign in to comment.