Skip to content

Commit

Permalink
fix: Celo maxFeePerGas calculation for fee currency transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
shazarre committed Sep 30, 2024
1 parent 6084c73 commit 92e65fd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-spies-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed Celo maxFeePerGas calculation for fee currency transactions.
9 changes: 5 additions & 4 deletions src/celo/fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ describe('celo/fees', () => {
const requestMock = vi.spyOn(client, 'request')
// @ts-ignore
requestMock.mockImplementation((request) => {
if (request.method === 'eth_gasPrice') return '11619349802'
if (request.method === 'eth_maxPriorityFeePerGas') return '2323869960'
if (request.method === 'eth_gasPrice') return '15057755162'
if (request.method === 'eth_maxPriorityFeePerGas') return '602286'
return
})

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

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

expect(fees).toMatchInlineSnapshot(`
{
"maxFeePerGas": 11619349802n,
"maxPriorityFeePerGas": 2323869960n,
"maxFeePerGas": 22587235029n,
"maxPriorityFeePerGas": 602286n,
}
`)
expect(requestMock).toHaveBeenCalledWith({
Expand Down
6 changes: 5 additions & 1 deletion src/celo/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const fees: ChainFees<typeof formatters> = {
params: ChainEstimateFeesPerGasFnParameters<typeof formatters>,
) => {
if (!params.request?.feeCurrency) return null
// TODO check if multiply is noe empty?

const [maxFeePerGas, maxPriorityFeePerGas] = await Promise.all([
estimateFeePerGasInFeeCurrency(params.client, params.request.feeCurrency),
Expand All @@ -30,8 +31,11 @@ export const fees: ChainFees<typeof formatters> = {
),
])

const suggestedMaxFeePerGas =
params.multiply(maxFeePerGas) + maxPriorityFeePerGas

return {
maxFeePerGas,
maxFeePerGas: suggestedMaxFeePerGas,
maxPriorityFeePerGas,
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/celo/sendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ describe('sendTransaction()', () => {
}

if (request.method === 'eth_maxPriorityFeePerGas') {
return 1n
return 602286n
}

if (
request.method === 'eth_gasPrice' &&
(request.params as string[])[0] === feeCurrencyAddress
) {
return 2n
return 15057755162n
}

if (request.method === 'eth_estimateGas') {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('sendTransaction()', () => {
expect(transportRequestMock).toHaveBeenLastCalledWith({
method: 'eth_sendRawTransaction',
params: [
'0x7bf87782a4ec8001020194f39fd6e51aad88f6f4ce6ab8827279cfffb922660180c0940000000000000000000000000000000000000fee80a0a3163f9ff91200f4c8000f0217d85d16c329c2f38d48a7b4b70119989e475e57a0555fd5b2a6eac95426e33cd07ca5fec121ad46194611a013001f76bbc4b33136',
'0x7bf87f82a4ec80830930ae8504350cec000194f39fd6e51aad88f6f4ce6ab8827279cfffb922660180c0940000000000000000000000000000000000000fee80a0b61a83b7fe73e24f223f563447cdb69f6dedc7f7f7b2acc4e41f2e57143ccd57a03ce0bcc81c026ff0eb17274940748e23250fe988f71370eba1e59e43557835b3',
],
})
})
Expand Down

0 comments on commit 92e65fd

Please sign in to comment.