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 the calculation for estimated gas price #3117

Merged
merged 5 commits into from
Jan 8, 2025
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/serious-yaks-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

**Celo:** Added support for gas price estimation on both Celo L1 and Celo L2.
33 changes: 29 additions & 4 deletions pnpm-lock.yaml

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

79 changes: 72 additions & 7 deletions src/celo/fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,29 @@ describe('celo/fees', () => {
expect(requestMock).not.toHaveBeenCalled()
})

test('calls the client when feeCurrency is provided', async () => {
test('calls the client when feeCurrency is provided celo L1', async () => {
const requestMock = vi.spyOn(client, 'request')

const baseFee = 15057755162n
const priorityFee = 602286n

expect(celo.fees.estimateFeesPerGas).toBeTypeOf('function')

// The check to determine if the chain is L1 or L2 is done by checking to
// see if there is code at the proxy admin address (by calling
// eth_getCode), if there is code then the chain is considered to be L2. A
// response of '0x' as used here is what is returned when there is no code
// at the address.

// @ts-ignore
requestMock.mockImplementation((request) => {
if (request.method === 'eth_gasPrice') return '15057755162'
if (request.method === 'eth_maxPriorityFeePerGas') return '602286'
if (request.method === 'eth_gasPrice') return baseFee.toString()
if (request.method === 'eth_maxPriorityFeePerGas')
return priorityFee.toString()
if (request.method === 'eth_getCode') return '0x'
return
})

expect(celo.fees.estimateFeesPerGas).toBeTypeOf('function')

const fees = await celoestimateFeesPerGasFn({
client,
multiply: (value: bigint) => (value * 150n) / 100n,
Expand All @@ -45,10 +57,63 @@ describe('celo/fees', () => {
},
} as any)

// For celo L1 the fees maxFeePerGas is calculated as `baseFee + maxPriorityFeePerGas`, the multiply method is not used.
expect(fees).toMatchInlineSnapshot(`
{
"maxFeePerGas": 22587235029n,
"maxPriorityFeePerGas": 602286n,
"maxFeePerGas": ${baseFee + priorityFee}n,
"maxPriorityFeePerGas": ${priorityFee}n,
}
`)
expect(requestMock).toHaveBeenCalledWith({
method: 'eth_maxPriorityFeePerGas',
params: ['0xfee'],
})
expect(requestMock).toHaveBeenCalledWith({
method: 'eth_gasPrice',
params: ['0xfee'],
})
})

test('calls the client when feeCurrency is provided celo L2', async () => {
const requestMock = vi.spyOn(client, 'request')

const baseFee = 15057755162n
const priorityFee = 602286n

expect(celo.fees.estimateFeesPerGas).toBeTypeOf('function')

// The check to determine if the chain is L1 or L2 is done by checking to
// see if there is code at the proxy admin address (by calling
// eth_getCode), if there is code then the chain is considered to be L2. A
// response longer than '0x' as used here is what is returned when there is
// code at the address.

// @ts-ignore
requestMock.mockImplementation((request) => {
if (request.method === 'eth_gasPrice')
return (baseFee + priorityFee).toString()
if (request.method === 'eth_maxPriorityFeePerGas')
return priorityFee.toString()
if (request.method === 'eth_getCode') return '0x00400400404040404040404'
return
})

const multiply = (value: bigint) => (value * 150n) / 100n
const feesCeloL1 = await celoestimateFeesPerGasFn({
client,
multiply: multiply,
request: {
feeCurrency: '0xfee',
},
} as any)

// For Celo L2 the fees maxFeePerGas is calculated as the following where
// multiply is the method passed to celoestimateFeesPerGasFn:
// `multiply(baseFeePerGas - maxPriorityFeePerGas) + maxPriorityFeePerGas`.
expect(feesCeloL1).toMatchInlineSnapshot(`
{
"maxFeePerGas": ${multiply(baseFee) + priorityFee}n,
"maxPriorityFeePerGas": ${priorityFee}n,
}
`)
expect(requestMock).toHaveBeenCalledWith({
Expand Down
19 changes: 15 additions & 4 deletions src/celo/fees.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCode } from '../actions/public/getCode.js'
import type { Client } from '../clients/createClient.js'
import type {
Address,
Expand All @@ -22,19 +23,23 @@ export const fees: ChainFees<typeof formatters> = {
) => {
if (!params.request?.feeCurrency) return null

const [maxFeePerGas, maxPriorityFeePerGas] = await Promise.all([
const [gasPrice, maxPriorityFeePerGas, cel2] = await Promise.all([
estimateFeePerGasInFeeCurrency(params.client, params.request.feeCurrency),
estimateMaxPriorityFeePerGasInFeeCurrency(
params.client,
params.request.feeCurrency,
),
isCel2(params.client),
])

const suggestedMaxFeePerGas =
params.multiply(maxFeePerGas) + maxPriorityFeePerGas
const maxFeePerGas = cel2
? // eth_gasPrice for cel2 returns baseFeePerGas + maxPriorityFeePerGas
params.multiply(gasPrice - maxPriorityFeePerGas) + maxPriorityFeePerGas
: // eth_gasPrice for Celo L1 returns (baseFeePerGas * multiplier), where the multiplier is 2 by default.
gasPrice + maxPriorityFeePerGas

return {
maxFeePerGas: suggestedMaxFeePerGas,
maxFeePerGas,
maxPriorityFeePerGas,
}
},
Expand Down Expand Up @@ -92,3 +97,9 @@ async function estimateMaxPriorityFeePerGasInFeeCurrency(
})
return BigInt(feesPerGas)
}

async function isCel2(client: Client) {
const proxyAdminAddress = '0x4200000000000000000000000000000000000018'
const code = await getCode(client, { address: proxyAdminAddress })
return Boolean(code)
}
2 changes: 1 addition & 1 deletion src/celo/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('sendTransaction()', () => {
expect(transportRequestMock).toHaveBeenLastCalledWith({
method: 'eth_sendRawTransaction',
params: [
'0x7bf87f82a4ec80830930ae8504350cec000194f39fd6e51aad88f6f4ce6ab8827279cfffb922660180c0940000000000000000000000000000000000000fee80a0b61a83b7fe73e24f223f563447cdb69f6dedc7f7f7b2acc4e41f2e57143ccd57a03ce0bcc81c026ff0eb17274940748e23250fe988f71370eba1e59e43557835b3',
'0x7bf87f82a4ec80830930ae8503818c4cc80194f39fd6e51aad88f6f4ce6ab8827279cfffb922660180c0940000000000000000000000000000000000000fee01a07dbc07e3ed73e889b085f29f2b4a1e794d578307199b5a677071a204328837a0a02da0ba218d0ab8164010f3c0efa7f84f4df6d879a242dc5110248e7014df75dc',
],
})
})
Expand Down