Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-operator-fee-isthmus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed Operator Fee estimation for Isthmus upgrade by using the `getOperatorFee` function from the Gas Price Oracle instead of manually computing from L1Block parameters.
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/op-stack/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export const gasPriceOracleAbi = [
stateMutability: 'view',
type: 'function',
},
{
inputs: [{ internalType: 'uint256', name: '_gasUsed', type: 'uint256' }],
name: 'getOperatorFee',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function',
},
{
inputs: [],
name: 'l1BaseFee',
Expand Down
12 changes: 6 additions & 6 deletions src/op-stack/actions/buildDepositTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('default', async () => {
"account": undefined,
"request": {
"data": undefined,
"gas": 21000n,
"gas": 23660n,
"isCreation": undefined,
"mint": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
Expand Down Expand Up @@ -56,7 +56,7 @@ test('args: account', async () => {
},
"request": {
"data": undefined,
"gas": 21000n,
"gas": 23660n,
"isCreation": undefined,
"mint": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
Expand Down Expand Up @@ -85,7 +85,7 @@ test('args: chain', async () => {
},
"request": {
"data": undefined,
"gas": 21000n,
"gas": 23660n,
"isCreation": undefined,
"mint": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
Expand Down Expand Up @@ -114,7 +114,7 @@ test('args: data', async () => {
},
"request": {
"data": "0xdeadbeef",
"gas": 21160n,
"gas": 23838n,
"isCreation": undefined,
"mint": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
Expand Down Expand Up @@ -172,7 +172,7 @@ test('args: mint', async () => {
},
"request": {
"data": undefined,
"gas": 21000n,
"gas": 23660n,
"isCreation": undefined,
"mint": 1000000000000000000n,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
Expand Down Expand Up @@ -201,7 +201,7 @@ test('args: value', async () => {
},
"request": {
"data": undefined,
"gas": 21000n,
"gas": 32646n,
"isCreation": undefined,
"mint": undefined,
"to": "0x70997970c51812dc3a010c7d01b50e0d17dc79c8",
Expand Down
2 changes: 1 addition & 1 deletion src/op-stack/actions/estimateContractL1Gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ test('default', async () => {
functionName: 'transfer',
args: ['0xc8373edfad6d5c5f600b6b2507f78431c5271ff5', 1n],
}),
).toBe(2544n)
).toBe(1600n)
})
14 changes: 7 additions & 7 deletions src/op-stack/actions/estimateL1Gas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@ const baseTransaction = {

test('default', async () => {
const gas = await estimateL1Gas(optimismClientWithAccount, baseTransaction)
expect(gas).toBe(2004n)
expect(gas).toBe(1600n)
})

test('minimal', async () => {
const gas = await estimateL1Gas(optimismClientWithAccount, {})
expect(gas).toBe(1604n)
expect(gas).toBe(1600n)
})

test('args: account', async () => {
const gas = await estimateL1Gas(optimismClient, {
...baseTransaction,
account: accounts[0].address,
})
expect(gas).toBe(2004n)
expect(gas).toBe(1600n)
})

test('args: data', async () => {
const gas = await estimateL1Gas(optimismClientWithAccount, {
...baseTransaction,
data: '0x00000000000000000000000000000000000000000000000004fefa17b7240000',
})
expect(gas).toBe(2156n)
expect(gas).toBe(1600n)
})

test('args: gasPriceOracleAddress', async () => {
const gas = await estimateL1Gas(optimismClientWithAccount, {
...baseTransaction,
gasPriceOracleAddress: '0x420000000000000000000000000000000000000F',
})
expect(gas).toBe(2004n)
expect(gas).toBe(1600n)
})

test('args: nonce', async () => {
const gas = await estimateL1Gas(optimismClientWithAccount, {
...baseTransaction,
nonce: 69,
})
expect(gas).toBe(2004n)
expect(gas).toBe(1600n)
})

test('args: nullish chain', async () => {
Expand All @@ -67,7 +67,7 @@ test('args: nullish chain', async () => {
account: accounts[0].address,
chain: null,
})
expect(gas).toBe(2004n)
expect(gas).toBe(1600n)
})

test('behavior: account with no funds', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/op-stack/actions/estimateOperatorFee.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ test('args: data', async () => {
expect(fee).toBeDefined()
})

test('args: l1BlockAddress', async () => {
test('args: gasPriceOracleAddress', async () => {
const fee = await estimateOperatorFee(optimismClientWithAccount, {
...baseTransaction,
l1BlockAddress: '0x4200000000000000000000000000000000000015',
gasPriceOracleAddress: '0x420000000000000000000000000000000000000F',
})
expect(fee).toBeDefined()
})
Expand Down
63 changes: 25 additions & 38 deletions src/op-stack/actions/estimateOperatorFee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { TransactionRequestEIP1559 } from '../../types/transaction.js'
import type { RequestErrorType } from '../../utils/buildRequest.js'
import { getChainContractAddress } from '../../utils/chain/getChainContractAddress.js'
import type { HexToNumberErrorType } from '../../utils/encoding/fromHex.js'
import { l1BlockAbi } from '../abis.js'
import { gasPriceOracleAbi } from '../abis.js'
import { contracts } from '../contracts.js'

export type EstimateOperatorFeeParameters<
Expand All @@ -28,7 +28,12 @@ export type EstimateOperatorFeeParameters<
> = Omit<TransactionRequestEIP1559, 'from'> &
GetAccountParameter<TAccount> &
GetChainParameter<TChain, TChainOverride> & {
/** L1 block attributes contract address. */
/** Gas price oracle address. */
gasPriceOracleAddress?: Address | undefined
/**
* L1 block attributes contract address.
* @deprecated Use `gasPriceOracleAddress` instead.
*/
l1BlockAddress?: Address | undefined
}

Expand All @@ -46,7 +51,6 @@ export type EstimateOperatorFeeErrorType =
*
* Operator fees are part of the Isthmus upgrade and allow 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.
*
* @param client - Client to use
* @param parameters - {@link EstimateOperatorFeeParameters}
Expand Down Expand Up @@ -75,45 +79,28 @@ export async function estimateOperatorFee<
client: Client<Transport, TChain, TAccount>,
args: EstimateOperatorFeeParameters<TChain, TAccount, TChainOverride>,
): Promise<EstimateOperatorFeeReturnType> {
const { chain = client.chain, l1BlockAddress: l1BlockAddress_ } = args
const {
chain = client.chain,
gasPriceOracleAddress: gasPriceOracleAddress_,
} = args

const l1BlockAddress = (() => {
if (l1BlockAddress_) return l1BlockAddress_
if (chain?.contracts?.l1Block)
const gasPriceOracleAddress = (() => {
if (gasPriceOracleAddress_) return gasPriceOracleAddress_
if (chain)
return getChainContractAddress({
chain,
contract: 'l1Block',
contract: 'gasPriceOracle',
})
return contracts.l1Block.address
})()
return contracts.gasPriceOracle.address
})() as Address

// Try to get operator fee parameters. If any of these calls fail,
// it means this is a pre-Isthmus chain and operator fees don't apply
try {
// Get operator fee parameters first to fail fast if not supported
const [operatorFeeScalar, operatorFeeConstant] = await Promise.all([
readContract(client, {
abi: l1BlockAbi,
address: l1BlockAddress,
functionName: 'operatorFeeScalar',
}),
readContract(client, {
abi: l1BlockAbi,
address: l1BlockAddress,
functionName: 'operatorFeeConstant',
}),
])
// Estimate gas for the transaction
const gasUsed = await estimateGas(client, args as EstimateGasParameters)

// Estimate gas for the actual transaction
const gasUsed = await estimateGas(client, args as EstimateGasParameters)

// Calculate operator fee: saturatingAdd(saturatingMul(gasUsed, scalar) / 1e6, constant)
// Using saturating arithmetic to prevent overflow
const scaledFee = (gasUsed * BigInt(operatorFeeScalar)) / 1_000_000n
return scaledFee + BigInt(operatorFeeConstant)
} catch {
// If any call fails, this is likely a pre-Isthmus chain or the contract
// doesn't support these functions. Return 0 for operator fee.
return 0n
}
return readContract(client, {
abi: gasPriceOracleAbi,
address: gasPriceOracleAddress,
functionName: 'getOperatorFee',
args: [gasUsed],
})
}
14 changes: 7 additions & 7 deletions src/op-stack/actions/estimateTotalGas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,44 @@ const baseTransaction = {

test('default', async () => {
const gas = await estimateTotalGas(optimismClientWithAccount, baseTransaction)
expect(gas).toBe(23028n)
expect(gas).toBe(34246n)
})

test('minimal', async () => {
const gas = await estimateTotalGas(optimismClientWithAccount, {})
expect(gas).toBe(54605n)
expect(gas).toBe(54601n)
})

test('args: account', async () => {
const gas = await estimateTotalGas(optimismClient, {
...baseTransaction,
account: accounts[0].address,
})
expect(gas).toBe(23028n)
expect(gas).toBe(34246n)
})

test('args: data', async () => {
const gas = await estimateTotalGas(optimismClientWithAccount, {
...baseTransaction,
data: '0x00000000000000000000000000000000000000000000000004fefa17b7240000',
})
expect(gas).toBe(23760n)
expect(gas).toBe(34560n)
})

test('args: gasPriceOracleAddress', async () => {
const gas = await estimateTotalGas(optimismClientWithAccount, {
...baseTransaction,
gasPriceOracleAddress: '0x420000000000000000000000000000000000000F',
})
expect(gas).toBe(23028n)
expect(gas).toBe(34246n)
})

test('args: nonce', async () => {
const gas = await estimateTotalGas(optimismClientWithAccount, {
...baseTransaction,
nonce: 69,
})
expect(gas).toBe(23028n)
expect(gas).toBe(34246n)
})

test('args: nullish chain', async () => {
Expand All @@ -64,5 +64,5 @@ test('args: nullish chain', async () => {
account: accounts[0].address,
chain: null,
})
expect(gas).toBe(23028n)
expect(gas).toBe(34246n)
})
2 changes: 1 addition & 1 deletion test/src/anvil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const anvilOptimism = defineAnvil({
'VITE_ANVIL_FORK_URL_OPTIMISM',
'https://mainnet.optimism.io',
),
forkBlockNumber: 113624777n,
forkBlockNumber: 147000000n,
port: 8645,
})

Expand Down
Loading