github.com/ethereum-optimism/optimism@v1.7.2/packages/fee-estimation/README.md (about)

     1  # @eth-optimism/fee-estimation
     2  
     3  Tools for estimating gas on OP chains
     4  
     5  - **Tip** the [specs file](./src/estimateFees.spec.ts) has usage examples of every method in this library.
     6  
     7  ## Overview
     8  
     9  This package is designed to provide an easy way to estimate gas on OP chains.
    10  
    11  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.
    12  As these tools add support for gas estimation natively this README will be updated with framework specific instructions.
    13  
    14  For more detailed information about gas fees on Optimism's Layer 2, you can visit their [official documentation](https://community.optimism.io/docs/developers/build/transaction-fees/#the-l2-execution-fee).
    15  
    16  ## GasPriceOracle contract
    17  
    18  - The l2 contract that can estimate l1Fees is called [GasPriceOracle](../contracts-bedrock/contracts/l2/GasPriceOracle.sol) contract. This library provides utils for interacting with it at a high level.
    19  - The GasPriceOracle is [deployed to Optimism](https://optimistic.etherscan.io/address/0x420000000000000000000000000000000000000F) and other OP chains at a predeployed address of `0x420000000000000000000000000000000000000F`
    20  
    21  This library provides a higher level abstraction over the gasPriceOracle
    22  
    23  ## Installation
    24  
    25  ```bash
    26  pnpm install @eth-optimism/fee-estimation
    27  ```
    28  
    29  ```bash
    30  npm install @eth-optimism/fee-estimation
    31  ```
    32  
    33  ```bash
    34  yarn add @eth-optimism/fee-estimation
    35  ```
    36  
    37  ### Basic Usage
    38  
    39  ```ts
    40  import { estimateFees } from '@eth-optimism/fee-estimation'
    41  import { optimistABI } from '@eth-optimism/contracts-ts'
    42  import { viemClient } from './viem-client'
    43  
    44  const optimistOwnerAddress =
    45    '0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const
    46  const tokenId = BigInt(optimistOwnerAddress)
    47  
    48  const fees = await estimateFees({
    49    client: viemClient,
    50    // If not using in viem can pass in this instead
    51    /*
    52    client: {
    53      chainId: 10,
    54      rpcUrl: 'https://mainnet.optimism.io',
    55    },
    56    */
    57    functionName: 'burn',
    58    abi: optimistABI,
    59    args: [tokenId],
    60    account: optimistOwnerAddress,
    61    to: '0x2335022c740d17c2837f9C884Bfe4fFdbf0A95D5',
    62  })
    63  ```
    64  
    65  ## API
    66  
    67  ### `estimateFees` function
    68  
    69  ```ts
    70  estimateFees(options: OracleTransactionParameters<TAbi, TFunctionName> & GasPriceOracleOptions & Omit<EstimateGasParameters, 'data'>): Promise<bigint>
    71  ```
    72  
    73  #### Parameters
    74  
    75  `options`: An object with the following fields:
    76  
    77  - `abi`: A JSON object ABI of contract.
    78  
    79  - `account`: A hex address of the account making the transaction.
    80  
    81  - `args`: Array of arguments to contract function. The types of this will be inferred from the ABI
    82  
    83  - `blockNumber`(optional): A BigInt representing the block number at which you want to estimate the fees.
    84  
    85  - `chainId`: An integer chain id.
    86  
    87  - `client`: An object with rpcUrl field, or an instance of a Viem PublicClient.
    88  
    89  - `functionName`: A string representing the function name for the transaction call data.
    90  
    91  - `maxFeePerGas`(optional): A BigInt representing the maximum fee per gas that the user is willing to pay.
    92  
    93  - `maxPriorityFeePerGas`(optional): A BigInt representing the maximum priority fee per gas that the user is willing to pay.
    94  
    95  - `to`: A hex address of the recipient of the transaction.
    96  
    97  - `value`(optional): A BigInt representing the value in wei sent along with the transaction.
    98  
    99  #### Returns
   100  
   101  A Promise that resolves to a BigInt representing the estimated fee in wei.
   102  
   103  ## Other methods
   104  
   105  This package also provides lower level methods for estimating gas
   106  
   107  ### getL2Client()
   108  
   109  This method returns a Layer 2 (L2) client that communicates with an L2 network.
   110  
   111  ```ts
   112  getL2Client(options: ClientOptions): PublicClient;
   113  ```
   114  
   115  #### Parameters
   116  
   117  - `options: ClientOptions` - The options required to initialize the L2 client.
   118  
   119  #### Returns
   120  
   121  - `PublicClient` - Returns a public client that can interact with the L2 network.
   122  
   123  #### Example
   124  
   125  ```ts
   126  const clientParams = {
   127    chainId: 10,
   128    rpcUrl: process.env.VITE_L2_RPC_URL ?? 'https://mainnet.optimism.io',
   129  } as const
   130  
   131  const client = getL2Client(clientParams)
   132  ```
   133  
   134  ---
   135  
   136  ### baseFee()
   137  
   138  Returns the base fee.
   139  
   140  ```ts
   141  baseFee({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   142  ```
   143  
   144  #### Parameters
   145  
   146  - `{ client, ...params }: GasPriceOracleOptions` - The options required to fetch the base fee.
   147  
   148  #### Returns
   149  
   150  - `Promise<bigint>` - Returns a promise that resolves to the base fee.
   151  
   152  #### Example
   153  
   154  ```ts
   155  const blockNumber = BigInt(106889079)
   156  const paramsWithClient = {
   157    client: clientParams,
   158    blockNumber,
   159  }
   160  const baseFeeValue = await baseFee(paramsWithClient)
   161  ```
   162  
   163  ---
   164  
   165  ### decimals()
   166  
   167  Returns the decimals used in the scalar.
   168  
   169  ```ts
   170  decimals({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   171  ```
   172  
   173  #### Parameters
   174  
   175  - `{ client, ...params }: GasPriceOracleOptions` - The options required to fetch the decimals.
   176  
   177  #### Returns
   178  
   179  - `Promise<bigint>` - Returns a promise that resolves to the decimals used in the scalar.
   180  
   181  #### Example
   182  
   183  ```ts
   184  const decimalsValue = await decimals(paramsWithClient)
   185  ```
   186  
   187  ---
   188  
   189  ### gasPrice()
   190  
   191  Returns the gas price.
   192  
   193  ```ts
   194  gasPrice({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   195  ```
   196  
   197  #### Parameters
   198  
   199  - `{ client, ...params }: GasPriceOracleOptions` - The options required to fetch the gas price.
   200  
   201  #### Returns
   202  
   203  - `Promise<bigint>` - Returns a promise that resolves to the gas price.
   204  
   205  #### Example
   206  
   207  ```ts
   208  const gasPriceValue = await gasPrice(paramsWithClient)
   209  ```
   210  
   211  ---
   212  
   213  ### getL1Fee()
   214  
   215  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.
   216  
   217  ```ts
   218  getL1Fee(data: Bytes, { client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   219  ```
   220  
   221  #### Parameters
   222  
   223  - `data: Bytes` - The transaction call data as a 0x-prefixed hex string.
   224  - `{ client, ...params }: GasPriceOracleOptions` - Optional lock options and provider options.
   225  
   226  #### Returns
   227  
   228  - `Promise<bigint>` - Returns a promise that resolves to the L1 portion of the fee.
   229  
   230  #### Example
   231  
   232  ```ts
   233  const data =
   234    '0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1'
   235  const l1FeeValue = await getL1Fee(data, paramsWithClient)
   236  ```
   237  
   238  ### getL1GasUsed()
   239  
   240  This method returns the amount of gas used on the L1 network for a given transaction.
   241  
   242  ```ts
   243  getL1GasUsed(data: Bytes, { client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   244  ```
   245  
   246  #### Parameters
   247  
   248  - `data: Bytes` - The transaction call data as a 0x-prefixed hex string.
   249  - `{ client, ...params }: GasPriceOracleOptions` - Optional lock options and provider options.
   250  
   251  #### Returns
   252  
   253  - `Promise<bigint>` - Returns a promise that resolves to the amount of gas used on the L1 network for the given transaction.
   254  
   255  #### Example
   256  
   257  ```ts
   258  const data =
   259    '0x5c19a95c00000000000000000000000046abfe1c972fca43766d6ad70e1c1df72f4bb4d1'
   260  const l1GasUsed = await getL1GasUsed(data, paramsWithClient)
   261  ```
   262  
   263  ---
   264  
   265  ### l1BaseFee()
   266  
   267  Returns the base fee on the L1 network.
   268  
   269  ```ts
   270  l1BaseFee({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   271  ```
   272  
   273  #### Parameters
   274  
   275  - `{ client, ...params }: GasPriceOracleOptions` - Optional lock options and provider options.
   276  
   277  #### Returns
   278  
   279  - `Promise<bigint>` - Returns a promise that resolves to the base fee on the L1 network.
   280  
   281  #### Example
   282  
   283  ```ts
   284  const l1BaseFeeValue = await l1BaseFee(paramsWithClient)
   285  ```
   286  
   287  ---
   288  
   289  ### overhead()
   290  
   291  Returns the overhead for the given transaction.
   292  
   293  ```ts
   294  overhead({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   295  ```
   296  
   297  #### Parameters
   298  
   299  - `{ client, ...params }: GasPriceOracleOptions` - Optional lock options and provider options.
   300  
   301  #### Returns
   302  
   303  - `Promise<bigint>` - Returns a promise that resolves to the overhead for the given transaction.
   304  
   305  #### Example
   306  
   307  ```ts
   308  const overheadValue = await overhead(paramsWithClient)
   309  ```
   310  
   311  ---
   312  
   313  ### scalar()
   314  
   315  Returns the scalar value for the gas estimation.
   316  
   317  ```ts
   318  scalar({ client, ...params }: GasPriceOracleOptions): Promise<bigint>;
   319  ```
   320  
   321  #### Parameters
   322  
   323  - `{ client, ...params }: GasPriceOracleOptions` - Optional lock options and provider options.
   324  
   325  #### Returns
   326  
   327  - `Promise<bigint>` - Returns a promise that resolves to the scalar value for the gas estimation.
   328  
   329  #### Example
   330  
   331  ```ts
   332  const scalarValue = await scalar(paramsWithClient)
   333  ```
   334  
   335  ---
   336  
   337  ### version()
   338  
   339  Returns the version of the fee estimation library.
   340  
   341  ```ts
   342  version({ client, ...params }: GasPriceOracleOptions): Promise<string>;
   343  ```
   344  
   345  #### Parameters
   346  
   347  - `{ client, ...params }: GasPriceOracleOptions` - Optional lock options and provider options.
   348  
   349  #### Returns
   350  
   351  - `Promise<string>` - Returns a promise that resolves to the version of the fee estimation library.
   352  
   353  #### Example
   354  
   355  ```ts
   356  const libraryVersion = await version(paramsWithClient)
   357  ```
   358  
   359  ---