Skip to content
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

fix(fee-estimation): Multiply gas fee in estimate gas #6609

Merged
Merged
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/twelve-hounds-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/fee-estimation': patch
---

Fixed bug with 'estimateFees' not taking into account the l2 gas price
8 changes: 3 additions & 5 deletions packages/fee-estimation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ yarn add @eth-optimism/fee-estimation
### Basic Usage

```ts
import {
estimateFees,
} from '@eth-optimism/fee-estimation'
import {optimistABI} from '@eth-optimism/contracts-ts'
import {viemClient} from './viem-client'
import { estimateFees } from '@eth-optimism/fee-estimation'
import { optimistABI } from '@eth-optimism/contracts-ts'
import { viemClient } from './viem-client'

const optimistOwnerAddress =
'0x77194aa25a06f932c10c0f25090f3046af2c85a6' as const
Expand Down
41 changes: 27 additions & 14 deletions packages/fee-estimation/src/estimateFees.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ import {
} from '@eth-optimism/contracts-ts'
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
const blockNumber = BigInt(107028270)
const optimistOwnerAddress =
Expand Down Expand Up @@ -95,44 +108,44 @@ beforeEach(() => {
test('estimateFees should return correct fees', async () => {
// burn
const res = await estimateFees({ ...paramsWithRpcUrl, ...functionDataBurn })
expect(res).toMatchInlineSnapshot('20573185261089n')
expect(formatEther(res)).toMatchInlineSnapshot('"0.000020573185261089"')
expect(res).toMatchInlineSnapshot('20573203833264n')
expect(formatEther(res)).toMatchInlineSnapshot('"0.000020573203833264"')
expect(
await estimateFees({ ...paramsWithRpcUrl, ...functionDataBurn })
).toMatchInlineSnapshot('20573185261089n')
).toMatchInlineSnapshot('20573203833264n')
expect(
await estimateFees({ ...paramsWithViemClient, ...functionDataBurn })
).toMatchInlineSnapshot('20573185261089n')
).toMatchInlineSnapshot('20573203833264n')
expect(
await estimateFees({
...paramsWithRpcUrl,
...functionDataBurnWithPriorityFees,
})
).toMatchInlineSnapshot('21536974118090n')
).toMatchInlineSnapshot('21536992690265n')
// what is the l2 and l1 part of the fees for reference?
const l1Fee = await getL1Fee({ ...paramsWithRpcUrl, ...functionDataBurn })
const l2Fee = res - l1Fee
expect(l1Fee).toMatchInlineSnapshot('20573185216764n')
expect(formatEther(l1Fee)).toMatchInlineSnapshot('"0.000020573185216764"')
expect(l2Fee).toMatchInlineSnapshot('44325n')
expect(formatEther(l2Fee)).toMatchInlineSnapshot('"0.000000000000044325"')
expect(l2Fee).toMatchInlineSnapshot('18616500n')
expect(formatEther(l2Fee)).toMatchInlineSnapshot('"0.0000000000186165"')

// withdraw
const res2 = await estimateFees({
...paramsWithRpcUrlWithdraw,
...functionDataWithdraw,
})
expect(res2).toMatchInlineSnapshot('62857039016380n')
expect(res2).toMatchInlineSnapshot('62857090247510n')
expect(
await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw })
).toMatchInlineSnapshot('62857039016380n')
).toMatchInlineSnapshot('62857090247510n')
expect(
await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw })
).toMatchInlineSnapshot('62857039016380n')
).toMatchInlineSnapshot('62857090247510n')
expect(
await estimateFees({ ...paramsWithRpcUrlWithdraw, ...functionDataWithdraw })
).toMatchInlineSnapshot('62857039016380n')
expect(formatEther(res2)).toMatchInlineSnapshot('"0.00006285703901638"')
).toMatchInlineSnapshot('62857090247510n')
expect(formatEther(res2)).toMatchInlineSnapshot('"0.00006285709024751"')
// what is the l2 and l1 part of the fees for reference?
const l1Fee2 = await getL1Fee({
...paramsWithRpcUrlWithdraw,
Expand All @@ -141,8 +154,8 @@ test('estimateFees should return correct fees', async () => {
const l2Fee2 = res2 - l1Fee
expect(l1Fee2).toMatchInlineSnapshot('62857038894110n')
expect(formatEther(l1Fee2)).toMatchInlineSnapshot('"0.00006285703889411"')
expect(l2Fee2).toMatchInlineSnapshot('42283853799616n')
expect(formatEther(l2Fee2)).toMatchInlineSnapshot('"0.000042283853799616"')
expect(l2Fee2).toMatchInlineSnapshot('42283905030746n')
expect(formatEther(l2Fee2)).toMatchInlineSnapshot('"0.000042283905030746"')
})

test('baseFee should return the correct result', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/fee-estimation/src/estimateFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export const estimateFees: EstimateFees = async (options) => {
args: options.args,
functionName: options.functionName,
} as EncodeFunctionDataParameters)
const [l1Fee, l2Fee] = await Promise.all([
const [l1Fee, l2Gas, l2GasPrice] = await Promise.all([
getL1Fee({
...options,
// account must be undefined or else viem will return undefined
Expand All @@ -347,6 +347,7 @@ export const estimateFees: EstimateFees = async (options) => {
data: encodedFunctionData,
value: options.value,
} as EstimateGasParameters<typeof chains.optimism>),
client.getGasPrice(),
])
return l1Fee + l2Fee
return l1Fee + l2Gas * l2GasPrice
}