Skip to content

Commit

Permalink
chore: clean
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Jan 8, 2025
1 parent c1a3901 commit b7163c2
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 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 @@ -7,27 +8,6 @@ import type {
} from '../index.js'
import type { formatters } from './formatters.js'

type RequestGetCodeParams = {
Method: 'eth_getCode'
Parameters: [Address, 'latest']
ReturnType: Hex
}
/*
* This checks if we're in L2 context, it's a port of the technique used in
* https://github.com/celo-org/celo-monorepo/blob/da9b4955c1fdc8631980dc4adf9b05e0524fc228/packages/protocol/contracts-0.8/common/IsL2Check.sol#L17
*/
const isCel2 = async (client: Client) => {
const proxyAdminAddress = '0x4200000000000000000000000000000000000018'
const code = await client.request<RequestGetCodeParams>({
method: 'eth_getCode',
params: [proxyAdminAddress, 'latest'],
})
if (typeof code === 'string') {
return code !== '0x' && code.length > 2
}
return false
}

export const fees: ChainFees<typeof formatters> = {
/*
* Estimates the fees per gas for a transaction.
Expand All @@ -43,26 +23,23 @@ export const fees: ChainFees<typeof formatters> = {
) => {
if (!params.request?.feeCurrency) return null

const [gasPrice, 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),
])

let maxFeePerGas: bigint;
if (await isCel2(params.client)) {
// eth_gasPrice for cel2 returns baseFeePerGas + maxPriorityFeePerGas
maxFeePerGas =
const maxFeePerGas = cel2
? // eth_gasPrice for cel2 returns baseFeePerGas + maxPriorityFeePerGas
params.multiply(gasPrice - maxPriorityFeePerGas) + maxPriorityFeePerGas
} else {
// eth_gasPrice for Celo L1 returns (baseFeePerGas * multiplier), where the multiplier is 2 by default.
maxFeePerGas = gasPrice + maxPriorityFeePerGas
}
: // eth_gasPrice for Celo L1 returns (baseFeePerGas * multiplier), where the multiplier is 2 by default.
gasPrice + maxPriorityFeePerGas

return {
maxFeePerGas: maxFeePerGas,
maxFeePerGas,
maxPriorityFeePerGas,
}
},
Expand Down Expand Up @@ -120,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)
}

0 comments on commit b7163c2

Please sign in to comment.