npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@eth-optimism/fee-estimation

v0.15.4

Published

Lightweight library for doing OP-Chain gas estimation

Downloads

9

Readme

@eth-optimism/fee-estimation

Tools for estimating gas on OP chains

  • Tip the specs file has usage examples of every method in this library.

Overview

This package is designed to provide an easy way to estimate gas on OP chains.

Fee estimation on OP-chains has both an l2 and l1 component. By default tools such as Viem, Wagmi, Ethers, and Web3.js do not support the l1 component. They will support this soon but in meantime, this library can help estimate fees for transactions, or act as a reference. As these tools add support for gas estimation natively this README will be updated with framework specific instructions.

For more detailed information about gas fees on Optimism's Layer 2, you can visit their official documentation.

GasPriceOracle contract

  • The l2 contract that can estimate l1Fees is called GasPriceOracle contract. This library provides utils for interacting with it at a high level.
  • The GasPriceOracle is deployed to Optimism and other OP chains at a predeployed address of 0x420000000000000000000000000000000000000F

This library provides a higher level abstraction over the gasPriceOracle

Installation

pnpm install @eth-optimism/fee-estimation
npm install @eth-optimism/fee-estimation
yarn add @eth-optimism/fee-estimation

Basic Usage

import { estimateFees } from '@eth-optimism/fee-estimation'
import { optimistABI } from '@eth-optimism/contracts-ts'
import { viemClient } from './viem-client'

const optimistOwnerAddress =
  '0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const
const tokenId = BigInt(optimistOwnerAddress)

const fees = await estimateFees({
  client: viemClient,
  // If not using in viem can pass in this instead
  /*
  client: {
    chainId: 10,
    rpcUrl: 'https://mainnet.optimism.io',
  },
  */
  functionName: 'burn',
  abi: optimistABI,
  args: [tokenId],
  account: optimistOwnerAddress,
  to: '0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5',
})

API

estimateFees function

estimateFees(options: OracleTransactionParameters<TAbi, TFunctionName> & GasPriceOracleOptions & Omit<EstimateGasParameters, 'data'>): Promise<bigint>

Parameters

options: An object with the following fields:

  • abi: A JSON object ABI of contract.

  • account: A hex address of the account making the transaction.

  • args: Array of arguments to contract function. The types of this will be inferred from the ABI

  • blockNumber(optional): A BigInt representing the block number at which you want to estimate the fees.

  • chainId: An integer chain id.

  • client: An object with rpcUrl field, or an instance of a Viem PublicClient.

  • functionName: A string representing the function name for the transaction call data.

  • maxFeePerGas(optional): A BigInt representing the maximum fee per gas that the user is willing to pay.

  • maxPriorityFeePerGas(optional): A BigInt representing the maximum priority fee per gas that the user is willing to pay.

  • to: A hex address of the recipient of the transaction.

  • value(optional): A BigInt representing the value in wei sent along with the transaction.

Returns

A Promise that resolves to a BigInt representing the estimated fee in wei.

Other methods

This package also provides lower level methods for estimating gas

getL2Client()

This method returns a Layer 2 (L2) client that communicates with an L2 network.

getL2Client(options: ClientOptions): PublicClient;

Parameters

  • options: ClientOptions - The options required to initialize the L2 client.

Returns

  • PublicClient - Returns a public client that can interact with the L2 network.

Example

const clientParams = {
  chainId: 10,
  rpcUrl: process.env.VITE_L2_RPC_URL ?? 'https://mainnet.optimism.io',
} as const

const client = getL2Client(clientParams)

baseFee()

Returns the base fee.

baseFee({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - The options required to fetch the base fee.

Returns

  • Promise<bigint> - Returns a promise that resolves to the base fee.

Example

const blockNumber = BigInt(106889079)
const paramsWithClient = {
  client: clientParams,
  blockNumber,
}
const baseFeeValue = await baseFee(paramsWithClient)

decimals()

Returns the decimals used in the scalar.

decimals({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - The options required to fetch the decimals.

Returns

  • Promise<bigint> - Returns a promise that resolves to the decimals used in the scalar.

Example

const decimalsValue = await decimals(paramsWithClient)

gasPrice()

Returns the gas price.

gasPrice({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - The options required to fetch the gas price.

Returns

  • Promise<bigint> - Returns a promise that resolves to the gas price.

Example

const gasPriceValue = await gasPrice(paramsWithClient)

getL1Fee()

Computes the L1 portion of the fee based on the size of the rlp encoded input transaction, the current L1 base fee, and the various dynamic parameters.

getL1Fee(data: Bytes, { client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • data: Bytes - The transaction call data as a 0x-prefixed hex string.
  • { client, ...params }: GasPriceOracleOptions - Optional lock options and provider options.

Returns

  • Promise<bigint> - Returns a promise that resolves to the L1 portion of the fee.

Example

const data =
  '0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1'
const l1FeeValue = await getL1Fee(data, paramsWithClient)

getL1GasUsed()

This method returns the amount of gas used on the L1 network for a given transaction.

getL1GasUsed(data: Bytes, { client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • data: Bytes - The transaction call data as a 0x-prefixed hex string.
  • { client, ...params }: GasPriceOracleOptions - Optional lock options and provider options.

Returns

  • Promise<bigint> - Returns a promise that resolves to the amount of gas used on the L1 network for the given transaction.

Example

const data =
  '0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1'
const l1GasUsed = await getL1GasUsed(data, paramsWithClient)

l1BaseFee()

Returns the base fee on the L1 network.

l1BaseFee({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - Optional lock options and provider options.

Returns

  • Promise<bigint> - Returns a promise that resolves to the base fee on the L1 network.

Example

const l1BaseFeeValue = await l1BaseFee(paramsWithClient)

overhead()

Returns the overhead for the given transaction.

overhead({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - Optional lock options and provider options.

Returns

  • Promise<bigint> - Returns a promise that resolves to the overhead for the given transaction.

Example

const overheadValue = await overhead(paramsWithClient)

scalar()

Returns the scalar value for the gas estimation.

scalar({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - Optional lock options and provider options.

Returns

  • Promise<bigint> - Returns a promise that resolves to the scalar value for the gas estimation.

Example

const scalarValue = await scalar(paramsWithClient)

version()

Returns the version of the fee estimation library.

version({ client, ...params }: GasPriceOracleOptions): Promise<string>;

Parameters

  • { client, ...params }: GasPriceOracleOptions - Optional lock options and provider options.

Returns

  • Promise<string> - Returns a promise that resolves to the version of the fee estimation library.

Example

const libraryVersion = await version(paramsWithClient)