Commit 58a2d894 authored by OptimismBot's avatar OptimismBot Committed by GitHub

Merge pull request #6609 from...

Merge pull request #6609 from ethereum-optimism/08-05-fix_fee-estimation_Multiply_gas_fee_in_estimate_gas

fix(fee-estimation): Multiply gas fee in estimate gas
parents 556e5ad2 0a98fbb5
---
'@eth-optimism/fee-estimation': patch
---
Fixed bug with 'estimateFees' not taking into account the l2 gas price
...@@ -37,11 +37,9 @@ yarn add @eth-optimism/fee-estimation ...@@ -37,11 +37,9 @@ yarn add @eth-optimism/fee-estimation
### Basic Usage ### Basic Usage
```ts ```ts
import { import { estimateFees } from '@eth-optimism/fee-estimation'
estimateFees, import { optimistABI } from '@eth-optimism/contracts-ts'
} from '@eth-optimism/fee-estimation' import { viemClient } from './viem-client'
import {optimistABI} from '@eth-optimism/contracts-ts'
import {viemClient} from './viem-client'
const optimistOwnerAddress = const optimistOwnerAddress =
'0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const '0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const
......
...@@ -24,6 +24,19 @@ import { ...@@ -24,6 +24,19 @@ import {
} from '@eth-optimism/contracts-ts' } from '@eth-optimism/contracts-ts'
import { parseEther, parseGwei } from 'viem' import { parseEther, parseGwei } from 'viem'
vi.mock('viem', async () => {
const _viem = (await vi.importActual('viem')) as any
return {
..._viem,
// no way to get historical gas price
createPublicClient: (...args: [any]) => {
const client = _viem.createPublicClient(...args)
client.getGasPrice = async () => parseGwei('0.00000042')
return client
},
}
})
// using this optimist https://optimistic.etherscan.io/tx/0xaa291efba7ea40b0742e5ff84a1e7831a2eb6c2fc35001fa03ba80fd3b609dc9 // using this optimist https://optimistic.etherscan.io/tx/0xaa291efba7ea40b0742e5ff84a1e7831a2eb6c2fc35001fa03ba80fd3b609dc9
const blockNumber = BigInt(107028270) const blockNumber = BigInt(107028270)
const optimistOwnerAddress = const optimistOwnerAddress =
...@@ -95,44 +108,44 @@ beforeEach(() => { ...@@ -95,44 +108,44 @@ beforeEach(() => {
test('estimateFees should return correct fees', async () => { test('estimateFees should return correct fees', async () => {
// burn // burn
const res = await estimateFees({ ...paramsWithRpcUrl, ...functionDataBurn }) const res = await estimateFees({ ...paramsWithRpcUrl, ...functionDataBurn })
expect(res).toMatchInlineSnapshot('20573185261089n') expect(res).toMatchInlineSnapshot('20573203833264n')
expect(formatEther(res)).toMatchInlineSnapshot('"0.000020573185261089"') expect(formatEther(res)).toMatchInlineSnapshot('"0.000020573203833264"')
expect( expect(
await estimateFees({ ...paramsWithRpcUrl, ...functionDataBurn }) await estimateFees({ ...paramsWithRpcUrl, ...functionDataBurn })
).toMatchInlineSnapshot('20573185261089n') ).toMatchInlineSnapshot('20573203833264n')
expect( expect(
await estimateFees({ ...paramsWithViemClient, ...functionDataBurn }) await estimateFees({ ...paramsWithViemClient, ...functionDataBurn })
).toMatchInlineSnapshot('20573185261089n') ).toMatchInlineSnapshot('20573203833264n')
expect( expect(
await estimateFees({ await estimateFees({
...paramsWithRpcUrl, ...paramsWithRpcUrl,
...functionDataBurnWithPriorityFees, ...functionDataBurnWithPriorityFees,
}) })
).toMatchInlineSnapshot('21536974118090n') ).toMatchInlineSnapshot('21536992690265n')
// what is the l2 and l1 part of the fees for reference? // what is the l2 and l1 part of the fees for reference?
const l1Fee = await getL1Fee({ ...paramsWithRpcUrl, ...functionDataBurn }) const l1Fee = await getL1Fee({ ...paramsWithRpcUrl, ...functionDataBurn })
const l2Fee = res - l1Fee const l2Fee = res - l1Fee
expect(l1Fee).toMatchInlineSnapshot('20573185216764n') expect(l1Fee).toMatchInlineSnapshot('20573185216764n')
expect(formatEther(l1Fee)).toMatchInlineSnapshot('"0.000020573185216764"') expect(formatEther(l1Fee)).toMatchInlineSnapshot('"0.000020573185216764"')
expect(l2Fee).toMatchInlineSnapshot('44325n') expect(l2Fee).toMatchInlineSnapshot('18616500n')
expect(formatEther(l2Fee)).toMatchInlineSnapshot('"0.000000000000044325"') expect(formatEther(l2Fee)).toMatchInlineSnapshot('"0.0000000000186165"')
// withdraw // withdraw
const res2 = await estimateFees({ const res2 = await estimateFees({
...paramsWithRpcUrlWithdraw, ...paramsWithRpcUrlWithdraw,
...functionDataWithdraw, ...functionDataWithdraw,
}) })
expect(res2).toMatchInlineSnapshot('62857039016380n') expect(res2).toMatchInlineSnapshot('62857090247510n')
expect( expect(
await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw }) await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw })
).toMatchInlineSnapshot('62857039016380n') ).toMatchInlineSnapshot('62857090247510n')
expect( expect(
await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw }) await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw })
).toMatchInlineSnapshot('62857039016380n') ).toMatchInlineSnapshot('62857090247510n')
expect( expect(
await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw }) await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw })
).toMatchInlineSnapshot('62857039016380n') ).toMatchInlineSnapshot('62857090247510n')
expect(formatEther(res2)).toMatchInlineSnapshot('"0.00006285703901638"') expect(formatEther(res2)).toMatchInlineSnapshot('"0.00006285709024751"')
// what is the l2 and l1 part of the fees for reference? // what is the l2 and l1 part of the fees for reference?
const l1Fee2 = await getL1Fee({ const l1Fee2 = await getL1Fee({
...paramsWithRpcUrlWithdraw, ...paramsWithRpcUrlWithdraw,
...@@ -141,8 +154,8 @@ test('estimateFees should return correct fees', async () => { ...@@ -141,8 +154,8 @@ test('estimateFees should return correct fees', async () => {
const l2Fee2 = res2 - l1Fee const l2Fee2 = res2 - l1Fee
expect(l1Fee2).toMatchInlineSnapshot('62857038894110n') expect(l1Fee2).toMatchInlineSnapshot('62857038894110n')
expect(formatEther(l1Fee2)).toMatchInlineSnapshot('"0.00006285703889411"') expect(formatEther(l1Fee2)).toMatchInlineSnapshot('"0.00006285703889411"')
expect(l2Fee2).toMatchInlineSnapshot('42283853799616n') expect(l2Fee2).toMatchInlineSnapshot('42283905030746n')
expect(formatEther(l2Fee2)).toMatchInlineSnapshot('"0.000042283853799616"') expect(formatEther(l2Fee2)).toMatchInlineSnapshot('"0.000042283905030746"')
}) })
test('baseFee should return the correct result', async () => { test('baseFee should return the correct result', async () => {
......
...@@ -332,7 +332,7 @@ export const estimateFees: EstimateFees = async (options) => { ...@@ -332,7 +332,7 @@ export const estimateFees: EstimateFees = async (options) => {
args: options.args, args: options.args,
functionName: options.functionName, functionName: options.functionName,
} as EncodeFunctionDataParameters) } as EncodeFunctionDataParameters)
const [l1Fee, l2Fee] = await Promise.all([ const [l1Fee, l2Gas, l2GasPrice] = await Promise.all([
getL1Fee({ getL1Fee({
...options, ...options,
// account must be undefined or else viem will return undefined // account must be undefined or else viem will return undefined
...@@ -347,6 +347,7 @@ export const estimateFees: EstimateFees = async (options) => { ...@@ -347,6 +347,7 @@ export const estimateFees: EstimateFees = async (options) => {
data: encodedFunctionData, data: encodedFunctionData,
value: options.value, value: options.value,
} as EstimateGasParameters<typeof chains.optimism>), } as EstimateGasParameters<typeof chains.optimism>),
client.getGasPrice(),
]) ])
return l1Fee + l2Fee return l1Fee + l2Gas * l2GasPrice
} }
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