Skip to content

Commit

Permalink
improve(GasPriceOracle): Standardize gas price oracle usage (#807)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai authored Dec 25, 2024
1 parent a2e7b41 commit e16f9fe
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 266 deletions.
74 changes: 0 additions & 74 deletions e2e/oracle.e2e.ts

This file was deleted.

13 changes: 0 additions & 13 deletions src/gasPriceOracle/adapters/arbitrum-viem.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/gasPriceOracle/adapters/ethereum-viem.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/gasPriceOracle/adapters/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ export async function eip1559Raw(
const maxPriorityFeePerGas = BigNumber.from(_maxPriorityFeePerGas);
assert(BigNumber.isBigNumber(baseFeePerGas), `No baseFeePerGas received on ${getNetworkName(chainId)}`);

const scaledBaseFee = baseFeePerGas.mul(baseFeeMultiplier);
return {
maxFeePerGas: maxPriorityFeePerGas.add(baseFeePerGas).mul(baseFeeMultiplier),
maxFeePerGas: maxPriorityFeePerGas.add(scaledBaseFee),
maxPriorityFeePerGas,
};
}
Expand Down
34 changes: 28 additions & 6 deletions src/gasPriceOracle/adapters/linea-viem.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import { PublicClient } from "viem";
import { Address, Hex, PublicClient } from "viem";
import { estimateGas } from "viem/linea";
import { DEFAULT_SIMULATED_RELAYER_ADDRESS as account } from "../../constants";
import { InternalGasPriceEstimate } from "../types";
import { PopulatedTransaction } from "ethers";

export async function eip1559(provider: PublicClient, _chainId?: number): Promise<InternalGasPriceEstimate> {
const { baseFeePerGas, priorityFeePerGas } = await estimateGas(provider, {
account,
to: account,
value: BigInt(1),
/**
* @notice The Linea viem provider calls the linea_estimateGas RPC endpoint to estimate gas. Linea is unique
* in that the recommended fee per gas is hardcoded to 7 gwei while the priority fee is dynamic based on the
* compressed transaction size, layer 1 verification costs and capacity, gas price ratio between layer 1 and layer 2,
* the transaction's gas usage, the minimum gas price on layer 2,
* and a minimum margin (for error) for gas price estimation.
* @dev Because the Linea priority fee is more volatile than the base fee, the base fee multiplier will be applied
* to the priority fee.
* @param provider
* @param _chainId
* @param baseFeeMultiplier
* @param _unsignedTx
* @returns
*/
export async function eip1559(
provider: PublicClient,
_chainId: number,
baseFeeMultiplier: number,
_unsignedTx?: PopulatedTransaction
): Promise<InternalGasPriceEstimate> {
const { baseFeePerGas, priorityFeePerGas: _priorityFeePerGas } = await estimateGas(provider, {
account: (_unsignedTx?.from as Address) ?? account,
to: (_unsignedTx?.to as Address) ?? account,
value: BigInt(_unsignedTx?.value?.toString() ?? "1"),
data: (_unsignedTx?.data as Hex) ?? "0x",
});
const priorityFeePerGas = _priorityFeePerGas * BigInt(baseFeeMultiplier);

return {
maxFeePerGas: baseFeePerGas + priorityFeePerGas,
Expand Down
86 changes: 0 additions & 86 deletions src/gasPriceOracle/adapters/polygon-viem.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/gasPriceOracle/adapters/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export async function gasStation(
let maxFeePerGas: BigNumber;
try {
({ maxPriorityFeePerGas, maxFeePerGas } = await gasStation.getFeeData());
maxFeePerGas = maxFeePerGas.mul(baseFeeMultiplier);
const baseFeeMinusPriorityFee = maxFeePerGas.sub(maxPriorityFeePerGas);
const scaledBaseFee = baseFeeMinusPriorityFee.mul(baseFeeMultiplier);
maxFeePerGas = scaledBaseFee.add(maxPriorityFeePerGas);
} catch (err) {
// Fall back to the RPC provider. May be less accurate.
// @dev Don't incorporate multiplier until after catch statement
({ maxPriorityFeePerGas, maxFeePerGas } = await eip1559(provider, chainId, baseFeeMultiplier));

// Per the GasStation docs, the minimum priority fee on Polygon is 30 Gwei.
Expand Down
Loading

0 comments on commit e16f9fe

Please sign in to comment.