-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add Operator Fee for OP Stack Chains #3950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2430b6c
Add Operator Fee to OP Stack
Kemperino f8fb4da
Fix parsing
Kemperino 9f5a6fd
Add changeset
Kemperino 21f8dd2
Merge branch 'wevm:main' into op-stack/add-operator-fee
Kemperino 3c95e35
Merge branch 'wevm:main' into op-stack/add-operator-fee
Kemperino 6e1a748
Update .gitignore
Kemperino 6fb677b
Update src/op-stack/actions/estimateL1Fee.ts
Kemperino 86bc44a
Update src/op-stack/actions/estimateL1Gas.ts
Kemperino b8d7a91
Update src/op-stack/actions/getL1BaseFee.ts
Kemperino 81c4ba3
add operator fee to decorator
Kemperino File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "viem": patch | ||
| --- | ||
|
|
||
| Added estimateOperatorFee action for OP Stack chains |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,3 +33,4 @@ vectors/**/*.json | |
| site/dist | ||
| .vercel | ||
| vocs.config.tsx.timestamp* | ||
| site/.cache | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| --- | ||
| description: Estimates the operator fee to execute an L2 transaction. | ||
| --- | ||
|
|
||
| # estimateOperatorFee | ||
|
|
||
| Estimates the [operator fee](https://docs.optimism.io/stack/transactions/fees#operator-fee) to execute an L2 transaction. | ||
|
|
||
| The operator fee is part of the Isthmus upgrade and allows OP Stack operators to recover costs related to Alt-DA, ZK proving, or custom gas tokens. Returns 0 for pre-Isthmus chains or when operator fee functions don't exist. | ||
|
|
||
| The fee is calculated using the formula: `operatorFee = operatorFeeConstant + operatorFeeScalar * gasUsed / 1e6` | ||
|
|
||
| ## Usage | ||
|
|
||
| :::code-group | ||
|
|
||
| ```ts [example.ts] | ||
| import { account, publicClient } from './config' | ||
|
|
||
| const fee = await publicClient.estimateOperatorFee({ // [!code focus:7] | ||
| account, | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ```ts [config.ts] | ||
| import { createPublicClient, http } from 'viem' | ||
| import { privateKeyToAccount } from 'viem/accounts' | ||
| import { base } from 'viem/chains' | ||
| import { publicActionsL2 } from 'viem/op-stack' | ||
|
|
||
| // JSON-RPC Account | ||
| export const account = '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266' | ||
| // Local Account | ||
| export const account = privateKeyToAccount(...) | ||
|
|
||
| export const publicClient = createPublicClient({ | ||
| chain: base, | ||
| transport: http() | ||
| }).extend(publicActionsL2()) | ||
| ``` | ||
|
|
||
| ::: | ||
|
|
||
| ## Returns | ||
|
|
||
| `bigint` | ||
|
|
||
| The operator fee (in wei). | ||
|
|
||
| ## Parameters | ||
|
|
||
| ### account | ||
|
|
||
| - **Type:** `Account | Address` | ||
|
|
||
| The Account to estimate fee from. | ||
|
|
||
| Accepts a [JSON-RPC Account](/docs/clients/wallet#json-rpc-accounts) or [Local Account (Private Key, etc)](/docs/clients/wallet#local-accounts-private-key-mnemonic-etc). | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', // [!code focus] | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### data (optional) | ||
|
|
||
| - **Type:** `0x${string}` | ||
|
|
||
| Contract code or a hashed method call with encoded args. | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| data: '0x...', // [!code focus] | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### l1BlockAddress (optional) | ||
|
|
||
| - **Type:** [`Address`](/docs/glossary/types#address) | ||
|
|
||
| Address of the L1Block predeploy contract. | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| l1BlockAddress: '0x4200000000000000000000000000000000000015', // [!code focus] | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### maxFeePerGas (optional) | ||
|
|
||
| - **Type:** `bigint` | ||
|
|
||
| Total fee per gas (in wei), inclusive of `maxPriorityFeePerGas`. | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| maxFeePerGas: parseGwei('20'), // [!code focus] | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### maxPriorityFeePerGas (optional) | ||
|
|
||
| - **Type:** `bigint` | ||
|
|
||
| Max priority fee per gas (in wei). | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| maxFeePerGas: parseGwei('20'), | ||
| maxPriorityFeePerGas: parseGwei('2'), // [!code focus] | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### nonce (optional) | ||
|
|
||
| - **Type:** `number` | ||
|
|
||
| Unique number identifying this transaction. | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| maxFeePerGas: parseGwei('20'), | ||
| maxPriorityFeePerGas: parseGwei('2'), | ||
| nonce: 69, // [!code focus] | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### to (optional) | ||
|
|
||
| - **Type:** [`Address`](/docs/glossary/types#address) | ||
|
|
||
| Transaction recipient. | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', // [!code focus] | ||
| value: parseEther('1') | ||
| }) | ||
| ``` | ||
|
|
||
| ### value (optional) | ||
|
|
||
| - **Type:** `bigint` | ||
|
|
||
| Value (in wei) sent with this transaction. | ||
|
|
||
| ```ts | ||
| const fee = await publicClient.estimateOperatorFee({ | ||
| account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', | ||
| to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8', | ||
| value: parseEther('1') // [!code focus] | ||
| }) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { expect, test } from 'vitest' | ||
|
|
||
| import { accounts } from '~test/src/constants.js' | ||
|
|
||
| import { anvilOptimism } from '../../../test/src/anvil.js' | ||
| import { parseGwei, type TransactionRequestEIP1559 } from '../../index.js' | ||
| import { parseEther } from '../../utils/unit/parseEther.js' | ||
| import { estimateOperatorFee } from './estimateOperatorFee.js' | ||
|
|
||
| const optimismClient = anvilOptimism.getClient() | ||
| const optimismClientWithAccount = anvilOptimism.getClient({ account: true }) | ||
| const optimismClientWithoutChain = anvilOptimism.getClient({ chain: false }) | ||
|
|
||
| const baseTransaction = { | ||
| maxFeePerGas: parseGwei('100'), | ||
| maxPriorityFeePerGas: parseGwei('1'), | ||
| to: accounts[1].address, | ||
| value: parseEther('0.1'), | ||
| } as const satisfies Omit<TransactionRequestEIP1559, 'from'> | ||
|
|
||
| test('default', async () => { | ||
| const fee = await estimateOperatorFee( | ||
| optimismClientWithAccount, | ||
| baseTransaction, | ||
| ) | ||
| expect(fee).toBeDefined() | ||
| }) | ||
|
|
||
| test('minimal', async () => { | ||
| const fee = await estimateOperatorFee(optimismClientWithAccount, {}) | ||
| expect(fee).toBeDefined() | ||
| }) | ||
|
|
||
| test('args: account', async () => { | ||
| const fee = await estimateOperatorFee(optimismClient, { | ||
| ...baseTransaction, | ||
| account: accounts[0].address, | ||
| }) | ||
| expect(fee).toBeDefined() | ||
| }) | ||
|
|
||
| test('args: data', async () => { | ||
| const fee = await estimateOperatorFee(optimismClientWithAccount, { | ||
| ...baseTransaction, | ||
| data: '0x00000000000000000000000000000000000000000000000004fefa17b7240000', | ||
| }) | ||
| expect(fee).toBeDefined() | ||
| }) | ||
|
|
||
| test('args: l1BlockAddress', async () => { | ||
| const fee = await estimateOperatorFee(optimismClientWithAccount, { | ||
| ...baseTransaction, | ||
| l1BlockAddress: '0x4200000000000000000000000000000000000015', | ||
| }) | ||
| expect(fee).toBeDefined() | ||
| }) | ||
|
|
||
| test('args: nonce', async () => { | ||
| const fee = await estimateOperatorFee(optimismClientWithAccount, { | ||
| ...baseTransaction, | ||
| nonce: 69, | ||
| }) | ||
| expect(fee).toBeDefined() | ||
| }) | ||
|
|
||
| test('args: nullish chain', async () => { | ||
| const fee = await estimateOperatorFee(optimismClientWithoutChain, { | ||
| ...baseTransaction, | ||
| account: accounts[0].address, | ||
| chain: null, | ||
| }) | ||
| expect(fee).toBeDefined() | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.