Commit 7ac9da29 authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub

Merge branch 'develop' into feat/multichain-upgrade

parents af163a33 dfcb144a
---
'@eth-optimism/core-utils': patch
---
Upgraded npm dependencies to latest
---
'@eth-optimism/chain-mon': patch
---
Upgraded npm dependencies to latest
......@@ -115,20 +115,23 @@ jobs:
name: Restore PNPM Package Cache
keys:
- pnpm-packages-v2-{{ checksum "pnpm-lock.yaml" }}
- run:
name: Install dependencies
command: pnpm install --frozen-lockfile
# Fetch node_modules into the pnpm store
# This will cache node_modules based on pnpm-lock so other steps can instantly install them with `pnpm install --prefer-offline`
# --prefer-offline installs node_modules instantly by just reading from cache if it exists rather than fetching from network
# when installing node_modules pnpm simply adds symlinks instead of copying the files which is why it is pretty much instant to run --prefer-offline
# this allows a caching strategy of only checking pnpm-lockfile so we don't have to keep it in sync with our packages
# For more information see https://pnpm.io/cli/fetch
- run:
name: Fetch dependencies
command: pnpm fetch --frozen-lockfile --prefer-offline
- save_cache:
name: Save PNPM Package Cache
key: pnpm-packages-v2-{{ checksum "pnpm-lock.yaml" }}
paths:
- "node_modules"
- "packages/chain-mon/node_modules"
- "packages/common-ts/node_modules"
- "packages/contracts-bedrock/node_modules"
- "packages/core-utils/node_modules"
- "packages/sdk/node_modules"
- "packages/contracts-ts/node_modules"
- run:
name: Install dependencies
command: pnpm install --frozen-lockfile --offline
- run:
name: print forge version
command: forge --version
......@@ -469,6 +472,10 @@ jobs:
name: Restore PNPM Package Cache
keys:
- pnpm-packages-v2-{{ checksum "pnpm-lock.yaml" }}
# populate node modules from the cache
- run:
name: Install dependencies
command: pnpm install --frozen-lockfile --prefer-offline
- check-changed:
patterns: contracts-bedrock
- run:
......@@ -476,51 +483,6 @@ jobs:
command: pnpm validate-spacers
working_directory: packages/contracts-bedrock
bedrock-echidna-build:
docker:
- image: us-docker.pkg.dev/oplabs-tools-artifacts/images/ci-builder:latest
steps:
- checkout
- attach_workspace: { at: "." }
- check-changed:
patterns: contracts-bedrock
- run:
name: Compile with metadata hash
command: pnpm clean && pnpm build:with-metadata
working_directory: packages/contracts-bedrock
- persist_to_workspace:
root: .
paths:
- "node_modules"
- packages/contracts-bedrock
bedrock-echidna-run:
docker:
- image: us-docker.pkg.dev/oplabs-tools-artifacts/images/ci-builder:latest
parameters:
echidna_target:
description: Which echidna fuzz contract to run
type: string
size:
description: Custom resource class size for the run
type: string
default: large
resource_class: <<parameters.size>>
steps:
- checkout
- attach_workspace: { at: "." }
- restore_cache:
name: Restore PNPM Package Cache
keys:
- pnpm-packages-v2-{{ checksum "pnpm-lock.yaml" }}
- check-changed:
patterns: contracts-bedrock
- run:
name: Echidna Fuzz <<parameters.echidna_target>>
command: pnpm echidna:<<parameters.echidna_target>>
working_directory: packages/contracts-bedrock
no_output_timeout: 15m
op-bindings-build:
docker:
- image: us-docker.pkg.dev/oplabs-tools-artifacts/images/ci-builder:latest
......@@ -556,6 +518,10 @@ jobs:
- pnpm-packages-v2-{{ checksum "pnpm-lock.yaml" }}
- check-changed:
patterns: <<parameters.package_name>>,<<parameters.dependencies>>
# populate node modules from the cache
- run:
name: Install dependencies
command: pnpm install --frozen-lockfile --prefer-offline
- run:
name: Lint
command: pnpm lint && git diff --exit-code
......@@ -581,6 +547,10 @@ jobs:
- pnpm-packages-v2-{{ checksum "pnpm.lock.yaml" }}
- check-changed:
patterns: sdk,contracts-bedrock,contracts
# populate node modules from the cache
- run:
name: Install dependencies
command: pnpm install --frozen-lockfile --prefer-offline
- run:
name: Check generated and build
command: pnpm generate:check
......@@ -599,6 +569,10 @@ jobs:
- pnpm-packages-v2-{{ checksum "pnpm.lock.yaml" }}
- check-changed:
patterns: sdk,contracts-bedrock,contracts
# populate node modules from the cache
- run:
name: Install dependencies
command: pnpm install --frozen-lockfile --prefer-offline
- run:
name: anvil-l1
background: true
......@@ -643,22 +617,13 @@ jobs:
- checkout
- check-changed:
patterns: specs/(.*)\.md$
# TODO remove me after ci builder updated
- run:
name: Install pnpm package manager
command: |
npm i pnpm --global
# TODO remove me after ci builder updated
# A github dep clones-with-immutable-args is installed via github
# packages installed via npm via github automatically run postpack scripts
# their postpack script happens to use yarn so we need it here
- run:
name: Install yarn package manager
command: |
npm i yarn@1 --global
- run:
name: pnpm dev deps
command: pnpm install
command: pnpm install --frozen-lockfile --prefer-offline
- run:
name: specs toc
command: pnpm lint:specs:toc && git diff --exit-code ./specs
......@@ -671,7 +636,6 @@ jobs:
image: ubuntu-2204:2022.07.1
steps:
- checkout
# TODO remove me after ci builder updated
- run:
name: Install pnpm package manager
command: |
......@@ -679,7 +643,7 @@ jobs:
- run:
name: Install node_modules
command: |
pnpm install
pnpm install --frozen-lockfile --prefer-offline
- run:
name: Lint check
command: |
......@@ -981,7 +945,7 @@ jobs:
- run:
name: Install and build
command: |
pnpm install && pnpm build
pnpm install --frozen-lockfile --prefer-offline && pnpm build
- run:
name: generate cannon prestate
command: make cannon-prestate
......
......@@ -42,7 +42,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Fetch tags
run: git fetch --tags origin
run: git fetch --tags origin --force
- name: Setup Python 3.10
uses: actions/setup-python@v4
with:
......
......@@ -11,6 +11,7 @@ require (
github.com/ethereum/go-ethereum v1.12.0
github.com/fsnotify/fsnotify v1.6.0
github.com/go-chi/chi/v5 v5.0.10
github.com/go-chi/docgen v1.2.0
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb
github.com/google/go-cmp v0.5.9
github.com/google/gofuzz v1.2.1-0.20220503160820-4a35382e8fc8
......
......@@ -192,8 +192,12 @@ github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclKsC9YodN5RgxqK/VD9HM9JsCSh7rNhMZE98=
github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs=
github.com/go-chi/chi/v5 v5.0.1/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-chi/docgen v1.2.0 h1:da0Nq2PKU9W9pSOTUfVrKI1vIgTGpauo9cfh4Iwivek=
github.com/go-chi/docgen v1.2.0/go.mod h1:G9W0G551cs2BFMSn/cnGwX+JBHEloAgo17MBhyrnhPI=
github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
......
......@@ -21,7 +21,7 @@ func NewApi(bv database.BridgeTransfersView, logger log.Logger) *Api {
r := chi.NewRouter()
h := routes.NewRoutes(logger, bv)
h := routes.NewRoutes(logger, bv, r)
api := &Api{Router: r}
......
package routes
import (
"net/http"
"github.com/go-chi/docgen"
)
func (h Routes) DocsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
docs := docgen.MarkdownRoutesDoc(h.Router, docgen.MarkdownOpts{
ProjectPath: "github.com/ethereum-optimism/optimism/indexer",
// Intro text included at the top of the generated markdown file.
Intro: "Generated documentation for Optimism indexer",
})
_, err := w.Write([]byte(docs))
if err != nil {
h.Logger.Error("error writing docs", "err", err)
http.Error(w, "Internal server error fetching docs", http.StatusInternalServerError)
}
}
......@@ -3,16 +3,19 @@ package routes
import (
"github.com/ethereum-optimism/optimism/indexer/database"
"github.com/ethereum/go-ethereum/log"
"github.com/go-chi/chi/v5"
)
type Routes struct {
Logger log.Logger
BridgeTransfersView database.BridgeTransfersView
Router *chi.Mux
}
func NewRoutes(logger log.Logger, bv database.BridgeTransfersView) Routes {
func NewRoutes(logger log.Logger, bv database.BridgeTransfersView, r *chi.Mux) Routes {
return Routes{
Logger: logger,
BridgeTransfersView: bv,
Router: r,
}
}
......@@ -25,11 +25,7 @@ type Cli struct {
}
func runIndexer(ctx *cli.Context) error {
logger := log.NewLogger(log.CLIConfig{
Level: "warn",
Color: false,
Format: "terminal",
})
logger := log.NewLogger(log.ReadCLIConfig(ctx))
configPath := ctx.String(ConfigFlag.Name)
cfg, err := config.LoadConfig(logger, configPath)
......@@ -38,8 +34,6 @@ func runIndexer(ctx *cli.Context) error {
return err
}
logger = log.NewLogger(cfg.Logger)
db, err := database.NewDB(cfg.DB)
if err != nil {
......
......@@ -6,8 +6,6 @@ import (
"reflect"
"github.com/BurntSushi/toml"
"github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/common"
geth_log "github.com/ethereum/go-ethereum/log"
"github.com/joho/godotenv"
......@@ -22,7 +20,6 @@ type Config struct {
DB DBConfig
API APIConfig
Metrics MetricsConfig
Logger log.CLIConfig
}
// fetch this via onchain config from RPCsConfig and remove from config in future
......
......@@ -101,7 +101,7 @@ services:
gateway-frontend:
command: pnpm nx start @gateway/frontend --host 0.0.0.0 --port 5173
# Change tag to `latest` after https://github.com/ethereum-optimism/gateway/pull/2541 merges
image: ethereumoptimism/gateway-frontend:0687c408e4f85cbe81acf7a197e861df8c7282df
image: ethereumoptimism/gateway-frontend:latest
ports:
- 5173:5173
healthcheck:
......
......@@ -16,7 +16,6 @@ import (
op_e2e "github.com/ethereum-optimism/optimism/op-e2e"
"github.com/ethereum-optimism/optimism/op-node/testlog"
op_log "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/log"
......@@ -60,9 +59,6 @@ func createE2ETestSuite(t *testing.T) E2ETestSuite {
// Indexer Configuration and Start
indexerCfg := config.Config{
Logger: op_log.CLIConfig{
Level: "warn",
},
DB: config.DBConfig{
Host: "127.0.0.1",
Port: 5432,
......
......@@ -22,11 +22,3 @@ port = 8080
host = "127.0.0.1"
port = 7300
[logger]
# Log level: trace, debug, info, warn, error, crit. Capitals are accepted too.
level = "info"
# Color the log output. Defaults to true if terminal is detected.
color = true
# Format the log output. Supported formats: 'text', 'terminal', 'logfmt', 'json', 'json-pretty'
format = "terminal"
......@@ -38,21 +38,21 @@
"url": "https://github.com/ethereum-optimism/optimism.git"
},
"dependencies": {
"@eth-optimism/common-ts": "0.8.3",
"@eth-optimism/contracts-bedrock": "0.16.0",
"@eth-optimism/common-ts": "workspace:*",
"@eth-optimism/contracts-bedrock": "workspace:*",
"@eth-optimism/contracts-periphery": "1.0.8",
"@eth-optimism/core-utils": "0.12.2",
"@eth-optimism/sdk": "3.1.0",
"@eth-optimism/core-utils": "workspace:*",
"@eth-optimism/sdk": "workspace:*",
"@types/dateformat": "^5.0.0",
"chai-as-promised": "^7.1.1",
"dateformat": "^4.5.1",
"dotenv": "^16.1.4",
"ethers": "^5.7.0"
"dotenv": "^16.3.1",
"ethers": "^5.7.2"
},
"devDependencies": {
"@ethersproject/abstract-provider": "^5.7.0",
"@nomiclabs/hardhat-ethers": "^2.0.6",
"@nomiclabs/hardhat-waffle": "^2.0.3",
"@nomiclabs/hardhat-waffle": "^2.0.6",
"hardhat": "^2.9.6",
"ts-node": "^10.9.1",
"tsx": "^3.12.7"
......
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
"skipLibCheck": true
},
"include": [
"package.json",
......
......@@ -52,5 +52,7 @@
"l2GenesisRegolithTimeOffset": "0x0",
"eip1559Denominator": 50,
"eip1559Elasticity": 10
"eip1559Elasticity": 10,
"systemConfigStartBlock": 0
}
......@@ -65,13 +65,20 @@ abstract contract Deployer is Script {
/// bytes32(uint256(keccak256('eip1967.proxy.admin')) - 1)
bytes32 internal constant OWNER_KEY = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
/// @notice Create the global variables and set up the filesystem
/// @notice Create the global variables and set up the filesystem.
/// Forge script will create a file where the prefix is the
/// name of the function that runs with the suffix `-latest.json`.
/// By default, `run()` is called. Allow the user to use the SIG
/// env var to specify what function signature was called so that
/// the `sync()` method can be used to create hardhat deploy style
/// artifacts.
function setUp() public virtual {
string memory root = vm.projectRoot();
deployScript = vm.envOr("DEPLOY_SCRIPT", name());
deploymentContext = _getDeploymentContext();
string memory deployFile = vm.envOr("DEPLOY_FILE", string("run-latest.json"));
string memory sig = vm.envOr("SIG", string("run"));
string memory deployFile = vm.envOr("DEPLOY_FILE", string.concat(sig, "-latest.json"));
uint256 chainId = vm.envOr("CHAIN_ID", block.chainid);
deployPath = string.concat(root, "/broadcast/", deployScript, ".s.sol/", vm.toString(chainId), "/", deployFile);
......
......@@ -43,13 +43,13 @@
"@ethersproject/keccak256": "^5.7.0",
"@ethersproject/properties": "^5.7.0",
"@ethersproject/rlp": "^5.7.0",
"@ethersproject/web": "^5.7.0",
"chai": "^4.3.4",
"ethers": "^5.7.0",
"@ethersproject/web": "^5.7.1",
"chai": "^4.3.7",
"ethers": "^5.7.2",
"node-fetch": "^2.6.7"
},
"devDependencies": {
"@types/node": "^20.5.0",
"mocha": "^10.0.0"
"mocha": "^10.2.0"
}
}
......@@ -70,7 +70,7 @@ export class Etherscan {
url.searchParams.append('apikey', this.apiKey)
const response = await fetch(url)
const result = await response.json()
return result.result[0]
return (result as { result: number[] }).result[0]
}
public async getContractABI(address: string): Promise<any> {
......
artifacts
cache
typechain
.deps
.envrc
.env
/dist/
module.exports = {
...require('../../.prettierrc.js'),
}
MIT License
Copyright (c) 2023 Optimism
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# @eth-optimism/web3js-plugin
This web3.js plugin adds utility functions for estimating L1 and L2 gas for OP chains by wrapping the [GasPriceOracle](../contracts-bedrock/contracts/l2/GasPriceOracle.sol) contract
The GasPriceOracle is [deployed to Optimism](https://optimistic.etherscan.io/address/0x420000000000000000000000000000000000000F) and other OP chains at a predeployed address of `0x420000000000000000000000000000000000000F`
For more detailed information about gas fees on Optimism's Layer 2, you can visit the [official documentation](https://community.optimism.io/docs/developers/build/transaction-fees/#the-l2-execution-fee)
## Installation
This plugin is intended to be [registered](https://docs.web3js.org/guides/web3_plugin_guide/plugin_users#registering-the-plugin) onto an instance of `Web3`. It has a [peerDependency](https://nodejs.org/es/blog/npm/peer-dependencies) of `web3` version `4.x`, so make sure you have that latest version of `web3` installed for your project before installing the plugin
### Installing the Plugin
```bash
pnpm install @eth-optimism/web3js-plugin
```
```bash
npm install @eth-optimism/web3js-plugin
```
```bash
yarn add @eth-optimism/web3js-plugin
```
### Registering the Plugin
```typescript
import Web3 from 'web3'
import OptimismFeeEstimationPlugin from '@eth-optimism/web3js-plugin'
const web3 = new Web3('http://yourProvider.com')
web3.registerPlugin(new OptimismFeeEstimationPlugin())
```
You will now have access to the following functions under the `op` namespace, i.e. `web3.op.someMethod`
## API
| Function Name | Returns |
| ---------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [estimateFees](#estimatefees) | The combined estimated L1 and L2 fees for a transaction |
| [getL1Fee](#getl1fee) | The L1 portion of the fee based on the size of the [RLP](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/) encoded transaction, the current L1 base fee, and other various dynamic parameters |
| [getL2Fee](#getl2fee) | The L2 portion of the fee based on the simulated execution of the provided transaction and current `gasPrice` |
| [getBaseFee](#getbasefee) | The current L2 base fee |
| [getDecimals](#getdecimals) | The decimals used in the scalar |
| [getGasPrice](#getgasprice) | The current L2 gas price |
| [getL1GasUsed](#getl1gasused) | The amount of L1 gas estimated to be used to execute a transaction |
| [getL1BaseFee](#getdegetl1basefeecimals) | The L1 base fee |
| [getOverhead](#getoverhead) | The current overhead |
| [getScalar](#getscalar) | The current fee scalar |
| [getVersion](#getversion) | The current version of `GasPriceOracle` |
---
### `estimateFees`
Computes the total (L1 + L2) fee estimate to execute a transaction
```typescript
async estimateFees(transaction: Transaction, returnFormat?: ReturnFormat)
```
#### Parameters
- `transaction: Transaction` - An unsigned web3.js [transaction](https://docs.web3js.org/api/web3-types/interface/Transaction) object
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The estimated total fee as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
import Web3 from 'web3'
import {
l2StandardBridgeABI,
l2StandardBridgeAddress,
} from '@eth-optimism/contracts-ts'
const web3 = new Web3('https://mainnet.optimism.io')
const l2BridgeContract = new web3.eth.Contract(
l2StandardBridgeABI,
optimistAddress[420]
)
const encodedWithdrawMethod = l2BridgeContract.methods
.withdraw(
// l2 token address
'0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000',
// amount
Web3.utils.toWei('0.00000001', 'ether'),
// l1 gas
0,
// extra data
'0x00'
)
.encodeABI()
const totalFee = await web3.op.estimateFees({
chainId: 10,
data: encodedWithdrawMethod,
value: Web3.utils.toWei('0.00000001', 'ether'),
type: 2,
to: '0x420000000000000000000000000000000000000F',
from: '0x6387a88a199120aD52Dd9742C7430847d3cB2CD4',
maxFeePerGas: Web3.utils.toWei('0.2', 'gwei'),
maxPriorityFeePerGas: Web3.utils.toWei('0.1', 'gwei'),
})
console.log(totalFee) // 26608988767659n
```
##### Formatting Response as a Hex String
```typescript
import Web3 from 'web3'
import {
l2StandardBridgeABI,
l2StandardBridgeAddress,
} from '@eth-optimism/contracts-ts'
const web3 = new Web3('https://mainnet.optimism.io')
const l2BridgeContract = new web3.eth.Contract(
l2StandardBridgeABI,
optimistAddress[420]
)
const encodedWithdrawMethod = l2BridgeContract.methods
.withdraw(
// l2 token address
'0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000',
// amount
Web3.utils.toWei('0.00000001', 'ether'),
// l1 gas
0,
// extra data
'0x00'
)
.encodeABI()
const totalFee = await web3.op.estimateFees(
{
chainId: 10,
data: encodedWithdrawMethod,
value: Web3.utils.toWei('0.00000001', 'ether'),
type: 2,
to: '0x420000000000000000000000000000000000000F',
from: '0x6387a88a199120aD52Dd9742C7430847d3cB2CD4',
maxFeePerGas: Web3.utils.toWei('0.2', 'gwei'),
maxPriorityFeePerGas: Web3.utils.toWei('0.1', 'gwei'),
},
{ number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }
)
console.log(totalFee) // 0x18336352c5ab
```
### `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
```typescript
async getL1Fee(transaction: Transaction, returnFormat?: ReturnFormat)
```
#### Parameters
- `transaction: Transaction` - An unsigned web3.js [transaction](https://docs.web3js.org/api/web3-types/interface/Transaction) object
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The estimated L1 fee as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
import { Contract } from 'web3'
import { optimistABI, optimistAddress } from '@eth-optimism/contracts-ts'
const optimistContract = new Contract(optimistABI, optimistAddress[420])
const encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
const l1Fee = await web3.op.getL1Fee({
chainId: 10,
data: encodedBurnMethod,
type: 2,
})
console.log(l1Fee) // 18589035222172n
```
##### Formatting Response as a Hex String
```typescript
import { Contract } from 'web3'
import { optimistABI, optimistAddress } from '@eth-optimism/contracts-ts'
const optimistContract = new Contract(optimistABI, optimistAddress[420])
const encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
const l1Fee = await web3.op.getL1Fee(
{
chainId: 10,
data: encodedBurnMethod,
type: 2,
},
{ number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }
)
console.log(l1Fee) // 0x10e818d7549c
```
### `getL2Fee`
Retrieves the amount of L2 gas estimated to execute `transaction`
```typescript
async getL2Fee(transaction: Transaction, returnFormat?: ReturnFormat)
```
#### Parameters
- `transaction: Transaction` - An unsigned web3.js [transaction](https://docs.web3js.org/api/web3-types/interface/Transaction) object
- `options?: { blockNumber?: BlockNumberOrTag, returnFormat?: ReturnFormat }` - An optional object with properties:
- `blockNumber?: BlockNumberOrTag` - Specifies what block to use for gas estimation. Can be either:
- **Note** Specifying a block to estimate L2 gas for is currently not working
- A web3.js [Numbers](https://docs.web3js.org/api/web3-types#Numbers)
- A web3.js [BlockTags](https://docs.web3js.org/api/web3-types/enum/BlockTags)
- If not provided, `BlockTags.LATEST` is used
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The estimated total fee as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
import { Contract } from 'web3'
import { optimistABI, optimistAddress } from '@eth-optimism/contracts-ts'
const optimistContract = new Contract(optimistABI, optimistAddress[420])
const encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
const l2Fee = await web3.op.getL2Fee({
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
to: optimistAddress[420],
from: '0x77194aa25a06f932c10c0f25090f3046af2c85a6',
})
console.log(l2Fee) // 2659500n
```
##### Formatting Response as a Hex String
```typescript
import { Contract } from 'web3'
import { optimistABI, optimistAddress } from '@eth-optimism/contracts-ts'
const optimistContract = new Contract(optimistABI, optimistAddress[420])
const encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
const l2Fee = await web3.op.getL2Fee(
{
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
to: optimistAddress[420],
from: '0x77194aa25a06f932c10c0f25090f3046af2c85a6',
},
{
returnFormat: { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX },
}
)
console.log(l2Fee) // 0x2894ac
```
### `getBaseFee`
Retrieves the current L2 base fee
```typescript
async getBaseFee(returnFormat?: ReturnFormat)
```
#### Parameters
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The L2 base fee as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
const baseFee = await web3.op.getBaseFee()
console.log(baseFee) // 68n
```
##### Formatting Response as a Hex String
```typescript
const baseFee = await web3.op.getBaseFee({
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
})
console.log(baseFee) // 0x44
```
### `getDecimals`
Retrieves the decimals used in the scalar
```typescript
async getDecimals(returnFormat?: ReturnFormat)
```
#### Parameters
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The number of decimals as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
const decimals = await web3.op.getDecimals()
console.log(decimals) // 6n
```
##### Formatting Response as a Hex String
```typescript
const decimals = await web3.op.getDecimals({
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
})
console.log(decimals) // 0x6
```
### `getGasPrice`
Retrieves the current L2 gas price (base fee)
```typescript
async getGasPrice(returnFormat?: ReturnFormat)
```
#### Parameters
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The current L2 gas price as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
const gasPrice = await web3.op.getGasPrice()
console.log(gasPrice) // 77n
```
##### Formatting Response as a Hex String
```typescript
const gasPrice = await web3.op.getGasPrice({
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
})
console.log(gasPrice) // 0x4d
```
### `getL1GasUsed`
Computes the amount of L1 gas used for {transaction}. Adds the overhead which represents the per-transaction gas overhead of posting the {transaction} and state roots to L1. Adds 68 bytes of padding to account for the fact that the input does not have a signature.
```typescript
async getL1GasUsed(transaction: Transaction, returnFormat?: ReturnFormat)
```
#### Parameters
- `transaction: Transaction` - An unsigned web3.js [transaction](https://docs.web3js.org/api/web3-types/interface/Transaction) object
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The amount of gas as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
import { Contract } from 'web3'
import { optimistABI, optimistAddress } from '@eth-optimism/contracts-ts'
const optimistContract = new Contract(optimistABI, optimistAddress[420])
const encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
const l1GasUsed = await web3.op.getL1GasUsed({
chainId: 10,
data: encodedBurnMethod,
type: 2,
})
console.log(l1GasUsed) // 1884n
```
##### Formatting Response as a Hex String
```typescript
import { Contract } from 'web3'
import { optimistABI, optimistAddress } from '@eth-optimism/contracts-ts'
const optimistContract = new Contract(optimistABI, optimistAddress[420])
const encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
const l1GasUsed = await web3.op.getL1GasUsed(
{
chainId: 10,
data: encodedBurnMethod,
type: 2,
},
{ number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }
)
console.log(l1GasUsed) // 0x75c
```
### `getL1BaseFee`
Retrieves the latest known L1 base fee
```typescript
async getL1BaseFee(returnFormat?: ReturnFormat)
```
#### Parameters
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The L1 base fee as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
const baseFee = await web3.op.getL1BaseFee()
console.log(baseFee) // 13752544112n
```
##### Formatting Response as a Hex String
```typescript
const baseFee = await web3.op.getL1BaseFee({
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
})
console.log(baseFee) // 0x333b72b70
```
### `getOverhead`
Retrieves the current fee overhead
```typescript
async getOverhead(returnFormat?: ReturnFormat)
```
#### Parameters
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The current overhead as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
const overhead = await web3.op.getOverhead()
console.log(overhead) // 188n
```
##### Formatting Response as a Hex String
```typescript
const overhead = await web3.op.getOverhead({
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
})
console.log(overhead) // 0xbc
```
### `getScalar`
Retrieves the current fee scalar
```typescript
async getScalar(returnFormat?: ReturnFormat)
```
#### Parameters
- `returnFormat?: ReturnFormat` - A web3.js [DataFormat](https://docs.web3js.org/api/web3-types#DataFormat) object that specifies how to format number and bytes values
- If `returnFormat` is not provided, [DEFAULT_RETURN_FORMAT](https://docs.web3js.org/api/web3-types#DEFAULT_RETURN_FORMAT) is used which will format numbers to `BigInt`s
#### Returns
- `Promise<Numbers>` - The current scalar fee as a `BigInt` by default, but `returnFormat` determines type
#### Example
```typescript
const scalarFee = await web3.op.getScalar()
console.log(scalarFee) // 684000n
```
##### Formatting Response as a Hex String
```typescript
const scalarFee = await web3.op.getScalar({
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
})
console.log(scalarFee) // 0xa6fe0
```
### `getVersion`
Retrieves the full semver version of GasPriceOracle
```typescript
async getVersion()
```
#### Returns
- `Promise<string>` - The semver version
#### Example
```typescript
const version = await web3.op.getVersion()
console.log(version) // 1.0.0
```
## Known Issues
- As of version `4.0.3` of web3.js, both `input` and `data` parameters are automatically added to a transaction objects causing the gas estimations to be inflated. This was corrected in [this](https://github.com/web3/web3.js/pull/6294) PR, but has yet to be released
- For the plugin function `getL2Fee`, you should be able to get the fee estimates using the state of the blockchain at a specified block, however, this doesn't seem to be working with web3.js and requires further investigation
{
"name": "@eth-optimism/web3.js-plugin",
"version": "0.1.0",
"description": "A Web3.js plugin for doing OP-Chain gas estimation",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/ethereum-optimism/optimism.git",
"directory": "packages/web3js-plugin"
},
"homepage": "https://optimism.io",
"type": "module",
"exports": {
".": {
"import": "./dist/plugin.js",
"require": "./dist/plugin.cjs",
"default": "./dist/plugin.js",
"types": "./src/plugin.d.ts"
}
},
"types": "dist/plugin.d.ts",
"files": [
"dist/",
"src/"
],
"scripts": {
"build": "tsup",
"lint": "prettier --check .",
"lint:fix": "prettier --write .",
"test": "vitest --coverage",
"test:coverage": "vitest run --coverage",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@eth-optimism/contracts-ts": "workspace:^",
"@swc/core": "^1.3.76",
"@vitest/coverage-istanbul": "^0.34.1",
"tsup": "^7.2.0",
"typescript": "^5.1.6",
"viem": "^1.6.0",
"vite": "^4.4.9",
"vitest": "^0.34.1",
"zod": "^3.22.0"
},
"dependencies": {
"@ethereumjs/rlp": "^5.0.0",
"web3-eth": "^4.0.3",
"web3-eth-accounts": "^4.0.3"
},
"peerDependencies": {
"web3": ">= 4.0.3 < 5.x"
}
}
import { beforeAll, describe, expect, test } from 'vitest'
import { z } from 'zod'
import Web3, { Contract, FMT_BYTES, FMT_NUMBER } from 'web3'
import {
l2StandardBridgeABI,
l2StandardBridgeAddress,
optimistABI,
optimistAddress,
} from '@eth-optimism/contracts-ts'
import { OptimismFeeEstimationPlugin } from './plugin'
const defaultProvider = 'https://mainnet.optimism.io'
const provider = z
.string()
.url()
.default(defaultProvider)
.parse(process.env['VITE_L2_RPC_URL'])
if (provider === defaultProvider)
console.warn(
'Warning: Using default public provider, this could cause tests to fail due to rate limits. Set the VITE_L2_RPC_URL env to override default provider'
)
describe('OptimismFeeEstimationPlugin', () => {
let web3: Web3
beforeAll(() => {
web3 = new Web3(provider)
web3.registerPlugin(new OptimismFeeEstimationPlugin())
})
test('should be registered under .op namespace', () =>
expect(web3.op).toMatchInlineSnapshot(`
OptimismFeeEstimationPlugin {
"_accountProvider": {
"create": [Function],
"decrypt": [Function],
"encrypt": [Function],
"hashMessage": [Function],
"privateKeyToAccount": [Function],
"recover": [Function],
"recoverTransaction": [Function],
"sign": [Function],
"signTransaction": [Function],
"wallet": Wallet [],
},
"_emitter": EventEmitter {
"_events": {},
"_eventsCount": 0,
"_maxListeners": undefined,
Symbol(kCapture): false,
},
"_gasPriceOracleContract": undefined,
"_requestManager": Web3RequestManager {
"_emitter": EventEmitter {
"_events": {
"BEFORE_PROVIDER_CHANGE": [Function],
"PROVIDER_CHANGED": [Function],
},
"_eventsCount": 2,
"_maxListeners": undefined,
Symbol(kCapture): false,
},
"_provider": HttpProvider {
"clientUrl": "https://mainnet.optimism.io",
"httpProviderOptions": undefined,
},
"useRpcCallSpecification": undefined,
},
"_subscriptionManager": Web3SubscriptionManager {
"_subscriptions": Map {},
"registeredSubscriptions": {
"logs": [Function],
"newBlockHeaders": [Function],
"newHeads": [Function],
"newPendingTransactions": [Function],
"pendingTransactions": [Function],
"syncing": [Function],
},
"requestManager": Web3RequestManager {
"_emitter": EventEmitter {
"_events": {
"BEFORE_PROVIDER_CHANGE": [Function],
"PROVIDER_CHANGED": [Function],
},
"_eventsCount": 2,
"_maxListeners": undefined,
Symbol(kCapture): false,
},
"_provider": HttpProvider {
"clientUrl": "https://mainnet.optimism.io",
"httpProviderOptions": undefined,
},
"useRpcCallSpecification": undefined,
},
"tolerateUnlinkedSubscription": false,
},
"_wallet": Wallet [],
"config": {
"blockHeaderTimeout": 10,
"defaultAccount": undefined,
"defaultBlock": "latest",
"defaultChain": "mainnet",
"defaultCommon": undefined,
"defaultHardfork": "london",
"defaultMaxPriorityFeePerGas": "0x9502f900",
"defaultNetworkId": undefined,
"defaultTransactionType": "0x0",
"enableExperimentalFeatures": {
"useRpcCallSpecification": false,
"useSubscriptionWhenCheckingBlockTimeout": false,
},
"handleRevert": false,
"maxListenersWarningThreshold": 100,
"transactionBlockTimeout": 50,
"transactionBuilder": undefined,
"transactionConfirmationBlocks": 24,
"transactionConfirmationPollingInterval": undefined,
"transactionPollingInterval": 1000,
"transactionPollingTimeout": 750000,
"transactionReceiptPollingInterval": undefined,
"transactionSendTimeout": 750000,
"transactionTypeParser": undefined,
},
"pluginNamespace": "op",
"providers": {
"HttpProvider": [Function],
"WebsocketProvider": [Function],
},
}
`))
describe('should return a bigint by default', () => {
test('getBaseFee', async () =>
expect(await web3.op.getBaseFee()).toBeTypeOf('bigint'))
test('getDecimals should return 6n', async () =>
expect(await web3.op.getDecimals()).toBe(BigInt(6)))
test('getGasPrice', async () =>
expect(await web3.op.getGasPrice()).toBeTypeOf('bigint'))
test('getL1BaseFee', async () =>
expect(await web3.op.getL1BaseFee()).toBeTypeOf('bigint'))
test('getOverhead should return 188n', async () =>
expect(await web3.op.getOverhead()).toBe(BigInt(188)))
test('getScalar should return 684000n', async () =>
expect(await web3.op.getScalar()).toBe(BigInt(684000)))
})
describe('should return a number', () => {
const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
test('getBaseFee', async () =>
expect(await web3.op.getBaseFee(numberFormat)).toBeTypeOf('number'))
test('getDecimals should return 6', async () =>
expect(await web3.op.getDecimals(numberFormat)).toBe(6))
test('getGasPrice', async () =>
expect(await web3.op.getGasPrice(numberFormat)).toBeTypeOf('number'))
test('getL1BaseFee', async () =>
expect(await web3.op.getL1BaseFee(numberFormat)).toBeTypeOf('number'))
test('getOverhead should return 188', async () =>
expect(await web3.op.getOverhead(numberFormat)).toBe(188))
test('getScalar should return 684000', async () =>
expect(await web3.op.getScalar(numberFormat)).toBe(684000))
})
test('getVersion should return the string 1.0.0', async () =>
expect(await web3.op.getVersion()).toBe('1.0.0'))
describe('Contract transaction gas estimates - optimistABI.burn', () => {
let optimistContract: Contract<typeof optimistABI>
let encodedBurnMethod: string
beforeAll(() => {
optimistContract = new web3.eth.Contract(optimistABI)
encodedBurnMethod = optimistContract.methods
.burn('0x77194aa25a06f932c10c0f25090f3046af2c85a6')
.encodeABI()
})
describe('should return a bigint by default', () => {
test('getL1Fee', async () => {
expect(
await web3.op.getL1Fee({
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
})
).toBeTypeOf('bigint')
})
test('getL1GasUsed should return 1884n', async () =>
expect(
await web3.op.getL1GasUsed({
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
})
).toBe(BigInt(1884)))
test('estimateFees', async () =>
expect(
await web3.op.estimateFees({
chainId: 10,
data: encodedBurnMethod,
type: 2,
to: optimistAddress[10],
from: '0x77194aa25a06f932c10c0f25090f3046af2c85a6',
})
).toBeTypeOf('bigint'))
test('getL2Fee', async () => {
expect(
await web3.op.getL2Fee({
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
to: optimistAddress[10],
from: '0x77194aa25a06f932c10c0f25090f3046af2c85a6',
})
).toBeTypeOf('bigint')
})
test('estimateFees', async () =>
expect(
await web3.op.estimateFees(
{
chainId: 10,
data: encodedBurnMethod,
type: 2,
to: optimistAddress[10],
from: '0x77194aa25a06f932c10c0f25090f3046af2c85a6',
}
)
).toBeTypeOf('bigint'))
})
describe('should return a hexString', () => {
const hexStringFormat = { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }
test('getL1Fee', async () => {
expect(
await web3.op.getL1Fee(
{
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
},
hexStringFormat
)
).toBeTypeOf('string')
})
test('getL1GasUsed should return 0x75c', async () =>
expect(
await web3.op.getL1GasUsed(
{
chainId: '0xa',
data: encodedBurnMethod,
type: '0x2',
},
hexStringFormat
)
).toBe('0x75c'))
test('estimateFees', async () =>
expect(
await web3.op.estimateFees(
{
chainId: 10,
data: encodedBurnMethod,
type: 2,
to: optimistAddress[10],
from: '0x77194aa25a06f932c10c0f25090f3046af2c85a6',
},
hexStringFormat
)
).toBeTypeOf('string'))
})
})
describe('Contract transaction gas estimates - l2StandardBridgeABI.withdraw', () => {
let l2BridgeContract: Contract<typeof l2StandardBridgeABI>
let encodedWithdrawMethod: string
beforeAll(() => {
l2BridgeContract = new Contract(
l2StandardBridgeABI,
l2StandardBridgeAddress[420]
)
encodedWithdrawMethod = l2BridgeContract.methods
.withdraw(
// l2 token address
'0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000',
// amount
Web3.utils.toWei('0.00000001', 'ether'),
// l1 gas
0,
// extra data
'0x00'
)
.encodeABI()
})
describe('should return a bigint by default', () => {
test('getL1Fee', async () => {
expect(
await web3.op.getL1Fee({
chainId: '0xa',
data: encodedWithdrawMethod,
type: '0x2',
})
).toBeTypeOf('bigint')
})
test('getL1GasUsed should return 2592n', async () =>
expect(
await web3.op.getL1GasUsed({
chainId: '0xa',
data: encodedWithdrawMethod,
type: '0x2',
})
).toBe(BigInt(2592)))
test('estimateFees', async () =>
expect(
await web3.op.estimateFees({
chainId: 10,
data: encodedWithdrawMethod,
value: Web3.utils.toWei('0.00000001', 'ether'),
type: 2,
to: l2StandardBridgeAddress[420],
from: '0x6387a88a199120aD52Dd9742C7430847d3cB2CD4',
maxFeePerGas: Web3.utils.toWei('0.2', 'gwei'),
maxPriorityFeePerGas: Web3.utils.toWei('0.1', 'gwei'),
})
).toBeTypeOf('bigint'))
})
})
})
import Web3, {
type BlockNumberOrTag,
BlockTags,
Contract,
type DataFormat,
DEFAULT_RETURN_FORMAT,
FMT_BYTES,
FMT_NUMBER,
type Transaction,
Web3PluginBase,
} from 'web3'
import { TransactionFactory, type TxData } from 'web3-eth-accounts'
import { estimateGas, formatTransaction } from 'web3-eth'
import {
gasPriceOracleABI,
gasPriceOracleAddress,
} from '@eth-optimism/contracts-ts'
import { RLP } from '@ethereumjs/rlp'
export class OptimismFeeEstimationPlugin extends Web3PluginBase {
public pluginNamespace = 'op'
private _gasPriceOracleContract:
| Contract<typeof gasPriceOracleABI>
| undefined
/**
* Retrieves the current L2 base fee
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<bigint>} - The L2 base fee as a BigInt by default, but {returnFormat} determines type
* @example
* const baseFeeValue: bigint = await web3.op.getBaseFee();
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const baseFeeValue: number = await web3.op.getBaseFee(numberFormat);
*/
public async getBaseFee<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance().methods.baseFee().call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Retrieves the decimals used in the scalar
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The number of decimals as a BigInt by default, but {returnFormat} determines type
* @example
* const decimalsValue: bigint = await web3.op.getDecimals();
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const decimalsValue: number = await web3.op.getDecimals(numberFormat);
*/
public async getDecimals<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance().methods.decimals().call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Retrieves the current L2 gas price (base fee)
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The current L2 gas price as a BigInt by default, but {returnFormat} determines type
* @example
* const gasPriceValue: bigint = await web3.op.getGasPrice();
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const gasPriceValue: number = await web3.op.getGasPrice(numberFormat);
*/
public async getGasPrice<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance().methods.gasPrice().call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* 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
* @param transaction - An unsigned web3.js {Transaction} object
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The fee as a BigInt by default, but {returnFormat} determines type
* @example
* const l1FeeValue: bigint = await getL1Fee(transaction);
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const l1FeeValue: number = await getL1Fee(transaction, numberFormat);
*/
public async getL1Fee<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(transaction: Transaction, returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance()
.methods.getL1Fee(this._serializeTransaction(transaction))
.call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Computes the amount of L1 gas used for {transaction}. Adds the overhead which
* represents the per-transaction gas overhead of posting the {transaction} and state
* roots to L1. Adds 68 bytes of padding to account for the fact that the input does
* not have a signature.
* @param transaction - An unsigned web3.js {Transaction} object
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The amount gas as a BigInt by default, but {returnFormat} determines type
* @example
* const gasUsedValue: bigint = await getL1GasUsed(transaction);
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const gasUsedValue: number = await getL1GasUsed(transaction, numberFormat);
*/
public async getL1GasUsed<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(transaction: Transaction, returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance()
.methods.getL1GasUsed(
this._serializeTransaction(transaction)
)
.call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Retrieves the latest known L1 base fee
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The L1 base fee as a BigInt by default, but {returnFormat} determines type
* @example
* const baseFeeValue: bigint = await web3.op.getL1BaseFee();
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const baseFeeValue: number = await web3.op.getL1BaseFee(numberFormat);
*/
public async getL1BaseFee<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance().methods.l1BaseFee().call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Retrieves the current fee overhead
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The current overhead fee as a BigInt by default, but {returnFormat} determines type
* @example
* const overheadValue: bigint = await web3.op.getOverhead();
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const overheadValue: number = await web3.op.getOverhead(numberFormat);
*/
public async getOverhead<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance().methods.overhead().call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Retrieves the current fee scalar
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The current scalar fee as a BigInt by default, but {returnFormat} determines type
* @example
* const scalarValue: bigint = await web3.op.getScalar();
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const scalarValue: number = await web3.op.getScalar(numberFormat);
*/
public async getScalar<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(returnFormat?: ReturnFormat) {
return Web3.utils.format(
{ format: 'uint' },
await this._getPriceOracleContractInstance().methods.scalar().call(),
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Retrieves the full semver version of GasPriceOracle
* @returns {Promise<string>} - The semver version
* @example
* const version = await web3.op.getVersion();
*/
public async getVersion() {
return this._getPriceOracleContractInstance().methods.version().call()
}
/**
* Retrieves the amount of L2 gas estimated to execute {transaction}
* @param transaction - An unsigned web3.js {Transaction} object
* @param {{ blockNumber: BlockNumberOrTag, returnFormat: DataFormat }} [options={blockNumber: BlockTags.LATEST, returnFormat: DEFAULT_RETURN_FORMAT}] -
* An options object specifying what block to use for gas estimates and the web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The gas estimate as a BigInt by default, but {returnFormat} determines type
* @example
* const l2Fee: bigint = await getL2Fee(transaction);
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const l2Fee: number = await getL2Fee(transaction, numberFormat);
*/
public async getL2Fee<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(
transaction: Transaction,
options?: {
blockNumber?: BlockNumberOrTag | undefined
returnFormat?: ReturnFormat
}
) {
const [gasCost, gasPrice] = await Promise.all([
estimateGas(
this,
transaction,
options?.blockNumber ?? BlockTags.LATEST,
DEFAULT_RETURN_FORMAT
),
this.getGasPrice(),
])
return Web3.utils.format(
{ format: 'uint' },
gasCost * gasPrice,
options?.returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Computes the total (L1 + L2) fee estimate to execute {transaction}
* @param transaction - An unsigned web3.js {Transaction} object
* @param {DataFormat} [returnFormat=DEFAULT_RETURN_FORMAT] - The web3.js format object that specifies how to format number and bytes values
* @returns {Promise<Numbers>} - The estimated total fee as a BigInt by default, but {returnFormat} determines type
* @example
* const estimatedFees: bigint = await estimateFees(transaction);
* @example
* const numberFormat = { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }
* const estimatedFees: number = await estimateFees(transaction, numberFormat);
*/
public async estimateFees<
ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT
>(
transaction: Transaction,
returnFormat?: ReturnFormat
) {
const [l1Fee, l2Fee] = await Promise.all([
this.getL1Fee(transaction),
this.getL2Fee(transaction),
])
return Web3.utils.format(
{ format: 'uint' },
l1Fee + l2Fee,
returnFormat ?? DEFAULT_RETURN_FORMAT
)
}
/**
* Used to get the web3.js contract instance for gas price oracle contract
* @returns {Contract<typeof gasPriceOracleABI>} - A web.js contract instance with an RPC provider inherited from root {web3} instance
*/
private _getPriceOracleContractInstance() {
if (this._gasPriceOracleContract === undefined) {
this._gasPriceOracleContract = new Contract(
gasPriceOracleABI,
gasPriceOracleAddress[420]
)
// This plugin's Web3Context is overridden with main Web3 instance's context
// when the plugin is registered. This overwrites the Contract instance's context
this._gasPriceOracleContract.link(this)
}
return this._gasPriceOracleContract
}
/**
* Returns the RLP encoded hex string for {transaction}
* @param transaction - A web3.js {Transaction} object
* @returns {string} - The RLP encoded hex string
*/
private _serializeTransaction(transaction: Transaction) {
const ethereumjsTransaction = TransactionFactory.fromTxData(
formatTransaction(transaction, {
number: FMT_NUMBER.HEX,
bytes: FMT_BYTES.HEX,
}) as TxData
)
return Web3.utils.bytesToHex(
Web3.utils.uint8ArrayConcat(
Web3.utils.hexToBytes(
ethereumjsTransaction.type.toString(16).padStart(2, '0')
),
// If <transaction> doesn't include a signature,
// <ethereumjsTransaction.raw()> will autofill v, r, and s
// with empty uint8Array. Because L1 fee calculation
// is dependent on the number of bytes, we are removing
// the zero values bytes
RLP.encode(ethereumjsTransaction.raw().slice(0, -3))
)
)
}
}
// Module Augmentation to add op namespace to root {web3} instance
declare module 'web3' {
interface Web3Context {
op: OptimismFeeEstimationPlugin
}
}
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "./src",
"noEmit": true,
"target": "ESNext",
"lib": ["esnext"],
"module": "esnext",
"moduleResolution": "Node",
"isolatedModules": true,
"allowUnreachableCode": false,
"skipLibCheck": false,
"allowUnusedLabels": false,
"alwaysStrict": true,
"exactOptionalPropertyTypes": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitOverride": true,
"noImplicitThis": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strict": true
},
"include": ["./src"]
}
import { defineConfig } from 'tsup'
import packageJson from './package.json'
// @see https://tsup.egoist.dev/
export default defineConfig({
name: packageJson.name,
entry: ['src/plugin.ts'],
outDir: 'dist',
format: ['esm', 'cjs'],
splitting: false,
sourcemap: true,
clean: false,
dts: true
})
import { defineConfig } from 'vitest/config'
// @see https://vitest.dev/config/
export default defineConfig({
test: {
environment: 'jsdom',
coverage: {
provider: 'istanbul',
},
},
})
This source diff could not be displayed because it is too large. You can view the blob instead.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment